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/iobuf.h>
35 #include <linux/locks.h>
37 #include <linux/obd_class.h>
38 #include <linux/lustre_fsfilt.h>
39 #include "filter_internal.h"
42 /* We should only change the file mtime (and not the ctime, like
43 * update_inode_times() in generic_file_write()) when we only change data. */
44 void inode_update_time(struct inode *inode, int ctime_too)
46 time_t now = CURRENT_TIME;
47 if (inode->i_mtime == now && (!ctime_too || inode->i_ctime == now))
52 mark_inode_dirty_sync(inode);
55 /* Bug 2254 -- this is better done in ext3_map_inode_page, but this
56 * workaround will suffice until everyone has upgraded their kernels */
57 static void check_pending_bhs(unsigned long *blocks, int nr_pages, dev_t dev,
60 #if (LUSTRE_KERNEL_VERSION < 32)
61 struct buffer_head *bh;
64 for (i = 0; i < nr_pages; i++) {
65 bh = get_hash_table(dev, blocks[i], size);
68 if (!buffer_dirty(bh)) {
72 mark_buffer_clean(bh);
74 clear_bit(BH_Req, &bh->b_state);
80 /* Must be called with i_sem taken; this will drop it */
81 static int filter_direct_io(int rw, struct dentry *dchild, struct kiobuf *iobuf,
82 struct obd_export *exp, struct iattr *attr,
83 struct obd_trans_info *oti, void **wait_handle)
85 struct obd_device *obd = exp->exp_obd;
86 struct inode *inode = dchild->d_inode;
88 unsigned long *b = iobuf->blocks;
89 int rc, i, create = (rw == OBD_BRW_WRITE), blocks_per_page;
90 int *cr, cleanup_phase = 0, *created = NULL;
94 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
95 if (iobuf->nr_pages * blocks_per_page > KIO_MAX_SECTORS)
96 GOTO(cleanup, rc = -EINVAL);
98 OBD_ALLOC(created, sizeof(*created) * iobuf->nr_pages*blocks_per_page);
100 GOTO(cleanup, rc = -ENOMEM);
103 rc = lock_kiovec(1, &iobuf, 1);
108 down(&exp->exp_obd->u.filter.fo_alloc_lock);
109 for (i = 0, cr = created, b = iobuf->blocks; i < iobuf->nr_pages; i++){
110 page = iobuf->maplist[i];
112 rc = fsfilt_map_inode_page(obd, inode, page, b, cr, create);
114 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
115 inode->i_ino, *b, *cr, create, rc);
116 up(&exp->exp_obd->u.filter.fo_alloc_lock);
120 b += blocks_per_page;
121 cr += blocks_per_page;
123 up(&exp->exp_obd->u.filter.fo_alloc_lock);
125 filter_tally_write(&obd->u.filter, iobuf->maplist, iobuf->nr_pages,
126 iobuf->blocks, blocks_per_page);
128 if (attr->ia_size > inode->i_size)
129 attr->ia_valid |= ATTR_SIZE;
130 rc = fsfilt_setattr(obd, dchild, oti->oti_handle, attr, 0);
137 rc = filter_finish_transno(exp, oti, 0);
141 rc = fsfilt_commit_async(obd, inode, oti->oti_handle, wait_handle);
142 oti->oti_handle = NULL;
147 check_pending_bhs(iobuf->blocks, iobuf->nr_pages, inode->i_dev,
148 1 << inode->i_blkbits);
150 rc = filemap_fdatasync(inode->i_mapping);
152 rc = fsync_inode_data_buffers(inode);
154 rc = filemap_fdatawait(inode->i_mapping);
158 rc = fsfilt_send_bio(obd, inode, iobuf);
160 CDEBUG(D_INFO, "tried to write %d pages, rc = %d\n",
161 iobuf->nr_pages, rc);
169 int err = fsfilt_commit_async(obd, inode,
170 oti->oti_handle, wait_handle);
171 oti->oti_handle = NULL;
173 CERROR("can't close transaction: %d\n", err);
175 * this is error path, so we prefer to return
176 * original error, not this one
180 switch(cleanup_phase) {
183 unlock_kiovec(1, &iobuf);
185 OBD_FREE(created, sizeof(*created) *
186 iobuf->nr_pages*blocks_per_page);
188 if (cleanup_phase == 3)
193 CERROR("corrupt cleanup_phase (%d)?\n", cleanup_phase);
200 /* See if there are unallocated parts in given file region */
201 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
203 int (*fs_bmap)(struct address_space *, long) =
204 inode->i_mapping->a_ops->bmap;
207 /* We can't know if the range is mapped already or not */
211 offset >>= inode->i_blkbits;
212 len >>= inode->i_blkbits;
214 for (j = 0; j <= len; j++)
215 if (fs_bmap(inode->i_mapping, offset + j) == 0)
221 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa, int objcount,
222 struct obd_ioobj *obj, int niocount,
223 struct niobuf_local *res, struct obd_trans_info *oti,
226 struct obd_device *obd = exp->exp_obd;
227 struct lvfs_run_ctxt saved;
228 struct niobuf_local *lnb;
229 struct fsfilt_objinfo fso;
230 struct iattr iattr = { 0 };
231 struct kiobuf *iobuf;
232 struct inode *inode = NULL;
233 int i, n, cleanup_phase = 0, err;
234 unsigned long now = jiffies; /* DEBUGGING OST TIMEOUTS */
237 LASSERT(oti != NULL);
238 LASSERT(objcount == 1);
239 LASSERT(current->journal_info == NULL);
244 rc = alloc_kiovec(1, &iobuf);
249 #ifdef HAVE_KIOBUF_DOVARY
250 iobuf->dovary = 0; /* this prevents corruption, not present in 2.4.20 */
252 rc = expand_kiobuf(iobuf, obj->ioo_bufcnt);
261 fso.fso_dentry = res->dentry;
262 fso.fso_bufcnt = obj->ioo_bufcnt;
263 inode = res->dentry->d_inode;
265 for (i = 0, lnb = res, n = 0; i < obj->ioo_bufcnt; i++, lnb++) {
268 /* If overwriting an existing block, we don't need a grant */
269 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
270 filter_range_is_mapped(inode, lnb->offset, lnb->len))
273 if (lnb->rc) /* ENOSPC, network RPC error */
276 iobuf->maplist[n++] = lnb->page;
277 iobuf->length += PAGE_SIZE;
280 /* We expect these pages to be in offset order, but we'll
282 this_size = lnb->offset + lnb->len;
283 if (this_size > iattr.ia_size)
284 iattr.ia_size = this_size;
287 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
291 oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
293 if (IS_ERR(oti->oti_handle)) {
294 rc = PTR_ERR(oti->oti_handle);
295 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
296 "error starting transaction: rc = %d\n", rc);
297 oti->oti_handle = NULL;
301 if (time_after(jiffies, now + 15 * HZ))
302 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
304 iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
305 rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
308 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
310 if (time_after(jiffies, now + 15 * HZ))
311 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
313 err = fsfilt_commit_wait(obd, inode, wait_handle);
317 LASSERT(oti->oti_transno <= obd->obd_last_committed);
318 if (time_after(jiffies, now + 15 * HZ))
319 CERROR("slow commitrw commit %lus\n", (jiffies - now) / HZ);
322 filter_grant_commit(exp, niocount, res);
324 switch (cleanup_phase) {
326 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
327 LASSERT(current->journal_info == NULL);
329 free_kiovec(1, &iobuf);
331 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
332 /* flip_.. gets a ref, while free_page only frees
333 * when it decrefs to 0 */
335 flip_into_page_cache(inode, lnb->page);
336 __free_page(lnb->page);