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