1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/obdfilter/filter_io_26.c
41 * Author: Peter Braam <braam@clusterfs.com>
42 * Author: Andreas Dilger <adilger@clusterfs.com>
43 * Author: Phil Schwan <phil@clusterfs.com>
46 #ifndef AUTOCONF_INCLUDED
47 #include <linux/config.h>
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>
54 #define DEBUG_SUBSYSTEM S_FILTER
56 #include <obd_class.h>
57 #include <lustre_fsfilt.h>
58 #include <lustre_quota.h>
59 #include "filter_internal.h"
61 /* 512byte block min */
62 #define MAX_BLOCKS_PER_PAGE (CFS_PAGE_SIZE / 512)
64 cfs_atomic_t dr_numreqs; /* number of reqs being processed */
69 struct page **dr_pages;
70 unsigned long *dr_blocks;
71 unsigned int dr_ignore_quota:1;
72 struct filter_obd *dr_filter;
75 static void record_start_io(struct filter_iobuf *iobuf, int rw, int size,
76 struct obd_export *exp)
78 struct filter_obd *filter = iobuf->dr_filter;
80 cfs_atomic_inc(&iobuf->dr_numreqs);
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],
89 if (exp->exp_nid_stats && exp->exp_nid_stats->nid_brw_stats) {
90 lprocfs_oh_tally(&exp->exp_nid_stats->nid_brw_stats->
92 cfs_atomic_read(&filter-> \
94 lprocfs_oh_tally_log2(&exp->exp_nid_stats->
95 nid_brw_stats->hist[BRW_R_DISK_IOSIZE],
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],
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-> \
110 lprocfs_oh_tally_log2(&exp->exp_nid_stats->
111 nid_brw_stats->hist[BRW_W_DISK_IOSIZE],
117 static void record_finish_io(struct filter_iobuf *iobuf, int rw, int rc)
119 struct filter_obd *filter = iobuf->dr_filter;
121 /* CAVEAT EMPTOR: possibly in IRQ context
122 * DO NOT record procfs stats here!!! */
124 if (rw == OBD_BRW_READ)
125 cfs_atomic_dec(&filter->fo_r_in_flight);
127 cfs_atomic_dec(&filter->fo_w_in_flight);
129 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
130 cfs_waitq_signal(&iobuf->dr_wait);
133 #ifdef HAVE_BIO_ENDIO_2ARG
134 #define DIO_RETURN(a)
135 static void dio_complete_routine(struct bio *bio, int error)
137 #define DIO_RETURN(a) return(a)
138 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
141 struct filter_iobuf *iobuf = bio->bi_private;
145 /* CAVEAT EMPTOR: possibly in IRQ context
146 * DO NOT record procfs stats here!!! */
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 */
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),
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);
185 record_finish_io(iobuf, OBD_BRW_READ, error);
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);
192 record_finish_io(iobuf, OBD_BRW_WRITE, error);
195 /* any real error is good enough -bzzz */
196 if (error != 0 && iobuf->dr_error == 0)
197 iobuf->dr_error = error;
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 */
208 static int can_be_merged(struct bio *bio, sector_t sector)
215 size = bio->bi_size >> 9;
216 return bio->bi_sector + size == sector ? 1 : 0;
219 struct filter_iobuf *filter_alloc_iobuf(struct filter_obd *filter,
220 int rw, int num_pages)
222 struct filter_iobuf *iobuf;
224 LASSERTF(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ, "%x\n", rw);
226 OBD_ALLOC(iobuf, sizeof(*iobuf));
230 OBD_ALLOC(iobuf->dr_pages, num_pages * sizeof(*iobuf->dr_pages));
231 if (iobuf->dr_pages == NULL)
234 OBD_ALLOC(iobuf->dr_blocks,
235 MAX_BLOCKS_PER_PAGE * num_pages * sizeof(*iobuf->dr_blocks));
236 if (iobuf->dr_blocks == NULL)
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;
249 OBD_FREE(iobuf->dr_pages,
250 num_pages * sizeof(*iobuf->dr_pages));
252 OBD_FREE(iobuf, sizeof(*iobuf));
254 RETURN(ERR_PTR(-ENOMEM));
257 static void filter_clear_iobuf(struct filter_iobuf *iobuf)
259 iobuf->dr_npages = 0;
261 cfs_atomic_set(&iobuf->dr_numreqs, 0);
264 void filter_free_iobuf(struct filter_iobuf *iobuf)
266 int num_pages = iobuf->dr_max_pages;
268 filter_clear_iobuf(iobuf);
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));
277 void filter_iobuf_put(struct filter_obd *filter, struct filter_iobuf *iobuf,
278 struct obd_trans_info *oti)
280 int thread_id = (oti && oti->oti_thread) ?
281 oti->oti_thread->t_id : -1;
283 if (unlikely(thread_id < 0)) {
284 filter_free_iobuf(iobuf);
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);
294 int filter_iobuf_add_page(struct obd_device *obd, struct filter_iobuf *iobuf,
295 struct inode *inode, struct page *page)
297 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
298 iobuf->dr_pages[iobuf->dr_npages++] = page;
303 int filter_do_bio(struct obd_export *exp, struct inode *inode,
304 struct filter_iobuf *iobuf, int rw)
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;
316 unsigned long start_time = jiffies;
318 unsigned int page_offset;
327 LASSERT(iobuf->dr_npages == npages);
328 LASSERT(total_blocks <= OBDFILTER_CREATED_SCRATCHPAD_ENTRIES);
330 for (page_idx = 0, block_idx = 0;
332 page_idx++, block_idx += blocks_per_page) {
334 page = pages[page_idx];
335 LASSERT (block_idx + blocks_per_page <= total_blocks);
337 for (i = 0, page_offset = 0;
339 i += nblocks, page_offset += blocksize * nblocks) {
343 if (blocks[block_idx + i] == 0) { /* hole */
344 LASSERT(rw == OBD_BRW_READ);
345 memset(kmap(page) + page_offset, 0, blocksize);
350 sector = (sector_t)blocks[block_idx + i] << sector_bits;
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] <<
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.
364 if ((rw == OBD_BRW_WRITE) &&
365 (nblocks == blocks_per_page) &&
366 mapping_cap_page_constant_write(inode->i_mapping))
367 SetPageConstant(page);
370 can_be_merged(bio, sector) &&
371 bio_add_page(bio, page,
372 blocksize * nblocks, page_offset) != 0)
373 continue; /* added this frag OK */
376 struct request_queue *q =
377 bdev_get_queue(bio->bi_bdev);
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",
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);
393 record_start_io(iobuf, rw, bio->bi_size, exp);
394 rc = fsfilt_send_bio(rw, obd, inode, bio);
396 CERROR("Can't send bio: %d\n", rc);
397 record_finish_io(iobuf, rw, rc);
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) *
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);
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;
420 rc = bio_add_page(bio, page,
421 blocksize * nblocks, page_offset);
427 record_start_io(iobuf, rw, bio->bi_size, exp);
428 rc = fsfilt_send_bio(rw, obd, inode, bio);
433 CERROR("Can't send bio: %d\n", rc);
434 record_finish_io(iobuf, rw, rc);
439 cfs_wait_event(iobuf->dr_wait,
440 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
442 if (rw == OBD_BRW_READ) {
443 lprocfs_oh_tally(&obd->u.filter.fo_filter_stats.
444 hist[BRW_R_DIO_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],
453 lprocfs_oh_tally_log2(&exp->exp_nid_stats->
454 nid_brw_stats->hist[BRW_R_IO_TIME],
455 jiffies - start_time);
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.
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],
467 lprocfs_oh_tally_log2(&exp->exp_nid_stats->
468 nid_brw_stats->hist[BRW_W_IO_TIME],
469 jiffies - start_time);
474 rc = iobuf->dr_error;
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)
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;
487 cfs_semaphore_t *sem;
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);
494 if (rw == OBD_BRW_READ) {
495 if (iobuf->dr_npages == 0)
500 LASSERTF(rw == OBD_BRW_WRITE, "%x\n", rw);
501 LASSERT(iobuf->dr_npages > 0);
503 sem = &obd->u.filter.fo_alloc_lock;
505 lquota_enforce(filter_quota_interface_ref, obd,
506 iobuf->dr_ignore_quota);
509 if (rw == OBD_BRW_WRITE &&
510 OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
513 rc = fsfilt_map_inode_pages(obd, inode, iobuf->dr_pages,
514 iobuf->dr_npages, iobuf->dr_blocks,
515 obdfilter_created_scratchpad, create, sem);
518 if (rw == OBD_BRW_WRITE) {
520 filter_tally(exp, iobuf->dr_pages,
521 iobuf->dr_npages, iobuf->dr_blocks,
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);
529 UNLOCK_INODE_MUTEX(inode);
531 /* Force commit to make the just-deleted blocks
532 * reusable. LU-456 */
534 fsfilt_commit(obd, inode, oti->oti_handle, 1);
538 rc2 = filter_finish_transno(exp, inode, oti, 0, 0);
540 CERROR("can't close transaction: %d\n", rc2);
546 rc2 = fsfilt_commit_async(obd,inode,oti->oti_handle,
549 rc2 = fsfilt_commit(obd, inode, oti->oti_handle, 0);
554 } else if (rc == 0) {
555 filter_tally(exp, iobuf->dr_pages, iobuf->dr_npages,
556 iobuf->dr_blocks, blocks_per_page, 0);
559 RETURN(filter_do_bio(exp, inode, iobuf, rw));
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)
565 sector_t (*fs_bmap)(struct address_space *, sector_t) =
566 inode->i_mapping->a_ops->bmap;
569 /* We can't know if we are overwriting or not */
573 offset >>= inode->i_blkbits;
574 len >>= inode->i_blkbits;
576 for (j = 0; j <= len; j++)
577 if (fs_bmap(inode->i_mapping, offset + j) == 0)
584 * interesting use cases on how it interacts with VM:
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.
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,
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;
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;
618 LASSERT(oti != NULL);
619 LASSERT(objcount == 1);
620 LASSERT(current->journal_info == NULL);
625 iobuf = filter_iobuf_get(&obd->u.filter, oti);
627 GOTO(cleanup, rc = PTR_ERR(iobuf));
630 fso.fso_dentry = res->dentry;
631 fso.fso_bufcnt = obj->ioo_bufcnt;
633 iobuf->dr_ignore_quota = 0;
634 for (i = 0, lnb = res; i < niocount; i++, lnb++) {
636 __u32 flags = lnb->flags;
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)
647 if (lnb->rc) { /* ENOSPC, network RPC error, etc. */
648 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i, lnb->rc);
652 LASSERT(PageLocked(lnb->page));
653 LASSERT(!PageWriteback(lnb->page));
655 /* since write & truncate are serialized by the i_alloc_sem,
656 * even partial truncate should not leave dirty pages in
658 LASSERT(!PageDirty(lnb->page));
660 SetPageUptodate(lnb->page);
662 err = filter_iobuf_add_page(obd, iobuf, inode, lnb->page);
665 total_size += lnb->len;
667 /* we expect these pages to be in offset order, but we'll
669 this_size = lnb->offset + lnb->len;
670 if (this_size > iattr.ia_size)
671 iattr.ia_size = this_size;
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)) ==
682 iobuf->dr_ignore_quota = 1;
684 if (!(lnb->flags & OBD_BRW_ASYNC)) {
685 sync_journal_commit = 1;
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);
697 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
700 ll_vfs_dq_init(inode);
701 fsfilt_check_slow(obd, now, "quota init");
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,
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;
716 /* have to call fsfilt_commit() from this point on */
718 fsfilt_check_slow(obd, now, "brw_start");
720 i = OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
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)
728 if (inode->i_mode & S_ISGID)
731 iattr_from_obdo(&iattr, oa, i);
732 if (iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
735 CDEBUG(D_INODE, "update UID/GID to %lu/%lu\n",
736 (unsigned long)oa->o_uid, (unsigned long)oa->o_gid);
738 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
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;
747 rc = filter_update_fidea(exp, inode, oti->oti_handle, oa);
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);
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",
765 oti->oti_handle = NULL;
766 fsfilt_check_slow(obd, now, "direct_io");
770 obdo_from_inode(oa, inode, NULL, rc == 0 ? FILTER_VALID_FLAGS : 0 |
771 OBD_MD_FLUID |OBD_MD_FLGID);
773 lquota_getflag(filter_quota_interface_ref, obd, oa);
775 fsfilt_check_slow(obd, now, "direct_io");
778 err = fsfilt_commit_wait(obd, inode, wait_handle);
783 CERROR("Failure to commit OST transaction (%d)?\n", err);
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);
793 fsfilt_check_slow(obd, now, "commitrw commit");
796 lquota_pending_commit(filter_quota_interface_ref, obd, qcids,
799 filter_grant_commit(exp, niocount, res);
801 switch (cleanup_phase) {
803 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
804 LASSERT(current->journal_info == NULL);
806 filter_iobuf_put(&obd->u.filter, iobuf, oti);
809 * lnb->page automatically returns back into per-thread page
815 /* trigger quota pre-acquire */
816 err = lquota_adjust(filter_quota_interface_ref, obd, qcids, NULL, rc,
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]);
831 for (i = 0, lnb = res; i < niocount; i++, lnb++) {
832 if (lnb->page == NULL)
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
840 ClearPageUptodate(lnb->page);
842 LASSERT(PageLocked(lnb->page));
843 unlock_page(lnb->page);
845 page_cache_release(lnb->page);
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);