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