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