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>
53 #include <lustre_fsfilt.h>
54 #include "filter_internal.h"
56 int *obdfilter_created_scratchpad;
58 /* Grab the dirty and seen grant announcements from the incoming obdo.
59 * We will later calculate the clients new grant and return it.
60 * Caller must hold osfs lock */
61 void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
63 struct filter_export_data *fed;
64 struct obd_device *obd = exp->exp_obd;
67 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
69 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
70 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
71 oa->o_valid &= ~OBD_MD_FLGRANT;
76 fed = &exp->exp_filter_data;
78 /* Add some margin, since there is a small race if other RPCs arrive
79 * out-or-order and have already consumed some grant. We want to
80 * leave this here in case there is a large error in accounting. */
82 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
83 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
84 oa->o_dropped, fed->fed_grant);
86 /* Update our accounting now so that statfs takes it into account.
87 * Note that fed_dirty is only approximate and can become incorrect
88 * if RPCs arrive out-of-order. No important calculations depend
89 * on fed_dirty however, but we must check sanity to not assert. */
90 if ((long long)oa->o_dirty < 0)
92 else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
93 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
94 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
95 if (fed->fed_grant < oa->o_dropped) {
96 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
97 obd->obd_name, exp->exp_client_uuid.uuid, exp,
98 oa->o_dropped, fed->fed_grant);
101 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
102 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
103 obd->obd_name, exp->exp_client_uuid.uuid, exp,
104 oa->o_dropped, obd->u.filter.fo_tot_granted);
107 obd->u.filter.fo_tot_granted -= oa->o_dropped;
108 fed->fed_grant -= oa->o_dropped;
109 fed->fed_dirty = oa->o_dirty;
111 if (oa->o_flags & OBD_FL_SHRINK_GRANT) {
112 obd_size left_space = filter_grant_space_left(exp);
113 struct filter_obd *filter = &exp->exp_obd->u.filter;
115 /*Only if left_space < fo_tot_clients * 32M,
116 *then the grant space could be shrinked */
117 if (left_space < filter->fo_tot_granted_clients *
118 FILTER_GRANT_SHRINK_LIMIT) {
119 fed->fed_grant -= oa->o_grant;
120 filter->fo_tot_granted -= oa->o_grant;
121 CDEBUG(D_CACHE, "%s: cli %s/%p shrink "LPU64
122 "fed_grant %ld total "LPU64"\n",
123 obd->obd_name, exp->exp_client_uuid.uuid,
124 exp, oa->o_grant, fed->fed_grant,
125 filter->fo_tot_granted);
130 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
131 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
132 obd->obd_name, exp->exp_client_uuid.uuid, exp,
133 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
134 spin_unlock(&obd->obd_osfs_lock);
140 /* Figure out how much space is available between what we've granted
141 * and what remains in the filesystem. Compensate for ext3 indirect
142 * block overhead when computing how much free space is left ungranted.
144 * Caller must hold obd_osfs_lock. */
145 obd_size filter_grant_space_left(struct obd_export *exp)
147 struct obd_device *obd = exp->exp_obd;
148 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
149 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
150 int rc, statfs_done = 0;
152 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
154 if (cfs_time_before_64(obd->obd_osfs_age, cfs_time_current_64() - HZ)) {
156 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
157 cfs_time_current_64() + HZ);
158 if (rc) /* N.B. statfs can't really fail */
163 avail = obd->obd_osfs.os_bavail;
164 left = avail - (avail >> (blockbits - 3)); /* (d)indirect */
165 if (left > GRANT_FOR_LLOG(obd)) {
166 left = (left - GRANT_FOR_LLOG(obd)) << blockbits;
168 left = 0 /* << blockbits */;
171 if (!statfs_done && left < 32 * FILTER_GRANT_CHUNK + tot_granted) {
172 CDEBUG(D_CACHE, "fs has no space left and statfs too old\n");
176 if (left >= tot_granted) {
179 if (left < tot_granted - obd->u.filter.fo_tot_pending) {
180 CERROR("%s: cli %s/%p grant "LPU64" > available "
181 LPU64" and pending "LPU64"\n", obd->obd_name,
182 exp->exp_client_uuid.uuid, exp, tot_granted,
183 left, obd->u.filter.fo_tot_pending);
188 CDEBUG(D_CACHE, "%s: cli %s/%p free: "LPU64" avail: "LPU64" grant "LPU64
189 " left: "LPU64" pending: "LPU64"\n", obd->obd_name,
190 exp->exp_client_uuid.uuid, exp,
191 obd->obd_osfs.os_bfree << blockbits, avail << blockbits,
192 tot_granted, left, obd->u.filter.fo_tot_pending);
197 /* Calculate how much grant space to allocate to this client, based on how
198 * much space is currently free and how much of that is already granted.
200 * Caller must hold obd_osfs_lock. */
201 long filter_grant(struct obd_export *exp, obd_size current_grant,
202 obd_size want, obd_size fs_space_left)
204 struct obd_device *obd = exp->exp_obd;
205 struct filter_export_data *fed = &exp->exp_filter_data;
206 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
209 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
211 /* Grant some fraction of the client's requested grant space so that
212 * they are not always waiting for write credits (not all of it to
213 * avoid overgranting in face of multiple RPCs in flight). This
214 * essentially will be able to control the OSC_MAX_RIF for a client.
216 * If we do have a large disparity between what the client thinks it
217 * has and what we think it has, don't grant very much and let the
218 * client consume its grant first. Either it just has lots of RPCs
219 * in flight, or it was evicted and its grants will soon be used up. */
220 if (want > 0x7fffffff) {
221 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
222 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
223 } else if (current_grant < want &&
224 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
225 grant = min((want >> blockbits),
226 (fs_space_left >> blockbits) / 8);
230 /* Allow >FILTER_GRANT_CHUNK size when clients
231 * reconnect due to a server reboot.
233 if ((grant > FILTER_GRANT_CHUNK) &&
234 (!obd->obd_recovering))
235 grant = FILTER_GRANT_CHUNK;
237 obd->u.filter.fo_tot_granted += grant;
238 fed->fed_grant += grant;
239 if (fed->fed_grant < 0) {
240 CERROR("%s: cli %s/%p grant %ld want "LPU64
242 obd->obd_name, exp->exp_client_uuid.uuid,
243 exp, fed->fed_grant, want,current_grant);
244 spin_unlock(&obd->obd_osfs_lock);
251 "%s: cli %s/%p wants: "LPU64" current grant "LPU64
252 " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
253 exp, want, current_grant, 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);
264 * the routine is used to request pages from pagecache
266 * use GFP_NOFS not allowing to enter FS as the client can run on this node
267 * and we might end waiting on a page he sent in the request we're serving.
269 * 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++) {
350 invalidate_mapping_pages(inode->i_mapping,
351 rnb->offset >> CFS_PAGE_SHIFT,
352 (rnb->offset + rnb->len) >>
358 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
359 int objcount, struct obd_ioobj *obj,
360 struct niobuf_remote *nb,
361 int *pages, struct niobuf_local *res,
362 struct obd_trans_info *oti)
364 struct obd_device *obd = exp->exp_obd;
365 struct timeval start, end;
366 struct lvfs_run_ctxt saved;
367 struct niobuf_local *lnb;
368 struct dentry *dentry = NULL;
369 struct inode *inode = NULL;
371 int rc = 0, i, tot_bytes = 0;
372 unsigned long now = jiffies;
376 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
377 * When we do this function's dentry cleanup will need to be fixed.
378 * These values are verified in ost_brw_write() from the wire. */
379 LASSERTF(objcount == 1, "%d\n", objcount);
380 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
382 if (oa->o_valid & OBD_MD_FLGRANT) {
383 spin_lock(&obd->obd_osfs_lock);
384 filter_grant_incoming(exp, oa);
386 if (!(oa->o_flags & OBD_FL_SHRINK_GRANT))
388 spin_unlock(&obd->obd_osfs_lock);
391 iobuf = filter_iobuf_get(&obd->u.filter, oti);
393 RETURN(PTR_ERR(iobuf));
395 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
396 dentry = filter_oa2dentry(obd, oa);
397 if (IS_ERR(dentry)) {
398 rc = PTR_ERR(dentry);
403 inode = dentry->d_inode;
405 obdo_to_inode(inode, oa, OBD_MD_FLATIME);
407 rc = filter_map_remote_to_local(objcount, obj, nb, pages, res);
411 fsfilt_check_slow(obd, now, "preprw_read setup");
413 /* find pages for all segments, fill array with them */
414 do_gettimeofday(&start);
415 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
417 lnb->dentry = dentry;
419 if (i_size_read(inode) <= lnb->offset)
420 /* If there's no more data, abort early. lnb->rc == 0,
421 * so it's easy to detect later. */
424 lnb->page = filter_get_page(obd, inode, lnb->offset);
425 if (lnb->page == NULL)
426 GOTO(cleanup, rc = -ENOMEM);
428 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
430 if (i_size_read(inode) < lnb->offset + lnb->len - 1)
431 lnb->rc = i_size_read(inode) - lnb->offset;
435 tot_bytes += lnb->rc;
437 if (PageUptodate(lnb->page)) {
438 lprocfs_counter_add(obd->obd_stats,
439 LPROC_FILTER_CACHE_HIT, 1);
443 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_MISS, 1);
444 filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
446 do_gettimeofday(&end);
447 timediff = cfs_timeval_sub(&end, &start, NULL);
448 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
450 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
451 GOTO(cleanup, rc = -ENOMEM);
453 fsfilt_check_slow(obd, now, "start_page_read");
455 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf,
456 exp, NULL, NULL, NULL);
460 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_READ_BYTES, tot_bytes);
461 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
462 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
463 LPROC_FILTER_READ_BYTES, tot_bytes);
468 /* unlock pages to allow access from concurrent OST_READ */
469 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
471 LASSERT(PageLocked(lnb->page));
472 unlock_page(lnb->page);
475 page_cache_release(lnb->page);
486 filter_iobuf_put(&obd->u.filter, iobuf, oti);
488 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
490 CERROR("io error %d\n", rc);
495 /* When clients have dirtied as much space as they've been granted they
496 * fall through to sync writes. These sync writes haven't been expressed
497 * in grants and need to error with ENOSPC when there isn't room in the
498 * filesystem for them after grants are taken into account. However,
499 * writeback of the dirty data that was already granted space can write
502 * Caller must hold obd_osfs_lock. */
503 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
504 int objcount, struct fsfilt_objinfo *fso,
505 int niocount, struct niobuf_local *lnb,
506 obd_size *left, struct inode *inode)
508 struct filter_export_data *fed = &exp->exp_filter_data;
509 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
510 unsigned long used = 0, ungranted = 0, using;
511 int i, rc = -ENOSPC, obj, n = 0;
513 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
515 for (obj = 0; obj < objcount; obj++) {
516 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
519 /* should match the code in osc_exit_cache */
521 bytes += lnb[n].offset & (blocksize - 1);
522 tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
524 bytes += blocksize - tmp;
526 if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
527 (oa->o_valid & OBD_MD_FLGRANT)) {
528 if (fed->fed_grant < used + bytes) {
530 "%s: cli %s/%p claims %ld+%d "
531 "GRANT, real grant %lu idx %d\n",
532 exp->exp_obd->obd_name,
533 exp->exp_client_uuid.uuid, exp,
534 used, bytes, fed->fed_grant, n);
537 lnb[n].flags |= OBD_BRW_GRANTED;
538 lnb[n].lnb_grant_used = bytes;
539 CDEBUG(0, "idx %d used=%lu\n", n, used);
544 if (*left > ungranted + bytes) {
545 /* if enough space, pretend it was granted */
547 lnb[n].flags |= OBD_BRW_GRANTED;
548 lnb[n].lnb_grant_used = bytes;
549 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
554 /* We can't check for already-mapped blocks here, as
555 * it requires dropping the osfs lock to do the bmap.
556 * Instead, we return ENOSPC and in that case we need
557 * to go through and verify if all of the blocks not
558 * marked BRW_GRANTED are already mapped and we can
559 * ignore this error. */
561 lnb[n].flags &= ~OBD_BRW_GRANTED;
562 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
563 exp->exp_obd->obd_name,
564 exp->exp_client_uuid.uuid, exp, n, bytes);
568 /* Now substract what client have used already. We don't subtract
569 * this from the tot_granted yet, so that other client's can't grab
570 * that space before we have actually allocated our blocks. That
571 * happens in filter_grant_commit() after the writes are done. */
573 fed->fed_grant -= used;
574 fed->fed_pending += used + ungranted;
575 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
576 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
579 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
580 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
581 ungranted, fed->fed_grant, fed->fed_dirty);
583 /* Rough calc in case we don't refresh cached statfs data */
584 using = (used + ungranted + 1 ) >>
585 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
586 if (exp->exp_obd->obd_osfs.os_bavail > using)
587 exp->exp_obd->obd_osfs.os_bavail -= using;
589 exp->exp_obd->obd_osfs.os_bavail = 0;
591 if (fed->fed_dirty < used) {
592 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
593 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
594 used, fed->fed_dirty);
595 used = fed->fed_dirty;
597 exp->exp_obd->u.filter.fo_tot_dirty -= used;
598 fed->fed_dirty -= used;
600 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
601 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
602 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
603 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
604 spin_unlock(&exp->exp_obd->obd_osfs_lock);
610 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
611 * on mulitple inodes. That isn't all, because there still exists the
612 * possibility of a truncate starting a new transaction while holding the ext3
613 * rwsem = write while some writes (which have started their transactions here)
614 * blocking on the ext3 rwsem = read => lock inversion.
616 * The handling gets very ugly when dealing with locked pages. It may be easier
617 * to just get rid of the locked page code (which has problems of its own) and
618 * either discover we do not need it anymore (i.e. it was a symptom of another
619 * bug) or ensure we get the page locks in an appropriate order. */
620 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
621 int objcount, struct obd_ioobj *obj,
622 struct niobuf_remote *nb, int *pages,
623 struct niobuf_local *res,
624 struct obd_trans_info *oti)
626 struct obd_device *obd = exp->exp_obd;
627 struct timeval start, end;
628 struct lvfs_run_ctxt saved;
629 struct niobuf_local *lnb = res;
630 struct fsfilt_objinfo fso;
631 struct filter_mod_data *fmd;
632 struct dentry *dentry = NULL;
635 unsigned long now = jiffies, timediff;
636 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0;
638 LASSERT(objcount == 1);
639 LASSERT(obj->ioo_bufcnt > 0);
641 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
642 iobuf = filter_iobuf_get(&obd->u.filter, oti);
644 GOTO(cleanup, rc = PTR_ERR(iobuf));
647 dentry = filter_fid2dentry(obd, NULL, obj->ioo_gr, obj->ioo_id);
649 GOTO(cleanup, rc = PTR_ERR(dentry));
652 if (dentry->d_inode == NULL) {
653 if (exp->exp_obd->obd_recovering) {
654 struct obdo *noa = oa;
659 GOTO(recreate_out, rc = -ENOMEM);
660 noa->o_id = obj->ioo_id;
661 noa->o_valid = OBD_MD_FLID;
664 if (filter_recreate(exp->exp_obd, noa) == 0) {
666 dentry = filter_fid2dentry(exp->exp_obd, NULL,
674 if (IS_ERR(dentry) || dentry->d_inode == NULL) {
675 CERROR("%s: BRW to missing obj "LPU64"/"LPU64":rc %d\n",
676 exp->exp_obd->obd_name,
677 obj->ioo_id, obj->ioo_gr,
678 IS_ERR(dentry) ? (int)PTR_ERR(dentry) : -ENOENT);
681 GOTO(cleanup, rc = -ENOENT);
685 rc = filter_map_remote_to_local(objcount, obj, nb, pages, res);
689 fsfilt_check_slow(obd, now, "preprw_write setup");
691 /* Don't update inode timestamps if this write is older than a
692 * setattr which modifies the timestamps. b=10150 */
693 /* XXX when we start having persistent reservations this needs to
694 * be changed to filter_fmd_get() to create the fmd if it doesn't
695 * already exist so we can store the reservation handle there. */
696 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_gr);
699 spin_lock(&obd->obd_osfs_lock);
701 filter_grant_incoming(exp, oa);
702 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
703 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
706 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
707 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
710 left = filter_grant_space_left(exp);
712 fso.fso_dentry = dentry;
713 fso.fso_bufcnt = *pages;
715 rc = filter_grant_check(exp, oa, objcount, &fso, *pages, res,
716 &left, dentry->d_inode);
718 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
719 * for setting UID/GID and fid EA in first write time. */
720 if (oa->o_valid & OBD_MD_FLGRANT)
721 oa->o_grant = filter_grant(exp,oa->o_grant,oa->o_undirty,left);
723 spin_unlock(&obd->obd_osfs_lock);
724 filter_fmd_put(exp, fmd);
730 do_gettimeofday(&start);
731 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
733 /* We still set up for ungranted pages so that granted pages
734 * can be written to disk as they were promised, and portals
735 * needs to keep the pages all aligned properly. */
736 lnb->dentry = dentry;
738 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset);
739 if (lnb->page == NULL)
740 GOTO(cleanup, rc = -ENOMEM);
742 /* DLM locking protects us from write and truncate competing
743 * for same region, but truncate can leave dirty page in the
744 * cache. it's possible the writeout on a such a page is in
745 * progress when we access it. it's also possible that during
746 * this writeout we put new (partial) data, but then won't
747 * be able to proceed in filter_commitrw_write(). thus let's
748 * just wait for writeout completion, should be rare enough.
750 if (obd->u.filter.fo_writethrough_cache)
751 wait_on_page_writeback(lnb->page);
752 BUG_ON(PageWriteback(lnb->page));
754 /* If the filter writes a partial page, then has the file
755 * extended, the client will read in the whole page. the
756 * filter has to be careful to zero the rest of the partial
757 * page on disk. we do it by hand for partial extending
758 * writes, send_bio() is responsible for zeroing pages when
759 * asked to read unmapped blocks -- brw_kiovec() does this. */
760 if (lnb->len != CFS_PAGE_SIZE) {
763 maxidx = ((i_size_read(dentry->d_inode) +
764 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
765 if (maxidx >= lnb->page->index) {
766 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
767 LPU64" flg %x before EOF %llu\n",
768 lnb->len, lnb->offset,lnb->flags,
769 i_size_read(dentry->d_inode));
770 filter_iobuf_add_page(obd, iobuf,
775 char *p = kmap(lnb->page);
777 off = lnb->offset & ~CFS_PAGE_MASK;
780 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
782 memset(p + off, 0, CFS_PAGE_SIZE - off);
787 tot_bytes += lnb->len;
789 do_gettimeofday(&end);
790 timediff = cfs_timeval_sub(&end, &start, NULL);
791 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
793 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
794 GOTO(cleanup, rc = -ENOMEM);
796 /* don't unlock pages to prevent any access */
797 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
800 fsfilt_check_slow(obd, now, "start_page_write");
802 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
804 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
805 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
806 LPROC_FILTER_WRITE_BYTES, tot_bytes);
809 switch(cleanup_phase) {
812 for (i = 0, lnb = res; i < *pages; i++, lnb++) {
813 if (lnb->page != NULL) {
814 unlock_page(lnb->page);
815 page_cache_release(lnb->page);
819 filter_grant_commit(exp, *pages, res);
822 filter_iobuf_put(&obd->u.filter, iobuf, oti);
824 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
829 filter_iobuf_put(&obd->u.filter, iobuf, oti);
831 spin_lock(&obd->obd_osfs_lock);
833 filter_grant_incoming(exp, oa);
834 spin_unlock(&obd->obd_osfs_lock);
835 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
842 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
843 int objcount, struct obd_ioobj *obj,
844 struct niobuf_remote *nb, int *pages,
845 struct niobuf_local *res, struct obd_trans_info *oti)
847 if (cmd == OBD_BRW_WRITE)
848 return filter_preprw_write(cmd, exp, oa, objcount, obj,
849 nb, pages, res, oti);
850 if (cmd == OBD_BRW_READ)
851 return filter_preprw_read(cmd, exp, oa, objcount, obj,
852 nb, pages, res, oti);
857 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
858 int objcount, struct obd_ioobj *obj,
859 struct niobuf_remote *rnb,
860 int pages, struct niobuf_local *res,
861 struct obd_trans_info *oti, int rc)
863 struct filter_obd *fo = &exp->exp_obd->u.filter;
864 struct inode *inode = NULL;
865 struct ldlm_res_id res_id = { .name = { obj->ioo_id } };
866 struct ldlm_resource *resource = NULL;
867 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
868 struct niobuf_local *lnb;
872 /* If oa != NULL then filter_preprw_read updated the inode atime
873 * and we should update the lvb so that other glimpses will also
874 * get the updated value. bug 5972 */
875 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
876 resource = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
878 if (resource != NULL) {
879 ns->ns_lvbo->lvbo_update(resource, NULL, 0, 1);
880 ldlm_resource_putref(resource);
884 if (res->dentry != NULL)
885 inode = res->dentry->d_inode;
887 for (i = 0, lnb = res; i < pages; i++, lnb++) {
888 if (lnb->page != NULL) {
889 page_cache_release(lnb->page);
894 if (inode && (fo->fo_read_cache == 0 ||
895 i_size_read(inode) > fo->fo_readcache_max_filesize))
896 filter_invalidate_cache(exp->exp_obd, obj, rnb, inode);
898 if (res->dentry != NULL)
903 void filter_grant_commit(struct obd_export *exp, int niocount,
904 struct niobuf_local *res)
906 struct filter_obd *filter = &exp->exp_obd->u.filter;
907 struct niobuf_local *lnb = res;
908 unsigned long pending = 0;
911 spin_lock(&exp->exp_obd->obd_osfs_lock);
912 for (i = 0, lnb = res; i < niocount; i++, lnb++)
913 pending += lnb->lnb_grant_used;
915 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
916 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
917 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
918 exp->exp_filter_data.fed_pending, pending);
919 exp->exp_filter_data.fed_pending -= pending;
920 LASSERTF(filter->fo_tot_granted >= pending,
921 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
922 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
923 exp->exp_obd->u.filter.fo_tot_granted, pending);
924 filter->fo_tot_granted -= pending;
925 LASSERTF(filter->fo_tot_pending >= pending,
926 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
927 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
928 filter->fo_tot_pending, pending);
929 filter->fo_tot_pending -= pending;
931 spin_unlock(&exp->exp_obd->obd_osfs_lock);
934 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
935 int objcount, struct obd_ioobj *obj,
936 struct niobuf_remote *nb, int pages,
937 struct niobuf_local *res, struct obd_trans_info *oti,
940 if (cmd == OBD_BRW_WRITE)
941 return filter_commitrw_write(exp, oa, objcount, obj,
942 nb, pages, res, oti, rc);
943 if (cmd == OBD_BRW_READ)
944 return filter_commitrw_read(exp, oa, objcount, obj,
945 nb, pages, res, oti, rc);
950 int filter_brw(int cmd, struct obd_export *exp, struct obd_info *oinfo,
951 obd_count oa_bufs, struct brw_page *pga,
952 struct obd_trans_info *oti)
954 struct obd_ioobj ioo;
955 struct niobuf_local *lnb;
956 struct niobuf_remote *rnb;
961 OBD_ALLOC(lnb, oa_bufs * sizeof(struct niobuf_local));
962 OBD_ALLOC(rnb, oa_bufs * sizeof(struct niobuf_remote));
964 if (lnb == NULL || rnb == NULL)
965 GOTO(out, ret = -ENOMEM);
967 for (i = 0; i < oa_bufs; i++) {
968 lnb[i].page = pga[i].pg;
969 rnb[i].offset = pga[i].off;
970 rnb[i].len = pga[i].count;
973 obdo_to_ioobj(oinfo->oi_oa, &ioo);
974 ioo.ioo_bufcnt = oa_bufs;
977 ret = filter_preprw(cmd, exp, oinfo->oi_oa, 1, &ioo,
978 rnb, &npages, lnb, oti);
981 LASSERTF(oa_bufs == npages, "%u != %u\n", oa_bufs, npages);
983 ret = filter_commitrw(cmd, exp, oinfo->oi_oa, 1, &ioo, rnb,
984 npages, lnb, oti, ret);
988 OBD_FREE(lnb, oa_bufs * sizeof(struct niobuf_local));
990 OBD_FREE(rnb, oa_bufs * sizeof(struct niobuf_remote));