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 static int filter_alloc_dio_page(struct obd_device *obd, struct inode *inode,
41 struct niobuf_local *lnb)
47 page = alloc_pages(GFP_HIGHUSER, 0);
49 CERROR("no memory for a temp page\n");
55 POISON_PAGE(page, 0xf1);
56 if (lnb->len != PAGE_SIZE) {
57 memset(kmap(page) + lnb->len, 0, PAGE_SIZE - lnb->len);
61 page->index = lnb->offset >> PAGE_SHIFT;
68 void filter_free_dio_pages(int objcount, struct obd_ioobj *obj,
69 int niocount, struct niobuf_local *res)
73 for (i = 0; i < objcount; i++, obj++) {
74 for (j = 0 ; j < obj->ioo_bufcnt ; j++, res++) {
75 if (res->page != NULL) {
76 __free_page(res->page);
83 /* Grab the dirty and seen grant announcements from the incoming obdo.
84 * We will later calculate the clients new grant and return it.
85 * Caller must hold osfs lock */
86 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
88 struct filter_export_data *fed;
89 struct obd_device *obd = exp->exp_obd;
90 static unsigned long last_msg;
91 static int last_count;
95 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
97 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
98 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
99 oa->o_valid &= ~OBD_MD_FLGRANT;
104 fed = &exp->exp_filter_data;
106 /* Don't print this to the console the first time it happens, since
107 * it can happen legitimately on occasion, but only rarely. */
108 if (time_after(jiffies, last_msg + 60 * HZ)) {
112 if ((last_count & (-last_count)) == last_count)
116 /* Add some margin, since there is a small race if other RPCs arrive
117 * out-or-order and have already consumed some grant. We want to
118 * leave this here in case there is a large error in accounting. */
119 CDEBUG(oa->o_grant > fed->fed_grant + FILTER_GRANT_CHUNK ? mask:D_CACHE,
120 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
121 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
122 oa->o_dropped, fed->fed_grant);
124 /* Update our accounting now so that statfs takes it into account.
125 * Note that fed_dirty is only approximate and can become incorrect
126 * if RPCs arrive out-of-order. No important calculations depend
127 * on fed_dirty however. */
128 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
129 if (fed->fed_grant < oa->o_dropped) {
130 CERROR("%s: cli %s/%p reports %u dropped > fed_grant %lu\n",
131 obd->obd_name, exp->exp_client_uuid.uuid, exp,
132 oa->o_dropped, fed->fed_grant);
135 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
136 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
137 obd->obd_name, exp->exp_client_uuid.uuid, exp,
138 oa->o_dropped, obd->u.filter.fo_tot_granted);
141 obd->u.filter.fo_tot_granted -= oa->o_dropped;
142 fed->fed_grant -= oa->o_dropped;
143 fed->fed_dirty = oa->o_dirty;
147 #define GRANT_FOR_LLOG(obd) 16
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.filter.fo_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 (time_before(obd->obd_osfs_age, jiffies - HZ)) {
165 rc = fsfilt_statfs(obd, obd->u.filter.fo_sb, jiffies + 1);
166 if (rc) /* N.B. statfs can't really fail */
171 avail = obd->obd_osfs.os_bavail;
172 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
173 if (left > GRANT_FOR_LLOG(obd)) {
174 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
176 left = 0 /* << blockbits */;
179 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
180 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
184 if (left >= tot_granted) {
187 static unsigned long next;
188 if (left < tot_granted - obd->u.filter.fo_tot_pending &&
189 time_after(jiffies, next)) {
190 spin_unlock(&obd->obd_osfs_lock);
191 CERROR("%s: cli %s/%p grant "LPU64" > available "
192 LPU64" and pending "LPU64"\n", obd->obd_name,
193 exp->exp_client_uuid.uuid, exp, tot_granted,
194 left, obd->u.filter.fo_tot_pending);
196 portals_debug_dumplog();
197 next = jiffies + 20 * HZ;
198 spin_lock(&obd->obd_osfs_lock);
203 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
204 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
205 exp->exp_client_uuid.uuid, exp,
206 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
207 tot_granted, left, obd->u.filter.fo_tot_pending);
212 /* Calculate how much grant space to allocate to this client, based on how
213 * much space is currently free and how much of that is already granted.
215 * Caller must hold obd_osfs_lock. */
216 long filter_grant(struct obd_export *exp, obd_size current_grant,
217 obd_size want, obd_size fs_space_left)
219 struct obd_device *obd = exp->exp_obd;
220 struct filter_export_data *fed = &exp->exp_filter_data;
221 int blockbits = obd->u.filter.fo_sb->s_blocksize_bits;
224 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
226 /* Grant some fraction of the client's requested grant space so that
227 * they are not always waiting for write credits (not all of it to
228 * avoid overgranting in face of multiple RPCs in flight). This
229 * essentially will be able to control the OSC_MAX_RIF for a client.
231 * If we do have a large disparity between what the client thinks it
232 * has and what we think it has, don't grant very much and let the
233 * client consume its grant first. Either it just has lots of RPCs
234 * in flight, or it was evicted and its grants will soon be used up. */
235 if (current_grant < want &&
236 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
237 grant = min((want >> blockbits) / 2,
238 (fs_space_left >> blockbits) / 8);
242 if (grant > FILTER_GRANT_CHUNK)
243 grant = FILTER_GRANT_CHUNK;
245 obd->u.filter.fo_tot_granted += grant;
246 fed->fed_grant += grant;
250 CDEBUG(D_CACHE,"%s: cli %s/%p wants: "LPU64" granting: "LPU64"\n",
251 obd->obd_name, exp->exp_client_uuid.uuid, exp, want, 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);
262 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
263 int objcount, struct obd_ioobj *obj,
264 int niocount, struct niobuf_remote *nb,
265 struct niobuf_local *res,
266 struct obd_trans_info *oti)
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 LASSERTF(objcount == 1, "%d\n", objcount);
282 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
284 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
285 spin_lock(&obd->obd_osfs_lock);
286 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 if (time_after(jiffies, now + 15 * HZ))
313 CERROR("slow preprw_read setup %lus\n", (jiffies - now) / HZ);
315 CDEBUG(D_INFO, "preprw_read setup: %lu jiffies\n",
318 for (i = 0, lnb = res, rnb = nb; i < obj->ioo_bufcnt;
320 lnb->dentry = dentry;
321 lnb->offset = rnb->offset;
323 lnb->flags = rnb->flags;
325 if (inode->i_size <= rnb->offset)
326 /* If there's no more data, abort early.
327 * lnb->page == NULL and lnb->rc == 0, so it's
328 * easy to detect later. */
331 rc = filter_alloc_dio_page(obd, inode, lnb);
333 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
334 "page err %u@"LPU64" %u/%u %p: rc %d\n",
335 lnb->len, lnb->offset, i, obj->ioo_bufcnt,
340 if (inode->i_size < lnb->offset + lnb->len - 1)
341 lnb->rc = inode->i_size - lnb->offset;
345 tot_bytes += lnb->rc;
347 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
350 if (time_after(jiffies, now + 15 * HZ))
351 CERROR("slow start_page_read %lus\n", (jiffies - now) / HZ);
353 CDEBUG(D_INFO, "start_page_read: %lu jiffies\n",
356 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
361 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
363 filter_tally_read(&exp->exp_obd->u.filter, res, niocount);
369 filter_free_dio_pages(objcount, obj, niocount, res);
374 CERROR("NULL dentry in cleanup -- tell CFS\n");
378 filter_free_iobuf(iobuf);
380 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
382 CERROR("io error %d\n", rc);
386 /* When clients have dirtied as much space as they've been granted they
387 * fall through to sync writes. These sync writes haven't been expressed
388 * in grants and need to error with ENOSPC when there isn't room in the
389 * filesystem for them after grants are taken into account. However,
390 * writeback of the dirty data that was already granted space can write
393 * Caller must hold obd_osfs_lock. */
394 static int filter_grant_check(struct obd_export *exp, int objcount,
395 struct fsfilt_objinfo *fso, int niocount,
396 struct niobuf_remote *rnb,
397 struct niobuf_local *lnb, obd_size *left,
400 struct filter_export_data *fed = &exp->exp_filter_data;
401 int blocksize = exp->exp_obd->u.filter.fo_sb->s_blocksize;
402 unsigned long used = 0, ungranted = 0, using;
403 int i, rc = -ENOSPC, obj, n = 0, mask = D_CACHE;
405 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
407 for (obj = 0; obj < objcount; obj++) {
408 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
411 /* FIXME: this is calculated with PAGE_SIZE on client */
413 bytes += rnb[n].offset & (blocksize - 1);
414 tmp = (rnb[n].offset + rnb[n].len) & (blocksize - 1);
416 bytes += blocksize - tmp;
418 if (rnb[n].flags & OBD_BRW_FROM_GRANT) {
419 if (fed->fed_grant < used + bytes) {
421 "%s: cli %s/%p claims %ld+%d "
422 "GRANT, real grant %lu idx %d\n",
423 exp->exp_obd->obd_name,
424 exp->exp_client_uuid.uuid, exp,
425 used, bytes, fed->fed_grant, n);
429 rnb[n].flags |= OBD_BRW_GRANTED;
430 lnb[n].lnb_grant_used = bytes;
431 CDEBUG(0, "idx %d used=%lu\n", n, used);
436 if (*left > ungranted) {
437 /* if enough space, pretend it was granted */
439 rnb[n].flags |= OBD_BRW_GRANTED;
440 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
445 /* We can't check for already-mapped blocks here, as
446 * it requires dropping the osfs lock to do the bmap.
447 * Instead, we return ENOSPC and in that case we need
448 * to go through and verify if all of the blocks not
449 * marked BRW_GRANTED are already mapped and we can
450 * ignore this error. */
452 rnb[n].flags &= OBD_BRW_GRANTED;
453 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
454 exp->exp_obd->obd_name,
455 exp->exp_client_uuid.uuid, exp, n, bytes);
459 /* Now substract what client have used already. We don't subtract
460 * this from the tot_granted yet, so that other client's can't grab
461 * that space before we have actually allocated our blocks. That
462 * happens in filter_grant_commit() after the writes are done. */
464 fed->fed_grant -= used;
465 fed->fed_pending += used;
466 exp->exp_obd->u.filter.fo_tot_pending += used;
469 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
470 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
471 ungranted, fed->fed_grant, fed->fed_dirty);
473 /* Rough calc in case we don't refresh cached statfs data */
474 using = (used + ungranted + 1 ) >>
475 exp->exp_obd->u.filter.fo_sb->s_blocksize_bits;
476 if (exp->exp_obd->obd_osfs.os_bavail > using)
477 exp->exp_obd->obd_osfs.os_bavail -= using;
479 exp->exp_obd->obd_osfs.os_bavail = 0;
481 if (fed->fed_dirty < used) {
482 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
483 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
484 used, fed->fed_dirty);
485 used = fed->fed_dirty;
487 exp->exp_obd->u.filter.fo_tot_dirty -= used;
488 fed->fed_dirty -= used;
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)
509 struct lvfs_run_ctxt saved;
510 struct niobuf_remote *rnb;
511 struct niobuf_local *lnb = res;
512 struct fsfilt_objinfo fso;
513 struct dentry *dentry = NULL;
516 unsigned long now = jiffies;
517 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
519 LASSERT(objcount == 1);
520 LASSERT(obj->ioo_bufcnt > 0);
522 memset(res, 0, niocount * sizeof(*res));
524 rc = filter_alloc_iobuf(OBD_BRW_READ, obj->ioo_bufcnt, &iobuf);
529 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
530 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
533 GOTO(cleanup, rc = PTR_ERR(dentry));
537 if (dentry->d_inode == NULL) {
538 CERROR("trying to BRW to non-existent file "LPU64"\n",
541 GOTO(cleanup, rc = -ENOENT);
544 fso.fso_dentry = dentry;
545 fso.fso_bufcnt = obj->ioo_bufcnt;
547 if (time_after(jiffies, now + 15 * HZ))
548 CERROR("slow preprw_write setup %lus\n", (jiffies - now) / HZ);
550 CDEBUG(D_INFO, "preprw_write setup: %lu jiffies\n",
553 spin_lock(&exp->exp_obd->obd_osfs_lock);
555 filter_grant_incoming(exp, oa);
559 left = filter_grant_space_left(exp);
561 rc = filter_grant_check(exp, objcount, &fso, niocount, nb, res,
562 &left, dentry->d_inode);
563 if (oa && oa->o_valid & OBD_MD_FLGRANT)
564 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
566 spin_unlock(&exp->exp_obd->obd_osfs_lock);
571 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt;
573 /* We still set up for ungranted pages so that granted pages
574 * can be written to disk as they were promised, and portals
575 * needs to keep the pages all aligned properly. */
576 lnb->dentry = dentry;
577 lnb->offset = rnb->offset;
579 lnb->flags = rnb->flags;
581 rc = filter_alloc_dio_page(exp->exp_obd, dentry->d_inode,lnb);
583 CERROR("page err %u@"LPU64" %u/%u %p: rc %d\n",
584 lnb->len, lnb->offset,
585 i, obj->ioo_bufcnt, dentry, rc);
590 /* If the filter writes a partial page, then has the file
591 * extended, the client will read in the whole page. the
592 * filter has to be careful to zero the rest of the partial
593 * page on disk. we do it by hand for partial extending
594 * writes, send_bio() is responsible for zeroing pages when
595 * asked to read unmapped blocks -- brw_kiovec() does this. */
596 if (lnb->len != PAGE_SIZE) {
597 if (lnb->offset + lnb->len < dentry->d_inode->i_size) {
598 filter_iobuf_add_page(exp->exp_obd, iobuf,
602 memset(kmap(lnb->page) + lnb->len, 0,
603 PAGE_SIZE - lnb->len);
608 tot_bytes += lnb->len;
611 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
614 if (time_after(jiffies, now + 15 * HZ))
615 CERROR("slow start_page_write %lus\n", (jiffies - now) / HZ);
617 CDEBUG(D_INFO, "start_page_write: %lu jiffies\n",
620 lprocfs_counter_add(exp->exp_obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
624 switch(cleanup_phase) {
627 filter_free_dio_pages(objcount, obj, niocount, res);
629 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
630 filter_free_iobuf(iobuf);
636 spin_lock(&exp->exp_obd->obd_osfs_lock);
638 filter_grant_incoming(exp, oa);
639 spin_unlock(&exp->exp_obd->obd_osfs_lock);
640 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
641 filter_free_iobuf(iobuf);
649 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
650 int objcount, struct obd_ioobj *obj, int niocount,
651 struct niobuf_remote *nb, struct niobuf_local *res,
652 struct obd_trans_info *oti)
654 if (cmd == OBD_BRW_WRITE)
655 return filter_preprw_write(cmd, exp, oa, objcount, obj,
656 niocount, nb, res, oti);
658 if (cmd == OBD_BRW_READ)
659 return filter_preprw_read(cmd, exp, oa, objcount, obj,
660 niocount, nb, res, oti);
666 void filter_release_read_page(struct filter_obd *filter, struct inode *inode,
672 (inode->i_size > filter->fo_readcache_max_filesize))
675 /* drop from cache like truncate_list_pages() */
676 if (drop && !TryLockPage(page)) {
678 ll_truncate_complete_page(page);
681 page_cache_release(page);
684 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
685 int objcount, struct obd_ioobj *obj,
686 int niocount, struct niobuf_local *res,
687 struct obd_trans_info *oti, int rc)
689 struct inode *inode = NULL;
692 if (res->dentry != NULL)
693 inode = res->dentry->d_inode;
695 filter_free_dio_pages(objcount, obj, niocount, res);
697 if (res->dentry != NULL)
702 void flip_into_page_cache(struct inode *inode, struct page *new_page)
704 struct page *old_page;
708 /* the dlm is protecting us from read/write concurrency, so we
709 * expect this find_lock_page to return quickly. even if we
710 * race with another writer it won't be doing much work with
711 * the page locked. we do this 'cause t_c_p expects a
712 * locked page, and it wants to grab the pagecache lock
714 old_page = find_lock_page(inode->i_mapping, new_page->index);
716 ll_truncate_complete_page(old_page);
717 unlock_page(old_page);
718 page_cache_release(old_page);
721 #if 0 /* this should be a /proc tunable someday */
722 /* racing o_directs (no locking ioctl) could race adding
723 * their pages, so we repeat the page invalidation unless
724 * we successfully added our new page */
725 rc = add_to_page_cache_unique(new_page, inode->i_mapping,
727 page_hash(inode->i_mapping,
730 /* add_to_page_cache clears uptodate|dirty and locks
732 SetPageUptodate(new_page);
733 unlock_page(new_page);
741 void filter_grant_commit(struct obd_export *exp, int niocount,
742 struct niobuf_local *res)
744 struct filter_obd *filter = &exp->exp_obd->u.filter;
745 struct niobuf_local *lnb = res;
746 unsigned long pending = 0;
749 spin_lock(&exp->exp_obd->obd_osfs_lock);
750 for (i = 0, lnb = res; i < niocount; i++, lnb++)
751 pending += lnb->lnb_grant_used;
753 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
754 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
755 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
756 exp->exp_filter_data.fed_pending, pending);
757 exp->exp_filter_data.fed_pending -= pending;
758 LASSERTF(filter->fo_tot_granted >= pending,
759 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
760 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
761 exp->exp_obd->u.filter.fo_tot_granted, pending);
762 filter->fo_tot_granted -= pending;
763 LASSERTF(filter->fo_tot_pending >= pending,
764 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
765 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
766 filter->fo_tot_pending, pending);
767 filter->fo_tot_pending -= pending;
769 spin_unlock(&exp->exp_obd->obd_osfs_lock);
771 int filter_do_cow(struct obd_export *exp, struct obd_ioobj *obj,
772 int nioo, struct niobuf_remote *rnb)
774 struct dentry *dentry;
775 struct lvfs_run_ctxt saved;
776 struct write_extents *extents = NULL;
777 int j, rc = 0, numexts = 0, flags = 0;
783 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
785 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
787 if (IS_ERR(dentry)) {
788 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
789 RETURN (PTR_ERR(dentry));
792 if (dentry->d_inode == NULL) {
793 CERROR("trying to write extents to non-existent file "LPU64"\n",
795 GOTO(cleanup, rc = -ENOENT);
798 flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
799 if (!(flags & SM_DO_COW)) {
802 OBD_ALLOC(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
804 CERROR("No Memory\n");
805 GOTO(cleanup, rc = -ENOMEM);
807 for (j = 0; j < obj->ioo_bufcnt; j++) {
808 if (rnb[j].len != 0) {
809 extents[numexts].w_count = rnb[j].len;
810 extents[numexts].w_pos = rnb[j].offset;
814 rc = fsfilt_do_write_cow(exp->exp_obd, dentry, extents, numexts);
816 CERROR("Do cow error id "LPU64" rc:%d \n",
823 OBD_FREE(extents, obj->ioo_bufcnt * sizeof(struct write_extents));
826 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
830 int filter_write_extents(struct obd_export *exp, struct obd_ioobj *obj, int nobj,
831 int niocount, struct niobuf_local *local, int rc)
833 struct lvfs_run_ctxt saved;
834 struct dentry *dentry;
835 struct niobuf_local *lnb;
844 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
846 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
848 if (IS_ERR(dentry)) {
849 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
850 RETURN (PTR_ERR(dentry));
853 if (dentry->d_inode == NULL) {
854 CERROR("trying to write extents to non-existent file "LPU64"\n",
856 GOTO(cleanup, rc = -ENOENT);
859 flags = fsfilt_get_fs_flags(exp->exp_obd, dentry);
860 if (!(flags & SM_DO_REC)) {
864 for (i = 0, lnb = local; i < obj->ioo_bufcnt; i++, lnb++) {
866 offset = lnb->offset;
868 } else if (lnb->offset == (offset + len)) {
871 rc = fsfilt_write_extents(exp->exp_obd, dentry,
874 CERROR("write exts off "LPU64" num %u rc:%d\n",
878 offset = lnb->offset;
883 rc = fsfilt_write_extents(exp->exp_obd, dentry,
886 CERROR("write exts off "LPU64" num %u rc:%d\n",
893 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
897 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
898 int objcount, struct obd_ioobj *obj, int niocount,
899 struct niobuf_local *res, struct obd_trans_info *oti,int rc)
901 if (cmd == OBD_BRW_WRITE)
902 return filter_commitrw_write(exp, oa, objcount, obj, niocount,
904 if (cmd == OBD_BRW_READ)
905 return filter_commitrw_read(exp, oa, objcount, obj, niocount,
911 int filter_brw(int cmd, struct obd_export *exp, struct obdo *oa,
912 struct lov_stripe_md *lsm, obd_count oa_bufs,
913 struct brw_page *pga, struct obd_trans_info *oti)
915 struct obd_ioobj ioo;
916 struct niobuf_local *lnb;
917 struct niobuf_remote *rnb;
922 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
923 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
925 if (lnb == NULL || rnb == NULL)
926 GOTO(out, ret = -ENOMEM);
928 for (i = 0; i < oa_bufs; i++) {
929 rnb[i].offset = pga[i].disk_offset;
930 rnb[i].len = pga[i].count;
933 obdo_to_ioobj(oa, &ioo);
934 ioo.ioo_bufcnt = oa_bufs;
936 ret = filter_preprw(cmd, exp, oa, 1, &ioo, oa_bufs, rnb, lnb, oti);
940 for (i = 0; i < oa_bufs; i++) {
945 if (lnb[i].page == NULL)
948 off = pga[i].disk_offset & ~PAGE_MASK;
949 virt = kmap(pga[i].pg);
950 addr = kmap(lnb[i].page);
952 /* 2 kmaps == vanishingly small deadlock opportunity */
954 if (cmd & OBD_BRW_WRITE)
955 memcpy(addr + off, virt + off, pga[i].count);
957 memcpy(virt + off, addr + off, pga[i].count);
963 ret = filter_commitrw(cmd, exp, oa, 1, &ioo, oa_bufs, lnb, oti, ret);
967 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
969 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));