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