1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdfilter/filter_io.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_FILTER
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/pagemap.h> // XXX kill me soon
50 #include <linux/version.h>
52 #include <obd_class.h>
54 #include <lustre_fsfilt.h>
55 #include "filter_internal.h"
57 int *obdfilter_created_scratchpad;
59 /* Grab the dirty and seen grant announcements from the incoming obdo.
60 * We will later calculate the clients new grant and return it.
61 * Caller must hold osfs lock */
62 static void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
64 struct filter_export_data *fed;
65 struct obd_device *obd = exp->exp_obd;
68 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
70 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
71 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
72 oa->o_valid &= ~OBD_MD_FLGRANT;
77 fed = &exp->exp_filter_data;
79 /* Add some margin, since there is a small race if other RPCs arrive
80 * out-or-order and have already consumed some grant. We want to
81 * leave this here in case there is a large error in accounting. */
83 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
84 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
85 oa->o_dropped, fed->fed_grant);
87 /* Update our accounting now so that statfs takes it into account.
88 * Note that fed_dirty is only approximate and can become incorrect
89 * if RPCs arrive out-of-order. No important calculations depend
90 * on fed_dirty however, but we must check sanity to not assert. */
91 if ((long long)oa->o_dirty < 0)
93 else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
94 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
95 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
96 if (fed->fed_grant < oa->o_dropped) {
97 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
98 obd->obd_name, exp->exp_client_uuid.uuid, exp,
99 oa->o_dropped, fed->fed_grant);
102 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
103 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
104 obd->obd_name, exp->exp_client_uuid.uuid, exp,
105 oa->o_dropped, obd->u.filter.fo_tot_granted);
108 obd->u.filter.fo_tot_granted -= oa->o_dropped;
109 fed->fed_grant -= oa->o_dropped;
110 fed->fed_dirty = oa->o_dirty;
111 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
112 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
113 obd->obd_name, exp->exp_client_uuid.uuid, exp,
114 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
115 spin_unlock(&obd->obd_osfs_lock);
121 /* Figure out how much space is available between what we've granted
122 * and what remains in the filesystem. Compensate for ext3 indirect
123 * block overhead when computing how much free space is left ungranted.
125 * Caller must hold obd_osfs_lock. */
126 obd_size filter_grant_space_left(struct obd_export *exp)
128 struct obd_device *obd = exp->exp_obd;
129 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
130 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
131 int rc, statfs_done = 0;
133 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
135 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
137 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
138 cfs_time_current_64() + HZ);
139 if (rc) /* N.B. statfs can't really fail */
144 avail = obd->obd_osfs.os_bavail;
145 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
146 if (left > GRANT_FOR_LLOG(obd)) {
147 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
149 left = 0 /* << blockbits */;
152 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
153 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
157 if (left >= tot_granted) {
160 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
161 CERROR("%s: cli %s/%p grant "LPU64" > available "
162 LPU64" and pending "LPU64"\n", obd->obd_name,
163 exp->exp_client_uuid.uuid, exp, tot_granted,
164 left, obd->u.filter.fo_tot_pending);
169 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
170 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
171 exp->exp_client_uuid.uuid, exp,
172 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
173 tot_granted, left, obd->u.filter.fo_tot_pending);
178 /* Calculate how much grant space to allocate to this client, based on how
179 * much space is currently free and how much of that is already granted.
181 * Caller must hold obd_osfs_lock. */
182 long filter_grant(struct obd_export *exp, obd_size current_grant,
183 obd_size want, obd_size fs_space_left)
185 struct obd_device *obd = exp->exp_obd;
186 struct filter_export_data *fed = &exp->exp_filter_data;
187 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
190 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
192 /* Grant some fraction of the client's requested grant space so that
193 * they are not always waiting for write credits (not all of it to
194 * avoid overgranting in face of multiple RPCs in flight). This
195 * essentially will be able to control the OSC_MAX_RIF for a client.
197 * If we do have a large disparity between what the client thinks it
198 * has and what we think it has, don't grant very much and let the
199 * client consume its grant first. Either it just has lots of RPCs
200 * in flight, or it was evicted and its grants will soon be used up. */
201 if (want > 0x7fffffff) {
202 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
203 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
204 } else if (current_grant < want &&
205 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
206 grant = min((want >> blockbits),
207 (fs_space_left >> blockbits) / 8);
211 /* Allow >FILTER_GRANT_CHUNK size when clients
212 * reconnect due to a server reboot.
214 if ((grant > FILTER_GRANT_CHUNK) &&
215 (!obd->obd_recovering))
216 grant = FILTER_GRANT_CHUNK;
218 obd->u.filter.fo_tot_granted += grant;
219 fed->fed_grant += grant;
220 if (fed->fed_grant < 0) {
221 CERROR("%s: cli %s/%p grant %ld want "LPU64
223 obd->obd_name, exp->exp_client_uuid.uuid,
224 exp, fed->fed_grant, want,current_grant);
225 spin_unlock(&obd->obd_osfs_lock);
232 "%s: cli %s/%p wants: "LPU64" current grant "LPU64
233 " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
234 exp, want, current_grant, grant);
236 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
237 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
238 exp, obd->u.filter.fo_tot_dirty,
239 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
245 * the routine is used to request pages from pagecache
247 * use GFP_NOFS not allowing to enter FS as the client can run on this node
248 * and we might end waiting on a page he sent in the request we're serving.
250 * use NORETRY so that the allocator doesn't go crazy: chance to more lucky
251 * thread have enough memory to complete his request. for our request client
252 * will do resend hopefully -bzzz
254 static struct page * filter_get_page(struct obd_device *obd,
260 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
261 GFP_NOFS | __GFP_NORETRY);
262 if (unlikely(page == NULL))
263 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_NO_PAGE, 1);
269 * the routine initializes array of local_niobuf from remote_niobuf
271 static int filter_map_remote_to_local(int objcount, struct obd_ioobj *obj,
272 struct niobuf_remote *nb,
273 int *nrpages, struct niobuf_local *res)
275 struct niobuf_remote *rnb;
276 struct niobuf_local *lnb;
280 /* we don't support multiobject RPC yet
281 * ost_brw_read() and ost_brw_write() check this */
282 LASSERT(objcount == 1);
286 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt; i++, rnb++) {
287 obd_off offset = rnb->offset;
288 unsigned int len = rnb->len;
291 int poff = offset & (CFS_PAGE_SIZE - 1);
292 int plen = CFS_PAGE_SIZE - poff;
294 if (*nrpages >= max) {
295 CERROR("small array of local bufs: %d\n", max);
301 lnb->offset = offset;
303 lnb->flags = rnb->flags;
306 lnb->lnb_grant_used = 0;
308 LASSERTF(plen <= len, "plen %u, len %u\n", plen, len);
319 * the function is used to free all pages used for request
320 * just to mimic cacheless OSS which don't occupy much memory
322 void filter_invalidate_cache(struct obd_device *obd, struct obd_ioobj *obj,
323 struct niobuf_remote *nb, struct inode *inode)
325 struct niobuf_remote *rnb;
328 LASSERT(inode != NULL);
330 for (i = 0, rnb = nb; i < obj->ioo_bufcnt; i++, rnb++) {
331 obd_off start = rnb->offset >> CFS_PAGE_SHIFT;
332 obd_off end = (rnb->offset + rnb->len) >> CFS_PAGE_SHIFT;
333 invalidate_mapping_pages(inode->i_mapping, start, end);
334 /* just to avoid warnings */
341 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
342 int objcount, struct obd_ioobj *obj,
343 struct niobuf_remote *nb,
344 int *pages, struct niobuf_local *res,
345 struct obd_trans_info *oti,
346 struct lustre_capa *capa)
348 struct obd_device *obd = exp->exp_obd;
349 struct filter_obd *fo = &obd->u.filter;
350 struct timeval start, end;
351 struct lvfs_run_ctxt saved;
352 struct niobuf_local *lnb;
353 struct dentry *dentry = NULL;
354 struct inode *inode = NULL;
356 int rc = 0, i, tot_bytes = 0;
357 unsigned long now = jiffies;
361 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
362 * When we do this function's dentry cleanup will need to be fixed.
363 * These values are verified in ost_brw_write() from the wire. */
364 LASSERTF(objcount == 1, "%d\n", objcount);
365 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
367 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
372 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
373 spin_lock(&obd->obd_osfs_lock);
374 filter_grant_incoming(exp, oa);
377 spin_unlock(&obd->obd_osfs_lock);
380 iobuf = filter_iobuf_get(&obd->u.filter, oti);
382 RETURN(PTR_ERR(iobuf));
384 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
385 dentry = filter_oa2dentry(obd, oa);
386 if (IS_ERR(dentry)) {
387 rc = PTR_ERR(dentry);
392 inode = dentry->d_inode;
394 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
396 rc = filter_map_remote_to_local(objcount, obj, nb, pages, res);
400 fsfilt_check_slow(obd, now, "preprw_read setup");
402 /* find pages for all segments, fill array with them */
403 do_gettimeofday(&start);
404 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
406 lnb->dentry = dentry;
408 if (i_size_read(inode) <= lnb->offset)
409 /* If there's no more data, abort early. lnb->rc == 0,
410 * so it's easy to detect later. */
413 lnb->page = filter_get_page(obd, inode, lnb->offset);
414 if (lnb->page == NULL)
415 GOTO(cleanup, rc = -ENOMEM);
417 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
419 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
420 lnb->rc = i_size_read(inode) - lnb->offset;
424 tot_bytes += lnb->rc;
426 if (PageUptodate(lnb->page)) {
427 lprocfs_counter_add(obd->obd_stats,
428 LPROC_FILTER_CACHE_HIT, 1);
432 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_MISS, 1);
433 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
435 do_gettimeofday(&end);
436 timediff = cfs_timeval_sub(&end, &start, NULL);
437 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
439 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
440 GOTO(cleanup, rc = -ENOMEM);
442 fsfilt_check_slow(obd, now, "start_page_read");
444 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
445 exp, NULL, NULL, NULL);
449 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
451 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
452 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
453 LPROC_FILTER_READ_BYTES, tot_bytes);
458 /* unlock pages to allow access from concurrent OST_READ */
459 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
461 LASSERT(PageLocked(lnb->page));
462 unlock_page(lnb->page);
465 page_cache_release(lnb->page);
471 if (inode && (fo->fo_read_cache == 0 ||
472 i_size_read(inode) > fo->fo_readcache_max_filesize))
473 filter_invalidate_cache(obd, obj, nb, inode);
480 filter_iobuf_put(&obd->u.filter, iobuf, oti);
482 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
484 CERROR("io error %d\n", rc);
489 /* When clients have dirtied as much space as they've been granted they
490 * fall through to sync writes. These sync writes haven't been expressed
491 * in grants and need to error with ENOSPC when there isn't room in the
492 * filesystem for them after grants are taken into account. However,
493 * writeback of the dirty data that was already granted space can write
496 * Caller must hold obd_osfs_lock. */
497 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
498 int objcount, struct fsfilt_objinfo *fso,
499 int niocount, struct niobuf_local *lnb,
500 obd_size *left, struct inode *inode)
502 struct filter_export_data *fed = &exp->exp_filter_data;
503 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
504 unsigned long used = 0, ungranted = 0, using;
505 int i, rc = -ENOSPC, obj, n = 0;
507 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
509 for (obj = 0; obj < objcount; obj++) {
510 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
513 /* should match the code in osc_exit_cache */
515 bytes += lnb[n].offset & (blocksize - 1);
516 tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
518 bytes += blocksize - tmp;
520 if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
521 (oa->o_valid & OBD_MD_FLGRANT)) {
522 if (fed->fed_grant < used + bytes) {
524 "%s: cli %s/%p claims %ld+%d "
525 "GRANT, real grant %lu idx %d\n",
526 exp->exp_obd->obd_name,
527 exp->exp_client_uuid.uuid, exp,
528 used, bytes, fed->fed_grant, n);
531 lnb[n].flags |= OBD_BRW_GRANTED;
532 lnb[n].lnb_grant_used = bytes;
533 CDEBUG(0, "idx %d used=%lu\n", n, used);
538 if (*left > ungranted + bytes) {
539 /* if enough space, pretend it was granted */
541 lnb[n].flags |= OBD_BRW_GRANTED;
542 lnb[n].lnb_grant_used = bytes;
543 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
548 /* We can't check for already-mapped blocks here, as
549 * it requires dropping the osfs lock to do the bmap.
550 * Instead, we return ENOSPC and in that case we need
551 * to go through and verify if all of the blocks not
552 * marked BRW_GRANTED are already mapped and we can
553 * ignore this error. */
555 lnb[n].flags &= ~OBD_BRW_GRANTED;
556 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
557 exp->exp_obd->obd_name,
558 exp->exp_client_uuid.uuid, exp, n, bytes);
562 /* Now substract what client have used already. We don't subtract
563 * this from the tot_granted yet, so that other client's can't grab
564 * that space before we have actually allocated our blocks. That
565 * happens in filter_grant_commit() after the writes are done. */
567 fed->fed_grant -= used;
568 fed->fed_pending += used + ungranted;
569 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
570 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
573 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
574 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
575 ungranted, fed->fed_grant, fed->fed_dirty);
577 /* Rough calc in case we don't refresh cached statfs data */
578 using = (used + ungranted + 1 ) >>
579 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
580 if (exp->exp_obd->obd_osfs.os_bavail > using)
581 exp->exp_obd->obd_osfs.os_bavail -= using;
583 exp->exp_obd->obd_osfs.os_bavail = 0;
585 if (fed->fed_dirty < used) {
586 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
587 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
588 used, fed->fed_dirty);
589 used = fed->fed_dirty;
591 exp->exp_obd->u.filter.fo_tot_dirty -= used;
592 fed->fed_dirty -= used;
594 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
595 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
596 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
597 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
598 spin_unlock(&exp->exp_obd->obd_osfs_lock);
604 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
605 * on mulitple inodes. That isn't all, because there still exists the
606 * possibility of a truncate starting a new transaction while holding the ext3
607 * rwsem = write while some writes (which have started their transactions here)
608 * blocking on the ext3 rwsem = read => lock inversion.
610 * The handling gets very ugly when dealing with locked pages. It may be easier
611 * to just get rid of the locked page code (which has problems of its own) and
612 * either discover we do not need it anymore (i.e. it was a symptom of another
613 * bug) or ensure we get the page locks in an appropriate order. */
614 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
615 int objcount, struct obd_ioobj *obj,
616 struct niobuf_remote *nb, int *pages,
617 struct niobuf_local *res,
618 struct obd_trans_info *oti,
619 struct lustre_capa *capa)
621 struct obd_device *obd = exp->exp_obd;
622 struct timeval start, end;
623 struct lvfs_run_ctxt saved;
624 struct niobuf_local *lnb = res;
625 struct fsfilt_objinfo fso;
626 struct filter_mod_data *fmd;
627 struct dentry *dentry = NULL;
630 unsigned long now = jiffies, timediff;
631 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
633 LASSERT(objcount == 1);
634 LASSERT(obj->ioo_bufcnt > 0);
636 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
641 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
642 iobuf = filter_iobuf_get(&exp->exp_obd->u.filter, oti);
644 GOTO(cleanup, rc = PTR_ERR(iobuf));
647 dentry = filter_fid2dentry(exp->exp_obd, NULL, obj->ioo_gr,
650 GOTO(cleanup, rc = PTR_ERR(dentry));
653 if (dentry->d_inode == NULL) {
654 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
655 exp->exp_obd->obd_name, obj->ioo_id);
656 GOTO(cleanup, rc = -ENOENT);
659 rc = filter_map_remote_to_local(objcount, obj, nb, pages, res);
663 fsfilt_check_slow(exp->exp_obd, now, "preprw_write setup");
665 /* Don't update inode timestamps if this write is older than a
666 * setattr which modifies the timestamps. b=10150 */
667 /* XXX when we start having persistent reservations this needs to
668 * be changed to filter_fmd_get() to create the fmd if it doesn't
669 * already exist so we can store the reservation handle there. */
670 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
673 spin_lock(&exp->exp_obd->obd_osfs_lock);
674 filter_grant_incoming(exp, oa);
675 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
676 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
679 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
680 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
683 left = filter_grant_space_left(exp);
685 fso.fso_dentry = dentry;
686 fso.fso_bufcnt = *pages;
688 rc = filter_grant_check(exp, oa, objcount, &fso, *pages, res,
689 &left, dentry->d_inode);
691 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
692 * for setting UID/GID and fid EA in first write time. */
693 if (oa->o_valid & OBD_MD_FLGRANT)
694 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
696 spin_unlock(&exp->exp_obd->obd_osfs_lock);
697 filter_fmd_put(exp, fmd);
702 do_gettimeofday(&start);
703 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
705 /* We still set up for ungranted pages so that granted pages
706 * can be written to disk as they were promised, and portals
707 * needs to keep the pages all aligned properly. */
708 lnb->dentry = dentry;
710 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset);
711 if (lnb->page == NULL)
712 GOTO(cleanup, rc = -ENOMEM);
715 /* DLM locking protects us from write and truncate competing
716 * for same region, but truncate can leave dirty page in the
717 * cache. it's possible the writeout on a such a page is in
718 * progress when we access it. it's also possible that during
719 * this writeout we put new (partial) data, but then won't
720 * be able to proceed in filter_commitrw_write(). thus let's
721 * just wait for writeout completion, should be rare enough.
723 wait_on_page_writeback(lnb->page);
725 /* If the filter writes a partial page, then has the file
726 * extended, the client will read in the whole page. the
727 * filter has to be careful to zero the rest of the partial
728 * page on disk. we do it by hand for partial extending
729 * writes, send_bio() is responsible for zeroing pages when
730 * asked to read unmapped blocks -- brw_kiovec() does this. */
731 if (lnb->len != CFS_PAGE_SIZE) {
734 maxidx = ((i_size_read(dentry->d_inode) +
735 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
736 if (maxidx >= lnb->page->index) {
737 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
738 LPU64" flg %x before EOF %llu\n",
739 lnb->len, lnb->offset,lnb->flags,
740 i_size_read(dentry->d_inode));
741 filter_iobuf_add_page(exp->exp_obd, iobuf,
746 char *p = kmap(lnb->page);
748 off = lnb->offset & ~CFS_PAGE_MASK;
751 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
753 memset(p + off, 0, CFS_PAGE_SIZE - off);
758 tot_bytes += lnb->len;
760 do_gettimeofday(&end);
761 timediff = cfs_timeval_sub(&end, &start, NULL);
762 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
764 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
765 GOTO(cleanup, rc = -ENOMEM);
767 /* don't unlock pages to prevent any access */
768 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
771 fsfilt_check_slow(exp->exp_obd, now, "start_page_write");
773 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
774 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
775 LPROC_FILTER_WRITE_BYTES, tot_bytes);
778 switch(cleanup_phase) {
781 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
782 if (lnb->page != NULL) {
783 unlock_page(lnb->page);
784 page_cache_release(lnb->page);
790 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
792 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
797 filter_iobuf_put(&exp->exp_obd->u.filter, iobuf, oti);
799 spin_lock(&exp->exp_obd->obd_osfs_lock);
801 filter_grant_incoming(exp, oa);
802 spin_unlock(&exp->exp_obd->obd_osfs_lock);
803 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
810 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
811 int objcount, struct obd_ioobj *obj,
812 struct niobuf_remote *nb, int *pages,
813 struct niobuf_local *res, struct obd_trans_info *oti,
814 struct lustre_capa *capa)
816 if (cmd == OBD_BRW_WRITE)
817 return filter_preprw_write(cmd, exp, oa, objcount, obj,
818 nb, pages, res, oti, capa);
819 if (cmd == OBD_BRW_READ)
820 return filter_preprw_read(cmd, exp, oa, objcount, obj,
821 nb, pages, res, oti, capa);
826 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
827 int objcount, struct obd_ioobj *obj,
828 struct niobuf_remote *rnb,
829 int pages, struct niobuf_local *res,
830 struct obd_trans_info *oti, int rc)
832 struct inode *inode = NULL;
833 struct ldlm_res_id res_id;
834 struct ldlm_resource *resource = NULL;
835 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
836 struct niobuf_local *lnb;
840 osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
841 /* If oa != NULL then filter_preprw_read updated the inode atime
842 * and we should update the lvb so that other glimpses will also
843 * get the updated value. bug 5972 */
844 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
845 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
847 if (resource != NULL) {
848 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
849 ldlm_resource_putref(resource);
853 if (res->dentry != NULL)
854 inode = res->dentry->d_inode;
856 for (i = 0, lnb = res; i < pages; i++, lnb++) {
857 if (lnb->page != NULL) {
858 page_cache_release(lnb->page);
863 if (res->dentry != NULL)
868 void filter_grant_commit(struct obd_export *exp, int niocount,
869 struct niobuf_local *res)
871 struct filter_obd *filter = &exp->exp_obd->u.filter;
872 struct niobuf_local *lnb = res;
873 unsigned long pending = 0;
876 spin_lock(&exp->exp_obd->obd_osfs_lock);
877 for (i = 0, lnb = res; i < niocount; i++, lnb++)
878 pending += lnb->lnb_grant_used;
880 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
881 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
882 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
883 exp->exp_filter_data.fed_pending, pending);
884 exp->exp_filter_data.fed_pending -= pending;
885 LASSERTF(filter->fo_tot_granted >= pending,
886 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
887 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
888 exp->exp_obd->u.filter.fo_tot_granted, pending);
889 filter->fo_tot_granted -= pending;
890 LASSERTF(filter->fo_tot_pending >= pending,
891 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
892 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
893 filter->fo_tot_pending, pending);
894 filter->fo_tot_pending -= pending;
896 spin_unlock(&exp->exp_obd->obd_osfs_lock);
899 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
900 int objcount, struct obd_ioobj *obj,
901 struct niobuf_remote *nb, int pages,
902 struct niobuf_local *res, struct obd_trans_info *oti,
905 if (cmd == OBD_BRW_WRITE)
906 return filter_commitrw_write(exp, oa, objcount, obj,
907 nb, pages, res, oti, rc);
908 if (cmd == OBD_BRW_READ)
909 return filter_commitrw_read(exp, oa, objcount, obj,
910 nb, pages, res, oti, rc);
915 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
916 obd_count oa_bufs, struct brw_page *pga,
917 struct obd_trans_info *oti)
919 struct obd_ioobj ioo;
920 struct niobuf_local *lnb;
921 struct niobuf_remote *rnb;
926 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
927 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
929 if (lnb == NULL || rnb == NULL)
930 GOTO(out, ret = -ENOMEM);
932 for (i = 0; i < oa_bufs; i++) {
933 lnb[i].page = pga[i].pg;
934 rnb[i].offset = pga[i].off;
935 rnb[i].len = pga[i].count;
938 obdo_to_ioobj(oinfo->oi_oa, &ioo);
939 ioo.ioo_bufcnt = oa_bufs;
942 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
943 rnb, &npages, lnb, oti, oinfo_capa(oinfo));
946 LASSERTF(oa_bufs == npages, "%u != %u\n", oa_bufs, npages);
948 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo, rnb,
949 npages, lnb, oti, ret);
953 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
955 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));