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 lprocfs_counter_add(exp->exp_ops_stats, LPROC_FILTER_READ_BYTES,
362 filter_free_dio_pages(objcount, obj, niocount, res);
368 filter_iobuf_put(&obd->u.filter, iobuf, oti);
370 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
372 CERROR("io error %d\n", rc);
377 /* When clients have dirtied as much space as they've been granted they
378 * fall through to sync writes. These sync writes haven't been expressed
379 * in grants and need to error with ENOSPC when there isn't room in the
380 * filesystem for them after grants are taken into account. However,
381 * writeback of the dirty data that was already granted space can write
384 * Caller must hold obd_osfs_lock. */
385 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
386 int objcount, struct fsfilt_objinfo *fso,
387 int niocount, struct niobuf_remote *rnb,
388 struct niobuf_local *lnb, obd_size *left,
391 struct filter_export_data *fed = &exp->exp_filter_data;
392 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
393 unsigned long used = 0, ungranted = 0, using;
394 int i, rc = -ENOSPC, obj, n = 0;
396 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
398 for (obj = 0; obj < objcount; obj++) {
399 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
402 /* should match the code in osc_exit_cache */
404 bytes += rnb[n].offset & (blocksize - 1);
405 tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
407 bytes += blocksize - tmp;
409 if ((rnb[n].flags & OBD_BRW_FROM_GRANT) &&
410 (oa->o_valid & OBD_MD_FLGRANT)) {
411 if (fed->fed_grant < used + bytes) {
413 "%s: cli %s/%p claims %ld+%d "
414 "GRANT, real grant %lu idx %d\n",
415 exp->exp_obd->obd_name,
416 exp->exp_client_uuid.uuid, exp,
417 used, bytes, fed->fed_grant, n);
420 rnb[n].flags |= OBD_BRW_GRANTED;
421 lnb[n].lnb_grant_used = bytes;
422 CDEBUG(0, "idx %d used=%lu\n", n, used);
427 if (*left > ungranted + bytes) {
428 /* if enough space, pretend it was granted */
430 rnb[n].flags |= OBD_BRW_GRANTED;
431 lnb[n].lnb_grant_used = bytes;
432 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
437 /* We can't check for already-mapped blocks here, as
438 * it requires dropping the osfs lock to do the bmap.
439 * Instead, we return ENOSPC and in that case we need
440 * to go through and verify if all of the blocks not
441 * marked BRW_GRANTED are already mapped and we can
442 * ignore this error. */
444 rnb[n].flags &= ~OBD_BRW_GRANTED;
445 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
446 exp->exp_obd->obd_name,
447 exp->exp_client_uuid.uuid, exp, n, bytes);
451 /* Now substract what client have used already. We don't subtract
452 * this from the tot_granted yet, so that other client's can't grab
453 * that space before we have actually allocated our blocks. That
454 * happens in filter_grant_commit() after the writes are done. */
456 fed->fed_grant -= used;
457 fed->fed_pending += used + ungranted;
458 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
459 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
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,
508 struct lustre_capa *capa)
510 struct lvfs_run_ctxt saved;
511 struct niobuf_remote *rnb;
512 struct niobuf_local *lnb = res;
513 struct fsfilt_objinfo fso;
514 struct filter_mod_data *fmd;
515 struct dentry *dentry = NULL;
518 unsigned long now = jiffies;
519 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
521 LASSERT(objcount == 1);
522 LASSERT(obj->ioo_bufcnt > 0);
524 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
529 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
530 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
532 GOTO(cleanup, rc = PTR_ERR(iobuf));
535 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
538 GOTO(cleanup, rc = PTR_ERR(dentry));
541 if (dentry->d_inode == NULL) {
542 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
543 exp->exp_obd->obd_name, obj->ioo_id);
544 GOTO(cleanup, rc = -ENOENT);
547 fso.fso_dentry = dentry;
548 fso.fso_bufcnt = obj->ioo_bufcnt;
550 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "preprw_write setup");
552 /* Don't update inode timestamps if this write is older than a
553 * setattr which modifies the timestamps. b=10150 */
554 /* XXX when we start having persistent reservations this needs to
555 * be changed to filter_fmd_get() to create the fmd if it doesn't
556 * already exist so we can store the reservation handle there. */
557 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
560 spin_lock(&exp->exp_obd->obd_osfs_lock);
561 filter_grant_incoming(exp, oa);
562 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
563 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
566 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
567 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
570 left = filter_grant_space_left(exp);
572 rc = filter_grant_check(exp, oa, objcount, &fso, niocount, nb, res,
573 &left, dentry->d_inode);
575 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
576 * for setting UID/GID and fid EA in first write time. */
577 if (oa->o_valid & OBD_MD_FLGRANT)
578 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
580 spin_unlock(&exp->exp_obd->obd_osfs_lock);
581 filter_fmd_put(exp, fmd);
586 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
588 /* We still set up for ungranted pages so that granted pages
589 * can be written to disk as they were promised, and portals
590 * needs to keep the pages all aligned properly. */
591 lnb->dentry = dentry;
592 lnb->offset = rnb->offset;
594 lnb->flags = rnb->flags;
597 * ost_brw_write()->ost_nio_pages_get() already initialized
598 * lnb->page to point to the page from the per-thread page
599 * pool (bug 5137), initialize page.
601 LASSERT(lnb->page != NULL);
602 if (lnb->len != CFS_PAGE_SIZE) {
603 memset(kmap(lnb->page) + lnb->len,
604 0, CFS_PAGE_SIZE - lnb->len);
607 lnb->page->index = lnb->offset >> CFS_PAGE_SHIFT;
611 /* If the filter writes a partial page, then has the file
612 * extended, the client will read in the whole page. the
613 * filter has to be careful to zero the rest of the partial
614 * page on disk. we do it by hand for partial extending
615 * writes, send_bio() is responsible for zeroing pages when
616 * asked to read unmapped blocks -- brw_kiovec() does this. */
617 if (lnb->len != CFS_PAGE_SIZE) {
620 maxidx = ((i_size_read(dentry->d_inode) +
621 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
622 if (maxidx >= lnb->page->index) {
623 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
624 LPU64" flg %x before EOF %llu\n",
625 lnb->len, lnb->offset,lnb->flags,
626 i_size_read(dentry->d_inode));
627 filter_iobuf_add_page(exp->exp_obd, iobuf,
632 char *p = kmap(lnb->page);
634 off = lnb->offset & ~CFS_PAGE_MASK;
637 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
639 memset(p + off, 0, CFS_PAGE_SIZE - off);
644 tot_bytes += lnb->len;
647 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
650 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "start_page_write");
652 lprocfs_counter_add(exp->exp_ops_stats, LPROC_FILTER_WRITE_BYTES,
656 switch(cleanup_phase) {
659 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
661 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
666 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
668 spin_lock(&exp->exp_obd->obd_osfs_lock);
670 filter_grant_incoming(exp, oa);
671 spin_unlock(&exp->exp_obd->obd_osfs_lock);
672 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
679 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
680 int objcount, struct obd_ioobj *obj, int niocount,
681 struct niobuf_remote *nb, struct niobuf_local *res,
682 struct obd_trans_info *oti, struct lustre_capa *capa)
684 if (cmd == OBD_BRW_WRITE)
685 return filter_preprw_write(cmd, exp, oa, objcount, obj,
686 niocount, nb, res, oti, capa);
687 if (cmd == OBD_BRW_READ)
688 return filter_preprw_read(cmd, exp, oa, objcount, obj,
689 niocount, nb, res, oti, capa);
694 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
700 (i_size_read(inode) > filter->fo_readcache_max_filesize))
703 /* drop from cache like truncate_list_pages() */
704 if (drop && !TryLockPage(page)) {
706 ll_truncate_complete_page(page);
709 page_cache_release(page);
712 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
713 int objcount, struct obd_ioobj *obj,
714 int niocount, struct niobuf_local *res,
715 struct obd_trans_info *oti, int rc)
717 struct inode *inode = NULL;
718 struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
720 struct ldlm_resource *resource = NULL;
721 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
724 /* If oa != NULL then filter_preprw_read updated the inode atime
725 * and we should update the lvb so that other glimpses will also
726 * get the updated value. bug 5972 */
727 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
728 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
730 if (resource != NULL) {
731 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
732 ldlm_resource_putref(resource);
736 if (res->dentry != NULL)
737 inode = res->dentry->d_inode;
739 filter_free_dio_pages(objcount, obj, niocount, res);
741 if (res->dentry != NULL)
746 void flip_into_page_cache(struct inode *inode, struct page *new_page)
748 struct page *old_page;
752 /* the dlm is protecting us from read/write concurrency, so we
753 * expect this find_lock_page to return quickly. even if we
754 * race with another writer it won't be doing much work with
755 * the page locked. we do this 'cause t_c_p expects a
756 * locked page, and it wants to grab the pagecache lock
758 old_page = find_lock_page(inode->i_mapping, new_page->index);
760 ll_truncate_complete_page(old_page);
761 unlock_page(old_page);
762 page_cache_release(old_page);
765 #if 0 /* this should be a /proc tunable someday */
766 /* racing o_directs (no locking ioctl) could race adding
767 * their pages, so we repeat the page invalidation unless
768 * we successfully added our new page */
769 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
771 page_hash(inode->i_mapping,
774 /* add_to_page_cache clears uptodate|dirty and locks
776 SetPageUptodate(new_page);
777 unlock_page(new_page);
785 void filter_grant_commit(struct obd_export *exp, int niocount,
786 struct niobuf_local *res)
788 struct filter_obd *filter = &exp->exp_obd->u.filter;
789 struct niobuf_local *lnb = res;
790 unsigned long pending = 0;
793 spin_lock(&exp->exp_obd->obd_osfs_lock);
794 for (i = 0, lnb = res; i < niocount; i++, lnb++)
795 pending += lnb->lnb_grant_used;
797 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
798 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
799 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
800 exp->exp_filter_data.fed_pending, pending);
801 exp->exp_filter_data.fed_pending -= pending;
802 LASSERTF(filter->fo_tot_granted >= pending,
803 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
804 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
805 exp->exp_obd->u.filter.fo_tot_granted, pending);
806 filter->fo_tot_granted -= pending;
807 LASSERTF(filter->fo_tot_pending >= pending,
808 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
809 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
810 filter->fo_tot_pending, pending);
811 filter->fo_tot_pending -= pending;
813 spin_unlock(&exp->exp_obd->obd_osfs_lock);
816 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
817 int objcount, struct obd_ioobj *obj, int niocount,
818 struct niobuf_local *res, struct obd_trans_info *oti,
821 if (cmd == OBD_BRW_WRITE)
822 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
824 if (cmd == OBD_BRW_READ)
825 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
831 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
832 obd_count oa_bufs, struct brw_page *pga,
833 struct obd_trans_info *oti)
835 struct obd_ioobj ioo;
836 struct niobuf_local *lnb;
837 struct niobuf_remote *rnb;
842 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
843 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
845 if (lnb == NULL || rnb == NULL)
846 GOTO(out, ret = -ENOMEM);
848 for (i = 0; i < oa_bufs; i++) {
849 lnb[i].page = pga[i].pg;
850 rnb[i].offset = pga[i].off;
851 rnb[i].len = pga[i].count;
854 obdo_to_ioobj(oinfo->oi_oa, &ioo);
855 ioo.ioo_bufcnt = oa_bufs;
857 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
858 oa_bufs, rnb, lnb, oti, oinfo_capa(oinfo));
862 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
863 oa_bufs, lnb, oti, ret);
867 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
869 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));