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 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;
112 if (oa->o_flags & OBD_FL_SHRINK_GRANT) {
113 obd_size left_space = filter_grant_space_left(exp);
114 struct filter_obd *filter = &exp->exp_obd->u.filter;
116 /*Only if left_space < fo_tot_clients * 32M,
117 *then the grant space could be shrinked */
118 if (left_space < filter->fo_tot_granted_clients *
119 FILTER_GRANT_SHRINK_LIMIT) {
120 fed->fed_grant -= oa->o_grant;
121 filter->fo_tot_granted -= oa->o_grant;
122 CDEBUG(D_CACHE, "%s: cli %s/%p shrink "LPU64
123 "fed_grant %ld total "LPU64"\n",
124 obd->obd_name, exp->exp_client_uuid.uuid,
125 exp, oa->o_grant, fed->fed_grant,
126 filter->fo_tot_granted);
131 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
132 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
133 obd->obd_name, exp->exp_client_uuid.uuid, exp,
134 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
135 spin_unlock(&obd->obd_osfs_lock);
141 /* Figure out how much space is available between what we've granted
142 * and what remains in the filesystem. Compensate for ext3 indirect
143 * block overhead when computing how much free space is left ungranted.
145 * Caller must hold obd_osfs_lock. */
146 obd_size filter_grant_space_left(struct obd_export *exp)
148 struct obd_device *obd = exp->exp_obd;
149 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
150 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
151 int rc, statfs_done = 0;
153 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
155 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
157 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
158 cfs_time_current_64() + HZ);
159 if (rc) /* N.B. statfs can't really fail */
164 avail = obd->obd_osfs.os_bavail;
165 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
166 if (left > GRANT_FOR_LLOG(obd)) {
167 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
169 left = 0 /* << blockbits */;
172 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
173 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
177 if (left >= tot_granted) {
180 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
181 CERROR("%s: cli %s/%p grant "LPU64" > available "
182 LPU64" and pending "LPU64"\n", obd->obd_name,
183 exp->exp_client_uuid.uuid, exp, tot_granted,
184 left, obd->u.filter.fo_tot_pending);
189 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
190 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
191 exp->exp_client_uuid.uuid, exp,
192 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
193 tot_granted, left, obd->u.filter.fo_tot_pending);
198 /* Calculate how much grant space to allocate to this client, based on how
199 * much space is currently free and how much of that is already granted.
201 * Caller must hold obd_osfs_lock. */
202 long filter_grant(struct obd_export *exp, obd_size current_grant,
203 obd_size want, obd_size fs_space_left)
205 struct obd_device *obd = exp->exp_obd;
206 struct filter_export_data *fed = &exp->exp_filter_data;
207 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
210 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
212 /* Grant some fraction of the client's requested grant space so that
213 * they are not always waiting for write credits (not all of it to
214 * avoid overgranting in face of multiple RPCs in flight). This
215 * essentially will be able to control the OSC_MAX_RIF for a client.
217 * If we do have a large disparity between what the client thinks it
218 * has and what we think it has, don't grant very much and let the
219 * client consume its grant first. Either it just has lots of RPCs
220 * in flight, or it was evicted and its grants will soon be used up. */
221 if (want > 0x7fffffff) {
222 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
223 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
224 } else if (current_grant < want &&
225 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
226 grant = min((want >> blockbits),
227 (fs_space_left >> blockbits) / 8);
231 /* Allow >FILTER_GRANT_CHUNK size when clients
232 * reconnect due to a server reboot.
234 if ((grant > FILTER_GRANT_CHUNK) &&
235 (!obd->obd_recovering))
236 grant = FILTER_GRANT_CHUNK;
238 obd->u.filter.fo_tot_granted += grant;
239 fed->fed_grant += grant;
240 if (fed->fed_grant < 0) {
241 CERROR("%s: cli %s/%p grant %ld want "LPU64
243 obd->obd_name, exp->exp_client_uuid.uuid,
244 exp, fed->fed_grant, want,current_grant);
245 spin_unlock(&obd->obd_osfs_lock);
252 "%s: cli %s/%p wants: "LPU64" current grant "LPU64
253 " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
254 exp, want, current_grant, grant);
256 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
257 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
258 exp, obd->u.filter.fo_tot_dirty,
259 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
265 * the routine is used to request pages from pagecache
267 * use GFP_NOFS not allowing to enter FS as the client can run on this node
268 * and we might end waiting on a page he sent in the request we're serving.
270 * use __GFP_HIGHMEM so that the pages can use all of the available memory
273 static struct page *filter_get_page(struct obd_device *obd,
279 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
280 GFP_NOFS | __GFP_HIGHMEM);
281 if (unlikely(page == NULL))
282 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_NO_PAGE, 1);
288 * the routine initializes array of local_niobuf from remote_niobuf
290 static int filter_map_remote_to_local(int objcount, struct obd_ioobj *obj,
291 struct niobuf_remote *nb,
292 int *nrpages, struct niobuf_local *res)
294 struct niobuf_remote *rnb;
295 struct niobuf_local *lnb;
299 /* we don't support multiobject RPC yet
300 * ost_brw_read() and ost_brw_write() check this */
301 LASSERT(objcount == 1);
305 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt; i++, rnb++) {
306 obd_off offset = rnb->offset;
307 unsigned int len = rnb->len;
310 int poff = offset & (CFS_PAGE_SIZE - 1);
311 int plen = CFS_PAGE_SIZE - poff;
313 if (*nrpages >= max) {
314 CERROR("small array of local bufs: %d\n", max);
320 lnb->offset = offset;
322 lnb->flags = rnb->flags;
325 lnb->lnb_grant_used = 0;
327 LASSERTF(plen <= len, "plen %u, len %u\n", plen, len);
338 * the function is used to free all pages used for request
339 * just to mimic cacheless OSS which don't occupy much memory
341 void filter_invalidate_cache(struct obd_device *obd, struct obd_ioobj *obj,
342 struct niobuf_remote *nb, struct inode *inode)
344 struct niobuf_remote *rnb;
347 LASSERT(inode != NULL);
349 for (i = 0, rnb = nb; i < obj->ioo_bufcnt; i++, rnb++) {
353 start = rnb->offset >> CFS_PAGE_SHIFT;
354 end = (rnb->offset + rnb->len) >> CFS_PAGE_SHIFT;
355 invalidate_mapping_pages(inode->i_mapping, start, end);
356 /* just to avoid warnings */
362 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
363 int objcount, struct obd_ioobj *obj,
364 struct niobuf_remote *nb,
365 int *npages, struct niobuf_local *res,
366 struct obd_trans_info *oti,
367 struct lustre_capa *capa)
369 struct obd_device *obd = exp->exp_obd;
370 struct timeval start, end;
371 struct lvfs_run_ctxt saved;
372 struct niobuf_local *lnb;
373 struct dentry *dentry = NULL;
374 struct inode *inode = NULL;
376 int rc = 0, i, tot_bytes = 0;
377 unsigned long now = jiffies;
381 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
382 * When we do this function's dentry cleanup will need to be fixed.
383 * These values are verified in ost_brw_write() from the wire. */
384 LASSERTF(objcount == 1, "%d\n", objcount);
385 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
387 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
392 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
393 spin_lock(&obd->obd_osfs_lock);
394 filter_grant_incoming(exp, oa);
396 if (!(oa->o_flags & OBD_FL_SHRINK_GRANT))
398 spin_unlock(&obd->obd_osfs_lock);
401 iobuf = filter_iobuf_get(&obd->u.filter, oti);
403 RETURN(PTR_ERR(iobuf));
405 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
406 dentry = filter_oa2dentry(obd, oa);
407 if (IS_ERR(dentry)) {
408 rc = PTR_ERR(dentry);
413 inode = dentry->d_inode;
415 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
417 rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
421 fsfilt_check_slow(obd, now, "preprw_read setup");
423 /* find pages for all segments, fill array with them */
424 do_gettimeofday(&start);
425 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
427 lnb->dentry = dentry;
429 if (i_size_read(inode) <= lnb->offset)
430 /* If there's no more data, abort early. lnb->rc == 0,
431 * so it's easy to detect later. */
434 lnb->page = filter_get_page(obd, inode, lnb->offset);
435 if (lnb->page == NULL)
436 GOTO(cleanup, rc = -ENOMEM);
438 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
440 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
441 lnb->rc = i_size_read(inode) - lnb->offset;
445 tot_bytes += lnb->rc;
447 if (PageUptodate(lnb->page)) {
448 lprocfs_counter_add(obd->obd_stats,
449 LPROC_FILTER_CACHE_HIT, 1);
453 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_MISS, 1);
454 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
456 do_gettimeofday(&end);
457 timediff = cfs_timeval_sub(&end, &start, NULL);
458 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
460 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
461 GOTO(cleanup, rc = -ENOMEM);
463 fsfilt_check_slow(obd, now, "start_page_read");
465 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
466 exp, NULL, NULL, NULL);
470 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
472 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
473 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
474 LPROC_FILTER_READ_BYTES, tot_bytes);
479 /* unlock pages to allow access from concurrent OST_READ */
480 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
482 LASSERT(PageLocked(lnb->page));
483 unlock_page(lnb->page);
486 page_cache_release(lnb->page);
497 filter_iobuf_put(&obd->u.filter, iobuf, oti);
499 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
501 CERROR("io error %d\n", rc);
506 /* When clients have dirtied as much space as they've been granted they
507 * fall through to sync writes. These sync writes haven't been expressed
508 * in grants and need to error with ENOSPC when there isn't room in the
509 * filesystem for them after grants are taken into account. However,
510 * writeback of the dirty data that was already granted space can write
513 * Caller must hold obd_osfs_lock. */
514 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
515 int objcount, struct fsfilt_objinfo *fso,
516 int niocount, struct niobuf_local *lnb,
517 obd_size *left, struct inode *inode)
519 struct filter_export_data *fed = &exp->exp_filter_data;
520 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
521 unsigned long used = 0, ungranted = 0, using;
522 int i, rc = -ENOSPC, obj, n = 0;
524 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
526 for (obj = 0; obj < objcount; obj++) {
527 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
530 /* should match the code in osc_exit_cache */
532 bytes += lnb[n].offset & (blocksize - 1);
533 tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
535 bytes += blocksize - tmp;
537 if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
538 (oa->o_valid & OBD_MD_FLGRANT)) {
539 if (fed->fed_grant < used + bytes) {
541 "%s: cli %s/%p claims %ld+%d "
542 "GRANT, real grant %lu idx %d\n",
543 exp->exp_obd->obd_name,
544 exp->exp_client_uuid.uuid, exp,
545 used, bytes, fed->fed_grant, n);
548 lnb[n].flags |= OBD_BRW_GRANTED;
549 lnb[n].lnb_grant_used = bytes;
550 CDEBUG(0, "idx %d used=%lu\n", n, used);
555 if (*left > ungranted + bytes) {
556 /* if enough space, pretend it was granted */
558 lnb[n].flags |= OBD_BRW_GRANTED;
559 lnb[n].lnb_grant_used = bytes;
560 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
565 /* We can't check for already-mapped blocks here, as
566 * it requires dropping the osfs lock to do the bmap.
567 * Instead, we return ENOSPC and in that case we need
568 * to go through and verify if all of the blocks not
569 * marked BRW_GRANTED are already mapped and we can
570 * ignore this error. */
572 lnb[n].flags &= ~OBD_BRW_GRANTED;
573 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
574 exp->exp_obd->obd_name,
575 exp->exp_client_uuid.uuid, exp, n, bytes);
579 /* Now substract what client have used already. We don't subtract
580 * this from the tot_granted yet, so that other client's can't grab
581 * that space before we have actually allocated our blocks. That
582 * happens in filter_grant_commit() after the writes are done. */
584 fed->fed_grant -= used;
585 fed->fed_pending += used + ungranted;
586 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
587 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
590 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
591 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
592 ungranted, fed->fed_grant, fed->fed_dirty);
594 /* Rough calc in case we don't refresh cached statfs data */
595 using = (used + ungranted + 1 ) >>
596 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
597 if (exp->exp_obd->obd_osfs.os_bavail > using)
598 exp->exp_obd->obd_osfs.os_bavail -= using;
600 exp->exp_obd->obd_osfs.os_bavail = 0;
602 if (fed->fed_dirty < used) {
603 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
604 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
605 used, fed->fed_dirty);
606 used = fed->fed_dirty;
608 exp->exp_obd->u.filter.fo_tot_dirty -= used;
609 fed->fed_dirty -= used;
611 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
612 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
613 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
614 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
615 spin_unlock(&exp->exp_obd->obd_osfs_lock);
621 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
622 * on mulitple inodes. That isn't all, because there still exists the
623 * possibility of a truncate starting a new transaction while holding the ext3
624 * rwsem = write while some writes (which have started their transactions here)
625 * blocking on the ext3 rwsem = read => lock inversion.
627 * The handling gets very ugly when dealing with locked pages. It may be easier
628 * to just get rid of the locked page code (which has problems of its own) and
629 * either discover we do not need it anymore (i.e. it was a symptom of another
630 * bug) or ensure we get the page locks in an appropriate order. */
631 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
632 int objcount, struct obd_ioobj *obj,
633 struct niobuf_remote *nb, int *npages,
634 struct niobuf_local *res,
635 struct obd_trans_info *oti,
636 struct lustre_capa *capa)
638 struct obd_device *obd = exp->exp_obd;
639 struct timeval start, end;
640 struct lvfs_run_ctxt saved;
641 struct niobuf_local *lnb = res;
642 struct fsfilt_objinfo fso;
643 struct filter_mod_data *fmd;
644 struct dentry *dentry = NULL;
647 unsigned long now = jiffies, timediff;
648 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
650 LASSERT(objcount == 1);
651 LASSERT(obj->ioo_bufcnt > 0);
653 rc = filter_auth_capa(exp, NULL, obdo_mdsno(oa), capa,
658 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
659 iobuf = filter_iobuf_get(&obd->u.filter, oti);
661 GOTO(cleanup, rc = PTR_ERR(iobuf));
664 dentry = filter_fid2dentry(obd, NULL, obj->ioo_gr,
667 GOTO(cleanup, rc = PTR_ERR(dentry));
670 if (dentry->d_inode == NULL) {
671 CERROR("%s: trying to BRW to non-existent file "LPU64"\n",
672 obd->obd_name, obj->ioo_id);
673 GOTO(cleanup, rc = -ENOENT);
676 if (oa->o_valid & (OBD_MD_FLUID | OBD_MD_FLGID) &&
677 dentry->d_inode->i_mode & (S_ISUID | S_ISGID)) {
678 rc = filter_capa_fixoa(exp, oa, obdo_mdsno(oa), capa);
683 rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
687 fsfilt_check_slow(obd, now, "preprw_write setup");
689 /* Don't update inode timestamps if this write is older than a
690 * setattr which modifies the timestamps. b=10150 */
691 /* XXX when we start having persistent reservations this needs to
692 * be changed to filter_fmd_get() to create the fmd if it doesn't
693 * already exist so we can store the reservation handle there. */
694 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
697 spin_lock(&obd->obd_osfs_lock);
698 filter_grant_incoming(exp, oa);
699 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
700 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
703 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
704 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
707 left = filter_grant_space_left(exp);
709 fso.fso_dentry = dentry;
710 fso.fso_bufcnt = *npages;
712 rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
713 &left, dentry->d_inode);
715 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
716 * for setting UID/GID and fid EA in first write time. */
717 if (oa->o_valid & OBD_MD_FLGRANT)
718 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
720 spin_unlock(&obd->obd_osfs_lock);
721 filter_fmd_put(exp, fmd);
726 do_gettimeofday(&start);
727 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
729 /* We still set up for ungranted pages so that granted pages
730 * can be written to disk as they were promised, and portals
731 * needs to keep the pages all aligned properly. */
732 lnb->dentry = dentry;
734 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset);
735 if (lnb->page == NULL)
736 GOTO(cleanup, rc = -ENOMEM);
739 /* DLM locking protects us from write and truncate competing
740 * for same region, but truncate can leave dirty page in the
741 * cache. it's possible the writeout on a such a page is in
742 * progress when we access it. it's also possible that during
743 * this writeout we put new (partial) data, but then won't
744 * be able to proceed in filter_commitrw_write(). thus let's
745 * just wait for writeout completion, should be rare enough.
747 if (obd->u.filter.fo_writethrough_cache)
748 wait_on_page_writeback(lnb->page);
749 BUG_ON(PageWriteback(lnb->page));
751 /* If the filter writes a partial page, then has the file
752 * extended, the client will read in the whole page. the
753 * filter has to be careful to zero the rest of the partial
754 * page on disk. we do it by hand for partial extending
755 * writes, send_bio() is responsible for zeroing pages when
756 * asked to read unmapped blocks -- brw_kiovec() does this. */
757 if (lnb->len != CFS_PAGE_SIZE) {
760 maxidx = ((i_size_read(dentry->d_inode) +
761 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
762 if (maxidx >= lnb->page->index) {
763 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
764 LPU64" flg %x before EOF %llu\n",
765 lnb->len, lnb->offset,lnb->flags,
766 i_size_read(dentry->d_inode));
767 filter_iobuf_add_page(obd, iobuf,
772 char *p = kmap(lnb->page);
774 off = lnb->offset & ~CFS_PAGE_MASK;
777 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
779 memset(p + off, 0, CFS_PAGE_SIZE - off);
784 tot_bytes += lnb->len;
786 do_gettimeofday(&end);
787 timediff = cfs_timeval_sub(&end, &start, NULL);
788 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
790 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
791 GOTO(cleanup, rc = -ENOMEM);
793 /* don't unlock pages to prevent any access */
794 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
797 fsfilt_check_slow(obd, now, "start_page_write");
799 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
800 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
801 LPROC_FILTER_WRITE_BYTES, tot_bytes);
804 switch(cleanup_phase) {
807 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
808 if (lnb->page != NULL) {
809 unlock_page(lnb->page);
810 page_cache_release(lnb->page);
816 filter_iobuf_put(&obd->u.filter, iobuf, oti);
818 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
823 filter_iobuf_put(&obd->u.filter, iobuf, oti);
825 spin_lock(&obd->obd_osfs_lock);
827 filter_grant_incoming(exp, oa);
828 spin_unlock(&obd->obd_osfs_lock);
829 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
836 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
837 int objcount, struct obd_ioobj *obj,
838 struct niobuf_remote *nb, int *npages,
839 struct niobuf_local *res, struct obd_trans_info *oti,
840 struct lustre_capa *capa)
842 if (cmd == OBD_BRW_WRITE)
843 return filter_preprw_write(cmd, exp, oa, objcount, obj,
844 nb, npages, res, oti, capa);
845 if (cmd == OBD_BRW_READ)
846 return filter_preprw_read(cmd, exp, oa, objcount, obj,
847 nb, npages, res, oti, capa);
852 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
853 int objcount, struct obd_ioobj *obj,
854 struct niobuf_remote *rnb,
855 int npages, struct niobuf_local *res,
856 struct obd_trans_info *oti, int rc)
858 struct filter_obd *fo = &exp->exp_obd->u.filter;
859 struct inode *inode = NULL;
860 struct ldlm_res_id res_id;
861 struct ldlm_resource *resource = NULL;
862 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
863 struct niobuf_local *lnb;
867 osc_build_res_name(obj->ioo_id, obj->ioo_gr, &res_id);
868 /* If oa != NULL then filter_preprw_read updated the inode atime
869 * and we should update the lvb so that other glimpses will also
870 * get the updated value. bug 5972 */
871 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
872 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
874 if (resource != NULL) {
875 LDLM_RESOURCE_ADDREF(resource);
876 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
877 LDLM_RESOURCE_DELREF(resource);
878 ldlm_resource_putref(resource);
882 if (res->dentry != NULL)
883 inode = res->dentry->d_inode;
885 for (i = 0, lnb = res; i < npages; i++, lnb++) {
886 if (lnb->page != NULL) {
887 page_cache_release(lnb->page);
892 if (inode && (fo->fo_read_cache == 0 ||
893 i_size_read(inode) > fo->fo_readcache_max_filesize))
894 filter_invalidate_cache(exp->exp_obd, obj, rnb, inode);
896 if (res->dentry != NULL)
901 void filter_grant_commit(struct obd_export *exp, int niocount,
902 struct niobuf_local *res)
904 struct filter_obd *filter = &exp->exp_obd->u.filter;
905 struct niobuf_local *lnb = res;
906 unsigned long pending = 0;
909 spin_lock(&exp->exp_obd->obd_osfs_lock);
910 for (i = 0, lnb = res; i < niocount; i++, lnb++)
911 pending += lnb->lnb_grant_used;
913 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
914 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
915 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
916 exp->exp_filter_data.fed_pending, pending);
917 exp->exp_filter_data.fed_pending -= pending;
918 LASSERTF(filter->fo_tot_granted >= pending,
919 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
920 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
921 exp->exp_obd->u.filter.fo_tot_granted, pending);
922 filter->fo_tot_granted -= pending;
923 LASSERTF(filter->fo_tot_pending >= pending,
924 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
925 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
926 filter->fo_tot_pending, pending);
927 filter->fo_tot_pending -= pending;
929 spin_unlock(&exp->exp_obd->obd_osfs_lock);
932 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
933 int objcount, struct obd_ioobj *obj,
934 struct niobuf_remote *nb, int npages,
935 struct niobuf_local *res, struct obd_trans_info *oti,
938 if (cmd == OBD_BRW_WRITE)
939 return filter_commitrw_write(exp, oa, objcount, obj,
940 nb, npages, res, oti, rc);
941 if (cmd == OBD_BRW_READ)
942 return filter_commitrw_read(exp, oa, objcount, obj,
943 nb, npages, res, oti, rc);
948 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
949 obd_count oa_bufs, struct brw_page *pga,
950 struct obd_trans_info *oti)
952 struct obd_ioobj ioo;
953 struct niobuf_local *lnb;
954 struct niobuf_remote *rnb;
959 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
960 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
962 if (lnb == NULL || rnb == NULL)
963 GOTO(out, ret = -ENOMEM);
965 for (i = 0; i < oa_bufs; i++) {
966 lnb[i].page = pga[i].pg;
967 rnb[i].offset = pga[i].off;
968 rnb[i].len = pga[i].count;
971 obdo_to_ioobj(oinfo->oi_oa, &ioo);
972 ioo.ioo_bufcnt = oa_bufs;
975 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
976 rnb, &npages, lnb, oti, oinfo_capa(oinfo));
979 LASSERTF(oa_bufs == npages, "%u != %u\n", oa_bufs, npages);
981 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo, rnb,
982 npages, lnb, oti, ret);
986 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
988 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));