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 Lustre, http://www.lustre.org.
13 * Lustre is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Lustre is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Lustre; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DEBUG_SUBSYSTEM S_FILTER
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/pagemap.h> // XXX kill me soon
32 #include <linux/version.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_smfs.h>
37 #include <linux/lustre_snap.h>
38 #include "filter_internal.h"
40 int *obdfilter_created_scratchpad;
42 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
43 struct niobuf_local *lnb)
49 page = alloc_pages(GFP_HIGHUSER, 0);
51 CERROR("no memory for a temp page\n");
57 POISON_PAGE(page, 0xf1);
58 if (lnb->len != PAGE_SIZE) {
59 memset(kmap(page) + lnb->len, 0, PAGE_SIZE - lnb->len);
63 page->index = lnb->offset >> PAGE_SHIFT;
70 void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
71 int niocount, struct niobuf_local *res)
75 for (i = 0; i < objcount; i++, obj++) {
76 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++) {
77 if (res->page != NULL) {
78 __free_page(res->page);
85 /* Grab the dirty and seen grant announcements from the incoming obdo.
86 * We will later calculate the clients new grant and return it.
87 * Caller must hold osfs lock */
88 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
90 struct filter_export_data *fed;
91 struct obd_device *obd = exp->exp_obd;
92 static unsigned long last_msg;
93 static int last_count;
97 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
99 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
100 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
101 oa->o_valid &= ~OBD_MD_FLGRANT;
106 fed = &exp->exp_filter_data;
108 /* Don't print this to the console the first time it happens, since
109 * it can happen legitimately on occasion, but only rarely. */
110 if (time_after(jiffies, last_msg + 60 * HZ)) {
114 if ((last_count & (-last_count)) == last_count)
118 /* Add some margin, since there is a small race if other RPCs arrive
119 * out-or-order and have already consumed some grant. We want to
120 * leave this here in case there is a large error in accounting. */
121 CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ? mask:D_CACHE,
122 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
123 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
124 oa->o_dropped, fed->fed_grant);
126 /* Update our accounting now so that statfs takes it into account.
127 * Note that fed_dirty is only approximate and can become incorrect
128 * if RPCs arrive out-of-order. No important calculations depend
129 * on fed_dirty however. */
130 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
131 if (fed->fed_grant < oa->o_dropped) {
132 CERROR("%s: cli %s/%p reports %u dropped > fed_grant %lu\n",
133 obd->obd_name, exp->exp_client_uuid.uuid, exp,
134 oa->o_dropped, fed->fed_grant);
137 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
138 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
139 obd->obd_name, exp->exp_client_uuid.uuid, exp,
140 oa->o_dropped, obd->u.filter.fo_tot_granted);
143 obd->u.filter.fo_tot_granted -= oa->o_dropped;
144 fed->fed_grant -= oa->o_dropped;
145 fed->fed_dirty = oa->o_dirty;
149 #define GRANT_FOR_LLOG(obd) 16
151 /* Figure out how much space is available between what we've granted
152 * and what remains in the filesystem. Compensate for ext3 indirect
153 * block overhead when computing how much free space is left ungranted.
155 * Caller must hold obd_osfs_lock. */
156 obd_size filter_grant_space_left(struct obd_export *exp)
158 struct obd_device *obd = exp->exp_obd;
159 int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
160 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
161 int rc, statfs_done = 0;
163 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
165 if (time_before(obd->obd_osfs_age, jiffies - HZ)) {
167 rc = fsfilt_statfs(obd, obd->u.filter.fo_sb, jiffies + 1);
168 if (rc) /* N.B. statfs can't really fail */
173 avail = obd->obd_osfs.os_bavail;
174 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
175 if (left > GRANT_FOR_LLOG(obd)) {
176 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
178 left = 0 /* << blockbits */;
181 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
182 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
186 if (left >= tot_granted) {
189 static unsigned long next;
190 if (left < tot_granted - obd->u.filter.fo_tot_pending &&
191 time_after(jiffies, next)) {
192 spin_unlock(&obd->obd_osfs_lock);
193 CERROR("%s: cli %s/%p grant "LPU64" > available "
194 LPU64" and pending "LPU64"\n", obd->obd_name,
195 exp->exp_client_uuid.uuid, exp, tot_granted,
196 left, obd->u.filter.fo_tot_pending);
198 portals_debug_dumplog();
199 next = jiffies + 20 * HZ;
200 spin_lock(&obd->obd_osfs_lock);
205 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
206 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
207 exp->exp_client_uuid.uuid, exp,
208 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
209 tot_granted, left, obd->u.filter.fo_tot_pending);
214 /* Calculate how much grant space to allocate to this client, based on how
215 * much space is currently free and how much of that is already granted.
217 * Caller must hold obd_osfs_lock. */
218 long filter_grant(struct obd_export *exp, obd_size current_grant,
219 obd_size want, obd_size fs_space_left)
221 struct obd_device *obd = exp->exp_obd;
222 struct filter_export_data *fed = &exp->exp_filter_data;
223 int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
226 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
228 /* Grant some fraction of the client's requested grant space so that
229 * they are not always waiting for write credits (not all of it to
230 * avoid overgranting in face of multiple RPCs in flight). This
231 * essentially will be able to control the OSC_MAX_RIF for a client.
233 * If we do have a large disparity between what the client thinks it
234 * has and what we think it has, don't grant very much and let the
235 * client consume its grant first. Either it just has lots of RPCs
236 * in flight, or it was evicted and its grants will soon be used up. */
237 if (current_grant < want &&
238 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
239 grant = min((want >> blockbits) / 2,
240 (fs_space_left >> blockbits) / 8);
244 if (grant > FILTER_GRANT_CHUNK)
245 grant = FILTER_GRANT_CHUNK;
247 obd->u.filter.fo_tot_granted += grant;
248 fed->fed_grant += grant;
252 CDEBUG(D_CACHE,"%s: cli %s/%p wants: "LPU64" granting: "LPU64"\n",
253 obd->obd_name, exp->exp_client_uuid.uuid, exp, want, grant);
255 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
256 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
257 exp, obd->u.filter.fo_tot_dirty,
258 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
263 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
264 int objcount, struct obd_ioobj *obj,
265 int niocount, struct niobuf_remote *nb,
266 struct niobuf_local *res,
267 struct obd_trans_info *oti)
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 LASSERTF(objcount == 1, "%d\n", objcount);
283 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
285 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
286 spin_lock(&obd->obd_osfs_lock);
287 filter_grant_incoming(exp, oa);
290 spin_unlock(&obd->obd_osfs_lock);
293 memset(res, 0, niocount * sizeof(*res));
295 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
296 rc = filter_alloc_iobuf(OBD_BRW_READ, obj->ioo_bufcnt, &iobuf);
300 dentry = filter_oa2dentry(obd, oa);
302 GOTO(cleanup, rc = PTR_ERR(dentry));
304 if (dentry->d_inode == NULL) {
305 CERROR("trying to BRW to non-existent file "LPU64"\n",
307 GOTO(cleanup, rc = -ENOENT);
310 inode = dentry->d_inode;
312 fsfilt_check_slow(now, obd_timeout, "preprw_read setup");
314 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
316 lnb->dentry = dentry;
317 lnb->offset = rnb->offset;
319 lnb->flags = rnb->flags;
321 if (inode->i_size <= rnb->offset)
322 /* If there's no more data, abort early.
323 * lnb->page == NULL and lnb->rc == 0, so it's
324 * easy to detect later. */
327 rc = filter_alloc_dio_page(obd, inode, lnb);
329 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
330 "page err %u@"LPU64" %u/%u %p: rc %d\n",
331 lnb->len, lnb->offset, i, obj->ioo_bufcnt,
336 if (inode->i_size < lnb->offset + lnb->len - 1)
337 lnb->rc = inode->i_size - lnb->offset;
341 tot_bytes += lnb->rc;
343 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
346 fsfilt_check_slow(now, obd_timeout, "start_page_read");
348 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
353 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
355 filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
361 filter_free_dio_pages(objcount, obj, niocount, res);
366 CERROR("NULL dentry in cleanup -- tell CFS\n");
370 filter_free_iobuf(iobuf);
372 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
374 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, int objcount,
387 struct fsfilt_objinfo *fso, int niocount,
388 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.filter.fo_sb->s_blocksize;
394 unsigned long used = 0, ungranted = 0, using;
395 int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
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 /* FIXME: this is calculated with PAGE_SIZE on client */
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 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);
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) {
429 /* if enough space, pretend it was granted */
431 rnb[n].flags |= OBD_BRW_GRANTED;
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;
458 exp->exp_obd->u.filter.fo_tot_pending += used;
461 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
462 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
463 ungranted, fed->fed_grant, fed->fed_dirty);
465 /* Rough calc in case we don't refresh cached statfs data */
466 using = (used + ungranted + 1 ) >>
467 exp->exp_obd->u.filter.fo_sb->s_blocksize_bits;
468 if (exp->exp_obd->obd_osfs.os_bavail > using)
469 exp->exp_obd->obd_osfs.os_bavail -= using;
471 exp->exp_obd->obd_osfs.os_bavail = 0;
473 if (fed->fed_dirty < used) {
474 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
475 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
476 used, fed->fed_dirty);
477 used = fed->fed_dirty;
479 exp->exp_obd->u.filter.fo_tot_dirty -= used;
480 fed->fed_dirty -= used;
485 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
486 * on mulitple inodes. That isn't all, because there still exists the
487 * possibility of a truncate starting a new transaction while holding the ext3
488 * rwsem = write while some writes (which have started their transactions here)
489 * blocking on the ext3 rwsem = read => lock inversion.
491 * The handling gets very ugly when dealing with locked pages. It may be easier
492 * to just get rid of the locked page code (which has problems of its own) and
493 * either discover we do not need it anymore (i.e. it was a symptom of another
494 * bug) or ensure we get the page locks in an appropriate order. */
495 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
496 int objcount, struct obd_ioobj *obj,
497 int niocount, struct niobuf_remote *nb,
498 struct niobuf_local *res,
499 struct obd_trans_info *oti)
501 struct lvfs_run_ctxt saved;
502 struct niobuf_remote *rnb;
503 struct niobuf_local *lnb = res;
504 struct fsfilt_objinfo fso;
505 struct dentry *dentry = NULL;
508 unsigned long now = jiffies;
509 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
511 LASSERT(objcount == 1);
512 LASSERT(obj->ioo_bufcnt > 0);
514 memset(res, 0, niocount * sizeof(*res));
516 rc = filter_alloc_iobuf(OBD_BRW_READ, obj->ioo_bufcnt, &iobuf);
521 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
522 dentry = filter_id2dentry(exp->exp_obd, NULL, obj->ioo_gr,
525 GOTO(cleanup, rc = PTR_ERR(dentry));
529 if (dentry->d_inode == NULL) {
530 CERROR("trying to BRW to non-existent file "LPU64"\n",
532 GOTO(cleanup, rc = -ENOENT);
535 fso.fso_dentry = dentry;
536 fso.fso_bufcnt = obj->ioo_bufcnt;
538 fsfilt_check_slow(now, obd_timeout, "preprw_write setup");
540 spin_lock(&exp->exp_obd->obd_osfs_lock);
542 filter_grant_incoming(exp, oa);
546 left = filter_grant_space_left(exp);
548 rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
549 &left, dentry->d_inode);
550 if (oa && oa->o_valid & OBD_MD_FLGRANT)
551 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
553 /* We're finishing using body->oa as an input variable, so reset
557 spin_unlock(&exp->exp_obd->obd_osfs_lock);
562 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
564 /* We still set up for ungranted pages so that granted pages
565 * can be written to disk as they were promised, and portals
566 * needs to keep the pages all aligned properly. */
567 lnb->dentry = dentry;
568 lnb->offset = rnb->offset;
570 lnb->flags = rnb->flags;
572 rc = filter_alloc_dio_page(exp->exp_obd, dentry->d_inode,lnb);
574 CERROR("page err %u@"LPU64" %u/%u %p: rc %d\n",
575 lnb->len, lnb->offset,
576 i, obj->ioo_bufcnt, dentry, rc);
581 /* If the filter writes a partial page, then has the file
582 * extended, the client will read in the whole page. the
583 * filter has to be careful to zero the rest of the partial
584 * page on disk. we do it by hand for partial extending
585 * writes, send_bio() is responsible for zeroing pages when
586 * asked to read unmapped blocks -- brw_kiovec() does this. */
587 if (lnb->len != PAGE_SIZE) {
588 if (lnb->offset + lnb->len < dentry->d_inode->i_size) {
589 filter_iobuf_add_page(exp->exp_obd, iobuf,
593 memset(kmap(lnb->page) + lnb->len, 0,
594 PAGE_SIZE - lnb->len);
599 tot_bytes += lnb->len;
602 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
605 fsfilt_check_slow(now, obd_timeout, "start_page_write");
607 lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
611 switch(cleanup_phase) {
614 filter_free_dio_pages(objcount, obj, niocount, res);
616 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
617 filter_free_iobuf(iobuf);
623 spin_lock(&exp->exp_obd->obd_osfs_lock);
625 filter_grant_incoming(exp, oa);
626 spin_unlock(&exp->exp_obd->obd_osfs_lock);
627 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
628 filter_free_iobuf(iobuf);
636 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
637 int objcount, struct obd_ioobj *obj, int niocount,
638 struct niobuf_remote *nb, struct niobuf_local *res,
639 struct obd_trans_info *oti)
641 if (cmd == OBD_BRW_WRITE)
642 return filter_preprw_write(cmd, exp, oa, objcount, obj,
643 niocount, nb, res, oti);
645 if (cmd == OBD_BRW_READ)
646 return filter_preprw_read(cmd, exp, oa, objcount, obj,
647 niocount, nb, res, oti);
653 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
659 (inode->i_size > filter->fo_readcache_max_filesize))
662 /* drop from cache like truncate_list_pages() */
663 if (drop && !TryLockPage(page)) {
665 ll_truncate_complete_page(page);
668 page_cache_release(page);
671 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
672 int objcount, struct obd_ioobj *obj,
673 int niocount, struct niobuf_local *res,
674 struct obd_trans_info *oti, int rc)
676 struct inode *inode = NULL;
679 if (res->dentry != NULL)
680 inode = res->dentry->d_inode;
682 filter_free_dio_pages(objcount, obj, niocount, res);
684 if (res->dentry != NULL)
689 void flip_into_page_cache(struct inode *inode, struct page *new_page)
691 struct page *old_page;
695 /* the dlm is protecting us from read/write concurrency, so we
696 * expect this find_lock_page to return quickly. even if we
697 * race with another writer it won't be doing much work with
698 * the page locked. we do this 'cause t_c_p expects a
699 * locked page, and it wants to grab the pagecache lock
701 old_page = find_lock_page(inode->i_mapping, new_page->index);
703 ll_truncate_complete_page(old_page);
704 unlock_page(old_page);
705 page_cache_release(old_page);
708 #if 0 /* this should be a /proc tunable someday */
709 /* racing o_directs (no locking ioctl) could race adding
710 * their pages, so we repeat the page invalidation unless
711 * we successfully added our new page */
712 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
714 page_hash(inode->i_mapping,
717 /* add_to_page_cache clears uptodate|dirty and locks
719 SetPageUptodate(new_page);
720 unlock_page(new_page);
728 void filter_grant_commit(struct obd_export *exp, int niocount,
729 struct niobuf_local *res)
731 struct filter_obd *filter = &exp->exp_obd->u.filter;
732 struct niobuf_local *lnb = res;
733 unsigned long pending = 0;
736 spin_lock(&exp->exp_obd->obd_osfs_lock);
737 for (i = 0, lnb = res; i < niocount; i++, lnb++)
738 pending += lnb->lnb_grant_used;
740 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
741 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
742 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
743 exp->exp_filter_data.fed_pending, pending);
744 exp->exp_filter_data.fed_pending -= pending;
745 LASSERTF(filter->fo_tot_granted >= pending,
746 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
747 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
748 exp->exp_obd->u.filter.fo_tot_granted, pending);
749 filter->fo_tot_granted -= pending;
750 LASSERTF(filter->fo_tot_pending >= pending,
751 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
752 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
753 filter->fo_tot_pending, pending);
754 filter->fo_tot_pending -= pending;
756 spin_unlock(&exp->exp_obd->obd_osfs_lock);
758 int filter_do_cow(struct obd_export *exp, struct obd_ioobj *obj,
759 int nioo, struct niobuf_remote *rnb)
761 struct dentry *dentry;
762 struct lvfs_run_ctxt saved;
763 struct write_extents *extents = NULL;
764 int j, rc = 0, numexts = 0, flags = 0;
770 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
772 dentry = filter_id2dentry(exp->exp_obd, NULL, obj->ioo_gr,
774 if (IS_ERR(dentry)) {
775 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
776 RETURN (PTR_ERR(dentry));
779 if (dentry->d_inode == NULL) {
780 CERROR("trying to write extents to non-existent file "LPU64"\n",
782 GOTO(cleanup, rc = -ENOENT);
785 flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
786 if (!(flags & SM_DO_COW)) {
789 OBD_ALLOC(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
791 CERROR("No Memory\n");
792 GOTO(cleanup, rc = -ENOMEM);
794 for (j = 0; j < obj->ioo_bufcnt; j++) {
795 if (rnb[j].len != 0) {
796 extents[numexts].w_count = rnb[j].len;
797 extents[numexts].w_pos = rnb[j].offset;
801 rc = fsfilt_do_write_cow(exp->exp_obd, dentry, extents, numexts);
803 CERROR("Do cow error id "LPU64" rc:%d \n",
810 OBD_FREE(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
813 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
817 int filter_write_extents(struct obd_export *exp, struct obd_ioobj *obj, int nobj,
818 int niocount, struct niobuf_local *local, int rc)
820 struct lvfs_run_ctxt saved;
821 struct dentry *dentry;
822 struct niobuf_local *lnb;
831 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
832 dentry = filter_id2dentry(exp->exp_obd, NULL, obj->ioo_gr,
834 if (IS_ERR(dentry)) {
835 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
836 RETURN (PTR_ERR(dentry));
839 if (dentry->d_inode == NULL) {
840 CERROR("trying to write extents to non-existent file "LPU64"\n",
842 GOTO(cleanup, rc = -ENOENT);
845 flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
846 if (!(flags & SM_DO_REC)) {
850 for (i = 0, lnb = local; i < obj->ioo_bufcnt; i++, lnb++) {
852 offset = lnb->offset;
854 } else if (lnb->offset == (offset + len)) {
857 rc = fsfilt_write_extents(exp->exp_obd, dentry,
860 CERROR("write exts off "LPU64" num %u rc:%d\n",
864 offset = lnb->offset;
869 rc = fsfilt_write_extents(exp->exp_obd, dentry,
872 CERROR("write exts off "LPU64" num %u rc:%d\n",
879 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
883 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
884 int objcount, struct obd_ioobj *obj, int niocount,
885 struct niobuf_local *res, struct obd_trans_info *oti,int rc)
887 if (cmd == OBD_BRW_WRITE)
888 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
890 if (cmd == OBD_BRW_READ)
891 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
897 int filter_brw(int cmd, struct obd_export *exp, struct obdo *oa,
898 struct lov_stripe_md *lsm, obd_count oa_bufs,
899 struct brw_page *pga, struct obd_trans_info *oti)
901 struct obd_ioobj ioo;
902 struct niobuf_local *lnb;
903 struct niobuf_remote *rnb;
908 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
909 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
911 if (lnb == NULL || rnb == NULL)
912 GOTO(out, ret = -ENOMEM);
914 for (i = 0; i < oa_bufs; i++) {
915 rnb[i].offset = pga[i].disk_offset;
916 rnb[i].len = pga[i].count;
919 obdo_to_ioobj(oa, &ioo);
920 ioo.ioo_bufcnt = oa_bufs;
922 ret = filter_preprw(cmd, exp, oa, 1, &ioo, oa_bufs, rnb, lnb, oti);
926 for (i = 0; i < oa_bufs; i++) {
931 if (lnb[i].page == NULL)
934 off = pga[i].disk_offset & ~PAGE_MASK;
935 virt = kmap(pga[i].pg);
936 addr = kmap(lnb[i].page);
938 /* 2 kmaps == vanishingly small deadlock opportunity */
940 if (cmd & OBD_BRW_WRITE)
941 memcpy(addr + off, virt + off, pga[i].count);
943 memcpy(virt + off, addr + off, pga[i].count);
949 ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti, ret);
953 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
955 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));