Whamcloud - gitweb
landing smfs.
[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 = fsfilt_send_bio(obd, inode, iobuf);
159
160         CDEBUG(D_INFO, "tried to write %d pages, rc = %d\n",
161                iobuf->nr_pages, rc);
162
163         if (rc > 0)
164                 rc = 0;
165
166         EXIT;
167 cleanup:
168         if (!committed) {
169                 int err = fsfilt_commit_async(obd, inode,
170                                               oti->oti_handle, wait_handle);
171                 oti->oti_handle = NULL;
172                 if (err)
173                         CERROR("can't close transaction: %d\n", err);
174                 /*
175                  * this is error path, so we prefer to return
176                  * original error, not this one
177                  */
178         }
179
180         switch(cleanup_phase) {
181         case 3:
182         case 2:
183                 unlock_kiovec(1, &iobuf);
184         case 1:
185                 OBD_FREE(created, sizeof(*created) *
186                          iobuf->nr_pages*blocks_per_page);
187         case 0:
188                 if (cleanup_phase == 3)
189                         break;
190                 up(&inode->i_sem);
191                 break;
192         default:
193                 CERROR("corrupt cleanup_phase (%d)?\n", cleanup_phase);
194                 LBUG();
195                 break;
196         }
197         return rc;
198 }
199
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)
202 {
203         int (*fs_bmap)(struct address_space *, long) =
204                 inode->i_mapping->a_ops->bmap;
205         int j;
206
207         /* We can't know if the range is mapped already or not */
208         if (fs_bmap == NULL)
209                 return 0;
210
211         offset >>= inode->i_blkbits;
212         len >>= inode->i_blkbits;
213
214         for (j = 0; j <= len; j++)
215                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
216                         return 0;
217
218         return 1;
219 }
220
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,
224                           int rc)
225 {
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 */
235         void *wait_handle;
236         ENTRY;
237         LASSERT(oti != NULL);
238         LASSERT(objcount == 1);
239         LASSERT(current->journal_info == NULL);
240
241         if (rc != 0)
242                 GOTO(cleanup, rc);
243
244         rc = alloc_kiovec(1, &iobuf);
245         if (rc)
246                 GOTO(cleanup, rc);
247         cleanup_phase = 1;
248
249 #ifdef HAVE_KIOBUF_DOVARY
250         iobuf->dovary = 0; /* this prevents corruption, not present in 2.4.20 */
251 #endif
252         rc = expand_kiobuf(iobuf, obj->ioo_bufcnt);
253         if (rc)
254                 GOTO(cleanup, rc);
255
256         iobuf->offset = 0;
257         iobuf->length = 0;
258         iobuf->nr_pages = 0;
259
260         cleanup_phase = 1;
261         fso.fso_dentry = res->dentry;
262         fso.fso_bufcnt = obj->ioo_bufcnt;
263         inode = res->dentry->d_inode;
264
265         for (i = 0, lnb = res, n = 0; i < obj->ioo_bufcnt; i++, lnb++) {
266                 loff_t this_size;
267
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))
271                         lnb->rc = 0;
272
273                 if (lnb->rc) /* ENOSPC, network RPC error */
274                         continue;
275
276                 iobuf->maplist[n++] = lnb->page;
277                 iobuf->length += PAGE_SIZE;
278                 iobuf->nr_pages++;
279
280                 /* We expect these pages to be in offset order, but we'll
281                  * be forgiving */
282                 this_size = lnb->offset + lnb->len;
283                 if (this_size > iattr.ia_size)
284                         iattr.ia_size = this_size;
285         }
286
287         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
288         cleanup_phase = 2;
289
290         down(&inode->i_sem);
291         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
292                                            oti);
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;
298                 GOTO(cleanup, rc);
299         }
300
301         if (time_after(jiffies, now + 15 * HZ))
302                 CERROR("slow brw_start %lus\n", (jiffies - now) / HZ);
303
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,
306                               oti, &wait_handle);
307         if (rc == 0)
308                 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
309
310         if (time_after(jiffies, now + 15 * HZ))
311                 CERROR("slow direct_io %lus\n", (jiffies - now) / HZ);
312
313         err = fsfilt_commit_wait(obd, inode, wait_handle);
314         if (err)
315                 rc = err;
316         if (obd_sync_filter)
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);
320
321 cleanup:
322         filter_grant_commit(exp, niocount, res);
323
324         switch (cleanup_phase) {
325         case 2:
326                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
327                 LASSERT(current->journal_info == NULL);
328         case 1:
329                 free_kiovec(1, &iobuf);
330         case 0:
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 */
334                         if (rc == 0)
335                                 flip_into_page_cache(inode, lnb->page);
336                         __free_page(lnb->page);
337                 }
338                 f_dput(res->dentry);
339         }
340
341         RETURN(rc);
342 }