Whamcloud - gitweb
c06c60b8b9a2bb32043db9e8cea6685e0251a5c4
[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 the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #ifndef AUTOCONF_INCLUDED
31 #include <linux/config.h>
32 #endif
33 #include <linux/module.h>
34 #include <linux/pagemap.h> // XXX kill me soon
35 #include <linux/version.h>
36 #include <linux/buffer_head.h>
37
38 #define DEBUG_SUBSYSTEM S_FILTER
39
40 #include <obd_class.h>
41 #include <lustre_fsfilt.h>
42 #include <lustre_quota.h>
43 #include "filter_internal.h"
44
45 /* 512byte block min */
46 #define MAX_BLOCKS_PER_PAGE (CFS_PAGE_SIZE / 512)
47 struct filter_iobuf {
48         atomic_t          dr_numreqs;  /* number of reqs being processed */
49         wait_queue_head_t dr_wait;
50         int               dr_max_pages;
51         int               dr_npages;
52         int               dr_error;
53         struct page     **dr_pages;
54         unsigned long    *dr_blocks;
55         spinlock_t        dr_lock;              /* IRQ lock */
56         unsigned int      dr_ignore_quota:1;
57         struct filter_obd *dr_filter;
58 };
59
60 static void record_start_io(struct filter_iobuf *iobuf, int rw, int size,
61                             struct obd_export *exp)
62 {
63         struct filter_obd *filter = iobuf->dr_filter;
64
65         atomic_inc(&iobuf->dr_numreqs);
66
67         if (rw == OBD_BRW_READ) {
68                 atomic_inc(&filter->fo_r_in_flight);
69                 lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_R_RPC_HIST],
70                                  atomic_read(&filter->fo_r_in_flight));
71                 lprocfs_oh_tally_log2(&filter->fo_filter_stats.hist[BRW_R_DISK_IOSIZE],
72                                       size);
73                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_RPC_HIST],
74                                  atomic_read(&filter->fo_r_in_flight));
75                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_DISK_IOSIZE], size);
76         } else {
77                 atomic_inc(&filter->fo_w_in_flight);
78                 lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_W_RPC_HIST],
79                                  atomic_read(&filter->fo_w_in_flight));
80                 lprocfs_oh_tally_log2(&filter->fo_filter_stats.hist[BRW_W_DISK_IOSIZE],
81                                       size);
82                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_RPC_HIST],
83                                  atomic_read(&filter->fo_w_in_flight));
84                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_DISK_IOSIZE], size);
85         }
86 }
87
88 static void record_finish_io(struct filter_iobuf *iobuf, int rw, int rc)
89 {
90         struct filter_obd *filter = iobuf->dr_filter;
91
92         /* CAVEAT EMPTOR: possibly in IRQ context 
93          * DO NOT record procfs stats here!!! */
94
95         if (rw == OBD_BRW_READ)
96                 atomic_dec(&filter->fo_r_in_flight);
97         else
98                 atomic_dec(&filter->fo_w_in_flight);
99
100         if (atomic_dec_and_test(&iobuf->dr_numreqs))
101                 wake_up(&iobuf->dr_wait);
102 }
103
104 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
105 {
106         struct filter_iobuf *iobuf = bio->bi_private;
107         unsigned long        flags;
108
109 #ifdef HAVE_PAGE_CONSTANT
110         struct bio_vec *bvl;
111         int i;
112 #endif
113
114         /* CAVEAT EMPTOR: possibly in IRQ context 
115          * DO NOT record procfs stats here!!! */
116
117         if (bio->bi_size)                       /* Not complete */
118                 return 1;
119
120         if (iobuf == NULL) {
121                 CERROR("***** bio->bi_private is NULL!  This should never "
122                        "happen.  Normally, I would crash here, but instead I "
123                        "will dump the bio contents to the console.  Please "
124                        "report this to CFS, along with any interesting "
125                        "messages leading up to this point (like SCSI errors, "
126                        "perhaps).  Because bi_private is NULL, I can't wake up "
127                        "the thread that initiated this I/O -- so you will "
128                        "probably have to reboot this node.\n");
129                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
130                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
131                        "bi_private: %p\n", bio->bi_next, bio->bi_flags,
132                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
133                        bio->bi_end_io, atomic_read(&bio->bi_cnt),
134                        bio->bi_private);
135                 return 0;
136         }
137
138 #ifdef HAVE_PAGE_CONSTANT
139         bio_for_each_segment(bvl, bio, i)
140                 ClearPageConstant(bvl->bv_page);
141 #endif
142
143         spin_lock_irqsave(&iobuf->dr_lock, flags);
144         if (iobuf->dr_error == 0)
145                 iobuf->dr_error = error;
146         spin_unlock_irqrestore(&iobuf->dr_lock, flags);
147
148         record_finish_io(iobuf, test_bit(BIO_RW, &bio->bi_rw) ?
149                          OBD_BRW_WRITE : OBD_BRW_READ, error);
150
151         /* Completed bios used to be chained off iobuf->dr_bios and freed in
152          * filter_clear_dreq().  It was then possible to exhaust the biovec-256
153          * mempool when serious on-disk fragmentation was encountered,
154          * deadlocking the OST.  The bios are now released as soon as complete
155          * so the pool cannot be exhausted while IOs are competing. bug 10076 */
156         bio_put(bio);
157         return 0;
158 }
159
160 static int can_be_merged(struct bio *bio, sector_t sector)
161 {
162         unsigned int size;
163
164         if (!bio)
165                 return 0;
166
167         size = bio->bi_size >> 9;
168         return bio->bi_sector + size == sector ? 1 : 0;
169 }
170
171 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
172                                         int rw, int num_pages)
173 {
174         struct filter_iobuf *iobuf;
175
176         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
177
178         OBD_ALLOC(iobuf, sizeof(*iobuf));
179         if (iobuf == NULL)
180                 goto failed_0;
181
182         OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
183         if (iobuf->dr_pages == NULL)
184                 goto failed_1;
185
186         OBD_ALLOC(iobuf->dr_blocks,
187                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
188         if (iobuf->dr_blocks == NULL)
189                 goto failed_2;
190
191         iobuf->dr_filter = filter;
192         init_waitqueue_head(&iobuf->dr_wait);
193         atomic_set(&iobuf->dr_numreqs, 0);
194         spin_lock_init(&iobuf->dr_lock);
195         iobuf->dr_max_pages = num_pages;
196         iobuf->dr_npages = 0;
197         iobuf->dr_error = 0;
198
199         RETURN(iobuf);
200
201  failed_2:
202         OBD_FREE(iobuf->dr_pages,
203                  num_pages * sizeof(*iobuf->dr_pages));
204  failed_1:
205         OBD_FREE(iobuf, sizeof(*iobuf));
206  failed_0:
207         RETURN(ERR_PTR(-ENOMEM));
208 }
209
210 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
211 {
212         iobuf->dr_npages = 0;
213         iobuf->dr_error = 0;
214         atomic_set(&iobuf->dr_numreqs, 0);
215 }
216
217 void filter_free_iobuf(struct filter_iobuf *iobuf)
218 {
219         int num_pages = iobuf->dr_max_pages;
220
221         filter_clear_iobuf(iobuf);
222
223         OBD_FREE(iobuf->dr_blocks,
224                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
225         OBD_FREE(iobuf->dr_pages,
226                  num_pages * sizeof(*iobuf->dr_pages));
227         OBD_FREE_PTR(iobuf);
228 }
229
230 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
231                       struct obd_trans_info *oti)
232 {
233         int thread_id = oti ? oti->oti_thread_id : -1;
234
235         if (unlikely(thread_id < 0)) {
236                 filter_free_iobuf(iobuf);
237                 return;
238         }
239
240         LASSERTF(filter->fo_iobuf_pool[thread_id] == iobuf,
241                  "iobuf mismatch for thread %d: pool %p iobuf %p\n",
242                  thread_id, filter->fo_iobuf_pool[thread_id], iobuf);
243         filter_clear_iobuf(iobuf);
244 }
245
246 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
247                           struct inode *inode, struct page *page)
248 {
249         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
250         iobuf->dr_pages[iobuf->dr_npages++] = page;
251
252         return 0;
253 }
254
255 int filter_do_bio(struct obd_export *exp, struct inode *inode,
256                   struct filter_iobuf *iobuf, int rw)
257 {
258         struct obd_device *obd = exp->exp_obd;
259         int            blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
260         struct page  **pages = iobuf->dr_pages;
261         int            npages = iobuf->dr_npages;
262         unsigned long *blocks = iobuf->dr_blocks;
263         int            total_blocks = npages * blocks_per_page;
264         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
265         unsigned int   blocksize = inode->i_sb->s_blocksize;
266         struct bio    *bio = NULL;
267         int            frags = 0;
268         unsigned long  start_time = jiffies;
269         struct page   *page;
270         unsigned int   page_offset;
271         sector_t       sector;
272         int            nblocks;
273         int            block_idx;
274         int            page_idx;
275         int            i;
276         int            rc = 0;
277         ENTRY;
278
279         LASSERT(iobuf->dr_npages == npages);
280         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
281
282         for (page_idx = 0, block_idx = 0;
283              page_idx < npages;
284              page_idx++, block_idx += blocks_per_page) {
285
286                 page = pages[page_idx];
287                 LASSERT (block_idx + blocks_per_page <= total_blocks);
288
289                 for (i = 0, page_offset = 0;
290                      i < blocks_per_page;
291                      i += nblocks, page_offset += blocksize * nblocks) {
292
293                         nblocks = 1;
294
295                         if (blocks[block_idx + i] == 0) {  /* hole */
296                                 LASSERT(rw == OBD_BRW_READ);
297                                 memset(kmap(page) + page_offset, 0, blocksize);
298                                 kunmap(page);
299                                 continue;
300                         }
301
302                         sector = (sector_t)blocks[block_idx + i] << sector_bits;
303
304                         /* Additional contiguous file blocks? */
305                         while (i + nblocks < blocks_per_page &&
306                                (sector + (nblocks << sector_bits)) ==
307                                ((sector_t)blocks[block_idx + i + nblocks] <<
308                                 sector_bits))
309                                 nblocks++;
310
311 #ifdef HAVE_PAGE_CONSTANT
312                         /* I only set the page to be constant only if it 
313                          * is mapped to a contiguous underlying disk block(s). 
314                          * It will then make sure the corresponding device 
315                          * cache of raid5 will be overwritten by this page. 
316                          * - jay */
317                         if ((rw == OBD_BRW_WRITE) && 
318                             (nblocks == blocks_per_page) && 
319                             mapping_cap_page_constant_write(inode->i_mapping))
320                                SetPageConstant(page);
321 #endif
322
323                         if (bio != NULL &&
324                             can_be_merged(bio, sector) &&
325                             bio_add_page(bio, page,
326                                          blocksize * nblocks, page_offset) != 0)
327                                 continue;       /* added this frag OK */
328
329                         if (bio != NULL) {
330                                 request_queue_t *q =
331                                         bdev_get_queue(bio->bi_bdev);
332
333                                 /* Dang! I have to fragment this I/O */
334                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
335                                        "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
336                                        bio->bi_size,
337                                        bio->bi_vcnt, bio->bi_max_vecs,
338                                        bio->bi_size >> 9, q->max_sectors,
339                                        bio_phys_segments(q, bio),
340                                        q->max_phys_segments,
341                                        bio_hw_segments(q, bio),
342                                        q->max_hw_segments);
343
344                                 record_start_io(iobuf, rw, bio->bi_size, exp);
345                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
346                                 if (rc < 0) {
347                                         CERROR("Can't send bio: %d\n", rc);
348                                         record_finish_io(iobuf, rw, rc);
349                                         goto out;
350                                 }
351                                 frags++;
352                         }
353
354                         /* allocate new bio */
355                         bio = bio_alloc(GFP_NOIO,
356                                         (npages - page_idx) * blocks_per_page);
357                         if (bio == NULL) {
358                                 CERROR("Can't allocate bio %u*%u = %u pages\n",
359                                        (npages - page_idx), blocks_per_page,
360                                        (npages - page_idx) * blocks_per_page);
361                                 rc = -ENOMEM;
362                                 goto out;
363                         }
364
365                         bio->bi_bdev = inode->i_sb->s_bdev;
366                         bio->bi_sector = sector;
367                         bio->bi_end_io = dio_complete_routine;
368                         bio->bi_private = iobuf;
369
370                         rc = bio_add_page(bio, page,
371                                           blocksize * nblocks, page_offset);
372                         LASSERT (rc != 0);
373                 }
374         }
375
376         if (bio != NULL) {
377                 record_start_io(iobuf, rw, bio->bi_size, exp);
378                 rc = fsfilt_send_bio(rw, obd, inode, bio);
379                 if (rc >= 0) {
380                         frags++;
381                         rc = 0;
382                 } else {
383                         CERROR("Can't send bio: %d\n", rc);
384                         record_finish_io(iobuf, rw, rc);
385                 }
386         }
387
388  out:
389         wait_event(iobuf->dr_wait, atomic_read(&iobuf->dr_numreqs) == 0);
390
391         if (rw == OBD_BRW_READ) {
392                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.hist[BRW_R_DIO_FRAGS],
393                                  frags);
394                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_DIO_FRAGS],
395                                  frags);
396                 lprocfs_oh_tally_log2(&obd->u.filter.fo_filter_stats.hist[BRW_R_IO_TIME],
397                                       jiffies - start_time);
398                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_IO_TIME], jiffies - start_time);
399                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
400                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_R_DIO_FRAGS],
401                                          frags);
402                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_R_IO_TIME],
403                                               jiffies - start_time);
404                 }
405         } else {
406                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.hist[BRW_W_DIO_FRAGS],
407                                  frags);
408                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_DIO_FRAGS],
409                                  frags);
410                 lprocfs_oh_tally_log2(&obd->u.filter.fo_filter_stats.hist[BRW_W_IO_TIME],
411                                       jiffies - start_time);
412                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_IO_TIME], jiffies - start_time);
413                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
414                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_W_DIO_FRAGS],
415                                          frags);
416                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_W_IO_TIME],
417                                               jiffies - start_time);
418                 }
419         }
420
421         if (rc == 0)
422                 rc = iobuf->dr_error;
423         RETURN(rc);
424 }
425
426 /* These are our hacks to keep our directio/bh IO coherent with ext3's
427  * page cache use.  Most notably ext3 reads file data into the page
428  * cache when it is zeroing the tail of partial-block truncates and
429  * leaves it there, sometimes generating io from it at later truncates.
430  * This removes the partial page and its buffers from the page cache,
431  * so it should only ever cause a wait in rare cases, as otherwise we
432  * always do full-page IO to the OST.
433  *
434  * The call to truncate_complete_page() will call journal_invalidatepage()
435  * to free the buffers and drop the page from cache.  The buffers should
436  * not be dirty, because we already called fdatasync/fdatawait on them.
437  */
438 static int filter_sync_inode_data(struct inode *inode, int locked)
439 {
440         int rc = 0;
441
442         /* This is nearly do_fsync(), without the waiting on the inode */
443         /* XXX: in 2.6.16 (at least) we don't need to hold i_mutex over
444          * filemap_fdatawrite() and filemap_fdatawait(), so we may no longer
445          * need this lock here at all. */
446         if (!locked)
447                 LOCK_INODE_MUTEX(inode);
448         if (inode->i_mapping->nrpages) {
449 #ifdef PF_SYNCWRITE
450                 current->flags |= PF_SYNCWRITE;
451 #endif
452                 rc = filemap_fdatawrite(inode->i_mapping);
453                 if (rc == 0)
454                         rc = filemap_fdatawait(inode->i_mapping);
455 #ifdef PF_SYNCWRITE
456                 current->flags &= ~PF_SYNCWRITE;
457 #endif
458         }
459         if (!locked)
460                 UNLOCK_INODE_MUTEX(inode);
461
462         return rc;
463 }
464 /* Clear pages from the mapping before we do direct IO to that offset.
465  * Now that the only source of such pages in the truncate path flushes
466  * these pages to disk and then discards them, this is error condition.
467  * If add back read cache this will happen again.  This could be disabled
468  * until that time if we never see the below error. */
469 static int filter_clear_page_cache(struct inode *inode,
470                                    struct filter_iobuf *iobuf)
471 {
472         struct page *page;
473         int i, rc;
474
475         rc = filter_sync_inode_data(inode, 0);
476         if (rc != 0)
477                 RETURN(rc);
478
479         /* be careful to call this after fsync_inode_data_buffers has waited
480          * for IO to complete before we evict it from the cache */
481         for (i = 0; i < iobuf->dr_npages; i++) {
482                 page = find_lock_page(inode->i_mapping,
483                                       iobuf->dr_pages[i]->index);
484                 if (page == NULL)
485                         continue;
486                 if (page->mapping != NULL) {
487                         CERROR("page %lu (%d/%d) in page cache during write!\n",
488                                page->index, i, iobuf->dr_npages);
489                         wait_on_page_writeback(page);
490                         ll_truncate_complete_page(page);
491                 }
492
493                 unlock_page(page);
494                 page_cache_release(page);
495         }
496
497         return 0;
498 }
499
500 int filter_clear_truncated_page(struct inode *inode)
501 {
502         struct page *page;
503         int rc;
504
505         /* Truncate on page boundary, so nothing to flush? */
506         if (!(i_size_read(inode) & ~CFS_PAGE_MASK))
507                 return 0;
508
509         rc = filter_sync_inode_data(inode, 1);
510         if (rc != 0)
511                 RETURN(rc);
512
513         /* be careful to call this after fsync_inode_data_buffers has waited
514          * for IO to complete before we evict it from the cache */
515         page = find_lock_page(inode->i_mapping,
516                               i_size_read(inode) >> CFS_PAGE_SHIFT);
517         if (page) {
518                 if (page->mapping != NULL) {
519                         wait_on_page_writeback(page);
520                         ll_truncate_complete_page(page);
521                 }
522                 unlock_page(page);
523                 page_cache_release(page);
524         }
525
526         return 0;
527 }
528
529 /* Must be called with i_mutex taken for writes; this will drop it */
530 int filter_direct_io(int rw, struct dentry *dchild, struct filter_iobuf *iobuf,
531                      struct obd_export *exp, struct iattr *attr,
532                      struct obd_trans_info *oti, void **wait_handle)
533 {
534         struct obd_device *obd = exp->exp_obd;
535         struct inode *inode = dchild->d_inode;
536         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
537         int rc, rc2, create;
538         struct semaphore *sem;
539         ENTRY;
540
541         LASSERTF(iobuf->dr_npages <= iobuf->dr_max_pages, "%d,%d\n",
542                  iobuf->dr_npages, iobuf->dr_max_pages);
543         LASSERT(iobuf->dr_npages <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
544
545         if (rw == OBD_BRW_READ) {
546                 if (iobuf->dr_npages == 0)
547                         RETURN(0);
548                 create = 0;
549                 sem = NULL;
550         } else {
551                 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
552                 LASSERT(iobuf->dr_npages > 0);
553                 create = 1;
554                 sem = &obd->u.filter.fo_alloc_lock;
555
556                 lquota_enforce(filter_quota_interface_ref, obd, iobuf->dr_ignore_quota);
557         }
558
559         rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
560                                     iobuf->dr_npages, iobuf->dr_blocks,
561                                     obdfilter_created_scratchpad, create, sem);
562
563         if (rw == OBD_BRW_WRITE) {
564                 if (rc == 0) {
565                         filter_tally(exp, iobuf->dr_pages,
566                                      iobuf->dr_npages, iobuf->dr_blocks,
567                                      blocks_per_page, 1);
568                         if (attr->ia_size > i_size_read(inode))
569                                 attr->ia_valid |= ATTR_SIZE;
570                         rc = fsfilt_setattr(obd, dchild,
571                                             oti->oti_handle, attr, 0);
572                 }
573
574                 UNLOCK_INODE_MUTEX(inode);
575
576                 rc2 = filter_finish_transno(exp, oti, 0, 0);
577                 if (rc2 != 0) {
578                         CERROR("can't close transaction: %d\n", rc2);
579                         if (rc == 0)
580                                 rc = rc2;
581                 }
582
583                 rc2 = fsfilt_commit_async(obd,inode,oti->oti_handle,
584                                           wait_handle);
585                 if (rc == 0)
586                         rc = rc2;
587                 if (rc != 0)
588                         RETURN(rc);
589         } else if (rc == 0) {
590                 filter_tally(exp, iobuf->dr_pages, iobuf->dr_npages,
591                              iobuf->dr_blocks, blocks_per_page, 0);
592         }
593
594         rc = filter_clear_page_cache(inode, iobuf);
595         if (rc != 0)
596                 RETURN(rc);
597
598         RETURN(filter_do_bio(exp, inode, iobuf, rw));
599 }
600
601 /* See if there are unallocated parts in given file region */
602 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
603 {
604         sector_t (*fs_bmap)(struct address_space *, sector_t) =
605                 inode->i_mapping->a_ops->bmap;
606         int j;
607
608         /* We can't know if we are overwriting or not */
609         if (fs_bmap == NULL)
610                 return 0;
611
612         offset >>= inode->i_blkbits;
613         len >>= inode->i_blkbits;
614
615         for (j = 0; j <= len; j++)
616                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
617                         return 0;
618
619         return 1;
620 }
621
622 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
623                           int objcount, struct obd_ioobj *obj, int niocount,
624                           struct niobuf_local *res, struct obd_trans_info *oti,
625                           int rc)
626 {
627         struct niobuf_local *lnb;
628         struct filter_iobuf *iobuf = NULL;
629         struct lvfs_run_ctxt saved;
630         struct fsfilt_objinfo fso;
631         struct iattr iattr = { 0 };
632         struct inode *inode = NULL;
633         unsigned long now = jiffies;
634         int i, err, cleanup_phase = 0;
635         struct obd_device *obd = exp->exp_obd;
636         void *wait_handle;
637         int   total_size = 0, rc2;
638         unsigned int qcids[MAXQUOTAS] = {0, 0};
639         ENTRY;
640
641         LASSERT(oti != NULL);
642         LASSERT(objcount == 1);
643         LASSERT(current->journal_info == NULL);
644
645         if (rc != 0)
646                 GOTO(cleanup, rc);
647
648         /* Unfortunately, if quota master is too busy to handle the
649          * pre-dqacq in time and quota hash on ost is used up, we
650          * have to wait for the completion of in flight dqacq/dqrel,
651          * then try again */
652         if ((rc2 = lquota_chkquota(filter_quota_interface_ref, obd, oa->o_uid,
653                                    oa->o_gid, niocount)) == QUOTA_RET_ACQUOTA) {
654                 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_HOLD_WRITE_RPC, 90);
655                 lquota_acquire(filter_quota_interface_ref, obd, oa->o_uid,
656                                oa->o_gid);
657         }
658
659         if (rc2 < 0) {
660                 rc = rc2;
661                 GOTO(cleanup, rc);
662         }
663
664         iobuf = filter_iobuf_get(&obd->u.filter, oti);
665         if (IS_ERR(iobuf))
666                 GOTO(cleanup, rc = PTR_ERR(iobuf));
667         cleanup_phase = 1;
668
669         fso.fso_dentry = res->dentry;
670         fso.fso_bufcnt = obj->ioo_bufcnt;
671         inode = res->dentry->d_inode;
672
673         iobuf->dr_ignore_quota = 0;
674         for (i = 0, lnb = res; i < obj->ioo_bufcnt; i++, lnb++) {
675                 loff_t this_size;
676
677                 /* If overwriting an existing block, we don't need a grant */
678                 if (!(lnb->flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
679                     filter_range_is_mapped(inode, lnb->offset, lnb->len))
680                         lnb->rc = 0;
681
682                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
683                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
684                         continue;
685                 }
686
687                 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
688                 LASSERT (err == 0);
689
690                 total_size += lnb->len;
691
692                 /* we expect these pages to be in offset order, but we'll
693                  * be forgiving */
694                 this_size = lnb->offset + lnb->len;
695                 if (this_size > iattr.ia_size)
696                         iattr.ia_size = this_size;
697
698                 /* if one page is a write-back page from client cache, or it's
699                  * written by root, then mark the whole io request as ignore
700                  * quota request */
701                 if (lnb->flags & (OBD_BRW_FROM_GRANT | OBD_BRW_NOQUOTA))
702                         iobuf->dr_ignore_quota = 1;
703         }
704
705         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
706         cleanup_phase = 2;
707
708         DQUOT_INIT(inode);
709
710         LOCK_INODE_MUTEX(inode);
711         fsfilt_check_slow(obd, now, obd_timeout, "i_mutex");
712         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
713                                            oti);
714         if (IS_ERR(oti->oti_handle)) {
715                 UNLOCK_INODE_MUTEX(inode);
716                 rc = PTR_ERR(oti->oti_handle);
717                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
718                        "error starting transaction: rc = %d\n", rc);
719                 oti->oti_handle = NULL;
720                 GOTO(cleanup, rc);
721         }
722         /* have to call fsfilt_commit() from this point on */
723
724         fsfilt_check_slow(obd, now, obd_timeout, "brw_start");
725
726         i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
727
728         /* If the inode still has SUID+SGID bits set (see filter_precreate())
729          * then we will accept the UID+GID if sent by the client for
730          * initializing the ownership of this inode.  We only allow this to
731          * happen once (so clear these bits) and later only allow setattr. */
732         if (inode->i_mode & S_ISUID)
733                 i |= OBD_MD_FLUID;
734         if (inode->i_mode & S_ISGID)
735                 i |= OBD_MD_FLGID;
736
737         iattr_from_obdo(&iattr, oa, i);
738         if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
739                 unsigned int save;
740
741                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
742                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
743
744                 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
745
746                 iattr.ia_valid |= ATTR_MODE;
747                 iattr.ia_mode = inode->i_mode;
748                 if (iattr.ia_valid & ATTR_UID)
749                         iattr.ia_mode &= ~S_ISUID;
750                 if (iattr.ia_valid & ATTR_GID)
751                         iattr.ia_mode &= ~S_ISGID;
752
753                 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
754
755                 /* To avoid problems with quotas, UID and GID must be set
756                  * in the inode before filter_direct_io() - see bug 10357. */
757                 save = iattr.ia_valid;
758                 iattr.ia_valid &= (ATTR_UID | ATTR_GID);
759                 rc = fsfilt_setattr(obd, res->dentry, oti->oti_handle, &iattr, 0);
760                 CDEBUG(D_QUOTA, "set uid(%u)/gid(%u) to ino(%lu). rc(%d)\n", 
761                                 iattr.ia_uid, iattr.ia_gid, inode->i_ino, rc);
762                 iattr.ia_valid = save & ~(ATTR_UID | ATTR_GID);
763         }
764
765         /* filter_direct_io drops i_mutex */
766         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
767                               oti, &wait_handle);
768         if (rc == 0)
769                 obdo_from_inode(oa, inode,
770                                 FILTER_VALID_FLAGS |OBD_MD_FLUID |OBD_MD_FLGID);
771         else
772                 obdo_from_inode(oa, inode, OBD_MD_FLUID | OBD_MD_FLGID);
773
774         lquota_getflag(filter_quota_interface_ref, obd, oa);
775
776         fsfilt_check_slow(obd, now, obd_timeout, "direct_io");
777
778         err = fsfilt_commit_wait(obd, inode, wait_handle);
779         if (err) {
780                 CERROR("Failure to commit OST transaction (%d)?\n", err);
781                 rc = err;
782         }
783
784         if (obd->obd_replayable && !rc)
785                 LASSERTF(oti->oti_transno <= obd->obd_last_committed,
786                          "oti_transno "LPU64" last_committed "LPU64"\n",
787                          oti->oti_transno, obd->obd_last_committed);
788
789         fsfilt_check_slow(obd, now, obd_timeout, "commitrw commit");
790
791 cleanup:
792         filter_grant_commit(exp, niocount, res);
793
794         switch (cleanup_phase) {
795         case 2:
796                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
797                 LASSERT(current->journal_info == NULL);
798         case 1:
799                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
800         case 0:
801                 /*
802                  * lnb->page automatically returns back into per-thread page
803                  * pool (bug 5137)
804                  */
805                 f_dput(res->dentry);
806         }
807
808         /* trigger quota pre-acquire */
809         qcids[USRQUOTA] = oa->o_uid;
810         qcids[GRPQUOTA] = oa->o_gid;
811         err = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
812                             FSFILT_OP_CREATE);
813         CDEBUG(err ? D_ERROR : D_QUOTA,
814                "filter adjust qunit! (rc:%d)\n", err);
815
816         RETURN(rc);
817 }