4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45 /* LUSTRE_VERSION_CODE */
46 #include <lustre_ver.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/types.h>
49 /* prerequisite for linux/xattr.h */
53 * struct OBD_{ALLOC,FREE}*()
56 #include <obd_support.h>
58 #include "osd_internal.h"
61 #include <ldiskfs/ldiskfs_extents.h>
63 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
64 int rw, int line, int pages)
68 LASSERTF(iobuf->dr_elapsed_valid == 0,
69 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
70 cfs_atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
72 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
74 init_waitqueue_head(&iobuf->dr_wait);
75 cfs_atomic_set(&iobuf->dr_numreqs, 0);
80 iobuf->dr_elapsed = 0;
81 /* must be counted before, so assert */
83 iobuf->dr_init_at = line;
85 blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
86 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
87 LASSERT(iobuf->dr_pg_buf.lb_len >=
88 pages * sizeof(iobuf->dr_pages[0]));
92 /* start with 1MB for 4K blocks */
94 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
97 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
98 (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
100 blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
101 iobuf->dr_max_pages = 0;
102 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
103 (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
105 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
106 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
107 if (unlikely(iobuf->dr_blocks == NULL))
110 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
111 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
112 if (unlikely(iobuf->dr_pages == NULL))
115 iobuf->dr_max_pages = pages;
119 #define osd_init_iobuf(dev, iobuf, rw, pages) \
120 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
122 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
124 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
125 iobuf->dr_pages[iobuf->dr_npages++] = page;
128 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
130 int rw = iobuf->dr_rw;
132 if (iobuf->dr_elapsed_valid) {
133 iobuf->dr_elapsed_valid = 0;
134 LASSERT(iobuf->dr_dev == d);
135 LASSERT(iobuf->dr_frags > 0);
136 lprocfs_oh_tally(&d->od_brw_stats.
137 hist[BRW_R_DIO_FRAGS+rw],
139 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
144 #ifndef REQ_WRITE /* pre-2.6.35 */
145 #define __REQ_WRITE BIO_RW
148 #ifdef HAVE_BIO_ENDIO_2ARG
149 #define DIO_RETURN(a)
150 static void dio_complete_routine(struct bio *bio, int error)
152 #define DIO_RETURN(a) return(a)
153 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
156 struct osd_iobuf *iobuf = bio->bi_private;
160 /* CAVEAT EMPTOR: possibly in IRQ context
161 * DO NOT record procfs stats here!!! */
163 if (unlikely(iobuf == NULL)) {
164 CERROR("***** bio->bi_private is NULL! This should never "
165 "happen. Normally, I would crash here, but instead I "
166 "will dump the bio contents to the console. Please "
167 "report this to <http://jira.whamcloud.com/> , along "
168 "with any interesting messages leading up to this point "
169 "(like SCSI errors, perhaps). Because bi_private is "
170 "NULL, I can't wake up the thread that initiated this "
171 "IO - you will probably have to reboot this node.\n");
172 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
173 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
174 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
175 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
176 bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
181 /* the check is outside of the cycle for performance reason -bzzz */
182 if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
183 bio_for_each_segment(bvl, bio, i) {
184 if (likely(error == 0))
185 SetPageUptodate(bvl->bv_page);
186 LASSERT(PageLocked(bvl->bv_page));
188 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
190 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
193 /* any real error is good enough -bzzz */
194 if (error != 0 && iobuf->dr_error == 0)
195 iobuf->dr_error = error;
198 * set dr_elapsed before dr_numreqs turns to 0, otherwise
199 * it's possible that service thread will see dr_numreqs
200 * is zero, but dr_elapsed is not set yet, leading to lost
201 * data in this processing and an assertion in a subsequent
204 if (cfs_atomic_read(&iobuf->dr_numreqs) == 1) {
205 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
206 iobuf->dr_elapsed_valid = 1;
208 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
209 wake_up(&iobuf->dr_wait);
211 /* Completed bios used to be chained off iobuf->dr_bios and freed in
212 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
213 * mempool when serious on-disk fragmentation was encountered,
214 * deadlocking the OST. The bios are now released as soon as complete
215 * so the pool cannot be exhausted while IOs are competing. bug 10076 */
220 static void record_start_io(struct osd_iobuf *iobuf, int size)
222 struct osd_device *osd = iobuf->dr_dev;
223 struct obd_histogram *h = osd->od_brw_stats.hist;
226 cfs_atomic_inc(&iobuf->dr_numreqs);
228 if (iobuf->dr_rw == 0) {
229 cfs_atomic_inc(&osd->od_r_in_flight);
230 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
231 cfs_atomic_read(&osd->od_r_in_flight));
232 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
233 } else if (iobuf->dr_rw == 1) {
234 cfs_atomic_inc(&osd->od_w_in_flight);
235 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
236 cfs_atomic_read(&osd->od_w_in_flight));
237 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
243 static void osd_submit_bio(int rw, struct bio *bio)
245 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
247 submit_bio(READ, bio);
249 submit_bio(WRITE, bio);
252 static int can_be_merged(struct bio *bio, sector_t sector)
259 size = bio->bi_size >> 9;
260 return bio->bi_sector + size == sector ? 1 : 0;
263 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
264 struct osd_iobuf *iobuf)
266 int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
267 struct page **pages = iobuf->dr_pages;
268 int npages = iobuf->dr_npages;
269 unsigned long *blocks = iobuf->dr_blocks;
270 int total_blocks = npages * blocks_per_page;
271 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
272 unsigned int blocksize = inode->i_sb->s_blocksize;
273 struct bio *bio = NULL;
275 unsigned int page_offset;
284 LASSERT(iobuf->dr_npages == npages);
286 osd_brw_stats_update(osd, iobuf);
287 iobuf->dr_start_time = cfs_time_current();
289 for (page_idx = 0, block_idx = 0;
291 page_idx++, block_idx += blocks_per_page) {
293 page = pages[page_idx];
294 LASSERT(block_idx + blocks_per_page <= total_blocks);
296 for (i = 0, page_offset = 0;
298 i += nblocks, page_offset += blocksize * nblocks) {
302 if (blocks[block_idx + i] == 0) { /* hole */
303 LASSERTF(iobuf->dr_rw == 0,
304 "page_idx %u, block_idx %u, i %u\n",
305 page_idx, block_idx, i);
306 memset(kmap(page) + page_offset, 0, blocksize);
311 sector = (sector_t)blocks[block_idx + i] << sector_bits;
313 /* Additional contiguous file blocks? */
314 while (i + nblocks < blocks_per_page &&
315 (sector + (nblocks << sector_bits)) ==
316 ((sector_t)blocks[block_idx + i + nblocks] <<
321 can_be_merged(bio, sector) &&
322 bio_add_page(bio, page,
323 blocksize * nblocks, page_offset) != 0)
324 continue; /* added this frag OK */
327 struct request_queue *q =
328 bdev_get_queue(bio->bi_bdev);
330 /* Dang! I have to fragment this I/O */
331 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
332 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
334 bio->bi_vcnt, bio->bi_max_vecs,
335 bio->bi_size >> 9, queue_max_sectors(q),
336 bio_phys_segments(q, bio),
337 queue_max_phys_segments(q),
338 bio_hw_segments(q, bio),
339 queue_max_hw_segments(q));
341 record_start_io(iobuf, bio->bi_size);
342 osd_submit_bio(iobuf->dr_rw, bio);
345 /* allocate new bio */
346 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
347 (npages - page_idx) *
350 CERROR("Can't allocate bio %u*%u = %u pages\n",
351 (npages - page_idx), blocks_per_page,
352 (npages - page_idx) * blocks_per_page);
357 bio->bi_bdev = inode->i_sb->s_bdev;
358 bio->bi_sector = sector;
359 bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
360 bio->bi_end_io = dio_complete_routine;
361 bio->bi_private = iobuf;
363 rc = bio_add_page(bio, page,
364 blocksize * nblocks, page_offset);
370 record_start_io(iobuf, bio->bi_size);
371 osd_submit_bio(iobuf->dr_rw, bio);
376 /* in order to achieve better IO throughput, we don't wait for writes
377 * completion here. instead we proceed with transaction commit in
378 * parallel and wait for IO completion once transaction is stopped
379 * see osd_trans_stop() for more details -bzzz */
380 if (iobuf->dr_rw == 0) {
381 wait_event(iobuf->dr_wait,
382 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
386 rc = iobuf->dr_error;
390 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
391 struct niobuf_local *lnb)
398 int poff = offset & (PAGE_CACHE_SIZE - 1);
399 int plen = PAGE_CACHE_SIZE - poff;
403 lnb->lnb_file_offset = offset;
404 lnb->lnb_page_offset = poff;
406 /* lb->flags = rnb->flags; */
411 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
422 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
424 struct inode *inode = osd_dt_obj(dt)->oo_inode;
425 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
430 page = find_or_create_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
431 GFP_NOFS | __GFP_HIGHMEM);
432 if (unlikely(page == NULL))
433 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
439 * there are following "locks":
456 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
457 ssize_t len, struct niobuf_local *lnb, int rw,
458 struct lustre_capa *capa)
460 struct osd_object *obj = osd_dt_obj(d);
461 int npages, i, rc = 0;
463 LASSERT(obj->oo_inode);
465 osd_map_remote_to_local(pos, len, &npages, lnb);
467 for (i = 0; i < npages; i++, lnb++) {
469 /* We still set up for ungranted pages so that granted pages
470 * can be written to disk as they were promised, and portals
471 * needs to keep the pages all aligned properly. */
472 lnb->dentry = (void *) obj;
474 lnb->page = osd_get_page(d, lnb->lnb_file_offset, rw);
475 if (lnb->page == NULL)
476 GOTO(cleanup, rc = -ENOMEM);
478 /* DLM locking protects us from write and truncate competing
479 * for same region, but truncate can leave dirty page in the
480 * cache. it's possible the writeout on a such a page is in
481 * progress when we access it. it's also possible that during
482 * this writeout we put new (partial) data, but then won't
483 * be able to proceed in filter_commitrw_write(). thus let's
484 * just wait for writeout completion, should be rare enough.
486 wait_on_page_writeback(lnb->page);
487 BUG_ON(PageWriteback(lnb->page));
489 lu_object_get(&d->do_lu);
497 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
498 struct niobuf_local *lnb, int npages)
500 struct osd_thread_info *oti = osd_oti_get(env);
501 struct osd_iobuf *iobuf = &oti->oti_iobuf;
502 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
505 /* to do IO stats, notice we do this here because
506 * osd_do_bio() doesn't wait for write to complete */
507 osd_fini_iobuf(d, iobuf);
509 for (i = 0; i < npages; i++) {
510 if (lnb[i].page == NULL)
512 LASSERT(PageLocked(lnb[i].page));
513 unlock_page(lnb[i].page);
514 page_cache_release(lnb[i].page);
515 lu_object_put(env, &dt->do_lu);
521 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
522 struct niobuf_local *lnb, int npages)
524 struct osd_thread_info *oti = osd_oti_get(env);
525 struct osd_iobuf *iobuf = &oti->oti_iobuf;
526 struct inode *inode = osd_dt_obj(dt)->oo_inode;
527 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
528 struct timeval start;
530 unsigned long timediff;
539 rc = osd_init_iobuf(osd, iobuf, 0, npages);
540 if (unlikely(rc != 0))
543 isize = i_size_read(inode);
544 maxidx = ((isize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 1;
546 if (osd->od_writethrough_cache)
548 if (isize > osd->od_readcache_max_filesize)
551 do_gettimeofday(&start);
552 for (i = 0; i < npages; i++) {
555 generic_error_remove_page(inode->i_mapping,
559 * till commit the content of the page is undefined
560 * we'll set it uptodate once bulk is done. otherwise
561 * subsequent reads can access non-stable data
563 ClearPageUptodate(lnb[i].page);
565 if (lnb[i].len == PAGE_CACHE_SIZE)
568 if (maxidx >= lnb[i].page->index) {
569 osd_iobuf_add_page(iobuf, lnb[i].page);
572 char *p = kmap(lnb[i].page);
574 off = lnb[i].lnb_page_offset;
577 off = (lnb[i].lnb_page_offset + lnb[i].len) &
580 memset(p + off, 0, PAGE_CACHE_SIZE - off);
584 do_gettimeofday(&end);
585 timediff = cfs_timeval_sub(&end, &start, NULL);
586 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
588 if (iobuf->dr_npages) {
589 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
593 if (likely(rc == 0)) {
594 rc = osd_do_bio(osd, inode, iobuf);
595 /* do IO stats for preparation reads */
596 osd_fini_iobuf(osd, iobuf);
602 /* Check if a block is allocated or not */
603 static int osd_is_mapped(struct inode *inode, obd_size offset)
605 sector_t (*fs_bmap)(struct address_space *, sector_t);
607 fs_bmap = inode->i_mapping->a_ops->bmap;
609 /* We can't know if we are overwriting or not */
610 if (unlikely(fs_bmap == NULL))
613 if (i_size_read(inode) == 0)
616 /* Beyond EOF, must not be mapped */
617 if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
618 (offset >> inode->i_blkbits))
621 if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
627 static int osd_declare_write_commit(const struct lu_env *env,
628 struct dt_object *dt,
629 struct niobuf_local *lnb, int npages,
630 struct thandle *handle)
632 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
633 struct inode *inode = osd_dt_obj(dt)->oo_inode;
634 struct osd_thandle *oh;
641 bool ignore_quota = false;
642 long long quota_space = 0;
645 LASSERT(handle != NULL);
646 oh = container_of0(handle, struct osd_thandle, ot_super);
647 LASSERT(oh->ot_handle == NULL);
651 /* calculate number of extents (probably better to pass nb) */
652 for (i = 0; i < npages; i++) {
653 if (i && lnb[i].lnb_file_offset !=
654 lnb[i - 1].lnb_file_offset + lnb[i - 1].len)
657 if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
658 quota_space += PAGE_CACHE_SIZE;
660 /* ignore quota for the whole request if any page is from
661 * client cache or written by root.
663 * XXX once we drop the 1.8 client support, the checking
664 * for whether page is from cache can be simplified as:
665 * !(lnb[i].flags & OBD_BRW_SYNC)
667 * XXX we could handle this on per-lnb basis as done by
669 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
670 (lnb[i].flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
676 * each extent can go into new leaf causing a split
677 * 5 is max tree depth: inode + 4 index blocks
678 * with blockmaps, depth is 3 at most
680 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
682 * many concurrent threads may grow tree by the time
683 * our transaction starts. so, consider 2 is a min depth
685 depth = ext_depth(inode);
686 depth = max(depth, 1) + 1;
688 oh->ot_credits++; /* inode */
689 oh->ot_credits += depth * 2 * extents;
693 oh->ot_credits++; /* inode */
694 oh->ot_credits += depth * extents;
697 /* quota space for metadata blocks */
698 quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
700 /* quota space should be reported in 1K blocks */
701 quota_space = toqb(quota_space);
703 /* each new block can go in different group (bitmap + gd) */
705 /* we can't dirty more bitmap blocks than exist */
706 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
707 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
709 oh->ot_credits += newblocks;
711 /* we can't dirty more gd blocks than exist */
712 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
713 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
715 oh->ot_credits += newblocks;
717 /* make sure the over quota flags were not set */
718 lnb[0].flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
720 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid,
721 quota_space, oh, true, true, &flags,
724 /* we need only to store the overquota flags in the first lnb for
725 * now, once we support multiple objects BRW, this code needs be
727 if (flags & QUOTA_FL_OVER_USRQUOTA)
728 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
729 if (flags & QUOTA_FL_OVER_GRPQUOTA)
730 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
735 /* Check if a block is allocated or not */
736 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
737 struct niobuf_local *lnb, int npages,
738 struct thandle *thandle)
740 struct osd_thread_info *oti = osd_oti_get(env);
741 struct osd_iobuf *iobuf = &oti->oti_iobuf;
742 struct inode *inode = osd_dt_obj(dt)->oo_inode;
743 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
749 rc = osd_init_iobuf(osd, iobuf, 1, npages);
750 if (unlikely(rc != 0))
753 isize = i_size_read(inode);
754 ll_vfs_dq_init(inode);
756 for (i = 0; i < npages; i++) {
757 if (lnb[i].rc == -ENOSPC &&
758 osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
759 /* Allow the write to proceed if overwriting an
764 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
765 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
767 LASSERT(lnb[i].page);
768 generic_error_remove_page(inode->i_mapping,lnb[i].page);
772 LASSERT(PageLocked(lnb[i].page));
773 LASSERT(!PageWriteback(lnb[i].page));
775 if (lnb[i].lnb_file_offset + lnb[i].len > isize)
776 isize = lnb[i].lnb_file_offset + lnb[i].len;
779 * Since write and truncate are serialized by oo_sem, even
780 * partial-page truncate should not leave dirty pages in the
783 LASSERT(!PageDirty(lnb[i].page));
785 SetPageUptodate(lnb[i].page);
787 osd_iobuf_add_page(iobuf, lnb[i].page);
790 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
792 } else if (iobuf->dr_npages > 0) {
793 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
798 /* no pages to write, no transno is needed */
799 thandle->th_local = 1;
802 if (likely(rc == 0)) {
803 if (isize > i_size_read(inode)) {
804 i_size_write(inode, isize);
805 LDISKFS_I(inode)->i_disksize = isize;
806 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
809 rc = osd_do_bio(osd, inode, iobuf);
810 /* we don't do stats here as in read path because
811 * write is async: we'll do this in osd_put_bufs() */
814 if (unlikely(rc != 0)) {
815 /* if write fails, we should drop pages from the cache */
816 for (i = 0; i < npages; i++) {
817 if (lnb[i].page == NULL)
819 LASSERT(PageLocked(lnb[i].page));
820 generic_error_remove_page(inode->i_mapping,lnb[i].page);
827 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
828 struct niobuf_local *lnb, int npages)
830 struct osd_thread_info *oti = osd_oti_get(env);
831 struct osd_iobuf *iobuf = &oti->oti_iobuf;
832 struct inode *inode = osd_dt_obj(dt)->oo_inode;
833 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
834 struct timeval start, end;
835 unsigned long timediff;
836 int rc = 0, i, m = 0, cache = 0;
840 rc = osd_init_iobuf(osd, iobuf, 0, npages);
841 if (unlikely(rc != 0))
844 if (osd->od_read_cache)
846 if (i_size_read(inode) > osd->od_readcache_max_filesize)
849 do_gettimeofday(&start);
850 for (i = 0; i < npages; i++) {
852 if (i_size_read(inode) <= lnb[i].lnb_file_offset)
853 /* If there's no more data, abort early.
854 * lnb->rc == 0, so it's easy to detect later. */
857 if (i_size_read(inode) <
858 lnb[i].lnb_file_offset + lnb[i].len - 1)
859 lnb[i].rc = i_size_read(inode) - lnb[i].lnb_file_offset;
861 lnb[i].rc = lnb[i].len;
864 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
865 if (PageUptodate(lnb[i].page)) {
866 lprocfs_counter_add(osd->od_stats,
867 LPROC_OSD_CACHE_HIT, 1);
869 lprocfs_counter_add(osd->od_stats,
870 LPROC_OSD_CACHE_MISS, 1);
871 osd_iobuf_add_page(iobuf, lnb[i].page);
874 generic_error_remove_page(inode->i_mapping,lnb[i].page);
876 do_gettimeofday(&end);
877 timediff = cfs_timeval_sub(&end, &start, NULL);
878 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
880 if (iobuf->dr_npages) {
881 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
885 rc = osd_do_bio(osd, inode, iobuf);
887 /* IO stats will be done in osd_bufs_put() */
894 * XXX: Another layering violation for now.
896 * We don't want to use ->f_op->read methods, because generic file write
898 * - serializes on ->i_sem, and
900 * - does a lot of extra work like balance_dirty_pages(),
902 * which doesn't work for globally shared files like /last_rcvd.
904 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
906 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
908 memcpy(buffer, (char *)ei->i_data, buflen);
913 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
915 struct buffer_head *bh;
923 /* prevent reading after eof */
924 spin_lock(&inode->i_lock);
925 if (i_size_read(inode) < *offs + size) {
926 loff_t diff = i_size_read(inode) - *offs;
927 spin_unlock(&inode->i_lock);
929 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
930 i_size_read(inode), *offs);
932 } else if (diff == 0) {
938 spin_unlock(&inode->i_lock);
941 blocksize = 1 << inode->i_blkbits;
944 block = *offs >> inode->i_blkbits;
945 boffs = *offs & (blocksize - 1);
946 csize = min(blocksize - boffs, size);
947 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
949 CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
950 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
951 csize, *offs, inode->i_ino, err);
955 memcpy(buf, bh->b_data + boffs, csize);
965 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
966 struct lu_buf *buf, loff_t *pos,
967 struct lustre_capa *capa)
969 struct inode *inode = osd_dt_obj(dt)->oo_inode;
972 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
975 /* Read small symlink from inode body as we need to maintain correct
976 * on-disk symlinks for ldiskfs.
978 if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
979 (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
980 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
982 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
987 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
988 const loff_t size, loff_t pos,
989 struct thandle *handle)
991 struct osd_thandle *oh;
997 LASSERT(handle != NULL);
999 oh = container_of0(handle, struct osd_thandle, ot_super);
1000 LASSERT(oh->ot_handle == NULL);
1002 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
1004 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1006 inode = osd_dt_obj(dt)->oo_inode;
1008 /* we may declare write to non-exist llog */
1012 /* dt_declare_write() is usually called for system objects, such
1013 * as llog or last_rcvd files. We needn't enforce quota on those
1014 * objects, so always set the lqi_space as 0. */
1015 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1016 true, true, NULL, false);
1020 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1022 /* LU-2634: clear the extent format for fast symlink */
1023 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1025 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1026 LDISKFS_I(inode)->i_disksize = buflen;
1027 i_size_write(inode, buflen);
1028 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1033 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1034 int write_NUL, loff_t *offs, handle_t *handle)
1036 struct buffer_head *bh = NULL;
1037 loff_t offset = *offs;
1038 loff_t new_size = i_size_read(inode);
1039 unsigned long block;
1040 int blocksize = 1 << inode->i_blkbits;
1044 int dirty_inode = 0;
1048 * long symlink write does not count the NUL terminator in
1049 * bufsize, we write it, and the inode's file size does not
1050 * count the NUL terminator as well.
1052 ((char *)buf)[bufsize] = '\0';
1055 while (bufsize > 0) {
1059 block = offset >> inode->i_blkbits;
1060 boffs = offset & (blocksize - 1);
1061 size = min(blocksize - boffs, bufsize);
1062 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1064 CERROR("%s: error reading offset %llu (block %lu): "
1066 inode->i_sb->s_id, offset, block, err);
1070 err = ldiskfs_journal_get_write_access(handle, bh);
1072 CERROR("journal_get_write_access() returned error %d\n",
1076 LASSERTF(boffs + size <= bh->b_size,
1077 "boffs %d size %d bh->b_size %lu",
1078 boffs, size, (unsigned long)bh->b_size);
1079 memcpy(bh->b_data + boffs, buf, size);
1080 err = ldiskfs_journal_dirty_metadata(handle, bh);
1084 if (offset + size > new_size)
1085 new_size = offset + size;
1095 /* correct in-core and on-disk sizes */
1096 if (new_size > i_size_read(inode)) {
1097 spin_lock(&inode->i_lock);
1098 if (new_size > i_size_read(inode))
1099 i_size_write(inode, new_size);
1100 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1101 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1104 spin_unlock(&inode->i_lock);
1106 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1114 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1115 const struct lu_buf *buf, loff_t *pos,
1116 struct thandle *handle, struct lustre_capa *capa,
1119 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1120 struct osd_thandle *oh;
1124 LASSERT(dt_object_exists(dt));
1126 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1129 LASSERT(handle != NULL);
1130 LASSERT(inode != NULL);
1131 ll_vfs_dq_init(inode);
1133 /* XXX: don't check: one declared chunk can be used many times */
1134 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
1136 oh = container_of(handle, struct osd_thandle, ot_super);
1137 LASSERT(oh->ot_handle->h_transaction != NULL);
1138 /* Write small symlink to inode body as we need to maintain correct
1139 * on-disk symlinks for ldiskfs.
1140 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1141 * does not count it in.
1143 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1144 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1145 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1147 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1148 buf->lb_len, is_link, pos,
1151 result = buf->lb_len;
1155 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1156 __u64 start, __u64 end, struct thandle *th)
1158 struct osd_thandle *oh;
1159 struct inode *inode;
1164 oh = container_of(th, struct osd_thandle, ot_super);
1167 * we don't need to reserve credits for whole truncate
1168 * it's not possible as truncate may need to free too many
1169 * blocks and that won't fit a single transaction. instead
1170 * we reserve credits to change i_size and put inode onto
1171 * orphan list. if needed truncate will extend or restart
1174 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
1175 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
1177 inode = osd_dt_obj(dt)->oo_inode;
1180 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1181 true, true, NULL, false);
1185 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1186 __u64 start, __u64 end, struct thandle *th,
1187 struct lustre_capa *capa)
1189 struct osd_thandle *oh;
1190 struct osd_object *obj = osd_dt_obj(dt);
1191 struct inode *inode = obj->oo_inode;
1195 int rc = 0, rc2 = 0;
1198 LASSERT(end == OBD_OBJECT_EOF);
1199 LASSERT(dt_object_exists(dt));
1200 LASSERT(osd_invariant(obj));
1201 LASSERT(inode != NULL);
1202 ll_vfs_dq_init(inode);
1205 oh = container_of(th, struct osd_thandle, ot_super);
1206 LASSERT(oh->ot_handle->h_transaction != NULL);
1208 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
1210 tid = oh->ot_handle->h_transaction->t_tid;
1212 oldsize=inode->i_size;
1213 i_size_write(inode, start);
1214 truncate_pagecache(inode, oldsize, start);
1215 if (inode->i_op->truncate)
1216 inode->i_op->truncate(inode);
1219 * For a partial-page truncate, flush the page to disk immediately to
1220 * avoid data corruption during direct disk write. b=17397
1222 if ((start & ~CFS_PAGE_MASK) != 0)
1223 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1225 h = journal_current_handle();
1227 LASSERT(h == oh->ot_handle);
1229 if (tid != h->h_transaction->t_tid) {
1230 int credits = oh->ot_credits;
1232 * transaction has changed during truncate
1233 * we need to restart the handle with our credits
1235 if (h->h_buffer_credits < credits) {
1236 if (ldiskfs_journal_extend(h, credits))
1237 rc2 = ldiskfs_journal_restart(h, credits);
1241 RETURN(rc == 0 ? rc2 : rc);
1244 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1245 struct ll_user_fiemap *fm)
1247 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1248 struct osd_thread_info *info = osd_oti_get(env);
1249 struct dentry *dentry = &info->oti_obj_dentry;
1250 struct file *file = &info->oti_file;
1251 mm_segment_t saved_fs;
1255 dentry->d_inode = inode;
1256 dentry->d_sb = inode->i_sb;
1257 file->f_dentry = dentry;
1258 file->f_mapping = inode->i_mapping;
1259 file->f_op = inode->i_fop;
1261 saved_fs = get_fs();
1263 /* ldiskfs_ioctl does not have a inode argument */
1264 if (inode->i_fop->unlocked_ioctl)
1265 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1274 * in some cases we may need declare methods for objects being created
1275 * e.g., when we create symlink
1277 const struct dt_body_operations osd_body_ops_new = {
1278 .dbo_declare_write = osd_declare_write,
1281 const struct dt_body_operations osd_body_ops = {
1282 .dbo_read = osd_read,
1283 .dbo_declare_write = osd_declare_write,
1284 .dbo_write = osd_write,
1285 .dbo_bufs_get = osd_bufs_get,
1286 .dbo_bufs_put = osd_bufs_put,
1287 .dbo_write_prep = osd_write_prep,
1288 .dbo_declare_write_commit = osd_declare_write_commit,
1289 .dbo_write_commit = osd_write_commit,
1290 .dbo_read_prep = osd_read_prep,
1291 .dbo_declare_punch = osd_declare_punch,
1292 .dbo_punch = osd_punch,
1293 .dbo_fiemap_get = osd_fiemap_get,