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