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) 2011, 2012, 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 #include <ldiskfs/ldiskfs.h>
54 #include <ldiskfs/ldiskfs_jbd2.h>
55 #include <ldiskfs/ldiskfs_extents.h>
58 * struct OBD_{ALLOC,FREE}*()
61 #include <obd_support.h>
63 #include "osd_internal.h"
65 #ifndef HAVE_PAGE_CONSTANT
66 #define mapping_cap_page_constant_write(mapping) 0
67 #define SetPageConstant(page) do {} while (0)
68 #define ClearPageConstant(page) do {} while (0)
71 #ifndef HAS_GENERIC_ERROR_REMOVE_PAGE
72 int generic_error_remove_page(struct address_space *mapping, struct page *page)
77 if (mapping != page->mapping)
80 * Only punch for normal data pages for now.
81 * Handling other types like directories would need more auditing.
83 if (!S_ISREG(mapping->host->i_mode))
86 if (page_mapped(page)) {
87 unmap_mapping_range(mapping,
88 (loff_t)page->index << PAGE_CACHE_SHIFT,
91 truncate_complete_page(mapping, page);
96 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
97 int rw, int line, int pages)
101 LASSERTF(iobuf->dr_elapsed_valid == 0,
102 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
103 cfs_atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
105 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
107 cfs_waitq_init(&iobuf->dr_wait);
108 cfs_atomic_set(&iobuf->dr_numreqs, 0);
109 iobuf->dr_npages = 0;
113 iobuf->dr_elapsed = 0;
114 /* must be counted before, so assert */
116 iobuf->dr_init_at = line;
118 blocks = pages * (CFS_PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
119 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
120 LASSERT(iobuf->dr_pg_buf.lb_len >=
121 pages * sizeof(iobuf->dr_pages[0]));
125 /* start with 1MB for 4K blocks */
127 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
130 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
131 (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
133 blocks = pages * (CFS_PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
134 iobuf->dr_max_pages = 0;
135 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
136 (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
138 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
139 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
140 if (unlikely(iobuf->dr_blocks == NULL))
143 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
144 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
145 if (unlikely(iobuf->dr_pages == NULL))
148 iobuf->dr_max_pages = pages;
152 #define osd_init_iobuf(dev, iobuf, rw, pages) \
153 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
155 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
157 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
158 iobuf->dr_pages[iobuf->dr_npages++] = page;
161 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
163 int rw = iobuf->dr_rw;
165 if (iobuf->dr_elapsed_valid) {
166 iobuf->dr_elapsed_valid = 0;
167 LASSERT(iobuf->dr_dev == d);
168 LASSERT(iobuf->dr_frags > 0);
169 lprocfs_oh_tally(&d->od_brw_stats.
170 hist[BRW_R_DIO_FRAGS+rw],
172 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
177 #ifndef __REQ_WRITE /* pre-2.6.35 */
178 #define __REQ_WRITE BIO_RW
181 #ifdef HAVE_BIO_ENDIO_2ARG
182 #define DIO_RETURN(a)
183 static void dio_complete_routine(struct bio *bio, int error)
185 #define DIO_RETURN(a) return(a)
186 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
189 struct osd_iobuf *iobuf = bio->bi_private;
193 /* CAVEAT EMPTOR: possibly in IRQ context
194 * DO NOT record procfs stats here!!! */
196 if (unlikely(iobuf == NULL)) {
197 CERROR("***** bio->bi_private is NULL! This should never "
198 "happen. Normally, I would crash here, but instead I "
199 "will dump the bio contents to the console. Please "
200 "report this to <http://jira.whamcloud.com/> , along "
201 "with any interesting messages leading up to this point "
202 "(like SCSI errors, perhaps). Because bi_private is "
203 "NULL, I can't wake up the thread that initiated this "
204 "IO - you will probably have to reboot this node.\n");
205 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
206 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
207 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
208 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
209 bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
214 /* the check is outside of the cycle for performance reason -bzzz */
215 if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
216 bio_for_each_segment(bvl, bio, i) {
217 if (likely(error == 0))
218 SetPageUptodate(bvl->bv_page);
219 LASSERT(PageLocked(bvl->bv_page));
220 ClearPageConstant(bvl->bv_page);
222 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
224 struct page *p = iobuf->dr_pages[0];
226 if (mapping_cap_page_constant_write(p->mapping)) {
227 bio_for_each_segment(bvl, bio, i) {
228 ClearPageConstant(bvl->bv_page);
232 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
235 /* any real error is good enough -bzzz */
236 if (error != 0 && iobuf->dr_error == 0)
237 iobuf->dr_error = error;
240 * set dr_elapsed before dr_numreqs turns to 0, otherwise
241 * it's possible that service thread will see dr_numreqs
242 * is zero, but dr_elapsed is not set yet, leading to lost
243 * data in this processing and an assertion in a subsequent
246 if (cfs_atomic_read(&iobuf->dr_numreqs) == 1) {
247 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
248 iobuf->dr_elapsed_valid = 1;
250 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
251 cfs_waitq_signal(&iobuf->dr_wait);
253 /* Completed bios used to be chained off iobuf->dr_bios and freed in
254 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
255 * mempool when serious on-disk fragmentation was encountered,
256 * deadlocking the OST. The bios are now released as soon as complete
257 * so the pool cannot be exhausted while IOs are competing. bug 10076 */
262 static void record_start_io(struct osd_iobuf *iobuf, int size)
264 struct osd_device *osd = iobuf->dr_dev;
265 struct obd_histogram *h = osd->od_brw_stats.hist;
268 cfs_atomic_inc(&iobuf->dr_numreqs);
270 if (iobuf->dr_rw == 0) {
271 cfs_atomic_inc(&osd->od_r_in_flight);
272 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
273 cfs_atomic_read(&osd->od_r_in_flight));
274 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
275 } else if (iobuf->dr_rw == 1) {
276 cfs_atomic_inc(&osd->od_w_in_flight);
277 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
278 cfs_atomic_read(&osd->od_w_in_flight));
279 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
285 static void osd_submit_bio(int rw, struct bio *bio)
287 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
289 submit_bio(READ, bio);
291 submit_bio(WRITE, bio);
294 static int can_be_merged(struct bio *bio, sector_t sector)
301 size = bio->bi_size >> 9;
302 return bio->bi_sector + size == sector ? 1 : 0;
305 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
306 struct osd_iobuf *iobuf)
308 int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
309 struct page **pages = iobuf->dr_pages;
310 int npages = iobuf->dr_npages;
311 unsigned long *blocks = iobuf->dr_blocks;
312 int total_blocks = npages * blocks_per_page;
313 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
314 unsigned int blocksize = inode->i_sb->s_blocksize;
315 struct bio *bio = NULL;
317 unsigned int page_offset;
326 LASSERT(iobuf->dr_npages == npages);
328 osd_brw_stats_update(osd, iobuf);
329 iobuf->dr_start_time = cfs_time_current();
331 for (page_idx = 0, block_idx = 0;
333 page_idx++, block_idx += blocks_per_page) {
335 page = pages[page_idx];
336 LASSERT(block_idx + blocks_per_page <= total_blocks);
338 for (i = 0, page_offset = 0;
340 i += nblocks, page_offset += blocksize * nblocks) {
344 if (blocks[block_idx + i] == 0) { /* hole */
345 LASSERTF(iobuf->dr_rw == 0,
346 "page_idx %u, block_idx %u, i %u\n",
347 page_idx, block_idx, i);
348 memset(kmap(page) + page_offset, 0, blocksize);
353 sector = (sector_t)blocks[block_idx + i] << sector_bits;
355 /* Additional contiguous file blocks? */
356 while (i + nblocks < blocks_per_page &&
357 (sector + (nblocks << sector_bits)) ==
358 ((sector_t)blocks[block_idx + i + nblocks] <<
362 /* I only set the page to be constant only if it
363 * is mapped to a contiguous underlying disk block(s).
364 * It will then make sure the corresponding device
365 * cache of raid5 will be overwritten by this page.
367 if (iobuf->dr_rw && (nblocks == blocks_per_page) &&
368 mapping_cap_page_constant_write(inode->i_mapping))
369 SetPageConstant(page);
372 can_be_merged(bio, sector) &&
373 bio_add_page(bio, page,
374 blocksize * nblocks, page_offset) != 0)
375 continue; /* added this frag OK */
378 struct request_queue *q =
379 bdev_get_queue(bio->bi_bdev);
381 /* Dang! I have to fragment this I/O */
382 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
383 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
385 bio->bi_vcnt, bio->bi_max_vecs,
386 bio->bi_size >> 9, queue_max_sectors(q),
387 bio_phys_segments(q, bio),
388 queue_max_phys_segments(q),
389 bio_hw_segments(q, bio),
390 queue_max_hw_segments(q));
392 record_start_io(iobuf, bio->bi_size);
393 osd_submit_bio(iobuf->dr_rw, bio);
396 /* allocate new bio */
397 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
398 (npages - page_idx) *
401 CERROR("Can't allocate bio %u*%u = %u pages\n",
402 (npages - page_idx), blocks_per_page,
403 (npages - page_idx) * blocks_per_page);
408 bio->bi_bdev = inode->i_sb->s_bdev;
409 bio->bi_sector = sector;
410 bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
411 bio->bi_end_io = dio_complete_routine;
412 bio->bi_private = iobuf;
414 rc = bio_add_page(bio, page,
415 blocksize * nblocks, page_offset);
421 record_start_io(iobuf, bio->bi_size);
422 osd_submit_bio(iobuf->dr_rw, bio);
427 /* in order to achieve better IO throughput, we don't wait for writes
428 * completion here. instead we proceed with transaction commit in
429 * parallel and wait for IO completion once transaction is stopped
430 * see osd_trans_stop() for more details -bzzz */
431 if (iobuf->dr_rw == 0) {
432 cfs_wait_event(iobuf->dr_wait,
433 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
437 rc = iobuf->dr_error;
441 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
442 struct niobuf_local *lnb)
449 int poff = offset & (CFS_PAGE_SIZE - 1);
450 int plen = CFS_PAGE_SIZE - poff;
454 lnb->lnb_file_offset = offset;
455 lnb->lnb_page_offset = poff;
457 /* lb->flags = rnb->flags; */
462 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
473 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
475 struct inode *inode = osd_dt_obj(dt)->oo_inode;
476 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
481 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
482 GFP_NOFS | __GFP_HIGHMEM);
483 if (unlikely(page == NULL))
484 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
490 * there are following "locks":
507 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
508 ssize_t len, struct niobuf_local *lnb, int rw,
509 struct lustre_capa *capa)
511 struct osd_object *obj = osd_dt_obj(d);
512 int npages, i, rc = 0;
514 LASSERT(obj->oo_inode);
516 osd_map_remote_to_local(pos, len, &npages, lnb);
518 for (i = 0; i < npages; i++, lnb++) {
520 /* We still set up for ungranted pages so that granted pages
521 * can be written to disk as they were promised, and portals
522 * needs to keep the pages all aligned properly. */
523 lnb->dentry = (void *) obj;
525 lnb->page = osd_get_page(d, lnb->lnb_file_offset, rw);
526 if (lnb->page == NULL)
527 GOTO(cleanup, rc = -ENOMEM);
529 /* DLM locking protects us from write and truncate competing
530 * for same region, but truncate can leave dirty page in the
531 * cache. it's possible the writeout on a such a page is in
532 * progress when we access it. it's also possible that during
533 * this writeout we put new (partial) data, but then won't
534 * be able to proceed in filter_commitrw_write(). thus let's
535 * just wait for writeout completion, should be rare enough.
537 wait_on_page_writeback(lnb->page);
538 BUG_ON(PageWriteback(lnb->page));
540 lu_object_get(&d->do_lu);
548 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
549 struct niobuf_local *lnb, int npages)
551 struct osd_thread_info *oti = osd_oti_get(env);
552 struct osd_iobuf *iobuf = &oti->oti_iobuf;
553 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
556 /* to do IO stats, notice we do this here because
557 * osd_do_bio() doesn't wait for write to complete */
558 osd_fini_iobuf(d, iobuf);
560 for (i = 0; i < npages; i++) {
561 if (lnb[i].page == NULL)
563 LASSERT(PageLocked(lnb[i].page));
564 unlock_page(lnb[i].page);
565 page_cache_release(lnb[i].page);
566 lu_object_put(env, &dt->do_lu);
572 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
573 struct niobuf_local *lnb, int npages)
575 struct osd_thread_info *oti = osd_oti_get(env);
576 struct osd_iobuf *iobuf = &oti->oti_iobuf;
577 struct inode *inode = osd_dt_obj(dt)->oo_inode;
578 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
579 struct timeval start;
581 unsigned long timediff;
590 rc = osd_init_iobuf(osd, iobuf, 0, npages);
591 if (unlikely(rc != 0))
594 isize = i_size_read(inode);
595 maxidx = ((isize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
597 if (osd->od_writethrough_cache)
599 if (isize > osd->od_readcache_max_filesize)
602 cfs_gettimeofday(&start);
603 for (i = 0; i < npages; i++) {
606 generic_error_remove_page(inode->i_mapping,
610 * till commit the content of the page is undefined
611 * we'll set it uptodate once bulk is done. otherwise
612 * subsequent reads can access non-stable data
614 ClearPageUptodate(lnb[i].page);
616 if (lnb[i].len == CFS_PAGE_SIZE)
619 if (maxidx >= lnb[i].page->index) {
620 osd_iobuf_add_page(iobuf, lnb[i].page);
623 char *p = kmap(lnb[i].page);
625 off = lnb[i].lnb_page_offset;
628 off = (lnb[i].lnb_page_offset + lnb[i].len) &
631 memset(p + off, 0, CFS_PAGE_SIZE - off);
635 cfs_gettimeofday(&end);
636 timediff = cfs_timeval_sub(&end, &start, NULL);
637 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
639 if (iobuf->dr_npages) {
640 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
644 if (likely(rc == 0)) {
645 rc = osd_do_bio(osd, inode, iobuf);
646 /* do IO stats for preparation reads */
647 osd_fini_iobuf(osd, iobuf);
653 /* Check if a block is allocated or not */
654 static int osd_is_mapped(struct inode *inode, obd_size offset)
656 sector_t (*fs_bmap)(struct address_space *, sector_t);
658 fs_bmap = inode->i_mapping->a_ops->bmap;
660 /* We can't know if we are overwriting or not */
661 if (unlikely(fs_bmap == NULL))
664 if (i_size_read(inode) == 0)
667 /* Beyond EOF, must not be mapped */
668 if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
669 (offset >> inode->i_blkbits))
672 if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
678 static int osd_declare_write_commit(const struct lu_env *env,
679 struct dt_object *dt,
680 struct niobuf_local *lnb, int npages,
681 struct thandle *handle)
683 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
684 struct inode *inode = osd_dt_obj(dt)->oo_inode;
685 struct osd_thandle *oh;
692 bool ignore_quota = false;
693 long long quota_space = 0;
696 LASSERT(handle != NULL);
697 oh = container_of0(handle, struct osd_thandle, ot_super);
698 LASSERT(oh->ot_handle == NULL);
702 /* calculate number of extents (probably better to pass nb) */
703 for (i = 0; i < npages; i++) {
704 if (i && lnb[i].lnb_file_offset !=
705 lnb[i - 1].lnb_file_offset + lnb[i - 1].len)
708 if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
709 quota_space += CFS_PAGE_SIZE;
711 /* ignore quota for the whole request if any page is from
712 * client cache or written by root.
714 * XXX we could handle this on per-lnb basis as done by
716 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
717 !(lnb[i].flags & OBD_BRW_SYNC))
722 * each extent can go into new leaf causing a split
723 * 5 is max tree depth: inode + 4 index blocks
724 * with blockmaps, depth is 3 at most
726 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
728 * many concurrent threads may grow tree by the time
729 * our transaction starts. so, consider 2 is a min depth
731 depth = ext_depth(inode);
732 depth = max(depth, 1) + 1;
734 oh->ot_credits++; /* inode */
735 oh->ot_credits += depth * 2 * extents;
739 oh->ot_credits++; /* inode */
740 oh->ot_credits += depth * extents;
743 /* quota space for metadata blocks */
744 quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
746 /* quota space should be reported in 1K blocks */
747 quota_space = toqb(quota_space);
749 /* each new block can go in different group (bitmap + gd) */
751 /* we can't dirty more bitmap blocks than exist */
752 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
753 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
755 oh->ot_credits += newblocks;
757 /* we can't dirty more gd blocks than exist */
758 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
759 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
761 oh->ot_credits += newblocks;
763 /* make sure the over quota flags were not set */
764 lnb[0].flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
766 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid,
767 quota_space, oh, true, true, &flags,
770 /* we need only to store the overquota flags in the first lnb for
771 * now, once we support multiple objects BRW, this code needs be
773 if (flags & QUOTA_FL_OVER_USRQUOTA)
774 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
775 if (flags & QUOTA_FL_OVER_GRPQUOTA)
776 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
781 /* Check if a block is allocated or not */
782 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
783 struct niobuf_local *lnb, int npages,
784 struct thandle *thandle)
786 struct osd_thread_info *oti = osd_oti_get(env);
787 struct osd_iobuf *iobuf = &oti->oti_iobuf;
788 struct inode *inode = osd_dt_obj(dt)->oo_inode;
789 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
795 rc = osd_init_iobuf(osd, iobuf, 1, npages);
796 if (unlikely(rc != 0))
799 isize = i_size_read(inode);
800 ll_vfs_dq_init(inode);
802 for (i = 0; i < npages; i++) {
803 if (lnb[i].rc == -ENOSPC &&
804 osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
805 /* Allow the write to proceed if overwriting an
810 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
811 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
813 LASSERT(lnb[i].page);
814 generic_error_remove_page(inode->i_mapping,lnb[i].page);
818 LASSERT(PageLocked(lnb[i].page));
819 LASSERT(!PageWriteback(lnb[i].page));
821 if (lnb[i].lnb_file_offset + lnb[i].len > isize)
822 isize = lnb[i].lnb_file_offset + lnb[i].len;
825 * Since write and truncate are serialized by oo_sem, even
826 * partial-page truncate should not leave dirty pages in the
829 LASSERT(!PageDirty(lnb[i].page));
831 SetPageUptodate(lnb[i].page);
833 osd_iobuf_add_page(iobuf, lnb[i].page);
836 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
838 } else if (iobuf->dr_npages > 0) {
839 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
844 /* no pages to write, no transno is needed */
845 thandle->th_local = 1;
848 if (likely(rc == 0)) {
849 if (isize > i_size_read(inode)) {
850 i_size_write(inode, isize);
851 LDISKFS_I(inode)->i_disksize = isize;
852 inode->i_sb->s_op->dirty_inode(inode);
855 rc = osd_do_bio(osd, inode, iobuf);
856 /* we don't do stats here as in read path because
857 * write is async: we'll do this in osd_put_bufs() */
860 if (unlikely(rc != 0)) {
861 /* if write fails, we should drop pages from the cache */
862 for (i = 0; i < npages; i++) {
863 if (lnb[i].page == NULL)
865 LASSERT(PageLocked(lnb[i].page));
866 generic_error_remove_page(inode->i_mapping,lnb[i].page);
873 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
874 struct niobuf_local *lnb, int npages)
876 struct osd_thread_info *oti = osd_oti_get(env);
877 struct osd_iobuf *iobuf = &oti->oti_iobuf;
878 struct inode *inode = osd_dt_obj(dt)->oo_inode;
879 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
880 struct timeval start, end;
881 unsigned long timediff;
882 int rc = 0, i, m = 0, cache = 0;
886 rc = osd_init_iobuf(osd, iobuf, 0, npages);
887 if (unlikely(rc != 0))
890 if (osd->od_read_cache)
892 if (i_size_read(inode) > osd->od_readcache_max_filesize)
895 cfs_gettimeofday(&start);
896 for (i = 0; i < npages; i++) {
898 if (i_size_read(inode) <= lnb[i].lnb_file_offset)
899 /* If there's no more data, abort early.
900 * lnb->rc == 0, so it's easy to detect later. */
903 if (i_size_read(inode) <
904 lnb[i].lnb_file_offset + lnb[i].len - 1)
905 lnb[i].rc = i_size_read(inode) - lnb[i].lnb_file_offset;
907 lnb[i].rc = lnb[i].len;
910 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
911 if (PageUptodate(lnb[i].page)) {
912 lprocfs_counter_add(osd->od_stats,
913 LPROC_OSD_CACHE_HIT, 1);
915 lprocfs_counter_add(osd->od_stats,
916 LPROC_OSD_CACHE_MISS, 1);
917 osd_iobuf_add_page(iobuf, lnb[i].page);
920 generic_error_remove_page(inode->i_mapping,lnb[i].page);
922 cfs_gettimeofday(&end);
923 timediff = cfs_timeval_sub(&end, &start, NULL);
924 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
926 if (iobuf->dr_npages) {
927 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
931 rc = osd_do_bio(osd, inode, iobuf);
933 /* IO stats will be done in osd_bufs_put() */
940 * XXX: Another layering violation for now.
942 * We don't want to use ->f_op->read methods, because generic file write
944 * - serializes on ->i_sem, and
946 * - does a lot of extra work like balance_dirty_pages(),
948 * which doesn't work for globally shared files like /last_rcvd.
950 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
952 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
954 memcpy(buffer, (char *)ei->i_data, buflen);
959 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
961 struct buffer_head *bh;
969 /* prevent reading after eof */
970 spin_lock(&inode->i_lock);
971 if (i_size_read(inode) < *offs + size) {
972 loff_t diff = i_size_read(inode) - *offs;
973 spin_unlock(&inode->i_lock);
975 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
976 i_size_read(inode), *offs);
978 } else if (diff == 0) {
984 spin_unlock(&inode->i_lock);
987 blocksize = 1 << inode->i_blkbits;
990 block = *offs >> inode->i_blkbits;
991 boffs = *offs & (blocksize - 1);
992 csize = min(blocksize - boffs, size);
993 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
995 CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
996 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
997 csize, *offs, inode->i_ino, err);
1001 memcpy(buf, bh->b_data + boffs, csize);
1011 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1012 struct lu_buf *buf, loff_t *pos,
1013 struct lustre_capa *capa)
1015 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1018 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
1021 /* Read small symlink from inode body as we need to maintain correct
1022 * on-disk symlinks for ldiskfs.
1024 if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
1025 (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1026 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
1028 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1033 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1034 const loff_t size, loff_t pos,
1035 struct thandle *handle)
1037 struct osd_thandle *oh;
1039 struct inode *inode;
1043 LASSERT(handle != NULL);
1045 oh = container_of0(handle, struct osd_thandle, ot_super);
1046 LASSERT(oh->ot_handle == NULL);
1048 /* XXX: size == 0 or INT_MAX indicating a catalog header update or
1049 * llog write, see comment in mdd_declare_llog_record().
1051 * This hack will be removed with llog over OSD landing
1053 if (size == DECLARE_LLOG_REWRITE)
1055 else if (size == DECLARE_LLOG_WRITE)
1058 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
1060 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1062 inode = osd_dt_obj(dt)->oo_inode;
1064 /* we may declare write to non-exist llog */
1068 /* dt_declare_write() is usually called for system objects, such
1069 * as llog or last_rcvd files. We needn't enforce quota on those
1070 * objects, so always set the lqi_space as 0. */
1071 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1072 true, true, NULL, false);
1076 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1078 /* LU-2634: clear the extent format for fast symlink */
1079 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1081 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1082 LDISKFS_I(inode)->i_disksize = buflen;
1083 i_size_write(inode, buflen);
1084 inode->i_sb->s_op->dirty_inode(inode);
1089 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1090 int write_NUL, loff_t *offs, handle_t *handle)
1092 struct buffer_head *bh = NULL;
1093 loff_t offset = *offs;
1094 loff_t new_size = i_size_read(inode);
1095 unsigned long block;
1096 int blocksize = 1 << inode->i_blkbits;
1100 int dirty_inode = 0;
1104 * long symlink write does not count the NUL terminator in
1105 * bufsize, we write it, and the inode's file size does not
1106 * count the NUL terminator as well.
1108 ((char *)buf)[bufsize] = '\0';
1111 while (bufsize > 0) {
1115 block = offset >> inode->i_blkbits;
1116 boffs = offset & (blocksize - 1);
1117 size = min(blocksize - boffs, bufsize);
1118 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1120 CERROR("%s: error reading offset %llu (block %lu): "
1122 inode->i_sb->s_id, offset, block, err);
1126 err = ldiskfs_journal_get_write_access(handle, bh);
1128 CERROR("journal_get_write_access() returned error %d\n",
1132 LASSERTF(boffs + size <= bh->b_size,
1133 "boffs %d size %d bh->b_size %lu",
1134 boffs, size, (unsigned long)bh->b_size);
1135 memcpy(bh->b_data + boffs, buf, size);
1136 err = ldiskfs_journal_dirty_metadata(handle, bh);
1140 if (offset + size > new_size)
1141 new_size = offset + size;
1151 /* correct in-core and on-disk sizes */
1152 if (new_size > i_size_read(inode)) {
1153 spin_lock(&inode->i_lock);
1154 if (new_size > i_size_read(inode))
1155 i_size_write(inode, new_size);
1156 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1157 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1160 spin_unlock(&inode->i_lock);
1162 inode->i_sb->s_op->dirty_inode(inode);
1170 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1171 const struct lu_buf *buf, loff_t *pos,
1172 struct thandle *handle, struct lustre_capa *capa,
1175 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1176 struct osd_thandle *oh;
1180 LASSERT(dt_object_exists(dt));
1182 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1185 LASSERT(handle != NULL);
1186 LASSERT(inode != NULL);
1187 ll_vfs_dq_init(inode);
1189 /* XXX: don't check: one declared chunk can be used many times */
1190 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
1192 oh = container_of(handle, struct osd_thandle, ot_super);
1193 LASSERT(oh->ot_handle->h_transaction != NULL);
1194 /* Write small symlink to inode body as we need to maintain correct
1195 * on-disk symlinks for ldiskfs.
1196 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1197 * does not count it in.
1199 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1200 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1201 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1203 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1204 buf->lb_len, is_link, pos,
1207 result = buf->lb_len;
1211 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1212 __u64 start, __u64 end, struct thandle *th)
1214 struct osd_thandle *oh;
1215 struct inode *inode;
1220 oh = container_of(th, struct osd_thandle, ot_super);
1223 * we don't need to reserve credits for whole truncate
1224 * it's not possible as truncate may need to free too many
1225 * blocks and that won't fit a single transaction. instead
1226 * we reserve credits to change i_size and put inode onto
1227 * orphan list. if needed truncate will extend or restart
1230 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
1231 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
1233 inode = osd_dt_obj(dt)->oo_inode;
1236 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1237 true, true, NULL, false);
1241 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1242 __u64 start, __u64 end, struct thandle *th,
1243 struct lustre_capa *capa)
1245 struct osd_thandle *oh;
1246 struct osd_object *obj = osd_dt_obj(dt);
1247 struct inode *inode = obj->oo_inode;
1253 LASSERT(end == OBD_OBJECT_EOF);
1254 LASSERT(dt_object_exists(dt));
1255 LASSERT(osd_invariant(obj));
1256 LASSERT(inode != NULL);
1257 ll_vfs_dq_init(inode);
1260 oh = container_of(th, struct osd_thandle, ot_super);
1261 LASSERT(oh->ot_handle->h_transaction != NULL);
1263 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
1265 tid = oh->ot_handle->h_transaction->t_tid;
1267 rc = vmtruncate(inode, start);
1270 * For a partial-page truncate, flush the page to disk immediately to
1271 * avoid data corruption during direct disk write. b=17397
1273 if (rc == 0 && (start & ~CFS_PAGE_MASK) != 0)
1274 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1276 h = journal_current_handle();
1278 LASSERT(h == oh->ot_handle);
1280 if (tid != h->h_transaction->t_tid) {
1281 int credits = oh->ot_credits;
1283 * transaction has changed during truncate
1284 * we need to restart the handle with our credits
1286 if (h->h_buffer_credits < credits) {
1287 if (ldiskfs_journal_extend(h, credits))
1288 rc2 = ldiskfs_journal_restart(h, credits);
1292 RETURN(rc == 0 ? rc2 : rc);
1295 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1296 struct ll_user_fiemap *fm)
1298 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1299 struct osd_thread_info *info = osd_oti_get(env);
1300 struct dentry *dentry = &info->oti_obj_dentry;
1301 struct file *file = &info->oti_file;
1302 mm_segment_t saved_fs;
1306 dentry->d_inode = inode;
1307 file->f_dentry = dentry;
1308 file->f_mapping = inode->i_mapping;
1309 file->f_op = inode->i_fop;
1311 saved_fs = get_fs();
1313 /* ldiskfs_ioctl does not have a inode argument */
1314 if (inode->i_fop->unlocked_ioctl)
1315 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1324 * in some cases we may need declare methods for objects being created
1325 * e.g., when we create symlink
1327 const struct dt_body_operations osd_body_ops_new = {
1328 .dbo_declare_write = osd_declare_write,
1331 const struct dt_body_operations osd_body_ops = {
1332 .dbo_read = osd_read,
1333 .dbo_declare_write = osd_declare_write,
1334 .dbo_write = osd_write,
1335 .dbo_bufs_get = osd_bufs_get,
1336 .dbo_bufs_put = osd_bufs_put,
1337 .dbo_write_prep = osd_write_prep,
1338 .dbo_declare_write_commit = osd_declare_write_commit,
1339 .dbo_write_commit = osd_write_commit,
1340 .dbo_read_prep = osd_read_prep,
1341 .do_declare_punch = osd_declare_punch,
1342 .do_punch = osd_punch,
1343 .dbo_fiemap_get = osd_fiemap_get,