Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 #define DEBUG_SUBSYSTEM S_FILTER
33
34 #include <linux/iobuf.h>
35 #include <linux/locks.h>
36
37 #include <linux/obd_class.h>
38 #include <linux/lustre_fsfilt.h>
39 #include "filter_internal.h"
40
41
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)
45 {
46         time_t now = CURRENT_TIME;
47         if (inode->i_mtime == now && (!ctime_too || inode->i_ctime == now))
48                 return;
49         inode->i_mtime = now;
50         if (ctime_too)
51                 inode->i_ctime = now;
52         mark_inode_dirty_sync(inode);
53 }
54
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,
58                               int size)
59 {
60 #if (LUSTRE_KERNEL_VERSION < 32)
61         struct buffer_head *bh;
62         int i;
63
64         for (i = 0; i < nr_pages; i++) {
65                 bh = get_hash_table(dev, blocks[i], size);
66                 if (bh == NULL)
67                         continue;
68                 if (!buffer_dirty(bh)) {
69                         put_bh(bh);
70                         continue;
71                 }
72                 mark_buffer_clean(bh);
73                 wait_on_buffer(bh);
74                 clear_bit(BH_Req, &bh->b_state);
75                 __brelse(bh);
76         }
77 #endif
78 }
79
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)
84 {
85         struct obd_device *obd = exp->exp_obd;
86         struct inode *inode = dchild->d_inode;
87         struct page *page;
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;
91         int committed = 0;
92         ENTRY;
93
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);
97
98         OBD_ALLOC(created, sizeof(*created) * iobuf->nr_pages*blocks_per_page);
99         if (created == NULL)
100                 GOTO(cleanup, rc = -ENOMEM);
101         cleanup_phase = 1;
102
103         rc = lock_kiovec(1, &iobuf, 1);
104         if (rc < 0)
105                 GOTO(cleanup, rc);
106         cleanup_phase = 2;
107
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];
111
112                 rc = fsfilt_map_inode_page(obd, inode, page, b, cr, create);
113                 if (rc) {
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);
117                         GOTO(cleanup, rc);
118                 }
119
120                 b += blocks_per_page;
121                 cr += blocks_per_page;
122         }
123         up(&exp->exp_obd->u.filter.fo_alloc_lock);
124
125         filter_tally_write(&obd->u.filter, iobuf->maplist, iobuf->nr_pages,
126                            iobuf->blocks, blocks_per_page);
127
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);
131         if (rc)
132                 GOTO(cleanup, rc);
133
134         up(&inode->i_sem);
135         cleanup_phase = 3;
136
137         rc = filter_finish_transno(exp, oti, 0);
138         if (rc)
139                 GOTO(cleanup, rc);
140
141         rc = fsfilt_commit_async(obd, inode, oti->oti_handle, wait_handle);
142         oti->oti_handle = NULL;
143         committed = 1;
144         if (rc)
145                 GOTO(cleanup, rc);
146
147         check_pending_bhs(iobuf->blocks, iobuf->nr_pages, inode->i_dev,
148                           1 << inode->i_blkbits);
149
150         rc = filemap_fdatasync(inode->i_mapping);
151         if (rc == 0)
152                 rc = fsync_inode_data_buffers(inode);
153         if (rc == 0)
154                 rc = filemap_fdatawait(inode->i_mapping);
155         if (rc < 0)
156                 GOTO(cleanup, rc);
157
158         rc = brw_kiovec(WRITE, 1, &iobuf, inode->i_dev, iobuf->blocks,
159                         1 << inode->i_blkbits);
160         CDEBUG(D_INFO, "tried to write %d pages, rc = %d\n",
161                iobuf->nr_pages, rc);
162         if (rc != (1 << inode->i_blkbits) * iobuf->nr_pages * blocks_per_page)
163                 CERROR("short write?  expected %d, wrote %d\n",
164                        (1 << inode->i_blkbits) * iobuf->nr_pages *
165                        blocks_per_page, rc);
166         if (rc > 0)
167                 rc = 0;
168
169         EXIT;
170 cleanup:
171         if (!committed) {
172                 int err = fsfilt_commit_async(obd, inode,
173                                               oti->oti_handle, wait_handle);
174                 oti->oti_handle = NULL;
175                 if (err)
176                         CERROR("can't close transaction: %d\n", err);
177                 /*
178                  * this is error path, so we prefer to return
179                  * original error, not this one
180                  */
181         }
182
183         switch(cleanup_phase) {
184         case 3:
185         case 2:
186                 unlock_kiovec(1, &iobuf);
187         case 1:
188                 OBD_FREE(created, sizeof(*created) *
189                          iobuf->nr_pages*blocks_per_page);
190         case 0:
191                 if (cleanup_phase == 3)
192                         break;
193                 up(&inode->i_sem);
194                 break;
195         default:
196                 CERROR("corrupt cleanup_phase (%d)?\n", cleanup_phase);
197                 LBUG();
198                 break;
199         }
200         return rc;
201 }
202
203 /* See if there are unallocated parts in given file region */
204 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
205 {
206         int (*fs_bmap)(struct address_space *, long) =
207                 inode->i_mapping->a_ops->bmap;
208         int j;
209
210         /* We can't know if the range is mapped already or not */
211         if (fs_bmap == NULL)
212                 return 0;
213
214         offset >>= inode->i_blkbits;
215         len >>= inode->i_blkbits;
216
217         for (j = 0; j <= len; j++)
218                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
219                         return 0;
220
221         return 1;
222 }
223
224 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa, int objcount,
225                           struct obd_ioobj *obj, int niocount,
226                           struct niobuf_local *res, struct obd_trans_info *oti,
227                           int rc)
228 {
229         struct obd_device *obd = exp->exp_obd;
230         struct obd_run_ctxt saved;
231         struct niobuf_local *lnb;
232         struct fsfilt_objinfo fso;
233         struct iattr iattr = { 0 };
234         struct kiobuf *iobuf;
235         struct inode *inode = NULL;
236         int i, n, cleanup_phase = 0, err;
237         unsigned long now = jiffies; /* DEBUGGING OST TIMEOUTS */
238         void *wait_handle;
239         ENTRY;
240         LASSERT(oti != NULL);
241         LASSERT(objcount == 1);
242         LASSERT(current->journal_info == NULL);
243
244         if (rc != 0)
245                 GOTO(cleanup, rc);
246
247         rc = alloc_kiovec(1, &iobuf);
248         if (rc)
249                 GOTO(cleanup, rc);
250         cleanup_phase = 1;
251
252 #ifdef HAVE_KIOBUF_DOVARY
253         iobuf->dovary = 0; /* this prevents corruption, not present in 2.4.20 */
254 #endif
255         rc = expand_kiobuf(iobuf, obj->ioo_bufcnt);
256         if (rc)
257                 GOTO(cleanup, rc);
258
259         iobuf->offset = 0;
260         iobuf->length = 0;
261         iobuf->nr_pages = 0;
262
263         cleanup_phase = 1;
264         fso.fso_dentry = res->dentry;
265         fso.fso_bufcnt = obj->ioo_bufcnt;
266         inode = res->dentry->d_inode;
267
268         for (i = 0, lnb = res, n = 0; i < obj->ioo_bufcnt; i++, lnb++) {
269                 loff_t this_size;
270
271                 /* If overwriting an existing block, we don't need a grant */
272                 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
273                     filter_range_is_mapped(inode, lnb->offset, lnb->len))
274                         lnb->rc = 0;
275
276                 if (lnb->rc) /* ENOSPC, network RPC error */
277                         continue;
278
279                 iobuf->maplist[n++] = lnb->page;
280                 iobuf->length += PAGE_SIZE;
281                 iobuf->nr_pages++;
282
283                 /* We expect these pages to be in offset order, but we'll
284                  * be forgiving */
285                 this_size = lnb->offset + lnb->len;
286                 if (this_size > iattr.ia_size)
287                         iattr.ia_size = this_size;
288         }
289
290         push_ctxt(&saved, &obd->obd_ctxt, NULL);
291         cleanup_phase = 2;
292
293         down(&inode->i_sem);
294         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
295                                            oti);
296         if (IS_ERR(oti->oti_handle)) {
297                 rc = PTR_ERR(oti->oti_handle);
298                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
299                        "error starting transaction: rc = %d\n", rc);
300                 oti->oti_handle = NULL;
301                 GOTO(cleanup, rc);
302         }
303
304         if (time_after(jiffies, now + 15 * HZ))
305                 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
306
307         iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
308         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
309                               oti, &wait_handle);
310         if (rc == 0)
311                 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
312
313         if (time_after(jiffies, now + 15 * HZ))
314                 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
315
316         err = fsfilt_commit_wait(obd, inode, wait_handle);
317         if (err)
318                 rc = err;
319         if (obd_sync_filter)
320                 LASSERT(oti->oti_transno <= obd->obd_last_committed);
321         if (time_after(jiffies, now + 15 * HZ))
322                 CERROR("slow commitrw commit %lus\n", (jiffies - now) / HZ);
323
324 cleanup:
325         filter_grant_commit(exp, niocount, res);
326
327         switch (cleanup_phase) {
328         case 2:
329                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
330                 LASSERT(current->journal_info == NULL);
331         case 1:
332                 free_kiovec(1, &iobuf);
333         case 0:
334                 for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
335                         /* flip_.. gets a ref, while free_page only frees
336                          * when it decrefs to 0 */
337                         if (rc == 0)
338                                 flip_into_page_cache(inode, lnb->page);
339                         __free_page(lnb->page);
340                 }
341                 f_dput(res->dentry);
342         }
343
344         RETURN(rc);
345 }