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,
275 struct lustre_capa *capa)
277 struct obd_device *obd = exp->exp_obd;
278 struct lvfs_run_ctxt saved;
279 struct niobuf_remote *rnb;
280 struct niobuf_local *lnb;
281 struct dentry *dentry = NULL;
284 int rc = 0, i, tot_bytes = 0;
285 unsigned long now = jiffies;
288 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
289 * When we do this function's dentry cleanup will need to be fixed.
290 * These values are verified in ost_brw_write() from the wire. */
291 LASSERTF(objcount == 1, "%d\n", objcount);
292 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
294 rc = filter_verify_capa(exp, NULL, obdo_mdsno(oa), capa,
299 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
300 spin_lock(&obd->obd_osfs_lock);
301 filter_grant_incoming(exp, oa);
304 spin_unlock(&obd->obd_osfs_lock);
307 iobuf = filter_iobuf_get(&obd->u.filter, oti);
309 RETURN(PTR_ERR(iobuf));
311 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
312 dentry = filter_oa2dentry(obd, oa);
313 if (IS_ERR(dentry)) {
314 rc = PTR_ERR(dentry);
319 inode = dentry->d_inode;
321 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
322 fsfilt_check_slow(obd, now, obd_timeout, "preprw_read setup");
324 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
326 lnb->dentry = dentry;
327 lnb->offset = rnb->offset;
329 lnb->flags = rnb->flags;
332 * ost_brw_write()->ost_nio_pages_get() already initialized
333 * lnb->page to point to the page from the per-thread page
334 * pool (bug 5137), initialize page.
336 LASSERT(lnb->page != NULL);
338 if (inode->i_size <= rnb->offset)
339 /* If there's no more data, abort early. lnb->rc == 0,
340 * so it's easy to detect later. */
343 filter_alloc_dio_page(obd, inode, lnb);
345 if (inode->i_size < lnb->offset + lnb->len - 1)
346 lnb->rc = inode->i_size - lnb->offset;
350 tot_bytes += lnb->rc;
352 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
355 fsfilt_check_slow(obd, now, obd_timeout, "start_page_read");
357 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
358 exp, NULL, NULL, NULL);
362 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
364 filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
370 filter_free_dio_pages(objcount, obj, niocount, res);
376 filter_iobuf_put(&obd->u.filter, iobuf, oti);
378 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
380 CERROR("io error %d\n", rc);
385 /* When clients have dirtied as much space as they've been granted they
386 * fall through to sync writes. These sync writes haven't been expressed
387 * in grants and need to error with ENOSPC when there isn't room in the
388 * filesystem for them after grants are taken into account. However,
389 * writeback of the dirty data that was already granted space can write
392 * Caller must hold obd_osfs_lock. */
393 static int filter_grant_check(struct obd_export *exp, int objcount,
394 struct fsfilt_objinfo *fso, int niocount,
395 struct niobuf_remote *rnb,
396 struct niobuf_local *lnb, obd_size *left,
399 struct filter_export_data *fed = &exp->exp_filter_data;
400 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
401 unsigned long used = 0, ungranted = 0, using;
402 int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
404 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
406 for (obj = 0; obj < objcount; obj++) {
407 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
410 /* should match the code in osc_exit_cache */
412 bytes += rnb[n].offset & (blocksize - 1);
413 tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
415 bytes += blocksize - tmp;
417 if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
418 if (fed->fed_grant < used + bytes) {
420 "%s: cli %s/%p claims %ld+%d "
421 "GRANT, real grant %lu idx %d\n",
422 exp->exp_obd->obd_name,
423 exp->exp_client_uuid.uuid, exp,
424 used, bytes, fed->fed_grant, n);
428 rnb[n].flags |= OBD_BRW_GRANTED;
429 lnb[n].lnb_grant_used = bytes;
430 CDEBUG(0, "idx %d used=%lu\n", n, used);
435 if (*left > ungranted) {
436 /* if enough space, pretend it was granted */
438 rnb[n].flags |= OBD_BRW_GRANTED;
439 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
444 /* We can't check for already-mapped blocks here, as
445 * it requires dropping the osfs lock to do the bmap.
446 * Instead, we return ENOSPC and in that case we need
447 * to go through and verify if all of the blocks not
448 * marked BRW_GRANTED are already mapped and we can
449 * ignore this error. */
451 rnb[n].flags &= ~OBD_BRW_GRANTED;
452 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
453 exp->exp_obd->obd_name,
454 exp->exp_client_uuid.uuid, exp, n, bytes);
458 /* Now substract what client have used already. We don't subtract
459 * this from the tot_granted yet, so that other client's can't grab
460 * that space before we have actually allocated our blocks. That
461 * happens in filter_grant_commit() after the writes are done. */
463 fed->fed_grant -= used;
464 fed->fed_pending += used;
465 exp->exp_obd->u.filter.fo_tot_pending += used;
468 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
469 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
470 ungranted, fed->fed_grant, fed->fed_dirty);
472 /* Rough calc in case we don't refresh cached statfs data */
473 using = (used + ungranted + 1 ) >>
474 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
475 if (exp->exp_obd->obd_osfs.os_bavail > using)
476 exp->exp_obd->obd_osfs.os_bavail -= using;
478 exp->exp_obd->obd_osfs.os_bavail = 0;
480 if (fed->fed_dirty < used) {
481 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
482 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
483 used, fed->fed_dirty);
484 used = fed->fed_dirty;
486 exp->exp_obd->u.filter.fo_tot_dirty -= used;
487 fed->fed_dirty -= used;
489 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
490 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
491 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
492 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
493 spin_unlock(&exp->exp_obd->obd_osfs_lock);
499 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
500 * on mulitple inodes. That isn't all, because there still exists the
501 * possibility of a truncate starting a new transaction while holding the ext3
502 * rwsem = write while some writes (which have started their transactions here)
503 * blocking on the ext3 rwsem = read => lock inversion.
505 * The handling gets very ugly when dealing with locked pages. It may be easier
506 * to just get rid of the locked page code (which has problems of its own) and
507 * either discover we do not need it anymore (i.e. it was a symptom of another
508 * bug) or ensure we get the page locks in an appropriate order. */
509 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
510 int objcount, struct obd_ioobj *obj,
511 int niocount, struct niobuf_remote *nb,
512 struct niobuf_local *res,
513 struct obd_trans_info *oti,
514 struct lustre_capa *capa)
516 struct lvfs_run_ctxt saved;
517 struct niobuf_remote *rnb;
518 struct niobuf_local *lnb = res;
519 struct fsfilt_objinfo fso;
520 struct filter_mod_data *fmd;
521 struct dentry *dentry = NULL;
524 unsigned long now = jiffies;
525 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
527 LASSERT(objcount == 1);
528 LASSERT(obj->ioo_bufcnt > 0);
530 rc = filter_verify_capa(exp, NULL, obdo_mdsno(oa), capa,
535 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
536 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
538 GOTO(cleanup, rc = PTR_ERR(iobuf));
541 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
544 GOTO(cleanup, rc = PTR_ERR(dentry));
547 if (dentry->d_inode == NULL) {
548 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
549 exp->exp_obd->obd_name, obj->ioo_id);
550 GOTO(cleanup, rc = -ENOENT);
553 fso.fso_dentry = dentry;
554 fso.fso_bufcnt = obj->ioo_bufcnt;
556 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "preprw_write setup");
558 /* Don't update inode timestamps if this write is older than a
559 * setattr which modifies the timestamps. b=10150 */
560 /* XXX when we start having persistent reservations this needs to
561 * be changed to filter_fmd_get() to create the fmd if it doesn't
562 * already exist so we can store the reservation handle there. */
563 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
565 spin_lock(&exp->exp_obd->obd_osfs_lock);
567 filter_grant_incoming(exp, oa);
568 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
569 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
572 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
573 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
577 left = filter_grant_space_left(exp);
579 rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
580 &left, dentry->d_inode);
582 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
583 * for setting UID/GID and fid EA in first write time. */
584 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
585 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
586 oa->o_valid |= OBD_MD_FLGRANT;
589 spin_unlock(&exp->exp_obd->obd_osfs_lock);
590 filter_fmd_put(exp, fmd);
595 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
597 /* We still set up for ungranted pages so that granted pages
598 * can be written to disk as they were promised, and portals
599 * needs to keep the pages all aligned properly. */
600 lnb->dentry = dentry;
601 lnb->offset = rnb->offset;
603 lnb->flags = rnb->flags;
606 * ost_brw_write()->ost_nio_pages_get() already initialized
607 * lnb->page to point to the page from the per-thread page
608 * pool (bug 5137), initialize page.
610 LASSERT(lnb->page != NULL);
611 if (lnb->len != PAGE_SIZE) {
612 memset(kmap(lnb->page) + lnb->len,
613 0, PAGE_SIZE - lnb->len);
616 lnb->page->index = lnb->offset >> PAGE_SHIFT;
620 /* If the filter writes a partial page, then has the file
621 * extended, the client will read in the whole page. the
622 * filter has to be careful to zero the rest of the partial
623 * page on disk. we do it by hand for partial extending
624 * writes, send_bio() is responsible for zeroing pages when
625 * asked to read unmapped blocks -- brw_kiovec() does this. */
626 if (lnb->len != PAGE_SIZE) {
629 maxidx = ((dentry->d_inode->i_size + PAGE_SIZE - 1) >>
631 if (maxidx >= lnb->page->index) {
632 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
633 LPU64" flg %x before EOF %llu\n",
634 lnb->len, lnb->offset,lnb->flags,
635 dentry->d_inode->i_size);
636 filter_iobuf_add_page(exp->exp_obd, iobuf,
641 char *p = kmap(lnb->page);
643 off = lnb->offset & ~PAGE_MASK;
646 off = (lnb->offset + lnb->len) & ~PAGE_MASK;
648 memset(p + off, 0, PAGE_SIZE - off);
653 tot_bytes += lnb->len;
656 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
659 fsfilt_check_slow(exp->exp_obd, now, obd_timeout, "start_page_write");
661 lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
665 switch(cleanup_phase) {
668 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
670 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
675 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
677 spin_lock(&exp->exp_obd->obd_osfs_lock);
679 filter_grant_incoming(exp, oa);
680 spin_unlock(&exp->exp_obd->obd_osfs_lock);
681 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
688 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
689 int objcount, struct obd_ioobj *obj, int niocount,
690 struct niobuf_remote *nb, struct niobuf_local *res,
691 struct obd_trans_info *oti, struct lustre_capa *capa)
693 if (cmd == OBD_BRW_WRITE)
694 return filter_preprw_write(cmd, exp, oa, objcount, obj,
695 niocount, nb, res, oti, capa);
696 if (cmd == OBD_BRW_READ)
697 return filter_preprw_read(cmd, exp, oa, objcount, obj,
698 niocount, nb, res, oti, capa);
703 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
709 (inode->i_size > filter->fo_readcache_max_filesize))
712 /* drop from cache like truncate_list_pages() */
713 if (drop && !TryLockPage(page)) {
715 ll_truncate_complete_page(page);
718 page_cache_release(page);
721 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
722 int objcount, struct obd_ioobj *obj,
723 int niocount, struct niobuf_local *res,
724 struct obd_trans_info *oti, int rc)
726 struct inode *inode = NULL;
727 struct ldlm_res_id res_id = { .name = { obj->ioo_id, 0,
729 struct ldlm_resource *resource = NULL;
730 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
733 /* If oa != NULL then filter_preprw_read updated the inode atime
734 * and we should update the lvb so that other glimpses will also
735 * get the updated value. bug 5972 */
736 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
737 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
739 if (resource != NULL) {
740 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
741 ldlm_resource_putref(resource);
745 if (res->dentry != NULL)
746 inode = res->dentry->d_inode;
748 filter_free_dio_pages(objcount, obj, niocount, res);
750 if (res->dentry != NULL)
755 void flip_into_page_cache(struct inode *inode, struct page *new_page)
757 struct page *old_page;
761 /* the dlm is protecting us from read/write concurrency, so we
762 * expect this find_lock_page to return quickly. even if we
763 * race with another writer it won't be doing much work with
764 * the page locked. we do this 'cause t_c_p expects a
765 * locked page, and it wants to grab the pagecache lock
767 old_page = find_lock_page(inode->i_mapping, new_page->index);
769 ll_truncate_complete_page(old_page);
770 unlock_page(old_page);
771 page_cache_release(old_page);
774 #if 0 /* this should be a /proc tunable someday */
775 /* racing o_directs (no locking ioctl) could race adding
776 * their pages, so we repeat the page invalidation unless
777 * we successfully added our new page */
778 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
780 page_hash(inode->i_mapping,
783 /* add_to_page_cache clears uptodate|dirty and locks
785 SetPageUptodate(new_page);
786 unlock_page(new_page);
794 void filter_grant_commit(struct obd_export *exp, int niocount,
795 struct niobuf_local *res)
797 struct filter_obd *filter = &exp->exp_obd->u.filter;
798 struct niobuf_local *lnb = res;
799 unsigned long pending = 0;
802 spin_lock(&exp->exp_obd->obd_osfs_lock);
803 for (i = 0, lnb = res; i < niocount; i++, lnb++)
804 pending += lnb->lnb_grant_used;
806 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
807 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
808 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
809 exp->exp_filter_data.fed_pending, pending);
810 exp->exp_filter_data.fed_pending -= pending;
811 LASSERTF(filter->fo_tot_granted >= pending,
812 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
813 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
814 exp->exp_obd->u.filter.fo_tot_granted, pending);
815 filter->fo_tot_granted -= pending;
816 LASSERTF(filter->fo_tot_pending >= pending,
817 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
818 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
819 filter->fo_tot_pending, pending);
820 filter->fo_tot_pending -= pending;
822 spin_unlock(&exp->exp_obd->obd_osfs_lock);
825 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
826 int objcount, struct obd_ioobj *obj, int niocount,
827 struct niobuf_local *res, struct obd_trans_info *oti,
830 if (cmd == OBD_BRW_WRITE)
831 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
833 if (cmd == OBD_BRW_READ)
834 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
840 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
841 obd_count oa_bufs, struct brw_page *pga,
842 struct obd_trans_info *oti)
844 struct obd_ioobj ioo;
845 struct niobuf_local *lnb;
846 struct niobuf_remote *rnb;
851 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
852 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
854 if (lnb == NULL || rnb == NULL)
855 GOTO(out, ret = -ENOMEM);
857 for (i = 0; i < oa_bufs; i++) {
858 lnb[i].page = pga[i].pg;
859 rnb[i].offset = pga[i].off;
860 rnb[i].len = pga[i].count;
863 obdo_to_ioobj(oinfo->oi_oa, &ioo);
864 ioo.ioo_bufcnt = oa_bufs;
866 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
867 oa_bufs, rnb, lnb, oti, oinfo_capa(oinfo));
871 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo,
872 oa_bufs, lnb, oti, ret);
876 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
878 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));