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