1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/fs/obdfilter/filter_io.c
6 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
7 * Author: Peter Braam <braam@clusterfs.com>
8 * Author: Andreas Dilger <adilger@clusterfs.com>
9 * Author: Phil Schwan <phil@clusterfs.com>
11 * This file is part of Lustre, http://www.lustre.org.
13 * Lustre is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Lustre is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Lustre; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/pagemap.h> // XXX kill me soon
30 #include <linux/version.h>
32 #define DEBUG_SUBSYSTEM S_FILTER
34 #include <linux/obd_class.h>
35 #include <linux/lustre_fsfilt.h>
36 #include "filter_internal.h"
38 #warning "implement writeback mode -bzzz"
40 /* 512byte block min */
41 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
43 atomic_t numreqs; /* number of reqs being processed */
44 struct bio *bio_list; /* list of completed bios */
45 wait_queue_head_t wait;
46 int created[MAX_BLOCKS_PER_PAGE];
47 unsigned long blocks[MAX_BLOCKS_PER_PAGE];
51 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
53 struct dio_request *dreq = bio->bi_private;
56 spin_lock_irqsave(&dreq->lock, flags);
57 bio->bi_private = dreq->bio_list;
59 spin_unlock_irqrestore(&dreq->lock, flags);
60 if (atomic_dec_and_test(&dreq->numreqs))
66 static int can_be_merged(struct bio *bio, sector_t sector)
73 size = bio->bi_size >> 9;
74 return bio->bi_sector + size == sector ? 1 : 0;
77 /* See if there are unallocated parts in given file region */
78 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
80 sector_t (*fs_bmap)(struct address_space *, sector_t) =
81 inode->i_mapping->a_ops->bmap;
84 /* We can't know if we are overwriting or not */
88 offset >>= inode->i_blkbits;
89 len >>= inode->i_blkbits;
91 for (j = 0; j <= len; j++)
92 if (fs_bmap(inode->i_mapping, offset + j) == 0)
98 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa, int objcount,
99 struct obd_ioobj *obj, int niocount,
100 struct niobuf_local *res, struct obd_trans_info *oti)
102 struct obd_device *obd = exp->exp_obd;
103 struct obd_run_ctxt saved;
104 struct niobuf_local *lnb;
105 struct fsfilt_objinfo fso;
106 struct iattr iattr = { .ia_valid = ATTR_SIZE, .ia_size = 0, };
107 struct inode *inode = NULL;
108 int rc = 0, i, k, cleanup_phase = 0, err;
109 unsigned long now = jiffies; /* DEBUGGING OST TIMEOUTS */
111 struct dio_request *dreq;
112 struct bio *bio = NULL;
114 LASSERT(oti != NULL);
115 LASSERT(objcount == 1);
116 LASSERT(current->journal_info == NULL);
118 inode = res->dentry->d_inode;
119 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
120 LASSERT(blocks_per_page <= MAX_BLOCKS_PER_PAGE);
122 OBD_ALLOC(dreq, sizeof(*dreq));
125 dreq->bio_list = NULL;
126 init_waitqueue_head(&dreq->wait);
127 atomic_set(&dreq->numreqs, 0);
128 spin_lock_init(&dreq->lock);
131 fso.fso_dentry = res->dentry;
132 fso.fso_bufcnt = obj->ioo_bufcnt;
134 push_ctxt(&saved, &obd->obd_ctxt, NULL);
137 oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res, oti);
138 if (IS_ERR(oti->oti_handle)) {
139 rc = PTR_ERR(oti->oti_handle);
140 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
141 "error starting transaction: rc = %d\n", rc);
142 oti->oti_handle = NULL;
146 if (time_after(jiffies, now + 15 * HZ))
147 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
149 iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
150 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
155 /* If overwriting an existing block, we don't need a grant */
156 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
157 filter_range_is_mapped(inode, lnb->offset, lnb->len))
160 if (lnb->rc) /* ENOSPC, network RPC error */
163 /* get block number for next page */
164 rc = fsfilt_map_inode_page(obd, inode, lnb->page, dreq->blocks,
169 for (k = 0; k < blocks_per_page; k++) {
170 sector = dreq->blocks[k] *(inode->i_sb->s_blocksize>>9);
171 offs = k * inode->i_sb->s_blocksize;
173 if (!bio || !can_be_merged(bio, sector) ||
174 !bio_add_page(bio, lnb->page, lnb->len, offs)) {
176 atomic_inc(&dreq->numreqs);
177 submit_bio(WRITE, bio);
180 /* allocate new bio */
181 bio = bio_alloc(GFP_NOIO, obj->ioo_bufcnt);
182 bio->bi_bdev = inode->i_sb->s_bdev;
183 bio->bi_sector = sector;
184 bio->bi_end_io = dio_complete_routine;
185 bio->bi_private = dreq;
187 if (!bio_add_page(bio, lnb->page, lnb->len, 0))
192 /* We expect these pages to be in offset order, but we'll
194 this_size = lnb->offset + lnb->len;
195 if (this_size > iattr.ia_size)
196 iattr.ia_size = this_size;
199 #warning This probably needs filemap_fdatasync() like filter_io_24 (bug 2366)
201 atomic_inc(&dreq->numreqs);
202 submit_bio(WRITE, bio);
205 /* time to wait for I/O completion */
206 wait_event(dreq->wait, atomic_read(&dreq->numreqs) == 0);
209 while (dreq->bio_list) {
210 bio = dreq->bio_list;
211 dreq->bio_list = bio->bi_private;
217 if (iattr.ia_size > inode->i_size) {
218 CDEBUG(D_INFO, "setting i_size to "LPU64"\n",
220 fsfilt_setattr(obd, res->dentry, oti->oti_handle,
226 if (time_after(jiffies, now + 15 * HZ))
227 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
229 rc = filter_finish_transno(exp, oti, rc);
230 err = fsfilt_commit(obd, inode, oti->oti_handle, obd_sync_filter);
234 LASSERT(oti->oti_transno <= obd->obd_last_committed);
235 if (time_after(jiffies, now + 15 * HZ))
236 CERROR("slow commitrw commit %lus\n", (jiffies - now) / HZ);
239 filter_grant_commit(exp, niocount, res);
241 switch (cleanup_phase) {
243 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
244 LASSERT(current->journal_info == NULL);
246 OBD_FREE(dreq, sizeof(*dreq));
248 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
249 /* flip_.. gets a ref, while free_page only frees
250 * when it decrefs to 0 */
252 flip_into_page_cache(inode, lnb->page);
253 __free_page(lnb->page);