Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / obdfilter / filter_io_24.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/obdfilter/filter_io.c
5  *
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>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
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.
16  *
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.
21  *
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.
25  */
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/pagemap.h> // XXX kill me soon
30 #include <linux/version.h>
31
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33
34 #define DEBUG_SUBSYSTEM S_FILTER
35
36 #include <linux/iobuf.h>
37 #include <linux/locks.h>
38
39 #include <linux/obd_class.h>
40 #include <linux/lustre_fsfilt.h>
41 #include "filter_internal.h"
42
43
44 /* We should only change the file mtime (and not the ctime, like
45  * update_inode_times() in generic_file_write()) when we only change data. */
46 void inode_update_time(struct inode *inode, int ctime_too)
47 {
48         time_t now = CURRENT_TIME;
49         if (inode->i_mtime == now && (!ctime_too || inode->i_ctime == now))
50                 return;
51         inode->i_mtime = now;
52         if (ctime_too)
53                 inode->i_ctime = now;
54         mark_inode_dirty_sync(inode);
55 }
56
57 /* Bug 2254 -- this is better done in ext3_map_inode_page, but this
58  * workaround will suffice until everyone has upgraded their kernels */
59 static void check_pending_bhs(unsigned long *blocks, int nr_pages, dev_t dev,
60                               int size)
61 {
62 #if (LUSTRE_KERNEL_VERSION < 32)
63         struct buffer_head *bh;
64         int i;
65
66         for (i = 0; i < nr_pages; i++) {
67                 bh = get_hash_table(dev, blocks[i], size);
68                 if (bh == NULL)
69                         continue;
70                 if (!buffer_dirty(bh)) {
71                         put_bh(bh);
72                         continue;
73                 }
74                 mark_buffer_clean(bh);
75                 wait_on_buffer(bh);
76                 clear_bit(BH_Req, &bh->b_state);
77                 __brelse(bh);
78         }
79 #endif
80 }
81
82 /* Must be called with i_sem taken; this will drop it */
83 static int filter_direct_io(int rw, struct dentry *dchild, struct kiobuf *iobuf,
84                             struct obd_export *exp, struct iattr *attr,
85                             struct obd_trans_info *oti, void **wait_handle)
86 {
87         struct obd_device *obd = exp->exp_obd;
88         struct inode *inode = dchild->d_inode;
89         struct page *page;
90         unsigned long *b = iobuf->blocks;
91         int rc, i, create = (rw == OBD_BRW_WRITE), blocks_per_page;
92         int *cr, cleanup_phase = 0, *created = NULL;
93         int committed = 0;
94         ENTRY;
95
96         blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
97         if (iobuf->nr_pages * blocks_per_page > KIO_MAX_SECTORS)
98                 GOTO(cleanup, rc = -EINVAL);
99
100         OBD_ALLOC(created, sizeof(*created) * iobuf->nr_pages*blocks_per_page);
101         if (created == NULL)
102                 GOTO(cleanup, rc = -ENOMEM);
103         cleanup_phase = 1;
104
105         rc = lock_kiovec(1, &iobuf, 1);
106         if (rc < 0)
107                 GOTO(cleanup, rc);
108         cleanup_phase = 2;
109
110         down(&exp->exp_obd->u.filter.fo_alloc_lock);
111         for (i = 0, cr = created, b = iobuf->blocks; i < iobuf->nr_pages; i++){
112                 page = iobuf->maplist[i];
113
114                 rc = fsfilt_map_inode_page(obd, inode, page, b, cr, create);
115                 if (rc) {
116                         CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
117                                inode->i_ino, *b, *cr, create, rc);
118                         up(&exp->exp_obd->u.filter.fo_alloc_lock);
119                         GOTO(cleanup, rc);
120                 }
121
122                 b += blocks_per_page;
123                 cr += blocks_per_page;
124         }
125         up(&exp->exp_obd->u.filter.fo_alloc_lock);
126
127         if (attr->ia_size > inode->i_size)
128                 attr->ia_valid |= ATTR_SIZE;
129         rc = fsfilt_setattr(obd, dchild, oti->oti_handle, attr, 0);
130         if (rc)
131                 GOTO(cleanup, rc);
132
133         up(&inode->i_sem);
134         cleanup_phase = 3;
135
136         rc = filter_finish_transno(exp, oti, 0);
137         if (rc)
138                 GOTO(cleanup, rc);
139
140         rc = fsfilt_commit_async(obd, inode, oti->oti_handle, wait_handle);
141         oti->oti_handle = NULL;
142         committed = 1;
143         if (rc)
144                 GOTO(cleanup, rc);
145
146         check_pending_bhs(iobuf->blocks, iobuf->nr_pages, inode->i_dev,
147                           1 << inode->i_blkbits);
148
149         rc = brw_kiovec(WRITE, 1, &iobuf, inode->i_dev, iobuf->blocks,
150                         1 << inode->i_blkbits);
151         CDEBUG(D_INFO, "tried to write %d pages, rc = %d\n",
152                iobuf->nr_pages, rc);
153         if (rc != (1 << inode->i_blkbits) * iobuf->nr_pages * blocks_per_page)
154                 CERROR("short write?  expected %d, wrote %d\n",
155                        (1 << inode->i_blkbits) * iobuf->nr_pages *
156                        blocks_per_page, rc);
157         if (rc > 0)
158                 rc = 0;
159
160         EXIT;
161 cleanup:
162         if (!committed) {
163                 int err = fsfilt_commit_async(obd, inode,
164                                               oti->oti_handle, wait_handle);
165                 oti->oti_handle = NULL;
166                 if (err)
167                         CERROR("can't close transaction: %d\n", err);
168                 /*
169                  * this is error path, so we prefer to return
170                  * original error, not this one
171                  */
172         }
173
174         switch(cleanup_phase) {
175         case 3:
176         case 2:
177                 unlock_kiovec(1, &iobuf);
178         case 1:
179                 OBD_FREE(created, sizeof(*created) *
180                          iobuf->nr_pages*blocks_per_page);
181         case 0:
182                 if (cleanup_phase == 3)
183                         break;
184                 up(&inode->i_sem);
185                 break;
186         default:
187                 CERROR("corrupt cleanup_phase (%d)?\n", cleanup_phase);
188                 LBUG();
189                 break;
190         }
191         return rc;
192 }
193
194 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa, int objcount,
195                           struct obd_ioobj *obj, int niocount,
196                           struct niobuf_local *res, struct obd_trans_info *oti)
197 {
198         struct obd_device *obd = exp->exp_obd;
199         struct obd_run_ctxt saved;
200         struct niobuf_local *lnb;
201         struct fsfilt_objinfo fso;
202         struct iattr iattr = { 0 };
203         struct kiobuf *iobuf;
204         struct inode *inode = NULL;
205         int rc = 0, i, cleanup_phase = 0, err;
206         unsigned long now = jiffies; /* DEBUGGING OST TIMEOUTS */
207         void *wait_handle;
208         ENTRY;
209         LASSERT(oti != NULL);
210         LASSERT(objcount == 1);
211         LASSERT(current->journal_info == NULL);
212
213         rc = alloc_kiovec(1, &iobuf);
214         if (rc)
215                 GOTO(cleanup, rc);
216         cleanup_phase = 1;
217
218 #if (LINUX_VERSION_CODE == KERNEL_VERSION(2,4,18))
219         iobuf->dovary = 0; /* this prevents corruption, not present in 2.4.20 */
220 #endif
221         rc = expand_kiobuf(iobuf, obj->ioo_bufcnt);
222         if (rc)
223                 GOTO(cleanup, rc);
224
225         iobuf->offset = 0;
226         iobuf->length = PAGE_SIZE * obj->ioo_bufcnt;
227         iobuf->nr_pages = obj->ioo_bufcnt;
228
229         cleanup_phase = 1;
230         fso.fso_dentry = res->dentry;
231         fso.fso_bufcnt = obj->ioo_bufcnt;
232         inode = res->dentry->d_inode;
233
234         iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
235         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
236                 loff_t this_size;
237                 iobuf->maplist[i] = lnb->page;
238                 /* We expect these pages to be in offset order, but we'll
239                  * be forgiving */
240                 this_size = lnb->offset + lnb->len;
241                 if (this_size > iattr.ia_size)
242                         iattr.ia_size = this_size;
243         }
244
245         push_ctxt(&saved, &obd->obd_ctxt, NULL);
246         cleanup_phase = 2;
247
248         down(&inode->i_sem);
249         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, oti);
250         if (IS_ERR(oti->oti_handle)) {
251                 rc = PTR_ERR(oti->oti_handle);
252                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
253                        "error starting transaction: rc = %d\n", rc);
254                 oti->oti_handle = NULL;
255                 GOTO(cleanup, rc);
256         }
257
258         if (time_after(jiffies, now + 15 * HZ))
259                 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
260
261         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
262                               oti, &wait_handle);
263         if (rc == 0)
264                 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
265
266         if (time_after(jiffies, now + 15 * HZ))
267                 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
268
269         err = fsfilt_commit_wait(obd, inode, wait_handle);
270         if (err)
271                 rc = err;
272         if (obd_sync_filter)
273                 LASSERT(oti->oti_transno <= obd->obd_last_committed);
274         if (time_after(jiffies, now + 15 * HZ))
275                 CERROR("slow commitrw commit %lus\n", (jiffies - now) / HZ);
276
277 cleanup:
278         switch (cleanup_phase) {
279         case 2:
280                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
281                 LASSERT(current->journal_info == NULL);
282         case 1:
283                 free_kiovec(1, &iobuf);
284         case 0:
285                 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
286                         /* flip_.. gets a ref, while free_page only frees
287                          * when it decrefs to 0 */
288                         if (rc == 0)
289                                 flip_into_page_cache(inode, lnb->page);
290                         __free_page(lnb->page);
291                 }
292                 f_dput(res->dentry);
293         }
294
295         RETURN(rc);
296 }
297
298 #endif
299