Whamcloud - gitweb
b=3031
[fs/lustre-release.git] / lustre / obdfilter / filter_io_26.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 #include <linux/buffer_head.h>
32
33 #define DEBUG_SUBSYSTEM S_FILTER
34
35 #include <linux/obd_class.h>
36 #include <linux/lustre_fsfilt.h>
37 #include "filter_internal.h"
38
39 #warning "implement writeback mode -bzzz"
40
41 /* 512byte block min */
42 #define MAX_BLOCKS_PER_PAGE (PAGE_SIZE / 512)
43 struct dio_request {
44         atomic_t          dr_numreqs;  /* number of reqs being processed */
45         struct bio       *dr_bios;     /* list of completed bios */
46         wait_queue_head_t dr_wait;
47         int               dr_max_pages;
48         int               dr_npages;
49         int               dr_error;
50         struct page     **dr_pages;
51         unsigned long    *dr_blocks;
52         spinlock_t        dr_lock;
53 };
54
55 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
56 {
57         struct dio_request *dreq = bio->bi_private;
58         unsigned long flags;
59
60         if (bio->bi_size) {
61                 CWARN("gets called against non-complete bio 0x%p: %d/%d/%d\n",
62                       bio, bio->bi_size, done, error);
63                 return 1;
64         }
65
66         if (dreq == NULL) {
67                 CERROR("***** bio->bi_private is NULL!  This should never "
68                        "happen.  Normally, I would crash here, but instead I "
69                        "will dump the bio contents to the console.  Please "
70                        "report this to CFS, along with any interesting messages "
71                        "leading up to this point (like SCSI errors, perhaps).  "
72                        "Because bi_private is NULL, I can't wake up the thread "
73                        "that initiated this I/O -- so you will probably have to "
74                        "reboot this node.");
75                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
76                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
77                        "bi_private: %p\n", bio->bi_next, bio->bi_flags,
78                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
79                        bio->bi_end_io, atomic_read(&bio->bi_cnt),
80                        bio->bi_private);
81                 return 0;
82         }
83
84         spin_lock_irqsave(&dreq->dr_lock, flags);
85         bio->bi_private = dreq->dr_bios;
86         dreq->dr_bios = bio;
87         if (dreq->dr_error == 0)
88                 dreq->dr_error = error;
89         spin_unlock_irqrestore(&dreq->dr_lock, flags);
90
91         if (atomic_dec_and_test(&dreq->dr_numreqs))
92                 wake_up(&dreq->dr_wait);
93
94         return 0;
95 }
96
97 static int can_be_merged(struct bio *bio, sector_t sector)
98 {
99         unsigned int size;
100         if (!bio)
101                 return 0;
102
103         size = bio->bi_size >> 9;
104         return bio->bi_sector + size == sector ? 1 : 0;
105 }
106
107
108 int filter_alloc_iobuf(int rw, int num_pages, void **ret)
109 {
110         struct dio_request *dreq;
111
112         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
113
114         OBD_ALLOC(dreq, sizeof(*dreq));
115         if (dreq == NULL)
116                 goto failed_0;
117         
118         OBD_ALLOC(dreq->dr_pages, num_pages * sizeof(*dreq->dr_pages));
119         if (dreq->dr_pages == NULL)
120                 goto failed_1;
121         
122         OBD_ALLOC(dreq->dr_blocks,
123                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*dreq->dr_blocks));
124         if (dreq->dr_blocks == NULL)
125                 goto failed_2;
126
127         dreq->dr_bios = NULL;
128         init_waitqueue_head(&dreq->dr_wait);
129         atomic_set(&dreq->dr_numreqs, 0);
130         spin_lock_init(&dreq->dr_lock);
131         dreq->dr_max_pages = num_pages;
132         dreq->dr_npages = 0;
133
134         *ret = dreq;
135         RETURN(0);
136         
137  failed_2:
138         OBD_FREE(dreq->dr_pages,
139                  num_pages * sizeof(*dreq->dr_pages));
140  failed_1:
141         OBD_FREE(dreq, sizeof(*dreq));
142  failed_0:
143         RETURN(-ENOMEM);
144 }
145
146 void filter_free_iobuf(void *iobuf)
147 {
148         struct dio_request *dreq = iobuf;
149         int                 num_pages = dreq->dr_max_pages;
150
151         /* free all bios */
152         while (dreq->dr_bios) {
153                 struct bio *bio = dreq->dr_bios;
154                 dreq->dr_bios = bio->bi_private;
155                 bio_put(bio);
156         }
157
158         OBD_FREE(dreq->dr_blocks,
159                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*dreq->dr_blocks));
160         OBD_FREE(dreq->dr_pages,
161                  num_pages * sizeof(*dreq->dr_pages));
162         OBD_FREE(dreq, sizeof(*dreq));
163 }
164
165 int filter_iobuf_add_page(struct obd_device *obd, void *iobuf,
166                           struct inode *inode, struct page *page)
167 {
168         struct dio_request *dreq = iobuf;
169
170         LASSERT (dreq->dr_npages < dreq->dr_max_pages);
171         dreq->dr_pages[dreq->dr_npages++] = page;
172
173         return 0;
174 }
175
176 int filter_do_bio(struct obd_device *obd, struct inode *inode,
177                   struct dio_request *dreq, int rw)
178 {
179         int            blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
180         struct page  **pages = dreq->dr_pages;
181         int            npages = dreq->dr_npages;
182         unsigned long *blocks = dreq->dr_blocks;
183         int            total_blocks = npages * blocks_per_page;
184         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
185         unsigned int   blocksize = inode->i_sb->s_blocksize;
186         struct bio    *bio = NULL;
187         struct page   *page;
188         unsigned int   page_offset;
189         sector_t       sector;
190         int            nblocks;
191         int            block_idx;
192         int            page_idx;
193         int            i;
194         int            rc = 0;
195         ENTRY;
196
197         LASSERT(dreq->dr_npages == npages);
198         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
199
200         for (page_idx = 0, block_idx = 0; 
201              page_idx < npages; 
202              page_idx++, block_idx += blocks_per_page) {
203                         
204                 page = pages[page_idx];
205                 LASSERT (block_idx + blocks_per_page <= total_blocks);
206
207                 for (i = 0, page_offset = 0; 
208                      i < blocks_per_page;
209                      i += nblocks, page_offset += blocksize * nblocks) {
210
211                         nblocks = 1;
212
213                         if (blocks[block_idx + i] == 0) {  /* hole */
214                                 LASSERT(rw == OBD_BRW_READ);
215                                 memset(kmap(page) + page_offset, 0, blocksize);
216                                 kunmap(page);
217                                 continue;
218                         }
219
220                         sector = blocks[block_idx + i] << sector_bits;
221
222                         /* Additional contiguous file blocks? */
223                         while (i + nblocks < blocks_per_page &&
224                                (sector + nblocks*(blocksize>>9)) ==
225                                (blocks[block_idx + i + nblocks] << sector_bits))
226                                 nblocks++;
227
228                         if (bio != NULL &&
229                             can_be_merged(bio, sector) &&
230                             bio_add_page(bio, page, 
231                                          blocksize * nblocks, page_offset) != 0)
232                                 continue;       /* added this frag OK */
233
234                         if (bio != NULL) {
235                                 request_queue_t *q = bdev_get_queue(bio->bi_bdev);
236
237                                 /* Dang! I have to fragment this I/O */
238                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
239                                        "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
240                                        bio->bi_size, 
241                                        bio->bi_vcnt, bio->bi_max_vecs,
242                                        bio->bi_size >> 9, q->max_sectors,
243                                        bio_phys_segments(q, bio), 
244                                        q->max_phys_segments,
245                                        bio_hw_segments(q, bio), 
246                                        q->max_hw_segments);
247
248                                 atomic_inc(&dreq->dr_numreqs);
249                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
250                                 if (rc < 0) {
251                                         CERROR("Can't send bio: %d\n", rc);
252                                         /* OK do dec; we do the waiting */
253                                         atomic_dec(&dreq->dr_numreqs);
254                                         goto out;
255                                 }
256                                 rc = 0;
257                                         
258                                 bio = NULL;
259                         }
260
261                         /* allocate new bio */
262                         bio = bio_alloc(GFP_NOIO, 
263                                         (npages - page_idx) * blocks_per_page);
264                         if (bio == NULL) {
265                                 CERROR ("Can't allocate bio\n");
266                                 rc = -ENOMEM;
267                                 goto out;
268                         }
269
270                         bio->bi_bdev = inode->i_sb->s_bdev;
271                         bio->bi_sector = sector;
272                         bio->bi_end_io = dio_complete_routine;
273                         bio->bi_private = dreq;
274
275                         rc = bio_add_page(bio, page, 
276                                           blocksize * nblocks, page_offset);
277                         LASSERT (rc != 0);
278                 }
279         }
280
281         if (bio != NULL) {
282                 atomic_inc(&dreq->dr_numreqs);
283                 rc = fsfilt_send_bio(rw, obd, inode, bio);
284                 if (rc >= 0) {
285                         rc = 0;
286                 } else {
287                         CERROR("Can't send bio: %d\n", rc);
288                         /* OK do dec; we do the waiting */
289                         atomic_dec(&dreq->dr_numreqs);
290                 }
291         }
292                         
293  out:
294         wait_event(dreq->dr_wait, atomic_read(&dreq->dr_numreqs) == 0);
295
296         if (rc == 0)
297                 rc = dreq->dr_error;
298         RETURN(rc);
299 }
300
301 static void filter_clear_page_cache(struct inode *inode, 
302                                     struct dio_request *iobuf)
303 {
304         struct page *page;
305         int i;
306
307         for (i = 0; i < iobuf->dr_npages ; i++) {
308                 page = find_lock_page(inode->i_mapping,
309                                       iobuf->dr_pages[i]->index);
310                 if (page == NULL)
311                         continue;
312                 if (page->mapping != NULL) {
313                         wait_on_page_writeback(page);
314                         block_invalidatepage(page, 0);
315                         ll_truncate_complete_page(page);
316                 }
317                 unlock_page(page);
318                 page_cache_release(page);
319         }
320 }
321
322 /* Must be called with i_sem taken for writes; this will drop it */
323 int filter_direct_io(int rw, struct dentry *dchild, void *iobuf,
324                      struct obd_export *exp, struct iattr *attr,
325                      struct obd_trans_info *oti, void **wait_handle)
326 {
327         struct obd_device *obd = exp->exp_obd;
328         struct inode *inode = dchild->d_inode;
329         struct dio_request *dreq = iobuf;
330         int rc, rc2;
331         ENTRY;
332
333         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
334         LASSERTF(dreq->dr_npages <= dreq->dr_max_pages, "%d,%d\n",
335                  dreq->dr_npages, dreq->dr_max_pages);
336
337         if (dreq->dr_npages == 0)
338                 RETURN(0);
339
340         if (dreq->dr_npages > OBDFILTER_CREATED_SCRATCHPAD_ENTRIES)
341                 RETURN(-EINVAL);
342         
343         rc = fsfilt_map_inode_pages(obd, inode,
344                                     dreq->dr_pages, dreq->dr_npages,
345                                     dreq->dr_blocks,
346                                     obdfilter_created_scratchpad,
347                                     rw == OBD_BRW_WRITE, NULL);
348
349         if (rw == OBD_BRW_WRITE) {
350                 if (rc == 0) {
351 #if 0
352                         filter_tally_write(&obd->u.filter, 
353                                            dreq->dr_pages,
354                                            dreq->dr_page_idx,
355                                            dreq->dr_blocks,
356                                            blocks_per_page);
357 #endif
358                         if (attr->ia_size > inode->i_size)
359                                 attr->ia_valid |= ATTR_SIZE;
360                         rc = fsfilt_setattr(obd, dchild, 
361                                             oti->oti_handle, attr, 0);
362                 }
363                 
364                 up(&inode->i_sem);
365
366                 rc2 = filter_finish_transno(exp, oti, 0);
367                 if (rc2 != 0)
368                         CERROR("can't close transaction: %d\n", rc);
369
370                 if (rc == 0)
371                         rc = rc2;
372                 if (rc != 0)
373                         RETURN(rc);
374         }
375
376         /* This is nearly osync_inode, without the waiting
377         rc = generic_osync_inode(inode, inode->i_mapping,
378                                  OSYNC_DATA|OSYNC_METADATA); */
379         rc = filemap_fdatawrite(inode->i_mapping);
380         rc2 = sync_mapping_buffers(inode->i_mapping);
381         if (rc == 0)
382                 rc = rc2;
383         rc2 = filemap_fdatawait(inode->i_mapping);
384         if (rc == 0)
385                 rc = rc2;
386
387         if (rc != 0)
388                 RETURN(rc);
389
390         /* be careful to call this after fsync_inode_data_buffers has waited
391          * for IO to complete before we evict it from the cache */
392         filter_clear_page_cache(inode, dreq);
393
394         RETURN(filter_do_bio(obd, inode, dreq, rw));
395 }
396
397 /* See if there are unallocated parts in given file region */
398 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
399 {
400         sector_t (*fs_bmap)(struct address_space *, sector_t) =
401                 inode->i_mapping->a_ops->bmap;
402         int j;
403
404         /* We can't know if we are overwriting or not */
405         if (fs_bmap == NULL)
406                 return 0;
407
408         offset >>= inode->i_blkbits;
409         len >>= inode->i_blkbits;
410
411         for (j = 0; j <= len; j++)
412                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
413                         return 0;
414
415         return 1;
416 }
417
418 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
419                           int objcount, struct obd_ioobj *obj, int niocount,
420                           struct niobuf_local *res, struct obd_trans_info *oti,
421                           int rc)
422 {
423         struct niobuf_local *lnb;
424         struct dio_request *dreq = NULL;
425         struct lvfs_run_ctxt saved;
426         struct fsfilt_objinfo fso;
427         struct iattr iattr = { 0 };
428         struct inode *inode = NULL;
429         unsigned long now = jiffies;
430         int i, err, cleanup_phase = 0;
431         struct obd_device *obd = exp->exp_obd;
432         int   total_size = 0;
433         ENTRY;
434
435         LASSERT(oti != NULL);
436         LASSERT(objcount == 1);
437         LASSERT(current->journal_info == NULL);
438
439         if (rc != 0)
440                 GOTO(cleanup, rc);
441         
442         rc = filter_alloc_iobuf(OBD_BRW_WRITE, obj->ioo_bufcnt, (void **)&dreq);
443         if (rc)
444                 GOTO(cleanup, rc);
445         cleanup_phase = 1;
446
447         fso.fso_dentry = res->dentry;
448         fso.fso_bufcnt = obj->ioo_bufcnt;
449         inode = res->dentry->d_inode;
450
451         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
452                 loff_t this_size;
453
454                 /* If overwriting an existing block, we don't need a grant */
455                 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
456                     filter_range_is_mapped(inode, lnb->offset, lnb->len))
457                         lnb->rc = 0;
458
459                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
460                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
461                         continue;
462                 }
463
464                 err = filter_iobuf_add_page(obd, dreq, inode, lnb->page);
465                 LASSERT (err == 0);
466
467                 total_size += lnb->len;
468
469                 /* we expect these pages to be in offset order, but we'll
470                  * be forgiving */
471                 this_size = lnb->offset + lnb->len;
472                 if (this_size > iattr.ia_size)
473                         iattr.ia_size = this_size;
474         }
475 #if 0
476         /* I use this when I'm checking our lovely 1M I/Os reach the disk -eeb */
477         if (total_size != (1<<20))
478                 CWARN("total size %d (%d pages)\n", 
479                       total_size, total_size/PAGE_SIZE);
480 #endif
481         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
482         cleanup_phase = 2;
483
484         down(&inode->i_sem);
485         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
486                                            oti);
487         if (IS_ERR(oti->oti_handle)) {
488                 up(&inode->i_sem);
489                 rc = PTR_ERR(oti->oti_handle);
490                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
491                        "error starting transaction: rc = %d\n", rc);
492                 oti->oti_handle = NULL;
493                 GOTO(cleanup, rc);
494         }
495         /* have to call fsfilt_commit() from this point on */
496
497         fsfilt_check_slow(now, obd_timeout, "brw_start");
498
499         iattr_from_obdo(&iattr,oa,OBD_MD_FLATIME|OBD_MD_FLMTIME|OBD_MD_FLCTIME);
500         /* filter_direct_io drops i_sem */
501         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, dreq, exp, &iattr,
502                               oti, NULL);
503         if (rc == 0)
504                 obdo_from_inode(oa, inode, FILTER_VALID_FLAGS);
505
506         fsfilt_check_slow(now, obd_timeout, "direct_io");
507
508         err = fsfilt_commit(obd, obd->u.filter.fo_sb, inode, oti->oti_handle,
509                             obd_sync_filter);
510         if (err)
511                 rc = err;
512
513         if (obd_sync_filter && !err)
514                 LASSERT(oti->oti_transno <= obd->obd_last_committed);
515
516         fsfilt_check_slow(now, obd_timeout, "commitrw commit");
517
518 cleanup:
519         filter_grant_commit(exp, niocount, res);
520
521         switch (cleanup_phase) {
522         case 2:
523                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
524                 LASSERT(current->journal_info == NULL);
525         case 1:
526                 filter_free_iobuf(dreq);
527         case 0:
528                 filter_free_dio_pages(objcount, obj, niocount, res);
529                 f_dput(res->dentry);
530         }
531
532         RETURN(rc);
533 }