Whamcloud - gitweb
LU-1693 obdfilter: Set bi_rw before calling bio_add_page()
[fs/lustre-release.git] / lustre / obdfilter / filter_io_26.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #include <linux/module.h>
44 #include <linux/pagemap.h> // XXX kill me soon
45 #include <linux/version.h>
46 #include <linux/buffer_head.h>
47
48 #define DEBUG_SUBSYSTEM S_FILTER
49
50 #include <obd_class.h>
51 #include <lustre_fsfilt.h>
52 #include <lustre_quota.h>
53 #include "filter_internal.h"
54
55 /* 512byte block min */
56 #define MAX_BLOCKS_PER_PAGE (CFS_PAGE_SIZE / 512)
57
58 static void record_start_io(struct filter_iobuf *iobuf, int rw, int size,
59                             struct obd_export *exp)
60 {
61         struct filter_obd *filter = iobuf->dr_filter;
62
63         cfs_atomic_inc(&iobuf->dr_numreqs);
64
65         if (rw == OBD_BRW_READ) {
66                 cfs_atomic_inc(&filter->fo_r_in_flight);
67                 lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_R_RPC_HIST],
68                                  cfs_atomic_read(&filter->fo_r_in_flight));
69                 lprocfs_oh_tally_log2(&filter->
70                                        fo_filter_stats.hist[BRW_R_DISK_IOSIZE],
71                                       size);
72                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
73                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
74                                          hist[BRW_R_RPC_HIST],
75                                          cfs_atomic_read(&filter-> \
76                                          fo_r_in_flight));
77                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
78                                          nid_brw_stats->hist[BRW_R_DISK_IOSIZE],
79                                               size);
80                 }
81         } else {
82                 cfs_atomic_inc(&filter->fo_w_in_flight);
83                 lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_W_RPC_HIST],
84                                  cfs_atomic_read(&filter->fo_w_in_flight));
85                 lprocfs_oh_tally_log2(&filter->
86                                        fo_filter_stats.hist[BRW_W_DISK_IOSIZE],
87                                       size);
88                 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
89                         lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
90                                           hist[BRW_W_RPC_HIST],
91                                          cfs_atomic_read(&filter-> \
92                                          fo_w_in_flight));
93                         lprocfs_oh_tally_log2(&exp->exp_nid_stats->
94                                         nid_brw_stats->hist[BRW_W_DISK_IOSIZE],
95                                               size);
96                 }
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                 cfs_atomic_dec(&filter->fo_r_in_flight);
109         else
110                 cfs_atomic_dec(&filter->fo_w_in_flight);
111
112         if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
113                 cfs_waitq_signal(&iobuf->dr_wait);
114 }
115
116 #ifdef HAVE_BIO_ENDIO_2ARG
117 #define DIO_RETURN(a)
118 static void dio_complete_routine(struct bio *bio, int error)
119 #else
120 #define DIO_RETURN(a)   return(a)
121 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
122 #endif
123 {
124         struct filter_iobuf *iobuf = bio->bi_private;
125         struct bio_vec *bvl;
126         int i;
127
128         /* CAVEAT EMPTOR: possibly in IRQ context
129          * DO NOT record procfs stats here!!! */
130
131 #ifdef HAVE_BIO_ENDIO_2ARG
132        /* The "bi_size" check was needed for kernels < 2.6.24 in order to
133         * handle the case where a SCSI request error caused this callback
134         * to be called before all of the biovecs had been processed.
135         * Without this check the server thread will hang.  In newer kernels
136         * the bio_end_io routine is never called for partial completions,
137         * so this check is no longer needed. */
138         if (bio->bi_size)                      /* Not complete */
139                 DIO_RETURN(1);
140 #endif
141
142         if (unlikely(iobuf == NULL)) {
143                 CERROR("***** bio->bi_private is NULL!  This should never "
144                        "happen.  Normally, I would crash here, but instead I "
145                        "will dump the bio contents to the console.  Please "
146                        "report this to <http://bugs.whamcloud.com/>, along "
147                        "with any interesting messages leading up to this point "
148                        "(like SCSI errors, perhaps).  Because bi_private is "
149                        "NULL, I can't wake up the thread that initiated this "
150                        "IO - you will probably have to reboot this node.\n");
151                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
152                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
153                        "bi_private: %p\n", bio->bi_next, bio->bi_flags,
154                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
155                        bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
156                        bio->bi_private);
157                 DIO_RETURN(0);
158         }
159
160         /* the check is outside of the cycle for performance reason -bzzz */
161         if (!cfs_test_bit(BIO_RW, &bio->bi_rw)) {
162                 bio_for_each_segment(bvl, bio, i) {
163                         if (likely(error == 0))
164                                 SetPageUptodate(bvl->bv_page);
165                         LASSERT(PageLocked(bvl->bv_page));
166                         ClearPageConstant(bvl->bv_page);
167                 }
168                 record_finish_io(iobuf, OBD_BRW_READ, error);
169         } else {
170                 if (mapping_cap_page_constant_write(iobuf->dr_pages[0]->mapping)){
171                         bio_for_each_segment(bvl, bio, i) {
172                                 ClearPageConstant(bvl->bv_page);
173                         }
174                 }
175                 record_finish_io(iobuf, OBD_BRW_WRITE, error);
176         }
177
178         /* any real error is good enough -bzzz */
179         if (error != 0 && iobuf->dr_error == 0)
180                 iobuf->dr_error = error;
181
182         /* Completed bios used to be chained off iobuf->dr_bios and freed in
183          * filter_clear_dreq().  It was then possible to exhaust the biovec-256
184          * mempool when serious on-disk fragmentation was encountered,
185          * deadlocking the OST.  The bios are now released as soon as complete
186          * so the pool cannot be exhausted while IOs are competing. bug 10076 */
187         bio_put(bio);
188         DIO_RETURN(0);
189 }
190
191 static int can_be_merged(struct bio *bio, sector_t sector)
192 {
193         unsigned int size;
194
195         if (!bio)
196                 return 0;
197
198         size = bio->bi_size >> 9;
199         return bio->bi_sector + size == sector ? 1 : 0;
200 }
201
202 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
203                                         int rw, int num_pages)
204 {
205         struct filter_iobuf *iobuf;
206
207         LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
208
209         OBD_ALLOC(iobuf, sizeof(*iobuf));
210         if (iobuf == NULL)
211                 goto failed_0;
212
213         OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
214         if (iobuf->dr_pages == NULL)
215                 goto failed_1;
216
217         OBD_ALLOC(iobuf->dr_blocks,
218                   MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
219         if (iobuf->dr_blocks == NULL)
220                 goto failed_2;
221
222         CFS_INIT_HLIST_NODE(&iobuf->dr_hlist);
223         iobuf->dr_filter = filter;
224         cfs_waitq_init(&iobuf->dr_wait);
225         cfs_atomic_set(&iobuf->dr_numreqs, 0);
226         iobuf->dr_max_pages = num_pages;
227         iobuf->dr_npages = 0;
228         iobuf->dr_error = 0;
229
230         RETURN(iobuf);
231
232  failed_2:
233         OBD_FREE(iobuf->dr_pages,
234                  num_pages * sizeof(*iobuf->dr_pages));
235  failed_1:
236         OBD_FREE(iobuf, sizeof(*iobuf));
237  failed_0:
238         RETURN(ERR_PTR(-ENOMEM));
239 }
240
241 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
242 {
243         iobuf->dr_npages = 0;
244         iobuf->dr_error = 0;
245         cfs_atomic_set(&iobuf->dr_numreqs, 0);
246 }
247
248 void filter_free_iobuf(struct filter_iobuf *iobuf)
249 {
250         int num_pages = iobuf->dr_max_pages;
251
252         filter_clear_iobuf(iobuf);
253
254         LASSERT(cfs_hlist_unhashed(&iobuf->dr_hlist));
255         OBD_FREE(iobuf->dr_blocks,
256                  MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
257         OBD_FREE(iobuf->dr_pages,
258                  num_pages * sizeof(*iobuf->dr_pages));
259         OBD_FREE_PTR(iobuf);
260 }
261
262 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
263                       struct obd_trans_info *oti)
264 {
265         int thread_id = (oti && oti->oti_thread) ?
266                         oti->oti_thread->t_id : -1;
267
268         if (unlikely(thread_id < 0)) {
269                 filter_free_iobuf(iobuf);
270                 return;
271         }
272
273         filter_clear_iobuf(iobuf);
274 }
275
276 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
277                           struct inode *inode, struct page *page)
278 {
279         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
280         iobuf->dr_pages[iobuf->dr_npages++] = page;
281
282         return 0;
283 }
284
285 int filter_do_bio(struct obd_export *exp, struct inode *inode,
286                   struct filter_iobuf *iobuf, int rw)
287 {
288         struct obd_device *obd = exp->exp_obd;
289         int            blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
290         struct page  **pages = iobuf->dr_pages;
291         int            npages = iobuf->dr_npages;
292         unsigned long *blocks = iobuf->dr_blocks;
293         int            total_blocks = npages * blocks_per_page;
294         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
295         unsigned int   blocksize = inode->i_sb->s_blocksize;
296         struct bio    *bio = NULL;
297         int            frags = 0;
298         unsigned long  start_time = jiffies;
299         struct page   *page;
300         unsigned int   page_offset;
301         sector_t       sector;
302         int            nblocks;
303         int            block_idx;
304         int            page_idx;
305         int            i;
306         int            rc = 0;
307         ENTRY;
308
309         LASSERT(iobuf->dr_npages == npages);
310         LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
311
312         for (page_idx = 0, block_idx = 0;
313              page_idx < npages;
314              page_idx++, block_idx += blocks_per_page) {
315
316                 page = pages[page_idx];
317                 LASSERT (block_idx + blocks_per_page <= total_blocks);
318
319                 for (i = 0, page_offset = 0;
320                      i < blocks_per_page;
321                      i += nblocks, page_offset += blocksize * nblocks) {
322
323                         nblocks = 1;
324
325                         if (blocks[block_idx + i] == 0) {  /* hole */
326                                 LASSERT(rw == OBD_BRW_READ);
327                                 memset(kmap(page) + page_offset, 0, blocksize);
328                                 kunmap(page);
329                                 continue;
330                         }
331
332                         sector = (sector_t)blocks[block_idx + i] << sector_bits;
333
334                         /* Additional contiguous file blocks? */
335                         while (i + nblocks < blocks_per_page &&
336                                (sector + (nblocks << sector_bits)) ==
337                                ((sector_t)blocks[block_idx + i + nblocks] <<
338                                 sector_bits))
339                                 nblocks++;
340
341                         /* I only set the page to be constant only if it
342                          * is mapped to a contiguous underlying disk block(s).
343                          * It will then make sure the corresponding device
344                          * cache of raid5 will be overwritten by this page.
345                          * - jay */
346                         if ((rw == OBD_BRW_WRITE) &&
347                             (nblocks == blocks_per_page) &&
348                             mapping_cap_page_constant_write(inode->i_mapping))
349                                SetPageConstant(page);
350
351                         if (bio != NULL &&
352                             can_be_merged(bio, sector) &&
353                             bio_add_page(bio, page,
354                                          blocksize * nblocks, page_offset) != 0)
355                                 continue;       /* added this frag OK */
356
357                         if (bio != NULL) {
358                                 struct request_queue *q =
359                                         bdev_get_queue(bio->bi_bdev);
360
361                                 /* Dang! I have to fragment this I/O */
362                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
363                                        "sectors %d(%d) psg %d(%d) hsg %d(%d) "
364                                        "sector %llu next %llu\n",
365                                        bio->bi_size,
366                                        bio->bi_vcnt, bio->bi_max_vecs,
367                                        bio->bi_size >> 9, queue_max_sectors(q),
368                                        bio_phys_segments(q, bio),
369                                        queue_max_phys_segments(q),
370                                        bio_hw_segments(q, bio),
371                                        queue_max_hw_segments(q),
372                                        (unsigned long long)bio->bi_sector,
373                                        (unsigned long long)sector);
374
375                                 record_start_io(iobuf, rw, bio->bi_size, exp);
376                                 rc = fsfilt_send_bio(rw, obd, inode, bio);
377                                 if (rc < 0) {
378                                         CERROR("Can't send bio: %d\n", rc);
379                                         record_finish_io(iobuf, rw, rc);
380                                         goto out;
381                                 }
382                                 frags++;
383                         }
384
385                         /* allocate new bio, limited by max BIO size, b=9945 */
386                         bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
387                                                       (npages - page_idx) *
388                                                       blocks_per_page));
389                         if (bio == NULL) {
390                                 CERROR("Can't allocate bio %u*%u = %u pages\n",
391                                        (npages - page_idx), blocks_per_page,
392                                        (npages - page_idx) * blocks_per_page);
393                                 rc = -ENOMEM;
394                                 goto out;
395                         }
396
397                         bio->bi_bdev = inode->i_sb->s_bdev;
398                         bio->bi_sector = sector;
399                         bio->bi_rw = (rw == OBD_BRW_READ ? READ : WRITE);
400                         bio->bi_end_io = dio_complete_routine;
401                         bio->bi_private = iobuf;
402
403                         rc = bio_add_page(bio, page,
404                                           blocksize * nblocks, page_offset);
405                         LASSERT (rc != 0);
406                 }
407         }
408
409         if (bio != NULL) {
410                 record_start_io(iobuf, rw, bio->bi_size, exp);
411                 rc = fsfilt_send_bio(rw, obd, inode, bio);
412                 if (rc >= 0) {
413                         frags++;
414                         rc = 0;
415                 } else {
416                         CERROR("Can't send bio: %d\n", rc);
417                         record_finish_io(iobuf, rw, rc);
418                 }
419         }
420
421  out:
422         cfs_wait_event(iobuf->dr_wait,
423                        cfs_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         cfs_mutex_t *mutex;
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                 mutex = NULL;
482         } else {
483                 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
484                 LASSERT(iobuf->dr_npages > 0);
485                 create = 1;
486                 mutex = &obd->u.filter.fo_alloc_lock;
487
488                 lquota_enforce(filter_quota_interface_ref, obd,
489                                iobuf->dr_ignore_quota);
490         }
491
492         if (rw == OBD_BRW_WRITE &&
493             OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
494                 rc = -ENOSPC;
495         } else {
496                 rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
497                                     iobuf->dr_npages, iobuf->dr_blocks,
498                                     obdfilter_created_scratchpad, create, mutex);
499         }
500
501         if (rw == OBD_BRW_WRITE) {
502                 if (rc == 0) {
503                         filter_tally(exp, iobuf->dr_pages,
504                                      iobuf->dr_npages, iobuf->dr_blocks,
505                                      blocks_per_page, 1);
506                         if (attr->ia_size > i_size_read(inode))
507                                 attr->ia_valid |= ATTR_SIZE;
508                         rc = fsfilt_setattr(obd, dchild,
509                                             oti->oti_handle, attr, 0);
510                 }
511
512                 mutex_unlock(&inode->i_mutex);
513
514                 /* Force commit to make the just-deleted blocks
515                  * reusable. LU-456 */
516                 if (rc == -ENOSPC) {
517                         fsfilt_commit(obd, inode, oti->oti_handle, 1);
518                         RETURN(rc);
519                 }
520
521                 rc2 = filter_finish_transno(exp, inode, oti, 0, 0);
522                 if (rc2 != 0) {
523                         CERROR("can't close transaction: %d\n", rc2);
524                         if (rc == 0)
525                                 rc = rc2;
526                 }
527
528                 if (wait_handle)
529                         rc2 = fsfilt_commit_async(obd,inode,oti->oti_handle,
530                                                   wait_handle);
531                 else
532                         rc2 = fsfilt_commit(obd, inode, oti->oti_handle, 0);
533                 if (rc == 0)
534                         rc = rc2;
535                 if (rc != 0)
536                         RETURN(rc);
537         } else if (rc == 0) {
538                 filter_tally(exp, iobuf->dr_pages, iobuf->dr_npages,
539                              iobuf->dr_blocks, blocks_per_page, 0);
540         }
541
542         RETURN(filter_do_bio(exp, inode, iobuf, rw));
543 }
544
545 /* See if there are unallocated parts in given file region */
546 static int filter_range_is_mapped(struct inode *inode, obd_size offset, int len)
547 {
548         sector_t (*fs_bmap)(struct address_space *, sector_t) =
549                 inode->i_mapping->a_ops->bmap;
550         int j;
551
552         /* We can't know if we are overwriting or not */
553         if (fs_bmap == NULL)
554                 return 0;
555
556         offset >>= inode->i_blkbits;
557         len >>= inode->i_blkbits;
558
559         for (j = 0; j <= len; j++)
560                 if (fs_bmap(inode->i_mapping, offset + j) == 0)
561                         return 0;
562
563         return 1;
564 }
565
566 /*
567  * interesting use cases on how it interacts with VM:
568  *
569  * - vm writeout -- shouldn't see our pages as we don't mark them dirty
570  *   though vm can find partial page left dirty by truncate. in this
571  *   usual writeout is used unless our write rewrite that page - then we
572  *   drop PG_dirty with PG_lock held.
573  *
574  * - else?
575  *
576  */
577 int filter_commitrw_write(struct obd_export *exp, struct obdo *oa,
578                           int objcount, struct obd_ioobj *obj,
579                           struct niobuf_remote *nb, int niocount,
580                           struct niobuf_local *res, struct obd_trans_info *oti,
581                           int rc)
582 {
583         struct niobuf_local *lnb;
584         struct filter_iobuf *iobuf = NULL;
585         struct lvfs_run_ctxt saved;
586         struct fsfilt_objinfo fso;
587         struct iattr iattr = { 0 };
588         struct inode *inode = res->dentry->d_inode;
589         unsigned long now = jiffies;
590         int i, err, cleanup_phase = 0;
591         struct obd_device *obd = exp->exp_obd;
592         struct filter_obd *fo = &obd->u.filter;
593         void *wait_handle = NULL;
594         int total_size = 0;
595         unsigned int qcids[MAXQUOTAS] = { oa->o_uid, oa->o_gid };
596         int rec_pending[MAXQUOTAS] = { 0, 0 }, quota_pages = 0;
597         int sync_journal_commit = obd->u.filter.fo_syncjournal;
598         int retries = 0;
599         ENTRY;
600
601         LASSERT(oti != NULL);
602         LASSERT(objcount == 1);
603         LASSERT(current->journal_info == NULL);
604
605         if (rc != 0)
606                 GOTO(cleanup, rc);
607
608         iobuf = filter_iobuf_get(&obd->u.filter, oti);
609         if (IS_ERR(iobuf))
610                 GOTO(cleanup, rc = PTR_ERR(iobuf));
611         cleanup_phase = 1;
612
613         fso.fso_dentry = res->dentry;
614         fso.fso_bufcnt = obj->ioo_bufcnt;
615
616         iobuf->dr_ignore_quota = 0;
617         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
618                 loff_t this_size;
619                 __u32 flags = lnb->flags;
620
621                 if (filter_range_is_mapped(inode, lnb->offset, lnb->len)) {
622                         /* If overwriting an existing block,
623                          * we don't need a grant */
624                         if (!(flags & OBD_BRW_GRANTED) && lnb->rc == -ENOSPC)
625                                 lnb->rc = 0;
626                 } else {
627                         quota_pages++;
628                 }
629
630                 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
631                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
632                         continue;
633                 }
634
635                 LASSERT(PageLocked(lnb->page));
636                 LASSERT(!PageWriteback(lnb->page));
637
638                 /* since write & truncate are serialized by the i_alloc_sem,
639                  * even partial truncate should not leave dirty pages in
640                  * the page cache */
641                 LASSERT(!PageDirty(lnb->page));
642
643                 SetPageUptodate(lnb->page);
644
645                 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
646                 LASSERT (err == 0);
647
648                 total_size += lnb->len;
649
650                 /* we expect these pages to be in offset order, but we'll
651                  * be forgiving */
652                 this_size = lnb->offset + lnb->len;
653                 if (this_size > iattr.ia_size)
654                         iattr.ia_size = this_size;
655
656                 /* if one page is a write-back page from client cache and
657                  * not from direct_io, or it's written by root, then mark
658                  * the whole io request as ignore quota request, remote
659                  * client can not break through quota. */
660                 if (exp_connect_rmtclient(exp))
661                         flags &= ~OBD_BRW_NOQUOTA;
662                 if ((flags & OBD_BRW_NOQUOTA) ||
663                     (flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
664                      OBD_BRW_FROM_GRANT)
665                         iobuf->dr_ignore_quota = 1;
666
667                 if (!(lnb->flags & OBD_BRW_ASYNC)) {
668                         sync_journal_commit = 1;
669                 }
670         }
671
672         /* we try to get enough quota to write here, and let ldiskfs
673          * decide if it is out of quota or not b=14783 */
674         rc = lquota_chkquota(filter_quota_interface_ref, obd, exp, qcids,
675                              rec_pending, quota_pages, oti, LQUOTA_FLAGS_BLK,
676                              (void *)inode, obj->ioo_bufcnt);
677         if (rc == -ENOTCONN)
678                 GOTO(cleanup, rc);
679
680         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
681                 GOTO(cleanup, rc = -EINPROGRESS);
682
683         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
684         cleanup_phase = 2;
685
686         fsfilt_check_slow(obd, now, "quota init");
687
688 retry:
689         mutex_lock(&inode->i_mutex);
690         fsfilt_check_slow(obd, now, "i_mutex");
691         oti->oti_handle = fsfilt_brw_start(obd, objcount, &fso, niocount, res,
692                                            oti);
693         if (IS_ERR(oti->oti_handle)) {
694                 mutex_unlock(&inode->i_mutex);
695                 rc = PTR_ERR(oti->oti_handle);
696                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
697                        "error starting transaction: rc = %d\n", rc);
698                 oti->oti_handle = NULL;
699                 GOTO(cleanup, rc);
700         }
701         /* have to call fsfilt_commit() from this point on */
702
703         fsfilt_check_slow(obd, now, "brw_start");
704
705         /* Locking order: i_mutex -> journal_lock -> dqptr_sem. LU-952 */
706         ll_vfs_dq_init(inode);
707
708         i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
709
710         /* If the inode still has SUID+SGID bits set (see filter_precreate())
711          * then we will accept the UID+GID if sent by the client for
712          * initializing the ownership of this inode.  We only allow this to
713          * happen once (so clear these bits) and later only allow setattr. */
714         if (inode->i_mode & S_ISUID)
715                 i |= OBD_MD_FLUID;
716         if (inode->i_mode & S_ISGID)
717                 i |= OBD_MD_FLGID;
718
719         iattr_from_obdo(&iattr, oa, i);
720         if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
721                 unsigned int save;
722
723                 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
724                        (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
725
726                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
727
728                 iattr.ia_valid |= ATTR_MODE;
729                 iattr.ia_mode = inode->i_mode;
730                 if (iattr.ia_valid & ATTR_UID)
731                         iattr.ia_mode &= ~S_ISUID;
732                 if (iattr.ia_valid & ATTR_GID)
733                         iattr.ia_mode &= ~S_ISGID;
734
735                 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
736
737                 /* To avoid problems with quotas, UID and GID must be set
738                  * in the inode before filter_direct_io() - see bug 10357. */
739                 save = iattr.ia_valid;
740                 iattr.ia_valid &= (ATTR_UID | ATTR_GID);
741                 rc = fsfilt_setattr(obd, res->dentry, oti->oti_handle,&iattr,0);
742                 CDEBUG(D_QUOTA, "set uid(%u)/gid(%u) to ino(%lu). rc(%d)\n",
743                                 iattr.ia_uid, iattr.ia_gid, inode->i_ino, rc);
744                 iattr.ia_valid = save & ~(ATTR_UID | ATTR_GID);
745         }
746
747         CDEBUG(D_INODE, "FID "DFID" to write: s="LPU64" m="LPU64" a="LPU64
748                         " c="LPU64" b="LPU64"\n",
749                         oa->o_id, (__u32)oa->o_seq, (__u32)oa->o_seq,
750                         oa->o_size, oa->o_mtime, oa->o_atime, oa->o_ctime,
751                         oa->o_blocks);
752         /* filter_direct_io drops i_mutex */
753         rc = filter_direct_io(OBD_BRW_WRITE, res->dentry, iobuf, exp, &iattr,
754                               oti, sync_journal_commit ? &wait_handle : NULL);
755         if (rc == -ENOSPC && retries++ < 3) {
756                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
757                        retries);
758                 oti->oti_handle = NULL;
759                 fsfilt_check_slow(obd, now, "direct_io");
760                 goto retry;
761         }
762
763         obdo_from_inode(oa, inode, (rc == 0 ? FILTER_VALID_FLAGS : 0) |
764                                    OBD_MD_FLUID | OBD_MD_FLGID);
765
766         CDEBUG(D_INODE, "FID "DFID" after write: s="LPU64" m="LPU64" a="LPU64
767                         " c="LPU64" b="LPU64"\n",
768                         oa->o_id, (__u32)oa->o_seq, (__u32)oa->o_seq,
769                         oa->o_size, oa->o_mtime, oa->o_atime, oa->o_ctime,
770                         oa->o_blocks);
771         lquota_getflag(filter_quota_interface_ref, obd, oa);
772
773         fsfilt_check_slow(obd, now, "direct_io");
774
775         if (wait_handle)
776                 err = fsfilt_commit_wait(obd, inode, wait_handle);
777         else
778                 err = 0;
779
780         if (err) {
781                 CERROR("Failure to commit OST transaction (%d)?\n", err);
782                 if (rc == 0)
783                         rc = err;
784         }
785
786         /* In rare cases fsfilt_commit_wait() will wake up and return after
787          * the transaction has finished its work and updated j_commit_sequence
788          * but the commit callbacks have not been run yet.  Wait here until
789          * that is finished so that clients requesting sync IO don't see the
790          * reply transno < last_committed.  LU-753 */
791         if (unlikely(obd->obd_replayable && !rc && wait_handle &&
792                      oti->oti_transno > obd->obd_last_committed)) {
793                 cfs_waitq_t wq;
794                 struct l_wait_info lwi =
795                         LWI_TIMEOUT_INTERVAL(cfs_time_seconds(5),
796                                              (cfs_duration_t)((HZ + 4)/5),
797                                              NULL, NULL);
798                 cfs_waitq_init(&wq);
799                 l_wait_event(wq,
800                              oti->oti_transno <= obd->obd_last_committed,
801                              &lwi);
802
803                 /* commit callback isn't done after waiting for 5 secs ? */
804                 if (unlikely(oti->oti_transno > obd->obd_last_committed))
805                         CERROR("transno:"LPU64" > last_committed:"LPU64"\n",
806                                oti->oti_transno, obd->obd_last_committed);
807         }
808
809         fsfilt_check_slow(obd, now, "commitrw commit");
810
811 cleanup:
812         lquota_pending_commit(filter_quota_interface_ref, obd, qcids,
813                               rec_pending, 1);
814
815         filter_grant_commit(exp, niocount, res);
816
817         switch (cleanup_phase) {
818         case 2:
819                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
820                 LASSERT(current->journal_info == NULL);
821         case 1:
822                 filter_iobuf_put(&obd->u.filter, iobuf, oti);
823         case 0:
824                 /*
825                  * lnb->page automatically returns back into per-thread page
826                  * pool (bug 5137)
827                  */
828                  break;
829         }
830
831         /* trigger quota pre-acquire */
832         err = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
833                             FSFILT_OP_CREATE);
834         CDEBUG(err ? D_ERROR : D_QUOTA, "filter adjust qunit! "
835                "(rc:%d, uid:%u, gid:%u)\n",
836                err, qcids[USRQUOTA], qcids[GRPQUOTA]);
837         if (qcids[USRQUOTA] != oa->o_uid || qcids[GRPQUOTA] != oa->o_gid) {
838                 qcids[USRQUOTA] = oa->o_uid;
839                 qcids[GRPQUOTA] = oa->o_gid;
840                 err = lquota_adjust(filter_quota_interface_ref, obd, qcids,
841                                     NULL, rc, FSFILT_OP_CREATE);
842                 CDEBUG(err ? D_ERROR : D_QUOTA, "filter adjust qunit! "
843                        "(rc:%d, uid:%u, gid:%u)\n",
844                        err, qcids[USRQUOTA], qcids[GRPQUOTA]);
845         }
846
847         for (i = 0, lnb = res; i < niocount; i++, lnb++) {
848                 if (lnb->page == NULL)
849                         continue;
850
851                 if (rc)
852                         /* If the write has failed, the page cache may
853                          * not be consitent with what is on disk, so
854                          * force pages to be reread next time it is
855                          * accessed */
856                         ClearPageUptodate(lnb->page);
857
858                 LASSERT(PageLocked(lnb->page));
859                 unlock_page(lnb->page);
860
861                 page_cache_release(lnb->page);
862                 lnb->page = NULL;
863         }
864         f_dput(res->dentry);
865
866         if (inode) {
867                 if (fo->fo_writethrough_cache == 0 ||
868                     i_size_read(inode) > fo->fo_readcache_max_filesize)
869                         filter_release_cache(obd, obj, nb, inode);
870                 up_read(&inode->i_alloc_sem);
871         }
872
873         RETURN(rc);
874 }