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