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