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