Whamcloud - gitweb
LU-1459 llite: Don't use unitialized variable
[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 (c) 2003, 2010, Oracle and/or its affiliates. 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->
84                                        fo_filter_stats.hist[BRW_R_DISK_IOSIZE],
85                                       size);
86                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
87                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
88                                           hist[BRW_R_RPC_HIST],
89                                          atomic_read(&filter->fo_r_in_flight));
90                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
91                                          nid_brw_stats->hist[BRW_R_DISK_IOSIZE],
92                                               size);
93                 }
94         } else {
95                 atomic_inc(&filter->fo_w_in_flight);
96                 lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_W_RPC_HIST],
97                                  atomic_read(&filter->fo_w_in_flight));
98                 lprocfs_oh_tally_log2(&filter->
99                                        fo_filter_stats.hist[BRW_W_DISK_IOSIZE],
100                                       size);
101                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
102                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
103                                           hist[BRW_W_RPC_HIST],
104                                          atomic_read(&filter->fo_w_in_flight));
105                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
106                                         nid_brw_stats->hist[BRW_W_DISK_IOSIZE],
107                                               size);
108                 }
109         }
110 }
111
112 static void record_finish_io(struct filter_iobuf *iobuf, int rw, int rc)
113 {
114         struct filter_obd *filter = iobuf->dr_filter;
115
116         /* CAVEAT EMPTOR: possibly in IRQ context
117          * DO NOT record procfs stats here!!! */
118
119         if (rw == OBD_BRW_READ)
120                 atomic_dec(&filter->fo_r_in_flight);
121         else
122                 atomic_dec(&filter->fo_w_in_flight);
123
124         if (atomic_dec_and_test(&iobuf->dr_numreqs))
125                 wake_up(&iobuf->dr_wait);
126 }
127
128 #ifdef HAVE_BIO_ENDIO_2ARG
129 #define DIO_RETURN(a)
130 static void dio_complete_routine(struct bio *bio, int error)
131 #else
132 #define DIO_RETURN(a)   a
133 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
134 #endif
135 {
136         struct filter_iobuf *iobuf = bio->bi_private;
137         struct bio_vec *bvl;
138         int i;
139
140         /* CAVEAT EMPTOR: possibly in IRQ context
141          * DO NOT record procfs stats here!!! */
142
143 #ifndef HAVE_BIO_ENDIO_2ARG
144         /* The "bi_size" check was needed for kernels < 2.6.24 in order to
145          * handle the case where a SCSI request error caused this callback
146          * to be called before all of the biovecs had been processed.
147          * Without this check the server thread will hang.  In newer kernels
148          * the bio_end_io routine is never called for partial completions,
149          * so this check is no longer needed. */
150         if (bio->bi_size)                      /* Not complete */
151                 return DIO_RETURN(1);
152 #endif
153
154         if (unlikely(iobuf == NULL)) {
155                 LCONSOLE_ERROR("bio->bi_private is NULL!  This should never "
156                        "happen.  Normally, I would crash here, but instead I "
157                        "will dump the bio contents to the console.  Please "
158                        "report this to <http://bugs.whamcloud.com/> , along "
159                        "with any interesting messages leading up to this point "
160                        "(like SCSI errors, perhaps).  Because bi_private is "
161                        "NULL, I can't wake up the thread that initiated this "
162                        "IO - you will probably have to reboot this node.\n");
163                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
164                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
165                        "bi_private: %p, error: %d\n",
166                        bio->bi_next, bio->bi_flags,
167                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
168                        bio->bi_end_io, atomic_read(&bio->bi_cnt),
169                        bio->bi_private, error);
170                 return DIO_RETURN(0);
171         }
172
173         /* the check is outside of the cycle for performance reason -bzzz */
174         if (!test_bit(BIO_RW, &bio->bi_rw)) {
175                 bio_for_each_segment(bvl, bio, i) {
176                         if (likely(error == 0))
177                                 SetPageUptodate(bvl->bv_page);
178                         LASSERT(PageLocked(bvl->bv_page));
179                         ClearPageConstant(bvl->bv_page);
180                 }
181                 record_finish_io(iobuf, OBD_BRW_READ, error);
182         } else {
183                 if (mapping_cap_page_constant_write(iobuf->dr_pages[0]->mapping)){
184                         bio_for_each_segment(bvl, bio, i) {
185                                 ClearPageConstant(bvl->bv_page);
186                         }
187                 }
188                 record_finish_io(iobuf, OBD_BRW_WRITE, error);
189         }
190
191         /* any real error is good enough -bzzz */
192         if (error != 0 && iobuf->dr_error == 0)
193                 iobuf->dr_error = error;
194
195         /* Completed bios used to be chained off iobuf->dr_bios and freed in
196          * filter_clear_dreq().  It was then possible to exhaust the biovec-256
197          * mempool when serious on-disk fragmentation was encountered,
198          * deadlocking the OST.  The bios are now released as soon as complete
199          * so the pool cannot be exhausted while IOs are competing. bug 10076 */
200         bio_put(bio);
201         return DIO_RETURN(0);
202 }
203
204 static int can_be_merged(struct bio *bio, sector_t sector)
205 {
206         unsigned int size;
207
208         if (!bio)
209                 return 0;
210
211         size = bio->bi_size >> 9;
212         return bio->bi_sector + size == sector ? 1 : 0;
213 }
214
215 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
216                                         int rw, int num_pages)
217 {
218         struct filter_iobuf *iobuf;
219
220         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
221
222         OBD_ALLOC(iobuf, sizeof(*iobuf));
223         if (iobuf == NULL)
224                 goto failed_0;
225
226         OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
227         if (iobuf->dr_pages == NULL)
228                 goto failed_1;
229
230         OBD_ALLOC(iobuf->dr_blocks,
231                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
232         if (iobuf->dr_blocks == NULL)
233                 goto failed_2;
234
235         iobuf->dr_filter = filter;
236         init_waitqueue_head(&iobuf->dr_wait);
237         atomic_set(&iobuf->dr_numreqs, 0);
238         iobuf->dr_max_pages = num_pages;
239         iobuf->dr_npages = 0;
240         iobuf->dr_error = 0;
241
242         RETURN(iobuf);
243
244  failed_2:
245         OBD_FREE(iobuf->dr_pages,
246                  num_pages * sizeof(*iobuf->dr_pages));
247  failed_1:
248         OBD_FREE(iobuf, sizeof(*iobuf));
249  failed_0:
250         RETURN(ERR_PTR(-ENOMEM));
251 }
252
253 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
254 {
255         iobuf->dr_npages = 0;
256         iobuf->dr_error = 0;
257         atomic_set(&iobuf->dr_numreqs, 0);
258 }
259
260 void filter_free_iobuf(struct filter_iobuf *iobuf)
261 {
262         int num_pages = iobuf->dr_max_pages;
263
264         filter_clear_iobuf(iobuf);
265
266         OBD_FREE(iobuf->dr_blocks,
267                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
268         OBD_FREE(iobuf->dr_pages,
269                  num_pages * sizeof(*iobuf->dr_pages));
270         OBD_FREE_PTR(iobuf);
271 }
272
273 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
274                       struct obd_trans_info *oti)
275 {
276         int thread_id = (oti && oti->oti_thread) ?
277                         oti->oti_thread->t_id : -1;
278
279         if (unlikely(thread_id < 0)) {
280                 filter_free_iobuf(iobuf);
281                 return;
282         }
283
284         LASSERTF(filter->fo_iobuf_pool[thread_id] == iobuf,
285                  "iobuf mismatch for thread %d: pool %p iobuf %p\n",
286                  thread_id, filter->fo_iobuf_pool[thread_id], iobuf);
287         filter_clear_iobuf(iobuf);
288 }
289
290 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
291                           struct inode *inode, struct page *page)
292 {
293         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
294         iobuf->dr_pages[iobuf->dr_npages++] = page;
295
296         return 0;
297 }
298
299 int filter_do_bio(struct obd_export *exp, struct inode *inode,
300                   struct filter_iobuf *iobuf, int rw)
301 {
302         struct obd_device *obd = exp->exp_obd;
303         int            blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
304         struct page  **pages = iobuf->dr_pages;
305         int            npages = iobuf->dr_npages;
306         unsigned long *blocks = iobuf->dr_blocks;
307         int            total_blocks = npages * blocks_per_page;
308         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
309         unsigned int   blocksize = inode->i_sb->s_blocksize;
310         struct bio    *bio = NULL;
311         int            frags = 0;
312         unsigned long  start_time = jiffies;
313         struct page   *page;
314         unsigned int   page_offset;
315         sector_t       sector;
316         int            nblocks;
317         int            block_idx;
318         int            page_idx;
319         int            i;
320         int            rc = 0;
321         ENTRY;
322
323         LASSERT(iobuf->dr_npages == npages);
324         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
325
326         for (page_idx = 0, block_idx = 0;
327              page_idx < npages;
328              page_idx++, block_idx += blocks_per_page) {
329
330                 page = pages[page_idx];
331                 LASSERT (block_idx + blocks_per_page <= total_blocks);
332
333                 for (i = 0, page_offset = 0;
334                      i < blocks_per_page;
335                      i += nblocks, page_offset += blocksize * nblocks) {
336
337                         nblocks = 1;
338
339                         if (blocks[block_idx + i] == 0) {  /* hole */
340                                 LASSERT(rw == OBD_BRW_READ);
341                                 memset(kmap(page) + page_offset, 0, blocksize);
342                                 kunmap(page);
343                                 continue;
344                         }
345
346                         sector = (sector_t)blocks[block_idx + i] << sector_bits;
347
348                         /* Additional contiguous file blocks? */
349                         while (i + nblocks < blocks_per_page &&
350                                (sector + (nblocks << sector_bits)) ==
351                                ((sector_t)blocks[block_idx + i + nblocks] <<
352                                 sector_bits))
353                                 nblocks++;
354
355                         /* I only set the page to be constant only if it
356                          * is mapped to a contiguous underlying disk block(s).
357                          * It will then make sure the corresponding device
358                          * cache of raid5 will be overwritten by this page.
359                          * - jay */
360                         if ((rw == OBD_BRW_WRITE) &&
361                             (nblocks == blocks_per_page) &&
362                             mapping_cap_page_constant_write(inode->i_mapping))
363                                SetPageConstant(page);
364
365                         if (bio != NULL &&
366                             can_be_merged(bio, sector) &&
367                             bio_add_page(bio, page,
368                                          blocksize * nblocks, page_offset) != 0)
369                                 continue;       /* added this frag OK */
370
371                         if (bio != NULL) {
372                                 struct request_queue *q =
373                                        bdev_get_queue(bio->bi_bdev);
374
375                                 /* Dang! I have to fragment this I/O */
376                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
377                                        "sectors %d(%d) psg %d(%d) max hsg %d "
378                                        "sector "LPU64" next "LPU64"\n",
379                                        bio->bi_size,
380                                        bio->bi_vcnt, bio->bi_max_vecs,
381                                        bio->bi_size >> 9, queue_max_sectors(q),
382                                        bio_phys_segments(q, bio),
383                                        queue_max_phys_segments(q),
384                                        queue_max_hw_segments(q),
385                                        (u64)bio->bi_sector,
386                                        (u64)sector);
387
388                                 record_start_io(iobuf, rw, bio->bi_size, exp);
389                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
390                                 if (rc < 0) {
391                                         CERROR("Can't send bio: %d\n", rc);
392                                         record_finish_io(iobuf, rw, rc);
393                                         goto out;
394                                 }
395                                 frags++;
396                         }
397
398                         /* allocate new bio, limited by max BIO size, b=9945 */
399                         bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
400                                                       (npages - page_idx) *
401                                                       blocks_per_page));
402                         if (bio == NULL) {
403                                 CERROR("Can't allocate bio %u*%u = %u pages\n",
404                                        (npages - page_idx), blocks_per_page,
405                                        (npages - page_idx) * blocks_per_page);
406                                 rc = -ENOMEM;
407                                 goto out;
408                         }
409
410                         bio->bi_bdev = inode->i_sb->s_bdev;
411                         bio->bi_sector = sector;
412                         bio->bi_end_io = dio_complete_routine;
413                         bio->bi_private = iobuf;
414
415                         rc = bio_add_page(bio, page,
416                                           blocksize * nblocks, page_offset);
417                         LASSERT (rc != 0);
418                 }
419         }
420
421         if (bio != NULL) {
422                 record_start_io(iobuf, rw, bio->bi_size, exp);
423                 rc = fsfilt_send_bio(rw, obd, inode, bio);
424                 if (rc >= 0) {
425                         frags++;
426                         rc = 0;
427                 } else {
428                         CERROR("Can't send bio: %d\n", rc);
429                         record_finish_io(iobuf, rw, rc);
430                 }
431         }
432
433  out:
434         wait_event(iobuf->dr_wait, atomic_read(&iobuf->dr_numreqs) == 0);
435
436         if (rw == OBD_BRW_READ) {
437                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.
438                                   hist[BRW_R_DIO_FRAGS],
439                                  frags);
440                 lprocfs_oh_tally_log2(&obd->u.filter.
441                                        fo_filter_stats.hist[BRW_R_IO_TIME],
442                                       jiffies - start_time);
443                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
444                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
445                                           hist[BRW_R_DIO_FRAGS],
446                                          frags);
447                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
448                                              nid_brw_stats->hist[BRW_R_IO_TIME],
449                                               jiffies - start_time);
450                 }
451         } else {
452                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.
453                                   hist[BRW_W_DIO_FRAGS], frags);
454                 lprocfs_oh_tally_log2(&obd->u.filter.fo_filter_stats.
455                                        hist[BRW_W_IO_TIME],
456                                       jiffies - start_time);
457                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
458                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
459                                           hist[BRW_W_DIO_FRAGS],
460                                          frags);
461                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
462                                              nid_brw_stats->hist[BRW_W_IO_TIME],
463                                               jiffies - start_time);
464                 }
465         }
466
467         if (rc == 0)
468                 rc = iobuf->dr_error;
469         RETURN(rc);
470 }
471
472 /* Must be called with i_mutex taken for writes; this will drop it */
473 int filter_direct_io(int rw, struct dentry *dchild, struct filter_iobuf *iobuf,
474                      struct obd_export *exp, struct iattr *attr,
475                      struct obd_trans_info *oti, void **wait_handle)
476 {
477         struct obd_device *obd = exp->exp_obd;
478         struct inode *inode = dchild->d_inode;
479         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
480         int rc, rc2, create;
481         struct semaphore *sem;
482         ENTRY;
483
484         LASSERTF(iobuf->dr_npages <= iobuf->dr_max_pages, "%d,%d\n",
485                  iobuf->dr_npages, iobuf->dr_max_pages);
486         LASSERT(iobuf->dr_npages <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
487
488         if (rw == OBD_BRW_READ) {
489                 if (iobuf->dr_npages == 0)
490                         RETURN(0);
491                 create = 0;
492                 sem = NULL;
493         } else {
494                 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
495                 LASSERT(iobuf->dr_npages > 0);
496                 create = 1;
497                 sem = &obd->u.filter.fo_alloc_lock;
498
499                 lquota_enforce(filter_quota_interface_ref, obd,
500                                iobuf->dr_ignore_quota);
501         }
502
503         if (rw == OBD_BRW_WRITE &&
504             OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
505                 rc = -ENOSPC;
506         } else {
507                 rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
508                                     iobuf->dr_npages, iobuf->dr_blocks,
509                                     obdfilter_created_scratchpad, create, sem);
510         }
511
512         if (rw == OBD_BRW_WRITE) {
513                 if (rc == 0) {
514                         filter_tally(exp, iobuf->dr_pages,
515                                      iobuf->dr_npages, iobuf->dr_blocks,
516                                      blocks_per_page, 1);
517                         if (attr->ia_size > i_size_read(inode))
518                                 attr->ia_valid |= ATTR_SIZE;
519                         rc = fsfilt_setattr(obd, dchild,
520                                             oti->oti_handle, attr, 0);
521                 }
522
523                 UNLOCK_INODE_MUTEX(inode);
524
525                 /* Force commit to make the just-deleted blocks
526                  * reusable. LU-456 */
527                 if (rc == -ENOSPC) {
528                         fsfilt_commit(obd, inode, oti->oti_handle, 1);
529                         RETURN(rc);
530                 }
531
532                 rc2 = filter_finish_transno(exp, inode, oti, 0, 0);
533                 if (rc2 != 0) {
534                         CERROR("can't close transaction: %d\n", rc2);
535                         if (rc == 0)
536                                 rc = rc2;
537                 }
538
539                 if (wait_handle)
540                         rc2 = fsfilt_commit_async(obd, inode, oti->oti_handle,
541                                                   wait_handle);
542                 else
543                         rc2 = fsfilt_commit(obd, inode, oti->oti_handle, 0);
544                 if (rc == 0)
545                         rc = rc2;
546                 if (rc != 0)
547                         RETURN(rc);
548         } else if (rc == 0) {
549                 filter_tally(exp, iobuf->dr_pages, iobuf->dr_npages,
550                              iobuf->dr_blocks, blocks_per_page, 0);
551         }
552
553         RETURN(filter_do_bio(exp, inode, iobuf, rw));
554 }
555
556 /* See if there are unallocated parts in given file region */
557 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
558 {
559         sector_t (*fs_bmap)(struct address_space *, sector_t) =
560                 inode->i_mapping->a_ops->bmap;
561         int j;
562
563         /* We can't know if we are overwriting or not */
564         if (fs_bmap == NULL)
565                 return 0;
566
567         offset >>= inode->i_blkbits;
568         len >>= inode->i_blkbits;
569
570         for (j = 0; j <= len; j++)
571                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
572                         return 0;
573
574         return 1;
575 }
576
577 /*
578  * interesting use cases on how it interacts with VM:
579  *
580  * - vm writeout -- shouldn't see our pages as we don't mark them dirty
581  *   though vm can find partial page left dirty by truncate. in this
582  *   usual writeout is used unless our write rewrite that page - then we
583  *   drop PG_dirty with PG_lock held.
584  *
585  * - else?
586  *
587  */
588 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
589                           int objcount, struct obd_ioobj *obj,
590                           struct niobuf_remote *nb, int niocount,
591                           struct niobuf_local *res, struct obd_trans_info *oti,
592                           int rc)
593 {
594         struct niobuf_local *lnb;
595         struct filter_iobuf *iobuf = NULL;
596         struct lvfs_run_ctxt saved;
597         struct fsfilt_objinfo fso;
598         struct iattr iattr = { 0 };
599         struct inode *inode = res->dentry->d_inode;
600         unsigned long now = jiffies;
601         int i, err, cleanup_phase = 0;
602         struct obd_device *obd = exp->exp_obd;
603         struct filter_obd *fo = &obd->u.filter;
604         void *wait_handle = NULL;
605         int total_size = 0;
606         int quota_pending[2] = {0, 0}, quota_pages = 0;
607         unsigned int qcids[MAXQUOTAS] = {oa->o_uid, oa->o_gid};
608         int sync_journal_commit = obd->u.filter.fo_syncjournal;
609         int retries = 0;
610         ENTRY;
611
612         LASSERT(oti != NULL);
613         LASSERT(objcount == 1);
614         LASSERT(current->journal_info == NULL);
615
616         if (rc != 0)
617                 GOTO(cleanup, rc);
618
619         iobuf = filter_iobuf_get(&obd->u.filter, oti);
620         if (IS_ERR(iobuf))
621                 GOTO(cleanup, rc = PTR_ERR(iobuf));
622         cleanup_phase = 1;
623
624         fso.fso_dentry = res->dentry;
625         fso.fso_bufcnt = obj->ioo_bufcnt;
626
627         iobuf->dr_ignore_quota = 0;
628         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
629                 loff_t this_size;
630
631
632                 if (filter_range_is_mapped(inode, lnb->offset, lnb->len)) {
633                         /* If overwriting an existing block,
634                          * we don't need a grant */
635                         if (!(lnb->flags & OBD_BRW_GRANTED) &&
636                             lnb->rc == -ENOSPC)
637                                 lnb->rc = 0;
638                 } else {
639                         quota_pages++;
640                 }
641
642                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
643                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
644                         continue;
645                 }
646
647                 LASSERT(PageLocked(lnb->page));
648                 LASSERT(!PageWriteback(lnb->page));
649
650                 /* Calling clear_page_dirty_for_io() is no longer needed since
651                  * write and truncate are serialized by i_alloc_sem and
652                  * filter_setattr_internal() flushes any partial-page
653                  * truncates before releasing i_alloc_sem, so there should
654                  * never be any dirty pages at this time. */
655                 LASSERT(!PageDirty(lnb->page));
656
657                 SetPageUptodate(lnb->page);
658
659                 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
660                 LASSERT (err == 0);
661
662                 total_size += lnb->len;
663
664                 /* we expect these pages to be in offset order, but we'll
665                  * be forgiving */
666                 this_size = lnb->offset + lnb->len;
667                 if (this_size > iattr.ia_size)
668                         iattr.ia_size = this_size;
669
670                 /* if one page is a write-back page from client cache and
671                  * not from direct_io, or it's written by root, then mark
672                  * the whole io request as ignore quota request */
673                 if (lnb->flags & OBD_BRW_NOQUOTA ||
674                     (lnb->flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
675                     OBD_BRW_FROM_GRANT)
676                         iobuf->dr_ignore_quota = 1;
677
678                 if (!(lnb->flags & OBD_BRW_ASYNC)) {
679                         sync_journal_commit = 1;
680                 }
681         }
682
683         /* we try to get enough quota to write here, and let ldiskfs
684          * decide if it is out of quota or not b=14783 */
685         rc = lquota_chkquota(filter_quota_interface_ref, exp, qcids[0],
686                              qcids[1], quota_pages, quota_pending, oti,
687                              inode, obj->ioo_bufcnt);
688         if (rc == -ENOTCONN)
689                 GOTO(cleanup, rc);
690
691         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
692         cleanup_phase = 2;
693
694         fsfilt_check_slow(obd, now, "quota init");
695
696 retry:
697         LOCK_INODE_MUTEX(inode);
698         fsfilt_check_slow(obd, now, "i_mutex");
699         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
700                                            oti);
701         if (IS_ERR(oti->oti_handle)) {
702                 UNLOCK_INODE_MUTEX(inode);
703                 rc = PTR_ERR(oti->oti_handle);
704                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
705                        "error starting transaction: rc = %d\n", rc);
706                 oti->oti_handle = NULL;
707                 GOTO(cleanup, rc);
708         }
709         /* have to call fsfilt_commit() from this point on */
710
711         fsfilt_check_slow(obd, now, "brw_start");
712
713         /* Locking order: i_mutex -> journal_lock -> dqptr_sem. LU-952 */
714         ll_vfs_dq_init(inode);
715
716         i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
717
718         /* If the inode still has SUID+SGID bits set (see filter_precreate())
719          * then we will accept the UID+GID if sent by the client for
720          * initializing the ownership of this inode.  We only allow this to
721          * happen once (so clear these bits) and later only allow setattr. */
722         if (inode->i_mode & S_ISUID)
723                 i |= OBD_MD_FLUID;
724         if (inode->i_mode & S_ISGID)
725                 i |= OBD_MD_FLGID;
726
727         iattr_from_obdo(&iattr, oa, i);
728         if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
729                 unsigned int save;
730
731                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
732                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
733
734                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
735
736                 iattr.ia_valid |= ATTR_MODE;
737                 iattr.ia_mode = inode->i_mode;
738                 if (iattr.ia_valid & ATTR_UID)
739                         iattr.ia_mode &= ~S_ISUID;
740                 if (iattr.ia_valid & ATTR_GID)
741                         iattr.ia_mode &= ~S_ISGID;
742
743                 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
744
745                 /* To avoid problems with quotas, UID and GID must be set
746                  * in the inode before filter_direct_io() - see bug 10357. */
747                 save = iattr.ia_valid;
748                 iattr.ia_valid &= (ATTR_UID | ATTR_GID);
749                 rc = fsfilt_setattr(obd, res->dentry, oti->oti_handle,&iattr,0);
750                 CDEBUG(D_QUOTA, "set uid(%u)/gid(%u) to ino(%lu). rc(%d)\n",
751                                 iattr.ia_uid, iattr.ia_gid, inode->i_ino, rc);
752                 iattr.ia_valid = save & ~(ATTR_UID | ATTR_GID);
753         }
754
755         /* filter_direct_io drops i_mutex */
756         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
757                               oti, sync_journal_commit ? &wait_handle : NULL);
758         if (rc == -ENOSPC && retries++ < 3) {
759                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
760                        retries);
761                 oti->oti_handle = NULL;
762                 fsfilt_check_slow(obd, now, "direct_io");
763                 goto retry;
764         }
765
766         if (rc == 0)
767                 obdo_from_inode(oa, inode,
768                                 FILTER_VALID_FLAGS |OBD_MD_FLUID |OBD_MD_FLGID);
769         else
770                 obdo_from_inode(oa, inode, OBD_MD_FLUID | OBD_MD_FLGID);
771
772         lquota_getflag(filter_quota_interface_ref, obd, oa);
773
774         fsfilt_check_slow(obd, now, "direct_io");
775
776         if (wait_handle)
777                 err = fsfilt_commit_wait(obd, inode, wait_handle);
778         else
779                 err = 0;
780
781         if (err) {
782                 CERROR("Failure to commit OST transaction (%d)?\n", err);
783                 if (rc == 0)
784                         rc = err;
785         }
786
787         if (obd->obd_replayable && !rc && wait_handle)
788                 LASSERTF(oti->oti_transno <= obd->obd_last_committed,
789                          "oti_transno "LPU64" last_committed "LPU64"\n",
790                          oti->oti_transno, obd->obd_last_committed);
791
792         fsfilt_check_slow(obd, now, "commitrw commit");
793
794 cleanup:
795         if (quota_pending[0] || quota_pending[1])
796                 lquota_pending_commit(filter_quota_interface_ref, obd,
797                                       qcids[0], qcids[1], quota_pending);
798
799         filter_grant_commit(exp, niocount, res);
800
801         switch (cleanup_phase) {
802         case 2:
803                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
804                 LASSERT(current->journal_info == NULL);
805         case 1:
806                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
807         case 0:
808                 /*
809                  * lnb->page automatically returns back into per-thread page
810                  * pool (bug 5137)
811                  */
812                  break;
813         }
814
815         /* trigger quota pre-acquire */
816         err = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
817                             FSFILT_OP_CREATE);
818         CDEBUG(err ? D_ERROR : D_QUOTA, "filter adjust qunit! "
819                "(rc:%d, uid:%u, gid:%u)\n", err, qcids[0], qcids[1]);
820         if (qcids[USRQUOTA] != oa->o_uid || qcids[GRPQUOTA] != oa->o_gid) {
821                 qcids[0] = oa->o_uid;
822                 qcids[1] = oa->o_gid;
823                 err = lquota_adjust(filter_quota_interface_ref, obd, qcids,
824                                     NULL, rc, FSFILT_OP_CREATE);
825                 CDEBUG(err ? D_ERROR : D_QUOTA, "filter adjust qunit! "
826                        "(rc:%d, uid:%u, gid:%u)\n", err, qcids[0], qcids[1]);
827         }
828
829         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
830                 if (lnb->page == NULL)
831                         continue;
832
833                 if (rc)
834                         /* If the write has failed, the page cache may
835                          * not be consitent with what is on disk, so
836                          * force pages to be reread next time it is
837                          * accessed */
838                         ClearPageUptodate(lnb->page);
839
840                 LASSERT(PageLocked(lnb->page));
841                 unlock_page(lnb->page);
842
843                 page_cache_release(lnb->page);
844                 lnb->page = NULL;
845         }
846
847         if (inode) {
848                 if (fo->fo_writethrough_cache == 0 ||
849                     i_size_read(inode) > fo->fo_readcache_max_filesize)
850                         filter_release_cache(obd, obj, nb, inode);
851                 up_read(&inode->i_alloc_sem);
852         }
853         f_dput(res->dentry);
854
855         RETURN(rc);
856 }