4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdfilter/filter_io.c
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_FILTER
43 #ifndef AUTOCONF_INCLUDED
44 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/pagemap.h> // XXX kill me soon
48 #include <linux/version.h>
50 #include <obd_class.h>
52 #include <lustre_fsfilt.h>
53 #include "filter_internal.h"
55 int *obdfilter_created_scratchpad;
57 /* Grab the dirty and seen grant announcements from the incoming obdo.
58 * We will later calculate the clients new grant and return it.
59 * Caller must hold osfs lock */
60 void filter_grant_incoming(struct obd_export *exp, struct obdo *oa)
62 struct filter_export_data *fed;
63 struct obd_device *obd = exp->exp_obd;
66 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
68 if ((oa->o_valid & (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) !=
69 (OBD_MD_FLBLOCKS|OBD_MD_FLGRANT)) {
70 oa->o_valid &= ~OBD_MD_FLGRANT;
75 fed = &exp->exp_filter_data;
77 /* Add some margin, since there is a small race if other RPCs arrive
78 * out-or-order and have already consumed some grant. We want to
79 * leave this here in case there is a large error in accounting. */
81 "%s: cli %s/%p reports grant: "LPU64" dropped: %u, local: %lu\n",
82 obd->obd_name, exp->exp_client_uuid.uuid, exp, oa->o_grant,
83 oa->o_dropped, fed->fed_grant);
85 /* Update our accounting now so that statfs takes it into account.
86 * Note that fed_dirty is only approximate and can become incorrect
87 * if RPCs arrive out-of-order. No important calculations depend
88 * on fed_dirty however, but we must check sanity to not assert. */
89 if ((long long)oa->o_dirty < 0)
91 else if (oa->o_dirty > fed->fed_grant + 4 * FILTER_GRANT_CHUNK)
92 oa->o_dirty = fed->fed_grant + 4 * FILTER_GRANT_CHUNK;
93 obd->u.filter.fo_tot_dirty += oa->o_dirty - fed->fed_dirty;
94 if (fed->fed_grant < oa->o_dropped) {
95 CDEBUG(D_CACHE,"%s: cli %s/%p reports %u dropped > grant %lu\n",
96 obd->obd_name, exp->exp_client_uuid.uuid, exp,
97 oa->o_dropped, fed->fed_grant);
100 if (obd->u.filter.fo_tot_granted < oa->o_dropped) {
101 CERROR("%s: cli %s/%p reports %u dropped > tot_grant "LPU64"\n",
102 obd->obd_name, exp->exp_client_uuid.uuid, exp,
103 oa->o_dropped, obd->u.filter.fo_tot_granted);
106 obd->u.filter.fo_tot_granted -= oa->o_dropped;
107 fed->fed_grant -= oa->o_dropped;
108 fed->fed_dirty = oa->o_dirty;
110 if (oa->o_valid & OBD_MD_FLFLAGS && oa->o_flags & OBD_FL_SHRINK_GRANT) {
111 obd_size left_space = filter_grant_space_left(exp);
112 struct filter_obd *filter = &exp->exp_obd->u.filter;
114 /*Only if left_space < fo_tot_clients * 32M,
115 *then the grant space could be shrinked */
116 if (left_space < filter->fo_tot_granted_clients *
117 FILTER_GRANT_SHRINK_LIMIT) {
118 fed->fed_grant -= oa->o_grant;
119 filter->fo_tot_granted -= oa->o_grant;
120 CDEBUG(D_CACHE, "%s: cli %s/%p shrink "LPU64
121 "fed_grant %ld total "LPU64"\n",
122 obd->obd_name, exp->exp_client_uuid.uuid,
123 exp, oa->o_grant, fed->fed_grant,
124 filter->fo_tot_granted);
129 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
130 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
131 obd->obd_name, exp->exp_client_uuid.uuid, exp,
132 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
133 cfs_spin_unlock(&obd->obd_osfs_lock);
139 /* Figure out how much space is available between what we've granted
140 * and what remains in the filesystem. Compensate for ext3 indirect
141 * block overhead when computing how much free space is left ungranted.
143 * Caller must hold obd_osfs_lock. */
144 obd_size filter_grant_space_left(struct obd_export *exp)
146 struct obd_device *obd = exp->exp_obd;
147 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
148 obd_size tot_granted = obd->u.filter.fo_tot_granted, avail, left = 0;
149 int rc, statfs_done = 0;
151 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
153 if (cfs_time_before_64(obd->obd_osfs_age,
154 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
156 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb,
157 cfs_time_shift_64(OBD_STATFS_CACHE_SECONDS));
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 * if @conservative != 0, we limit the maximum grant to FILTER_GRANT_CHUNK;
201 * otherwise we'll satisfy the requested amount as possible as we can, this
202 * is usually due to client reconnect.
204 * Caller must hold obd_osfs_lock. */
205 long filter_grant(struct obd_export *exp, obd_size current_grant,
206 obd_size want, obd_size fs_space_left, int conservative)
208 struct obd_device *obd = exp->exp_obd;
209 struct filter_export_data *fed = &exp->exp_filter_data;
210 int blockbits = obd->u.obt.obt_sb->s_blocksize_bits;
213 LASSERT_SPIN_LOCKED(&obd->obd_osfs_lock);
215 /* Grant some fraction of the client's requested grant space so that
216 * they are not always waiting for write credits (not all of it to
217 * avoid overgranting in face of multiple RPCs in flight). This
218 * essentially will be able to control the OSC_MAX_RIF for a client.
220 * If we do have a large disparity between what the client thinks it
221 * has and what we think it has, don't grant very much and let the
222 * client consume its grant first. Either it just has lots of RPCs
223 * in flight, or it was evicted and its grants will soon be used up. */
224 if (want > 0x7fffffff) {
225 CERROR("%s: client %s/%p requesting > 2GB grant "LPU64"\n",
226 obd->obd_name, exp->exp_client_uuid.uuid, exp, want);
227 } else if (current_grant < want &&
228 current_grant < fed->fed_grant + FILTER_GRANT_CHUNK) {
229 grant = min(want + (1 << blockbits) - 1, fs_space_left / 8);
230 grant &= ~((1ULL << blockbits) - 1);
233 if (grant > FILTER_GRANT_CHUNK && conservative)
234 grant = FILTER_GRANT_CHUNK;
236 obd->u.filter.fo_tot_granted += grant;
237 fed->fed_grant += grant;
238 if (fed->fed_grant < 0) {
239 CERROR("%s: cli %s/%p grant %ld want "LPU64
241 obd->obd_name, exp->exp_client_uuid.uuid,
242 exp, fed->fed_grant, want,current_grant);
243 cfs_spin_unlock(&obd->obd_osfs_lock);
250 "%s: cli %s/%p wants: "LPU64" current grant "LPU64
251 " granting: "LPU64"\n", obd->obd_name, exp->exp_client_uuid.uuid,
252 exp, want, current_grant, grant);
254 "%s: cli %s/%p tot cached:"LPU64" granted:"LPU64
255 " num_exports: %d\n", obd->obd_name, exp->exp_client_uuid.uuid,
256 exp, obd->u.filter.fo_tot_dirty,
257 obd->u.filter.fo_tot_granted, obd->obd_num_exports);
263 * the routine is used to request pages from pagecache
265 * use GFP_NOFS for requests from a local client not allowing to enter FS
266 * as we might end up waiting on a page he sent in the request we're serving.
267 * use __GFP_HIGHMEM so that the pages can use all of the available memory
269 * use more aggressive GFP_HIGHUSER flags from non-local clients to be able to
270 * generate more memory pressure.
272 * See Bug 19529 and Bug 19917 for details.
274 static struct page *filter_get_page(struct obd_device *obd,
281 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
282 (localreq ? (GFP_NOFS | __GFP_HIGHMEM)
284 if (unlikely(page == NULL))
285 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_NO_PAGE, 1);
291 * the routine initializes array of local_niobuf from remote_niobuf
293 static int filter_map_remote_to_local(int objcount, struct obd_ioobj *obj,
294 struct niobuf_remote *nb,
295 int *nrpages, struct niobuf_local *res)
297 struct niobuf_remote *rnb;
298 struct niobuf_local *lnb;
302 /* we don't support multiobject RPC yet
303 * ost_brw_read() and ost_brw_write() check this */
304 LASSERT(objcount == 1);
308 for (i = 0, rnb = nb, lnb = res; i < obj->ioo_bufcnt; i++, rnb++) {
309 obd_off offset = rnb->offset;
310 unsigned int len = rnb->len;
313 int poff = offset & (CFS_PAGE_SIZE - 1);
314 int plen = CFS_PAGE_SIZE - poff;
316 if (*nrpages >= max) {
317 CERROR("small array of local bufs: %d\n", max);
323 lnb->offset = offset;
325 lnb->flags = rnb->flags;
328 lnb->lnb_grant_used = 0;
330 LASSERTF(plen <= len, "plen %u, len %u\n", plen, len);
341 * the invalidate above doesn't work during read because lnet pins pages.
342 * The truncate is used here instead to drop pages from cache
344 void filter_release_cache(struct obd_device *obd, struct obd_ioobj *obj,
345 struct niobuf_remote *rnb, struct inode *inode)
349 LASSERT(inode != NULL);
350 for (i = 0; i < obj->ioo_bufcnt; i++, rnb++) {
351 /* remove pages in which range is fit */
352 truncate_inode_pages_range(inode->i_mapping,
353 rnb->offset & CFS_PAGE_MASK,
354 (rnb->offset + rnb->len - 1) |
359 static int filter_preprw_read(int cmd, struct obd_export *exp, struct obdo *oa,
360 int objcount, struct obd_ioobj *obj,
361 struct niobuf_remote *nb,
362 int *npages, struct niobuf_local *res,
363 struct obd_trans_info *oti,
364 struct lustre_capa *capa)
366 struct obd_device *obd = exp->exp_obd;
367 struct timeval start, end;
368 struct lvfs_run_ctxt saved;
369 struct niobuf_local *lnb;
370 struct dentry *dentry = NULL;
371 struct inode *inode = NULL;
373 int rc = 0, i, tot_bytes = 0;
374 unsigned long now = jiffies;
379 /* We are currently not supporting multi-obj BRW_READ RPCS at all.
380 * When we do this function's dentry cleanup will need to be fixed.
381 * These values are verified in ost_brw_write() from the wire. */
382 LASSERTF(objcount == 1, "%d\n", objcount);
383 LASSERTF(obj->ioo_bufcnt > 0, "%d\n", obj->ioo_bufcnt);
385 rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
390 if (oa && oa->o_valid & OBD_MD_FLGRANT) {
391 cfs_spin_lock(&obd->obd_osfs_lock);
392 filter_grant_incoming(exp, oa);
394 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
395 !(oa->o_flags & OBD_FL_SHRINK_GRANT))
397 cfs_spin_unlock(&obd->obd_osfs_lock);
400 iobuf = filter_iobuf_get(&obd->u.filter, oti);
402 RETURN(PTR_ERR(iobuf));
404 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
405 dentry = filter_oa2dentry(obd, &oa->o_oi);
406 if (IS_ERR(dentry)) {
407 rc = PTR_ERR(dentry);
412 inode = dentry->d_inode;
413 isize = i_size_read(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 cfs_gettimeofday(&start);
425 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
427 lnb->dentry = dentry;
429 if (isize <= 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, 0);
435 if (lnb->page == NULL)
436 GOTO(cleanup, rc = -ENOMEM);
438 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_CACHE_ACCESS, 1);
440 if (isize < lnb->offset + lnb->len - 1)
441 lnb->rc = isize - 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 cfs_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;
525 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
526 (oa->o_flags & OBD_FL_RECOV_RESEND)) {
528 CDEBUG(D_CACHE, "Recoverable resend arrived, skipping "
532 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
534 for (obj = 0; obj < objcount; obj++) {
535 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
538 /* should match the code in osc_exit_cache */
540 bytes += lnb[n].offset & (blocksize - 1);
541 tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
543 bytes += blocksize - tmp;
545 if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
546 (oa->o_valid & OBD_MD_FLGRANT)) {
548 /* this is a recoverable resent */
549 lnb[n].flags |= OBD_BRW_GRANTED;
552 } else if (fed->fed_grant < used + bytes) {
554 "%s: cli %s/%p claims %ld+%d "
555 "GRANT, real grant %lu idx %d\n",
556 exp->exp_obd->obd_name,
557 exp->exp_client_uuid.uuid, exp,
558 used, bytes, fed->fed_grant, n);
561 lnb[n].flags |= OBD_BRW_GRANTED;
562 lnb[n].lnb_grant_used = bytes;
563 CDEBUG(0, "idx %d used=%lu\n", n, used);
568 if (*left > ungranted + bytes) {
569 /* if enough space, pretend it was granted */
571 lnb[n].flags |= OBD_BRW_GRANTED;
572 lnb[n].lnb_grant_used = bytes;
573 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
578 /* We can't check for already-mapped blocks here, as
579 * it requires dropping the osfs lock to do the bmap.
580 * Instead, we return ENOSPC and in that case we need
581 * to go through and verify if all of the blocks not
582 * marked BRW_GRANTED are already mapped and we can
583 * ignore this error. */
585 lnb[n].flags &= ~OBD_BRW_GRANTED;
586 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
587 exp->exp_obd->obd_name,
588 exp->exp_client_uuid.uuid, exp, n, bytes);
592 /* Now substract what client have used already. We don't subtract
593 * this from the tot_granted yet, so that other client's can't grab
594 * that space before we have actually allocated our blocks. That
595 * happens in filter_grant_commit() after the writes are done. */
597 fed->fed_grant -= used;
598 fed->fed_pending += used + ungranted;
599 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
600 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
603 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
604 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
605 ungranted, fed->fed_grant, fed->fed_dirty);
607 /* Rough calc in case we don't refresh cached statfs data */
608 using = (used + ungranted + 1 ) >>
609 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
610 if (exp->exp_obd->obd_osfs.os_bavail > using)
611 exp->exp_obd->obd_osfs.os_bavail -= using;
613 exp->exp_obd->obd_osfs.os_bavail = 0;
615 if (fed->fed_dirty < used) {
616 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
617 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
618 used, fed->fed_dirty);
619 used = fed->fed_dirty;
621 exp->exp_obd->u.filter.fo_tot_dirty -= used;
622 fed->fed_dirty -= used;
624 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
625 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
626 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
627 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
628 cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
634 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
635 * on mulitple inodes. That isn't all, because there still exists the
636 * possibility of a truncate starting a new transaction while holding the ext3
637 * rwsem = write while some writes (which have started their transactions here)
638 * blocking on the ext3 rwsem = read => lock inversion.
640 * The handling gets very ugly when dealing with locked pages. It may be easier
641 * to just get rid of the locked page code (which has problems of its own) and
642 * either discover we do not need it anymore (i.e. it was a symptom of another
643 * bug) or ensure we get the page locks in an appropriate order. */
644 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
645 int objcount, struct obd_ioobj *obj,
646 struct niobuf_remote *nb, int *npages,
647 struct niobuf_local *res,
648 struct obd_trans_info *oti,
649 struct lustre_capa *capa)
651 struct obd_device *obd = exp->exp_obd;
652 struct timeval start, end;
653 struct lvfs_run_ctxt saved;
654 struct niobuf_local *lnb = res;
655 struct fsfilt_objinfo fso;
656 struct filter_mod_data *fmd;
657 struct dentry *dentry = NULL;
660 unsigned long now = jiffies, timediff;
661 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0, localreq = 0;
663 LASSERT(objcount == 1);
664 LASSERT(obj->ioo_bufcnt > 0);
666 rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
671 if (exp->exp_connection &&
672 exp->exp_connection->c_peer.nid == exp->exp_connection->c_self)
675 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
676 iobuf = filter_iobuf_get(&obd->u.filter, oti);
678 GOTO(cleanup, rc = PTR_ERR(iobuf));
681 dentry = filter_fid2dentry(obd, NULL, obj->ioo_seq,
684 GOTO(cleanup, rc = PTR_ERR(dentry));
687 if (dentry->d_inode == NULL) {
688 if (exp->exp_obd->obd_recovering) {
689 struct obdo *noa = oa;
694 GOTO(recreate_out, rc = -ENOMEM);
695 noa->o_id = obj->ioo_id;
696 noa->o_valid = OBD_MD_FLID;
699 if (filter_create(exp, noa, NULL, oti) == 0) {
701 dentry = filter_fid2dentry(exp->exp_obd, NULL,
709 if (IS_ERR(dentry) || dentry->d_inode == NULL) {
710 CERROR("%s: BRW to missing obj "LPU64"/"LPU64":rc %d\n",
711 exp->exp_obd->obd_name,
712 obj->ioo_id, obj->ioo_seq,
713 IS_ERR(dentry) ? (int)PTR_ERR(dentry) : -ENOENT);
716 GOTO(cleanup, rc = -ENOENT);
720 if (oa->o_valid & (OBD_MD_FLUID | OBD_MD_FLGID) &&
721 dentry->d_inode->i_mode & (S_ISUID | S_ISGID)) {
722 rc = filter_capa_fixoa(exp, oa, oa->o_seq, capa);
727 rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
731 fsfilt_check_slow(obd, now, "preprw_write setup");
733 /* Filter truncate first locks i_mutex then partially truncated
734 * page, filter write code first locks pages then take
735 * i_mutex. To avoid a deadlock in case of concurrent
736 * punch/write requests from one client, filter writes and
737 * filter truncates are serialized by i_alloc_sem, allowing
738 * multiple writes or single truncate. */
739 down_read(&dentry->d_inode->i_alloc_sem);
740 fsfilt_check_slow(obd, now, "i_alloc_sem");
742 /* Don't update inode timestamps if this write is older than a
743 * setattr which modifies the timestamps. b=10150 */
744 /* XXX when we start having persistent reservations this needs to
745 * be changed to filter_fmd_get() to create the fmd if it doesn't
746 * already exist so we can store the reservation handle there. */
747 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_seq);
750 cfs_spin_lock(&obd->obd_osfs_lock);
751 filter_grant_incoming(exp, oa);
752 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
753 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
756 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
757 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
760 left = filter_grant_space_left(exp);
762 fso.fso_dentry = dentry;
763 fso.fso_bufcnt = *npages;
765 rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
766 &left, dentry->d_inode);
768 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
769 * for setting UID/GID and fid EA in first write time. */
770 /* If OBD_FL_SHRINK_GRANT is set, the client just returned us some grant
771 * so no sense in allocating it some more. We either return the grant
772 * back to the client if we have plenty of space or we don't return
773 * anything if we are short. This was decided in filter_grant_incoming*/
774 if ((oa->o_valid & OBD_MD_FLGRANT) &&
775 (!(oa->o_valid & OBD_MD_FLFLAGS) ||
776 !(oa->o_flags & OBD_FL_SHRINK_GRANT)))
777 oa->o_grant = filter_grant(exp, oa->o_grant, oa->o_undirty,
780 cfs_spin_unlock(&obd->obd_osfs_lock);
781 filter_fmd_put(exp, fmd);
783 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK2, (obd_timeout + 1) / 4);
789 cfs_gettimeofday(&start);
790 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
792 /* We still set up for ungranted pages so that granted pages
793 * can be written to disk as they were promised, and portals
794 * needs to keep the pages all aligned properly. */
795 lnb->dentry = dentry;
797 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset,
799 if (lnb->page == NULL)
800 GOTO(cleanup, rc = -ENOMEM);
802 /* DLM locking protects us from write and truncate competing
803 * for same region, but truncate can leave dirty page in the
804 * cache. it's possible the writeout on a such a page is in
805 * progress when we access it. it's also possible that during
806 * this writeout we put new (partial) data, but then won't
807 * be able to proceed in filter_commitrw_write(). thus let's
808 * just wait for writeout completion, should be rare enough.
810 wait_on_page_writeback(lnb->page);
811 BUG_ON(PageWriteback(lnb->page));
813 /* If the filter writes a partial page, then has the file
814 * extended, the client will read in the whole page. the
815 * filter has to be careful to zero the rest of the partial
816 * page on disk. we do it by hand for partial extending
817 * writes, send_bio() is responsible for zeroing pages when
818 * asked to read unmapped blocks -- brw_kiovec() does this. */
819 if (lnb->len != CFS_PAGE_SIZE) {
822 maxidx = ((i_size_read(dentry->d_inode) +
823 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
824 if (maxidx >= lnb->page->index) {
825 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
826 LPU64" flg %x before EOF %llu\n",
827 lnb->len, lnb->offset,lnb->flags,
828 i_size_read(dentry->d_inode));
829 filter_iobuf_add_page(obd, iobuf,
834 char *p = kmap(lnb->page);
836 off = lnb->offset & ~CFS_PAGE_MASK;
839 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
841 memset(p + off, 0, CFS_PAGE_SIZE - off);
846 tot_bytes += lnb->len;
848 cfs_gettimeofday(&end);
849 timediff = cfs_timeval_sub(&end, &start, NULL);
850 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
852 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
853 GOTO(cleanup, rc = -ENOMEM);
855 /* don't unlock pages to prevent any access */
856 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
859 fsfilt_check_slow(obd, now, "start_page_write");
861 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
864 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
865 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
866 LPROC_FILTER_WRITE_BYTES, tot_bytes);
869 switch(cleanup_phase) {
872 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
873 if (lnb->page != NULL) {
874 unlock_page(lnb->page);
875 page_cache_release(lnb->page);
882 up_read(&dentry->d_inode->i_alloc_sem);
884 filter_iobuf_put(&obd->u.filter, iobuf, oti);
886 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
891 filter_iobuf_put(&obd->u.filter, iobuf, oti);
893 cfs_spin_lock(&obd->obd_osfs_lock);
895 filter_grant_incoming(exp, oa);
896 cfs_spin_unlock(&obd->obd_osfs_lock);
897 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
904 int filter_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
905 int objcount, struct obd_ioobj *obj,
906 struct niobuf_remote *nb, int *npages,
907 struct niobuf_local *res, struct obd_trans_info *oti,
908 struct lustre_capa *capa)
910 if (cmd == OBD_BRW_WRITE)
911 return filter_preprw_write(cmd, exp, oa, objcount, obj,
912 nb, npages, res, oti, capa);
913 if (cmd == OBD_BRW_READ)
914 return filter_preprw_read(cmd, exp, oa, objcount, obj,
915 nb, npages, res, oti, capa);
920 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
921 int objcount, struct obd_ioobj *obj,
922 struct niobuf_remote *rnb,
923 int npages, struct niobuf_local *res,
924 struct obd_trans_info *oti, int rc)
926 struct filter_obd *fo = &exp->exp_obd->u.filter;
927 struct inode *inode = NULL;
928 struct ldlm_res_id res_id;
929 struct ldlm_resource *resource = NULL;
930 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
931 struct niobuf_local *lnb;
935 osc_build_res_name(obj->ioo_id, obj->ioo_seq, &res_id);
936 /* If oa != NULL then filter_preprw_read updated the inode atime
937 * and we should update the lvb so that other glimpses will also
938 * get the updated value. bug 5972 */
939 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
940 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
942 if (resource != NULL) {
943 LDLM_RESOURCE_ADDREF(resource);
944 ns->ns_lvbo->lvbo_update(resource, NULL, 1);
945 LDLM_RESOURCE_DELREF(resource);
946 ldlm_resource_putref(resource);
950 if (res->dentry != NULL)
951 inode = res->dentry->d_inode;
953 for (i = 0, lnb = res; i < npages; i++, lnb++) {
954 if (lnb->page != NULL) {
955 page_cache_release(lnb->page);
959 if (inode && (fo->fo_read_cache == 0 ||
960 i_size_read(inode) > fo->fo_readcache_max_filesize))
961 filter_release_cache(exp->exp_obd, obj, rnb, inode);
963 if (res->dentry != NULL)
968 void filter_grant_commit(struct obd_export *exp, int niocount,
969 struct niobuf_local *res)
971 struct filter_obd *filter = &exp->exp_obd->u.filter;
972 struct niobuf_local *lnb = res;
973 unsigned long pending = 0;
976 cfs_spin_lock(&exp->exp_obd->obd_osfs_lock);
977 for (i = 0, lnb = res; i < niocount; i++, lnb++)
978 pending += lnb->lnb_grant_used;
980 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
981 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
982 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
983 exp->exp_filter_data.fed_pending, pending);
984 exp->exp_filter_data.fed_pending -= pending;
985 LASSERTF(filter->fo_tot_granted >= pending,
986 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
987 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
988 exp->exp_obd->u.filter.fo_tot_granted, pending);
989 filter->fo_tot_granted -= pending;
990 LASSERTF(filter->fo_tot_pending >= pending,
991 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
992 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
993 filter->fo_tot_pending, pending);
994 filter->fo_tot_pending -= pending;
996 cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
999 int filter_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1000 int objcount, struct obd_ioobj *obj,
1001 struct niobuf_remote *nb, int npages,
1002 struct niobuf_local *res, struct obd_trans_info *oti,
1005 if (cmd == OBD_BRW_WRITE)
1006 return filter_commitrw_write(exp, oa, objcount, obj,
1007 nb, npages, res, oti, rc);
1008 if (cmd == OBD_BRW_READ)
1009 return filter_commitrw_read(exp, oa, objcount, obj,
1010 nb, npages, res, oti, rc);