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