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;
87 int rc, create = (rw == OBD_BRW_WRITE), blocks_per_page;
88 int cleanup_phase = 0, *created = NULL;
92 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
93 if (iobuf->nr_pages * blocks_per_page > KIO_MAX_SECTORS)
94 GOTO(cleanup, rc = -EINVAL);
96 OBD_ALLOC(created, sizeof(*created) * iobuf->nr_pages*blocks_per_page);
98 GOTO(cleanup, rc = -ENOMEM);
101 rc = lock_kiovec(1, &iobuf, 1);
106 rc = fsfilt_map_inode_pages(obd, inode, iobuf->maplist,
107 iobuf->nr_pages, iobuf->blocks, created,
108 create, &obd->u.filter.fo_alloc_lock);
112 filter_tally_write(&obd->u.filter, iobuf->maplist, iobuf->nr_pages,
113 iobuf->blocks, blocks_per_page);
115 if (attr->ia_size > inode->i_size)
116 attr->ia_valid |= ATTR_SIZE;
117 rc = fsfilt_setattr(obd, dchild, oti->oti_handle, attr, 0);
124 rc = filter_finish_transno(exp, oti, 0);
128 rc = fsfilt_commit_async(obd, inode, oti->oti_handle, wait_handle);
129 oti->oti_handle = NULL;
134 check_pending_bhs(iobuf->blocks, iobuf->nr_pages, inode->i_dev,
135 1 << inode->i_blkbits);
137 rc = filemap_fdatasync(inode->i_mapping);
139 rc = fsync_inode_data_buffers(inode);
141 rc = filemap_fdatawait(inode->i_mapping);
145 rc = fsfilt_send_bio(obd, inode, iobuf);
147 CDEBUG(D_INFO, "tried to write %d pages, rc = %d\n",
148 iobuf->nr_pages, rc);
156 int err = fsfilt_commit_async(obd, inode,
157 oti->oti_handle, wait_handle);
158 oti->oti_handle = NULL;
160 CERROR("can't close transaction: %d\n", err);
162 * this is error path, so we prefer to return
163 * original error, not this one
167 switch(cleanup_phase) {
170 unlock_kiovec(1, &iobuf);
172 OBD_FREE(created, sizeof(*created) *
173 iobuf->nr_pages*blocks_per_page);
175 if (cleanup_phase == 3)
180 CERROR("corrupt cleanup_phase (%d)?\n", cleanup_phase);
187 /* See if there are unallocated parts in given file region */
188 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
190 int (*fs_bmap)(struct address_space *, long) =
191 inode->i_mapping->a_ops->bmap;
194 /* We can't know if the range is mapped already or not */
198 offset >>= inode->i_blkbits;
199 len >>= inode->i_blkbits;
201 for (j = 0; j <= len; j++)
202 if (fs_bmap(inode->i_mapping, offset + j) == 0)
208 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa, int objcount,
209 struct obd_ioobj *obj, int niocount,
210 struct niobuf_local *res, struct obd_trans_info *oti,
213 struct obd_device *obd = exp->exp_obd;
214 struct lvfs_run_ctxt saved;
215 struct niobuf_local *lnb;
216 struct fsfilt_objinfo fso;
217 struct iattr iattr = { 0 };
218 struct kiobuf *iobuf;
219 struct inode *inode = NULL;
220 int i, n, cleanup_phase = 0, err;
221 unsigned long now = jiffies; /* DEBUGGING OST TIMEOUTS */
224 LASSERT(oti != NULL);
225 LASSERT(objcount == 1);
226 LASSERT(current->journal_info == NULL);
231 rc = alloc_kiovec(1, &iobuf);
236 #ifdef HAVE_KIOBUF_DOVARY
237 iobuf->dovary = 0; /* this prevents corruption, not present in 2.4.20 */
239 rc = expand_kiobuf(iobuf, obj->ioo_bufcnt);
248 fso.fso_dentry = res->dentry;
249 fso.fso_bufcnt = obj->ioo_bufcnt;
250 inode = res->dentry->d_inode;
252 for (i = 0, lnb = res, n = 0; i < obj->ioo_bufcnt; i++, lnb++) {
255 /* If overwriting an existing block, we don't need a grant */
256 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
257 filter_range_is_mapped(inode, lnb->offset, lnb->len))
260 if (lnb->rc) /* ENOSPC, network RPC error */
263 iobuf->maplist[n++] = lnb->page;
264 iobuf->length += PAGE_SIZE;
267 /* We expect these pages to be in offset order, but we'll
269 this_size = lnb->offset + lnb->len;
270 if (this_size > iattr.ia_size)
271 iattr.ia_size = this_size;
274 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
278 oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
280 if (IS_ERR(oti->oti_handle)) {
281 rc = PTR_ERR(oti->oti_handle);
282 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
283 "error starting transaction: rc = %d\n", rc);
284 oti->oti_handle = NULL;
288 if (time_after(jiffies, now + 15 * HZ))
289 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
291 iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
292 rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
295 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
297 if (time_after(jiffies, now + 15 * HZ))
298 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
300 err = fsfilt_commit_wait(obd, inode, wait_handle);
304 LASSERT(oti->oti_transno <= obd->obd_last_committed);
305 if (time_after(jiffies, now + 15 * HZ))
306 CERROR("slow commitrw commit %lus\n", (jiffies - now) / HZ);
308 filter_grant_commit(exp, niocount, res);
310 switch (cleanup_phase) {
312 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
313 LASSERT(current->journal_info == NULL);
315 free_kiovec(1, &iobuf);
317 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
318 filter_release_write_page(&obd->u.filter,
319 res->dentry->d_inode, lnb,