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