Whamcloud - gitweb
44178ee380e91d585ca4dead63493fafbe57d008
[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                        "IO - 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                         ClearPageConstant(bvl->bv_page);
153                 }
154                 record_finish_io(iobuf, OBD_BRW_READ, error);
155         } else {
156                 if (mapping_cap_page_constant_write(iobuf->dr_pages[0]->mapping)){
157                         bio_for_each_segment(bvl, bio, i) {
158                                 ClearPageConstant(bvl->bv_page);
159                         }
160                 }
161                 record_finish_io(iobuf, OBD_BRW_WRITE, error);
162         }
163
164         /* any real error is good enough -bzzz */
165         if (error != 0 && iobuf->dr_error == 0)
166                 iobuf->dr_error = error;
167
168         /* Completed bios used to be chained off iobuf->dr_bios and freed in
169          * filter_clear_dreq().  It was then possible to exhaust the biovec-256
170          * mempool when serious on-disk fragmentation was encountered,
171          * deadlocking the OST.  The bios are now released as soon as complete
172          * so the pool cannot be exhausted while IOs are competing. bug 10076 */
173         bio_put(bio);
174         return 0;
175 }
176
177 static int can_be_merged(struct bio *bio, sector_t sector)
178 {
179         unsigned int size;
180
181         if (!bio)
182                 return 0;
183
184         size = bio->bi_size >> 9;
185         return bio->bi_sector + size == sector ? 1 : 0;
186 }
187
188 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
189                                         int rw, int num_pages)
190 {
191         struct filter_iobuf *iobuf;
192
193         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
194
195         OBD_ALLOC(iobuf, sizeof(*iobuf));
196         if (iobuf == NULL)
197                 goto failed_0;
198
199         OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
200         if (iobuf->dr_pages == NULL)
201                 goto failed_1;
202
203         OBD_ALLOC(iobuf->dr_blocks,
204                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
205         if (iobuf->dr_blocks == NULL)
206                 goto failed_2;
207
208         iobuf->dr_filter = filter;
209         init_waitqueue_head(&iobuf->dr_wait);
210         atomic_set(&iobuf->dr_numreqs, 0);
211         iobuf->dr_max_pages = num_pages;
212         iobuf->dr_npages = 0;
213         iobuf->dr_error = 0;
214
215         RETURN(iobuf);
216
217  failed_2:
218         OBD_FREE(iobuf->dr_pages,
219                  num_pages * sizeof(*iobuf->dr_pages));
220  failed_1:
221         OBD_FREE(iobuf, sizeof(*iobuf));
222  failed_0:
223         RETURN(ERR_PTR(-ENOMEM));
224 }
225
226 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
227 {
228         iobuf->dr_npages = 0;
229         iobuf->dr_error = 0;
230         atomic_set(&iobuf->dr_numreqs, 0);
231 }
232
233 void filter_free_iobuf(struct filter_iobuf *iobuf)
234 {
235         int num_pages = iobuf->dr_max_pages;
236
237         filter_clear_iobuf(iobuf);
238
239         OBD_FREE(iobuf->dr_blocks,
240                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
241         OBD_FREE(iobuf->dr_pages,
242                  num_pages * sizeof(*iobuf->dr_pages));
243         OBD_FREE_PTR(iobuf);
244 }
245
246 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
247                       struct obd_trans_info *oti)
248 {
249         int thread_id = (oti && oti->oti_thread) ?
250                         oti->oti_thread->t_id : -1;
251
252         if (unlikely(thread_id < 0)) {
253                 filter_free_iobuf(iobuf);
254                 return;
255         }
256
257         LASSERTF(filter->fo_iobuf_pool[thread_id] == iobuf,
258                  "iobuf mismatch for thread %d: pool %p iobuf %p\n",
259                  thread_id, filter->fo_iobuf_pool[thread_id], iobuf);
260         filter_clear_iobuf(iobuf);
261 }
262
263 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
264                           struct inode *inode, struct page *page)
265 {
266         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
267         iobuf->dr_pages[iobuf->dr_npages++] = page;
268
269         return 0;
270 }
271
272 int filter_do_bio(struct obd_export *exp, struct inode *inode,
273                   struct filter_iobuf *iobuf, int rw)
274 {
275         struct obd_device *obd = exp->exp_obd;
276         int            blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
277         struct page  **pages = iobuf->dr_pages;
278         int            npages = iobuf->dr_npages;
279         unsigned long *blocks = iobuf->dr_blocks;
280         int            total_blocks = npages * blocks_per_page;
281         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
282         unsigned int   blocksize = inode->i_sb->s_blocksize;
283         struct bio    *bio = NULL;
284         int            frags = 0;
285         unsigned long  start_time = jiffies;
286         struct page   *page;
287         unsigned int   page_offset;
288         sector_t       sector;
289         int            nblocks;
290         int            block_idx;
291         int            page_idx;
292         int            i;
293         int            rc = 0;
294         ENTRY;
295
296         LASSERT(iobuf->dr_npages == npages);
297         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
298
299         for (page_idx = 0, block_idx = 0;
300              page_idx < npages;
301              page_idx++, block_idx += blocks_per_page) {
302
303                 page = pages[page_idx];
304                 LASSERT (block_idx + blocks_per_page <= total_blocks);
305
306                 for (i = 0, page_offset = 0;
307                      i < blocks_per_page;
308                      i += nblocks, page_offset += blocksize * nblocks) {
309
310                         nblocks = 1;
311
312                         if (blocks[block_idx + i] == 0) {  /* hole */
313                                 LASSERT(rw == OBD_BRW_READ);
314                                 memset(kmap(page) + page_offset, 0, blocksize);
315                                 kunmap(page);
316                                 continue;
317                         }
318
319                         sector = (sector_t)blocks[block_idx + i] << sector_bits;
320
321                         /* Additional contiguous file blocks? */
322                         while (i + nblocks < blocks_per_page &&
323                                (sector + (nblocks << sector_bits)) ==
324                                ((sector_t)blocks[block_idx + i + nblocks] <<
325                                 sector_bits))
326                                 nblocks++;
327
328                         /* I only set the page to be constant only if it
329                          * is mapped to a contiguous underlying disk block(s).
330                          * It will then make sure the corresponding device
331                          * cache of raid5 will be overwritten by this page.
332                          * - jay */
333                         if ((rw == OBD_BRW_WRITE) &&
334                             (nblocks == blocks_per_page) &&
335                             mapping_cap_page_constant_write(inode->i_mapping))
336                                SetPageConstant(page);
337
338                         if (bio != NULL &&
339                             can_be_merged(bio, sector) &&
340                             bio_add_page(bio, page,
341                                          blocksize * nblocks, page_offset) != 0)
342                                 continue;       /* added this frag OK */
343
344                         if (bio != NULL) {
345                                 request_queue_t *q =
346                                         bdev_get_queue(bio->bi_bdev);
347
348                                 /* Dang! I have to fragment this I/O */
349                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
350                                        "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
351                                        bio->bi_size,
352                                        bio->bi_vcnt, bio->bi_max_vecs,
353                                        bio->bi_size >> 9, q->max_sectors,
354                                        bio_phys_segments(q, bio),
355                                        q->max_phys_segments,
356                                        bio_hw_segments(q, bio),
357                                        q->max_hw_segments);
358
359                                 record_start_io(iobuf, rw, bio->bi_size, exp);
360                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
361                                 if (rc < 0) {
362                                         CERROR("Can't send bio: %d\n", rc);
363                                         record_finish_io(iobuf, rw, rc);
364                                         goto out;
365                                 }
366                                 frags++;
367                         }
368
369                         /* allocate new bio, limited by max BIO size, b=9945 */
370                         bio = bio_alloc(GFP_NOIO, max(BIO_MAX_PAGES,
371                                                       (npages - page_idx) *
372                                                       blocks_per_page));
373                         if (bio == NULL) {
374                                 CERROR("Can't allocate bio %u*%u = %u pages\n",
375                                        (npages - page_idx), blocks_per_page,
376                                        (npages - page_idx) * blocks_per_page);
377                                 rc = -ENOMEM;
378                                 goto out;
379                         }
380
381                         bio->bi_bdev = inode->i_sb->s_bdev;
382                         bio->bi_sector = sector;
383                         bio->bi_end_io = dio_complete_routine;
384                         bio->bi_private = iobuf;
385
386                         rc = bio_add_page(bio, page,
387                                           blocksize * nblocks, page_offset);
388                         LASSERT (rc != 0);
389                 }
390         }
391
392         if (bio != NULL) {
393                 record_start_io(iobuf, rw, bio->bi_size, exp);
394                 rc = fsfilt_send_bio(rw, obd, inode, bio);
395                 if (rc >= 0) {
396                         frags++;
397                         rc = 0;
398                 } else {
399                         CERROR("Can't send bio: %d\n", rc);
400                         record_finish_io(iobuf, rw, rc);
401                 }
402         }
403
404  out:
405         wait_event(iobuf->dr_wait, atomic_read(&iobuf->dr_numreqs) == 0);
406
407         if (rw == OBD_BRW_READ) {
408                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.hist[BRW_R_DIO_FRAGS],
409                                  frags);
410                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_DIO_FRAGS],
411                                  frags);
412                 lprocfs_oh_tally_log2(&obd->u.filter.fo_filter_stats.hist[BRW_R_IO_TIME],
413                                       jiffies - start_time);
414                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_R_IO_TIME], jiffies - start_time);
415                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
416                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_R_DIO_FRAGS],
417                                          frags);
418                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_R_IO_TIME],
419                                               jiffies - start_time);
420                 }
421         } else {
422                 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.hist[BRW_W_DIO_FRAGS],
423                                  frags);
424                 lprocfs_oh_tally(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_DIO_FRAGS],
425                                  frags);
426                 lprocfs_oh_tally_log2(&obd->u.filter.fo_filter_stats.hist[BRW_W_IO_TIME],
427                                       jiffies - start_time);
428                 lprocfs_oh_tally_log2(&exp->exp_filter_data.fed_brw_stats.hist[BRW_W_IO_TIME], jiffies - start_time);
429                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
430                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_W_DIO_FRAGS],
431                                          frags);
432                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->nid_brw_stats->hist[BRW_W_IO_TIME],
433                                               jiffies - start_time);
434                 }
435         }
436
437         if (rc == 0)
438                 rc = iobuf->dr_error;
439         RETURN(rc);
440 }
441
442 /* Must be called with i_mutex taken for writes; this will drop it */
443 int filter_direct_io(int rw, struct dentry *dchild, struct filter_iobuf *iobuf,
444                      struct obd_export *exp, struct iattr *attr,
445                      struct obd_trans_info *oti, void **wait_handle)
446 {
447         struct obd_device *obd = exp->exp_obd;
448         struct inode *inode = dchild->d_inode;
449         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
450         int rc, rc2, create;
451         struct semaphore *sem;
452         ENTRY;
453
454         LASSERTF(iobuf->dr_npages <= iobuf->dr_max_pages, "%d,%d\n",
455                  iobuf->dr_npages, iobuf->dr_max_pages);
456         LASSERT(iobuf->dr_npages <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
457
458         if (rw == OBD_BRW_READ) {
459                 if (iobuf->dr_npages == 0)
460                         RETURN(0);
461                 create = 0;
462                 sem = NULL;
463         } else {
464                 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
465                 LASSERT(iobuf->dr_npages > 0);
466                 create = 1;
467                 sem = &obd->u.filter.fo_alloc_lock;
468
469                 lquota_enforce(filter_quota_interface_ref, obd,
470                                iobuf->dr_ignore_quota);
471         }
472
473         rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
474                                     iobuf->dr_npages, iobuf->dr_blocks,
475                                     obdfilter_created_scratchpad, create, sem);
476
477         if (rw == OBD_BRW_WRITE) {
478                 if (rc == 0) {
479                         filter_tally(exp, iobuf->dr_pages,
480                                      iobuf->dr_npages, iobuf->dr_blocks,
481                                      blocks_per_page, 1);
482                         if (attr->ia_size > i_size_read(inode))
483                                 attr->ia_valid |= ATTR_SIZE;
484                         rc = fsfilt_setattr(obd, dchild,
485                                             oti->oti_handle, attr, 0);
486                 }
487
488                 UNLOCK_INODE_MUTEX(inode);
489
490                 rc2 = filter_finish_transno(exp, oti, 0, 0);
491                 if (rc2 != 0) {
492                         CERROR("can't close transaction: %d\n", rc2);
493                         if (rc == 0)
494                                 rc = rc2;
495                 }
496
497                 rc2 = fsfilt_commit_async(obd,inode,oti->oti_handle,
498                                           wait_handle);
499                 if (rc == 0)
500                         rc = rc2;
501                 if (rc != 0)
502                         RETURN(rc);
503         } else if (rc == 0) {
504                 filter_tally(exp, iobuf->dr_pages, iobuf->dr_npages,
505                              iobuf->dr_blocks, blocks_per_page, 0);
506         }
507
508         RETURN(filter_do_bio(exp, inode, iobuf, rw));
509 }
510
511 /* See if there are unallocated parts in given file region */
512 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
513 {
514         sector_t (*fs_bmap)(struct address_space *, sector_t) =
515                 inode->i_mapping->a_ops->bmap;
516         int j;
517
518         /* We can't know if we are overwriting or not */
519         if (fs_bmap == NULL)
520                 return 0;
521
522         offset >>= inode->i_blkbits;
523         len >>= inode->i_blkbits;
524
525         for (j = 0; j <= len; j++)
526                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
527                         return 0;
528
529         return 1;
530 }
531
532 /*
533  * interesting use cases on how it interacts with VM:
534  *
535  * - vm writeout -- shouldn't see our pages as we don't mark them dirty
536  *   though vm can find partial page left dirty by truncate. in this
537  *   usual writeout is used unless our write rewrite that page - then we
538  *   drop PG_dirty with PG_lock held.
539  *
540  * - else?
541  *
542  */
543 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
544                           int objcount, struct obd_ioobj *obj,
545                           struct niobuf_remote *nb, int niocount,
546                           struct niobuf_local *res, struct obd_trans_info *oti,
547                           int rc)
548 {
549         struct niobuf_local *lnb;
550         struct filter_iobuf *iobuf = NULL;
551         struct lvfs_run_ctxt saved;
552         struct fsfilt_objinfo fso;
553         struct iattr iattr = { 0 };
554         struct inode *inode = res->dentry->d_inode;
555         unsigned long now = jiffies;
556         int i, err, cleanup_phase = 0;
557         struct obd_device *obd = exp->exp_obd;
558         struct filter_obd *fo = &obd->u.filter;
559         void *wait_handle;
560         int total_size = 0;
561         int rec_pending = 0;
562         unsigned int qcids[MAXQUOTAS] = {0, 0};
563         ENTRY;
564
565         LASSERT(oti != NULL);
566         LASSERT(objcount == 1);
567         LASSERT(current->journal_info == NULL);
568
569         if (rc != 0)
570                 GOTO(cleanup, rc);
571
572         /* we try to get enough quota to write here, and let ldiskfs
573          * decide if it is out of quota or not b=14783 */
574         lquota_chkquota(filter_quota_interface_ref, obd, oa->o_uid,
575                         oa->o_gid, niocount, &rec_pending, oti,
576                         LQUOTA_FLAGS_BLK, inode, obj->ioo_bufcnt);
577
578         iobuf = filter_iobuf_get(&obd->u.filter, oti);
579         if (IS_ERR(iobuf))
580                 GOTO(cleanup, rc = PTR_ERR(iobuf));
581         cleanup_phase = 1;
582
583         fso.fso_dentry = res->dentry;
584         fso.fso_bufcnt = obj->ioo_bufcnt;
585
586         iobuf->dr_ignore_quota = 0;
587         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
588                 loff_t this_size;
589                 __u32 flags = lnb->flags;
590
591                 /* If overwriting an existing block, we don't need a grant */
592                 if (!(flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC &&
593                     filter_range_is_mapped(inode, lnb->offset, lnb->len))
594                         lnb->rc = 0;
595
596                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
597                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
598                         continue;
599                 }
600
601                 LASSERT(PageLocked(lnb->page));
602                 LASSERT(!PageWriteback(lnb->page));
603
604                 /* preceding filemap_write_and_wait() should have clean pages */
605                 if (fo->fo_writethrough_cache)
606                         clear_page_dirty_for_io(lnb->page);
607                 LASSERT(!PageDirty(lnb->page));
608
609                 SetPageUptodate(lnb->page);
610
611                 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
612                 LASSERT (err == 0);
613
614                 total_size += lnb->len;
615
616                 /* we expect these pages to be in offset order, but we'll
617                  * be forgiving */
618                 this_size = lnb->offset + lnb->len;
619                 if (this_size > iattr.ia_size)
620                         iattr.ia_size = this_size;
621
622                 /* if one page is a write-back page from client cache and
623                  * not from direct_io, or it's written by root, then mark
624                  * the whole io request as ignore quota request, remote
625                  * client can not break through quota. */
626                 if (exp_connect_rmtclient(exp))
627                         flags &= ~OBD_BRW_NOQUOTA;
628                 if ((flags & OBD_BRW_NOQUOTA) ||
629                     (flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
630                      OBD_BRW_FROM_GRANT)
631                         iobuf->dr_ignore_quota = 1;
632         }
633
634         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
635         cleanup_phase = 2;
636
637         DQUOT_INIT(inode);
638
639         LOCK_INODE_MUTEX(inode);
640         fsfilt_check_slow(obd, now, "i_mutex");
641         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
642                                            oti);
643         if (IS_ERR(oti->oti_handle)) {
644                 UNLOCK_INODE_MUTEX(inode);
645                 rc = PTR_ERR(oti->oti_handle);
646                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
647                        "error starting transaction: rc = %d\n", rc);
648                 oti->oti_handle = NULL;
649                 GOTO(cleanup, rc);
650         }
651         /* have to call fsfilt_commit() from this point on */
652
653         fsfilt_check_slow(obd, now, "brw_start");
654
655         i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
656
657         /* If the inode still has SUID+SGID bits set (see filter_precreate())
658          * then we will accept the UID+GID if sent by the client for
659          * initializing the ownership of this inode.  We only allow this to
660          * happen once (so clear these bits) and later only allow setattr. */
661         if (inode->i_mode & S_ISUID)
662                 i |= OBD_MD_FLUID;
663         if (inode->i_mode & S_ISGID)
664                 i |= OBD_MD_FLGID;
665
666         iattr_from_obdo(&iattr, oa, i);
667         if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
668                 unsigned int save;
669
670                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
671                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
672
673                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
674
675                 iattr.ia_valid |= ATTR_MODE;
676                 iattr.ia_mode = inode->i_mode;
677                 if (iattr.ia_valid & ATTR_UID)
678                         iattr.ia_mode &= ~S_ISUID;
679                 if (iattr.ia_valid & ATTR_GID)
680                         iattr.ia_mode &= ~S_ISGID;
681
682                 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
683
684                 /* To avoid problems with quotas, UID and GID must be set
685                  * in the inode before filter_direct_io() - see bug 10357. */
686                 save = iattr.ia_valid;
687                 iattr.ia_valid &= (ATTR_UID | ATTR_GID);
688                 rc = fsfilt_setattr(obd, res->dentry, oti->oti_handle,&iattr,0);
689                 CDEBUG(D_QUOTA, "set uid(%u)/gid(%u) to ino(%lu). rc(%d)\n",
690                                 iattr.ia_uid, iattr.ia_gid, inode->i_ino, rc);
691                 iattr.ia_valid = save & ~(ATTR_UID | ATTR_GID);
692         }
693
694         /* filter_direct_io drops i_mutex */
695         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
696                               oti, &wait_handle);
697         if (rc == 0)
698                 obdo_from_inode(oa, inode,
699                                 FILTER_VALID_FLAGS |OBD_MD_FLUID |OBD_MD_FLGID);
700         else
701                 obdo_from_inode(oa, inode, OBD_MD_FLUID | OBD_MD_FLGID);
702
703         lquota_getflag(filter_quota_interface_ref, obd, oa);
704
705         fsfilt_check_slow(obd, now, "direct_io");
706
707         err = fsfilt_commit_wait(obd, inode, wait_handle);
708         if (err) {
709                 CERROR("Failure to commit OST transaction (%d)?\n", err);
710                 rc = err;
711         }
712
713         if (obd->obd_replayable && !rc)
714                 LASSERTF(oti->oti_transno <= obd->obd_last_committed,
715                          "oti_transno "LPU64" last_committed "LPU64"\n",
716                          oti->oti_transno, obd->obd_last_committed);
717
718         fsfilt_check_slow(obd, now, "commitrw commit");
719
720 cleanup:
721         if (rec_pending)
722                 lquota_pending_commit(filter_quota_interface_ref, obd, oa->o_uid,
723                                       oa->o_gid, rec_pending, 1);
724
725         filter_grant_commit(exp, niocount, res);
726
727         switch (cleanup_phase) {
728         case 2:
729                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
730                 LASSERT(current->journal_info == NULL);
731         case 1:
732                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
733         case 0:
734                 /*
735                  * lnb->page automatically returns back into per-thread page
736                  * pool (bug 5137)
737                  */
738                 f_dput(res->dentry);
739         }
740
741         /* trigger quota pre-acquire */
742         qcids[USRQUOTA] = oa->o_uid;
743         qcids[GRPQUOTA] = oa->o_gid;
744         err = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
745                             FSFILT_OP_CREATE);
746         CDEBUG(err ? D_ERROR : D_QUOTA,
747                "filter adjust qunit! (rc:%d)\n", err);
748
749         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
750                 if (lnb->page == NULL)
751                         continue;
752
753                 LASSERT(PageLocked(lnb->page));
754                 unlock_page(lnb->page);
755
756                 page_cache_release(lnb->page);
757                 lnb->page = NULL;
758         }
759
760         if (inode && (fo->fo_writethrough_cache == 0 ||
761                         i_size_read(inode) > fo->fo_readcache_max_filesize))
762                 filter_invalidate_cache(obd, obj, nb, inode);
763
764         RETURN(rc);
765 }