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);
475 filter_counter_incr(exp, LPROC_FILTER_STATS_READ,
476 oti ? oti->oti_jobid : NULL, tot_bytes);
480 /* unlock pages to allow access from concurrent OST_READ */
481 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
483 LASSERT(PageLocked(lnb->page));
484 unlock_page(lnb->page);
487 page_cache_release(lnb->page);
498 filter_iobuf_put(&obd->u.filter, iobuf, oti);
500 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
502 CERROR("io error %d\n", rc);
507 /* When clients have dirtied as much space as they've been granted they
508 * fall through to sync writes. These sync writes haven't been expressed
509 * in grants and need to error with ENOSPC when there isn't room in the
510 * filesystem for them after grants are taken into account. However,
511 * writeback of the dirty data that was already granted space can write
514 * Caller must hold obd_osfs_lock. */
515 static int filter_grant_check(struct obd_export *exp, struct obdo *oa,
516 int objcount, struct fsfilt_objinfo *fso,
517 int niocount, struct niobuf_local *lnb,
518 obd_size *left, struct inode *inode)
520 struct filter_export_data *fed = &exp->exp_filter_data;
521 int blocksize = exp->exp_obd->u.obt.obt_sb->s_blocksize;
522 unsigned long used = 0, ungranted = 0, using;
523 int i, rc = -ENOSPC, obj, n = 0;
526 if ((oa->o_valid & OBD_MD_FLFLAGS) &&
527 (oa->o_flags & OBD_FL_RECOV_RESEND)) {
529 CDEBUG(D_CACHE, "Recoverable resend arrived, skipping "
533 LASSERT_SPIN_LOCKED(&exp->exp_obd->obd_osfs_lock);
535 for (obj = 0; obj < objcount; obj++) {
536 for (i = 0; i < fso[obj].fso_bufcnt; i++, n++) {
539 /* should match the code in osc_exit_cache */
541 bytes += lnb[n].offset & (blocksize - 1);
542 tmp = (lnb[n].offset + lnb[n].len) & (blocksize - 1);
544 bytes += blocksize - tmp;
546 if ((lnb[n].flags & OBD_BRW_FROM_GRANT) &&
547 (oa->o_valid & OBD_MD_FLGRANT)) {
549 /* this is a recoverable resent */
550 lnb[n].flags |= OBD_BRW_GRANTED;
553 } else if (fed->fed_grant < used + bytes) {
555 "%s: cli %s/%p claims %ld+%d "
556 "GRANT, real grant %lu idx %d\n",
557 exp->exp_obd->obd_name,
558 exp->exp_client_uuid.uuid, exp,
559 used, bytes, fed->fed_grant, n);
562 lnb[n].flags |= OBD_BRW_GRANTED;
563 lnb[n].lnb_grant_used = bytes;
564 CDEBUG(0, "idx %d used=%lu\n", n, used);
569 if (*left > ungranted + bytes) {
570 /* if enough space, pretend it was granted */
572 lnb[n].flags |= OBD_BRW_GRANTED;
573 lnb[n].lnb_grant_used = bytes;
574 CDEBUG(0, "idx %d ungranted=%lu\n",n,ungranted);
579 /* We can't check for already-mapped blocks here, as
580 * it requires dropping the osfs lock to do the bmap.
581 * Instead, we return ENOSPC and in that case we need
582 * to go through and verify if all of the blocks not
583 * marked BRW_GRANTED are already mapped and we can
584 * ignore this error. */
586 lnb[n].flags &= ~OBD_BRW_GRANTED;
587 CDEBUG(D_CACHE,"%s: cli %s/%p idx %d no space for %d\n",
588 exp->exp_obd->obd_name,
589 exp->exp_client_uuid.uuid, exp, n, bytes);
593 /* Now substract what client have used already. We don't subtract
594 * this from the tot_granted yet, so that other client's can't grab
595 * that space before we have actually allocated our blocks. That
596 * happens in filter_grant_commit() after the writes are done. */
598 fed->fed_grant -= used;
599 fed->fed_pending += used + ungranted;
600 exp->exp_obd->u.filter.fo_tot_granted += ungranted;
601 exp->exp_obd->u.filter.fo_tot_pending += used + ungranted;
604 "%s: cli %s/%p used: %lu ungranted: %lu grant: %lu dirty: %lu\n",
605 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp, used,
606 ungranted, fed->fed_grant, fed->fed_dirty);
608 /* Rough calc in case we don't refresh cached statfs data */
609 using = (used + ungranted + 1 ) >>
610 exp->exp_obd->u.obt.obt_sb->s_blocksize_bits;
611 if (exp->exp_obd->obd_osfs.os_bavail > using)
612 exp->exp_obd->obd_osfs.os_bavail -= using;
614 exp->exp_obd->obd_osfs.os_bavail = 0;
616 if (fed->fed_dirty < used) {
617 CERROR("%s: cli %s/%p claims used %lu > fed_dirty %lu\n",
618 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
619 used, fed->fed_dirty);
620 used = fed->fed_dirty;
622 exp->exp_obd->u.filter.fo_tot_dirty -= used;
623 fed->fed_dirty -= used;
625 if (fed->fed_dirty < 0 || fed->fed_grant < 0 || fed->fed_pending < 0) {
626 CERROR("%s: cli %s/%p dirty %ld pend %ld grant %ld\n",
627 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
628 fed->fed_dirty, fed->fed_pending, fed->fed_grant);
629 cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
635 /* If we ever start to support multi-object BRW RPCs, we will need to get locks
636 * on mulitple inodes. That isn't all, because there still exists the
637 * possibility of a truncate starting a new transaction while holding the ext3
638 * rwsem = write while some writes (which have started their transactions here)
639 * blocking on the ext3 rwsem = read => lock inversion.
641 * The handling gets very ugly when dealing with locked pages. It may be easier
642 * to just get rid of the locked page code (which has problems of its own) and
643 * either discover we do not need it anymore (i.e. it was a symptom of another
644 * bug) or ensure we get the page locks in an appropriate order. */
645 static int filter_preprw_write(int cmd, struct obd_export *exp, struct obdo *oa,
646 int objcount, struct obd_ioobj *obj,
647 struct niobuf_remote *nb, int *npages,
648 struct niobuf_local *res,
649 struct obd_trans_info *oti,
650 struct lustre_capa *capa)
652 struct obd_device *obd = exp->exp_obd;
653 struct timeval start, end;
654 struct lvfs_run_ctxt saved;
655 struct niobuf_local *lnb = res;
656 struct fsfilt_objinfo fso;
657 struct filter_mod_data *fmd;
658 struct dentry *dentry = NULL;
661 unsigned long now = jiffies, timediff;
662 int rc = 0, i, tot_bytes = 0, cleanup_phase = 0, localreq = 0;
664 LASSERT(objcount == 1);
665 LASSERT(obj->ioo_bufcnt > 0);
667 rc = filter_auth_capa(exp, NULL, oa->o_seq, capa,
672 if (exp->exp_connection &&
673 exp->exp_connection->c_peer.nid == exp->exp_connection->c_self)
676 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
677 iobuf = filter_iobuf_get(&obd->u.filter, oti);
679 GOTO(cleanup, rc = PTR_ERR(iobuf));
682 dentry = filter_fid2dentry(obd, NULL, obj->ioo_seq,
685 GOTO(cleanup, rc = PTR_ERR(dentry));
688 if (dentry->d_inode == NULL) {
689 if (exp->exp_obd->obd_recovering) {
690 struct obdo *noa = oa;
695 GOTO(recreate_out, rc = -ENOMEM);
696 noa->o_id = obj->ioo_id;
697 noa->o_valid = OBD_MD_FLID;
700 if (filter_create(NULL, exp, noa, NULL, oti) == 0) {
702 dentry = filter_fid2dentry(exp->exp_obd, NULL,
710 if (IS_ERR(dentry) || dentry->d_inode == NULL) {
711 CERROR("%s: BRW to missing obj "LPU64"/"LPU64":rc %d\n",
712 exp->exp_obd->obd_name,
713 obj->ioo_id, obj->ioo_seq,
714 IS_ERR(dentry) ? (int)PTR_ERR(dentry) : -ENOENT);
717 GOTO(cleanup, rc = -ENOENT);
721 if (oa->o_valid & (OBD_MD_FLUID | OBD_MD_FLGID) &&
722 dentry->d_inode->i_mode & (S_ISUID | S_ISGID)) {
723 rc = filter_capa_fixoa(exp, oa, oa->o_seq, capa);
728 rc = filter_map_remote_to_local(objcount, obj, nb, npages, res);
732 fsfilt_check_slow(obd, now, "preprw_write setup");
734 /* Filter truncate first locks i_mutex then partially truncated
735 * page, filter write code first locks pages then take
736 * i_mutex. To avoid a deadlock in case of concurrent
737 * punch/write requests from one client, filter writes and
738 * filter truncates are serialized by i_alloc_sem, allowing
739 * multiple writes or single truncate. */
740 down_read(&dentry->d_inode->i_alloc_sem);
741 fsfilt_check_slow(obd, now, "i_alloc_sem");
743 /* Don't update inode timestamps if this write is older than a
744 * setattr which modifies the timestamps. b=10150 */
745 /* XXX when we start having persistent reservations this needs to
746 * be changed to filter_fmd_get() to create the fmd if it doesn't
747 * already exist so we can store the reservation handle there. */
748 fmd = filter_fmd_find(exp, obj->ioo_id, obj->ioo_seq);
751 cfs_spin_lock(&obd->obd_osfs_lock);
752 filter_grant_incoming(exp, oa);
753 if (fmd && fmd->fmd_mactime_xid > oti->oti_xid)
754 oa->o_valid &= ~(OBD_MD_FLMTIME | OBD_MD_FLCTIME |
757 obdo_to_inode(dentry->d_inode, oa, OBD_MD_FLATIME |
758 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
761 left = filter_grant_space_left(exp);
763 fso.fso_dentry = dentry;
764 fso.fso_bufcnt = *npages;
766 rc = filter_grant_check(exp, oa, objcount, &fso, *npages, res,
767 &left, dentry->d_inode);
769 /* do not zero out oa->o_valid as it is used in filter_commitrw_write()
770 * for setting UID/GID and fid EA in first write time. */
771 /* If OBD_FL_SHRINK_GRANT is set, the client just returned us some grant
772 * so no sense in allocating it some more. We either return the grant
773 * back to the client if we have plenty of space or we don't return
774 * anything if we are short. This was decided in filter_grant_incoming*/
775 if ((oa->o_valid & OBD_MD_FLGRANT) &&
776 (!(oa->o_valid & OBD_MD_FLFLAGS) ||
777 !(oa->o_flags & OBD_FL_SHRINK_GRANT)))
778 oa->o_grant = filter_grant(exp, oa->o_grant, oa->o_undirty,
781 cfs_spin_unlock(&obd->obd_osfs_lock);
782 filter_fmd_put(exp, fmd);
784 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_BRW_PAUSE_BULK2, (obd_timeout + 1) / 4);
790 cfs_gettimeofday(&start);
791 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
793 /* We still set up for ungranted pages so that granted pages
794 * can be written to disk as they were promised, and portals
795 * needs to keep the pages all aligned properly. */
796 lnb->dentry = dentry;
798 lnb->page = filter_get_page(obd, dentry->d_inode, lnb->offset,
800 if (lnb->page == NULL)
801 GOTO(cleanup, rc = -ENOMEM);
803 /* DLM locking protects us from write and truncate competing
804 * for same region, but truncate can leave dirty page in the
805 * cache. it's possible the writeout on a such a page is in
806 * progress when we access it. it's also possible that during
807 * this writeout we put new (partial) data, but then won't
808 * be able to proceed in filter_commitrw_write(). thus let's
809 * just wait for writeout completion, should be rare enough.
811 wait_on_page_writeback(lnb->page);
812 BUG_ON(PageWriteback(lnb->page));
814 /* If the filter writes a partial page, then has the file
815 * extended, the client will read in the whole page. the
816 * filter has to be careful to zero the rest of the partial
817 * page on disk. we do it by hand for partial extending
818 * writes, send_bio() is responsible for zeroing pages when
819 * asked to read unmapped blocks -- brw_kiovec() does this. */
820 if (lnb->len != CFS_PAGE_SIZE) {
823 maxidx = ((i_size_read(dentry->d_inode) +
824 CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
825 if (maxidx >= lnb->page->index) {
826 LL_CDEBUG_PAGE(D_PAGE, lnb->page, "write %u @ "
827 LPU64" flg %x before EOF %llu\n",
828 lnb->len, lnb->offset,lnb->flags,
829 i_size_read(dentry->d_inode));
830 filter_iobuf_add_page(obd, iobuf,
835 char *p = kmap(lnb->page);
837 off = lnb->offset & ~CFS_PAGE_MASK;
840 off = (lnb->offset + lnb->len) & ~CFS_PAGE_MASK;
842 memset(p + off, 0, CFS_PAGE_SIZE - off);
847 tot_bytes += lnb->len;
849 cfs_gettimeofday(&end);
850 timediff = cfs_timeval_sub(&end, &start, NULL);
851 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_GET_PAGE, timediff);
853 if (OBD_FAIL_CHECK(OBD_FAIL_OST_NOMEM))
854 GOTO(cleanup, rc = -ENOMEM);
856 /* don't unlock pages to prevent any access */
857 rc = filter_direct_io(OBD_BRW_READ, dentry, iobuf, exp,
860 fsfilt_check_slow(obd, now, "start_page_write");
862 lprocfs_counter_add(obd->obd_stats, LPROC_FILTER_WRITE_BYTES,
865 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats)
866 lprocfs_counter_add(exp->exp_nid_stats->nid_stats,
867 LPROC_FILTER_WRITE_BYTES, tot_bytes);
868 filter_counter_incr(exp, LPROC_FILTER_STATS_WRITE,
869 oti ? oti->oti_jobid : NULL, tot_bytes);
872 switch(cleanup_phase) {
875 for (i = 0, lnb = res; i < *npages; i++, lnb++) {
876 if (lnb->page != NULL) {
877 unlock_page(lnb->page);
878 page_cache_release(lnb->page);
885 up_read(&dentry->d_inode->i_alloc_sem);
887 filter_iobuf_put(&obd->u.filter, iobuf, oti);
889 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
894 filter_iobuf_put(&obd->u.filter, iobuf, oti);
896 cfs_spin_lock(&obd->obd_osfs_lock);
898 filter_grant_incoming(exp, oa);
899 cfs_spin_unlock(&obd->obd_osfs_lock);
900 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
907 int filter_preprw(const struct lu_env *env, int cmd, struct obd_export *exp,
908 struct obdo *oa, int objcount, struct obd_ioobj *obj,
909 struct niobuf_remote *nb, int *npages,
910 struct niobuf_local *res, struct obd_trans_info *oti,
911 struct lustre_capa *capa)
913 if (cmd == OBD_BRW_WRITE)
914 return filter_preprw_write(cmd, exp, oa, objcount, obj,
915 nb, npages, res, oti, capa);
916 if (cmd == OBD_BRW_READ)
917 return filter_preprw_read(cmd, exp, oa, objcount, obj,
918 nb, npages, res, oti, capa);
923 static int filter_commitrw_read(struct obd_export *exp, struct obdo *oa,
924 int objcount, struct obd_ioobj *obj,
925 struct niobuf_remote *rnb,
926 int npages, struct niobuf_local *res,
927 struct obd_trans_info *oti, int rc)
929 struct filter_obd *fo = &exp->exp_obd->u.filter;
930 struct inode *inode = NULL;
931 struct ldlm_res_id res_id;
932 struct ldlm_resource *resource = NULL;
933 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
934 struct niobuf_local *lnb;
938 osc_build_res_name(obj->ioo_id, obj->ioo_seq, &res_id);
939 /* If oa != NULL then filter_preprw_read updated the inode atime
940 * and we should update the lvb so that other glimpses will also
941 * get the updated value. bug 5972 */
942 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
943 resource = ldlm_resource_get(ns, NULL, &res_id, LDLM_EXTENT, 0);
945 if (resource != NULL) {
946 LDLM_RESOURCE_ADDREF(resource);
947 ns->ns_lvbo->lvbo_update(resource, NULL, 1);
948 LDLM_RESOURCE_DELREF(resource);
949 ldlm_resource_putref(resource);
953 if (res->dentry != NULL)
954 inode = res->dentry->d_inode;
956 for (i = 0, lnb = res; i < npages; i++, lnb++) {
957 if (lnb->page != NULL) {
958 page_cache_release(lnb->page);
962 if (inode && (fo->fo_read_cache == 0 ||
963 i_size_read(inode) > fo->fo_readcache_max_filesize))
964 filter_release_cache(exp->exp_obd, obj, rnb, inode);
966 if (res->dentry != NULL)
971 void filter_grant_commit(struct obd_export *exp, int niocount,
972 struct niobuf_local *res)
974 struct filter_obd *filter = &exp->exp_obd->u.filter;
975 struct niobuf_local *lnb = res;
976 unsigned long pending = 0;
979 cfs_spin_lock(&exp->exp_obd->obd_osfs_lock);
980 for (i = 0, lnb = res; i < niocount; i++, lnb++)
981 pending += lnb->lnb_grant_used;
983 LASSERTF(exp->exp_filter_data.fed_pending >= pending,
984 "%s: cli %s/%p fed_pending: %lu grant_used: %lu\n",
985 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
986 exp->exp_filter_data.fed_pending, pending);
987 exp->exp_filter_data.fed_pending -= pending;
988 LASSERTF(filter->fo_tot_granted >= pending,
989 "%s: cli %s/%p tot_granted: "LPU64" grant_used: %lu\n",
990 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
991 exp->exp_obd->u.filter.fo_tot_granted, pending);
992 filter->fo_tot_granted -= pending;
993 LASSERTF(filter->fo_tot_pending >= pending,
994 "%s: cli %s/%p tot_pending: "LPU64" grant_used: %lu\n",
995 exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
996 filter->fo_tot_pending, pending);
997 filter->fo_tot_pending -= pending;
999 cfs_spin_unlock(&exp->exp_obd->obd_osfs_lock);
1002 int filter_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
1003 struct obdo *oa, int objcount, struct obd_ioobj *obj,
1004 struct niobuf_remote *nb, int npages,
1005 struct niobuf_local *res, struct obd_trans_info *oti,
1008 if (cmd == OBD_BRW_WRITE)
1009 return filter_commitrw_write(exp, oa, objcount, obj,
1010 nb, npages, res, oti, rc);
1011 if (cmd == OBD_BRW_READ)
1012 return filter_commitrw_read(exp, oa, objcount, obj,
1013 nb, npages, res, oti, rc);