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 #ifndef HAVE_PAGE_CONSTANT
64 #define mapping_cap_page_constant_write(mapping) 0
65 #define SetPageConstant(page) do {} while (0)
66 #define ClearPageConstant(page) do {} while (0)
69 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
70 int rw, int line, int pages)
74 LASSERTF(iobuf->dr_elapsed_valid == 0,
75 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
76 cfs_atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
78 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
80 init_waitqueue_head(&iobuf->dr_wait);
81 cfs_atomic_set(&iobuf->dr_numreqs, 0);
86 iobuf->dr_elapsed = 0;
87 /* must be counted before, so assert */
89 iobuf->dr_init_at = line;
91 blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
92 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
93 LASSERT(iobuf->dr_pg_buf.lb_len >=
94 pages * sizeof(iobuf->dr_pages[0]));
98 /* start with 1MB for 4K blocks */
100 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
103 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
104 (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
106 blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
107 iobuf->dr_max_pages = 0;
108 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
109 (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
111 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
112 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
113 if (unlikely(iobuf->dr_blocks == NULL))
116 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
117 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
118 if (unlikely(iobuf->dr_pages == NULL))
121 iobuf->dr_max_pages = pages;
125 #define osd_init_iobuf(dev, iobuf, rw, pages) \
126 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
128 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
130 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
131 iobuf->dr_pages[iobuf->dr_npages++] = page;
134 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
136 int rw = iobuf->dr_rw;
138 if (iobuf->dr_elapsed_valid) {
139 iobuf->dr_elapsed_valid = 0;
140 LASSERT(iobuf->dr_dev == d);
141 LASSERT(iobuf->dr_frags > 0);
142 lprocfs_oh_tally(&d->od_brw_stats.
143 hist[BRW_R_DIO_FRAGS+rw],
145 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
150 #ifndef REQ_WRITE /* pre-2.6.35 */
151 #define __REQ_WRITE BIO_RW
154 #ifdef HAVE_BIO_ENDIO_2ARG
155 #define DIO_RETURN(a)
156 static void dio_complete_routine(struct bio *bio, int error)
158 #define DIO_RETURN(a) return(a)
159 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
162 struct osd_iobuf *iobuf = bio->bi_private;
166 /* CAVEAT EMPTOR: possibly in IRQ context
167 * DO NOT record procfs stats here!!! */
169 if (unlikely(iobuf == NULL)) {
170 CERROR("***** bio->bi_private is NULL! This should never "
171 "happen. Normally, I would crash here, but instead I "
172 "will dump the bio contents to the console. Please "
173 "report this to <http://jira.whamcloud.com/> , along "
174 "with any interesting messages leading up to this point "
175 "(like SCSI errors, perhaps). Because bi_private is "
176 "NULL, I can't wake up the thread that initiated this "
177 "IO - you will probably have to reboot this node.\n");
178 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
179 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
180 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
181 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
182 bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
187 /* the check is outside of the cycle for performance reason -bzzz */
188 if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
189 bio_for_each_segment(bvl, bio, i) {
190 if (likely(error == 0))
191 SetPageUptodate(bvl->bv_page);
192 LASSERT(PageLocked(bvl->bv_page));
193 ClearPageConstant(bvl->bv_page);
195 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
197 struct page *p = iobuf->dr_pages[0];
199 if (mapping_cap_page_constant_write(p->mapping)) {
200 bio_for_each_segment(bvl, bio, i) {
201 ClearPageConstant(bvl->bv_page);
205 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
208 /* any real error is good enough -bzzz */
209 if (error != 0 && iobuf->dr_error == 0)
210 iobuf->dr_error = error;
213 * set dr_elapsed before dr_numreqs turns to 0, otherwise
214 * it's possible that service thread will see dr_numreqs
215 * is zero, but dr_elapsed is not set yet, leading to lost
216 * data in this processing and an assertion in a subsequent
219 if (cfs_atomic_read(&iobuf->dr_numreqs) == 1) {
220 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
221 iobuf->dr_elapsed_valid = 1;
223 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
224 wake_up(&iobuf->dr_wait);
226 /* Completed bios used to be chained off iobuf->dr_bios and freed in
227 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
228 * mempool when serious on-disk fragmentation was encountered,
229 * deadlocking the OST. The bios are now released as soon as complete
230 * so the pool cannot be exhausted while IOs are competing. bug 10076 */
235 static void record_start_io(struct osd_iobuf *iobuf, int size)
237 struct osd_device *osd = iobuf->dr_dev;
238 struct obd_histogram *h = osd->od_brw_stats.hist;
241 cfs_atomic_inc(&iobuf->dr_numreqs);
243 if (iobuf->dr_rw == 0) {
244 cfs_atomic_inc(&osd->od_r_in_flight);
245 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
246 cfs_atomic_read(&osd->od_r_in_flight));
247 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
248 } else if (iobuf->dr_rw == 1) {
249 cfs_atomic_inc(&osd->od_w_in_flight);
250 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
251 cfs_atomic_read(&osd->od_w_in_flight));
252 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
258 static void osd_submit_bio(int rw, struct bio *bio)
260 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
262 submit_bio(READ, bio);
264 submit_bio(WRITE, bio);
267 static int can_be_merged(struct bio *bio, sector_t sector)
274 size = bio->bi_size >> 9;
275 return bio->bi_sector + size == sector ? 1 : 0;
278 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
279 struct osd_iobuf *iobuf)
281 int blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
282 struct page **pages = iobuf->dr_pages;
283 int npages = iobuf->dr_npages;
284 unsigned long *blocks = iobuf->dr_blocks;
285 int total_blocks = npages * blocks_per_page;
286 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
287 unsigned int blocksize = inode->i_sb->s_blocksize;
288 struct bio *bio = NULL;
290 unsigned int page_offset;
299 LASSERT(iobuf->dr_npages == npages);
301 osd_brw_stats_update(osd, iobuf);
302 iobuf->dr_start_time = cfs_time_current();
304 for (page_idx = 0, block_idx = 0;
306 page_idx++, block_idx += blocks_per_page) {
308 page = pages[page_idx];
309 LASSERT(block_idx + blocks_per_page <= total_blocks);
311 for (i = 0, page_offset = 0;
313 i += nblocks, page_offset += blocksize * nblocks) {
317 if (blocks[block_idx + i] == 0) { /* hole */
318 LASSERTF(iobuf->dr_rw == 0,
319 "page_idx %u, block_idx %u, i %u\n",
320 page_idx, block_idx, i);
321 memset(kmap(page) + page_offset, 0, blocksize);
326 sector = (sector_t)blocks[block_idx + i] << sector_bits;
328 /* Additional contiguous file blocks? */
329 while (i + nblocks < blocks_per_page &&
330 (sector + (nblocks << sector_bits)) ==
331 ((sector_t)blocks[block_idx + i + nblocks] <<
335 /* I only set the page to be constant only if it
336 * is mapped to a contiguous underlying disk block(s).
337 * It will then make sure the corresponding device
338 * cache of raid5 will be overwritten by this page.
340 if (iobuf->dr_rw && (nblocks == blocks_per_page) &&
341 mapping_cap_page_constant_write(inode->i_mapping))
342 SetPageConstant(page);
345 can_be_merged(bio, sector) &&
346 bio_add_page(bio, page,
347 blocksize * nblocks, page_offset) != 0)
348 continue; /* added this frag OK */
351 struct request_queue *q =
352 bdev_get_queue(bio->bi_bdev);
354 /* Dang! I have to fragment this I/O */
355 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
356 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
358 bio->bi_vcnt, bio->bi_max_vecs,
359 bio->bi_size >> 9, queue_max_sectors(q),
360 bio_phys_segments(q, bio),
361 queue_max_phys_segments(q),
362 bio_hw_segments(q, bio),
363 queue_max_hw_segments(q));
365 record_start_io(iobuf, bio->bi_size);
366 osd_submit_bio(iobuf->dr_rw, bio);
369 /* allocate new bio */
370 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
371 (npages - page_idx) *
374 CERROR("Can't allocate bio %u*%u = %u pages\n",
375 (npages - page_idx), blocks_per_page,
376 (npages - page_idx) * blocks_per_page);
381 bio->bi_bdev = inode->i_sb->s_bdev;
382 bio->bi_sector = sector;
383 bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
384 bio->bi_end_io = dio_complete_routine;
385 bio->bi_private = iobuf;
387 rc = bio_add_page(bio, page,
388 blocksize * nblocks, page_offset);
394 record_start_io(iobuf, bio->bi_size);
395 osd_submit_bio(iobuf->dr_rw, bio);
400 /* in order to achieve better IO throughput, we don't wait for writes
401 * completion here. instead we proceed with transaction commit in
402 * parallel and wait for IO completion once transaction is stopped
403 * see osd_trans_stop() for more details -bzzz */
404 if (iobuf->dr_rw == 0) {
405 wait_event(iobuf->dr_wait,
406 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
410 rc = iobuf->dr_error;
414 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
415 struct niobuf_local *lnb)
422 int poff = offset & (PAGE_CACHE_SIZE - 1);
423 int plen = PAGE_CACHE_SIZE - poff;
427 lnb->lnb_file_offset = offset;
428 lnb->lnb_page_offset = poff;
430 /* lb->flags = rnb->flags; */
435 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
446 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
448 struct inode *inode = osd_dt_obj(dt)->oo_inode;
449 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
454 page = find_or_create_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
455 GFP_NOFS | __GFP_HIGHMEM);
456 if (unlikely(page == NULL))
457 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
463 * there are following "locks":
480 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
481 ssize_t len, struct niobuf_local *lnb, int rw,
482 struct lustre_capa *capa)
484 struct osd_object *obj = osd_dt_obj(d);
485 int npages, i, rc = 0;
487 LASSERT(obj->oo_inode);
489 osd_map_remote_to_local(pos, len, &npages, lnb);
491 for (i = 0; i < npages; i++, lnb++) {
493 /* We still set up for ungranted pages so that granted pages
494 * can be written to disk as they were promised, and portals
495 * needs to keep the pages all aligned properly. */
496 lnb->dentry = (void *) obj;
498 lnb->page = osd_get_page(d, lnb->lnb_file_offset, rw);
499 if (lnb->page == NULL)
500 GOTO(cleanup, rc = -ENOMEM);
502 /* DLM locking protects us from write and truncate competing
503 * for same region, but truncate can leave dirty page in the
504 * cache. it's possible the writeout on a such a page is in
505 * progress when we access it. it's also possible that during
506 * this writeout we put new (partial) data, but then won't
507 * be able to proceed in filter_commitrw_write(). thus let's
508 * just wait for writeout completion, should be rare enough.
510 wait_on_page_writeback(lnb->page);
511 BUG_ON(PageWriteback(lnb->page));
513 lu_object_get(&d->do_lu);
521 static int osd_bufs_put(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 osd_device *d = osd_obj2dev(osd_dt_obj(dt));
529 /* to do IO stats, notice we do this here because
530 * osd_do_bio() doesn't wait for write to complete */
531 osd_fini_iobuf(d, iobuf);
533 for (i = 0; i < npages; i++) {
534 if (lnb[i].page == NULL)
536 LASSERT(PageLocked(lnb[i].page));
537 unlock_page(lnb[i].page);
538 page_cache_release(lnb[i].page);
539 lu_object_put(env, &dt->do_lu);
545 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
546 struct niobuf_local *lnb, int npages)
548 struct osd_thread_info *oti = osd_oti_get(env);
549 struct osd_iobuf *iobuf = &oti->oti_iobuf;
550 struct inode *inode = osd_dt_obj(dt)->oo_inode;
551 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
552 struct timeval start;
554 unsigned long timediff;
563 rc = osd_init_iobuf(osd, iobuf, 0, npages);
564 if (unlikely(rc != 0))
567 isize = i_size_read(inode);
568 maxidx = ((isize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 1;
570 if (osd->od_writethrough_cache)
572 if (isize > osd->od_readcache_max_filesize)
575 do_gettimeofday(&start);
576 for (i = 0; i < npages; i++) {
579 generic_error_remove_page(inode->i_mapping,
583 * till commit the content of the page is undefined
584 * we'll set it uptodate once bulk is done. otherwise
585 * subsequent reads can access non-stable data
587 ClearPageUptodate(lnb[i].page);
589 if (lnb[i].len == PAGE_CACHE_SIZE)
592 if (maxidx >= lnb[i].page->index) {
593 osd_iobuf_add_page(iobuf, lnb[i].page);
596 char *p = kmap(lnb[i].page);
598 off = lnb[i].lnb_page_offset;
601 off = (lnb[i].lnb_page_offset + lnb[i].len) &
604 memset(p + off, 0, PAGE_CACHE_SIZE - off);
608 do_gettimeofday(&end);
609 timediff = cfs_timeval_sub(&end, &start, NULL);
610 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
612 if (iobuf->dr_npages) {
613 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
617 if (likely(rc == 0)) {
618 rc = osd_do_bio(osd, inode, iobuf);
619 /* do IO stats for preparation reads */
620 osd_fini_iobuf(osd, iobuf);
626 /* Check if a block is allocated or not */
627 static int osd_is_mapped(struct inode *inode, obd_size offset)
629 sector_t (*fs_bmap)(struct address_space *, sector_t);
631 fs_bmap = inode->i_mapping->a_ops->bmap;
633 /* We can't know if we are overwriting or not */
634 if (unlikely(fs_bmap == NULL))
637 if (i_size_read(inode) == 0)
640 /* Beyond EOF, must not be mapped */
641 if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
642 (offset >> inode->i_blkbits))
645 if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
651 static int osd_declare_write_commit(const struct lu_env *env,
652 struct dt_object *dt,
653 struct niobuf_local *lnb, int npages,
654 struct thandle *handle)
656 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
657 struct inode *inode = osd_dt_obj(dt)->oo_inode;
658 struct osd_thandle *oh;
665 bool ignore_quota = false;
666 long long quota_space = 0;
669 LASSERT(handle != NULL);
670 oh = container_of0(handle, struct osd_thandle, ot_super);
671 LASSERT(oh->ot_handle == NULL);
675 /* calculate number of extents (probably better to pass nb) */
676 for (i = 0; i < npages; i++) {
677 if (i && lnb[i].lnb_file_offset !=
678 lnb[i - 1].lnb_file_offset + lnb[i - 1].len)
681 if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
682 quota_space += PAGE_CACHE_SIZE;
684 /* ignore quota for the whole request if any page is from
685 * client cache or written by root.
687 * XXX once we drop the 1.8 client support, the checking
688 * for whether page is from cache can be simplified as:
689 * !(lnb[i].flags & OBD_BRW_SYNC)
691 * XXX we could handle this on per-lnb basis as done by
693 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
694 (lnb[i].flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
700 * each extent can go into new leaf causing a split
701 * 5 is max tree depth: inode + 4 index blocks
702 * with blockmaps, depth is 3 at most
704 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
706 * many concurrent threads may grow tree by the time
707 * our transaction starts. so, consider 2 is a min depth
709 depth = ext_depth(inode);
710 depth = max(depth, 1) + 1;
712 oh->ot_credits++; /* inode */
713 oh->ot_credits += depth * 2 * extents;
717 oh->ot_credits++; /* inode */
718 oh->ot_credits += depth * extents;
721 /* quota space for metadata blocks */
722 quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
724 /* quota space should be reported in 1K blocks */
725 quota_space = toqb(quota_space);
727 /* each new block can go in different group (bitmap + gd) */
729 /* we can't dirty more bitmap blocks than exist */
730 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
731 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
733 oh->ot_credits += newblocks;
735 /* we can't dirty more gd blocks than exist */
736 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
737 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
739 oh->ot_credits += newblocks;
741 /* make sure the over quota flags were not set */
742 lnb[0].flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
744 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid,
745 quota_space, oh, true, true, &flags,
748 /* we need only to store the overquota flags in the first lnb for
749 * now, once we support multiple objects BRW, this code needs be
751 if (flags & QUOTA_FL_OVER_USRQUOTA)
752 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
753 if (flags & QUOTA_FL_OVER_GRPQUOTA)
754 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
759 /* Check if a block is allocated or not */
760 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
761 struct niobuf_local *lnb, int npages,
762 struct thandle *thandle)
764 struct osd_thread_info *oti = osd_oti_get(env);
765 struct osd_iobuf *iobuf = &oti->oti_iobuf;
766 struct inode *inode = osd_dt_obj(dt)->oo_inode;
767 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
773 rc = osd_init_iobuf(osd, iobuf, 1, npages);
774 if (unlikely(rc != 0))
777 isize = i_size_read(inode);
778 ll_vfs_dq_init(inode);
780 for (i = 0; i < npages; i++) {
781 if (lnb[i].rc == -ENOSPC &&
782 osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
783 /* Allow the write to proceed if overwriting an
788 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
789 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
791 LASSERT(lnb[i].page);
792 generic_error_remove_page(inode->i_mapping,lnb[i].page);
796 LASSERT(PageLocked(lnb[i].page));
797 LASSERT(!PageWriteback(lnb[i].page));
799 if (lnb[i].lnb_file_offset + lnb[i].len > isize)
800 isize = lnb[i].lnb_file_offset + lnb[i].len;
803 * Since write and truncate are serialized by oo_sem, even
804 * partial-page truncate should not leave dirty pages in the
807 LASSERT(!PageDirty(lnb[i].page));
809 SetPageUptodate(lnb[i].page);
811 osd_iobuf_add_page(iobuf, lnb[i].page);
814 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
816 } else if (iobuf->dr_npages > 0) {
817 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
822 /* no pages to write, no transno is needed */
823 thandle->th_local = 1;
826 if (likely(rc == 0)) {
827 if (isize > i_size_read(inode)) {
828 i_size_write(inode, isize);
829 LDISKFS_I(inode)->i_disksize = isize;
830 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
833 rc = osd_do_bio(osd, inode, iobuf);
834 /* we don't do stats here as in read path because
835 * write is async: we'll do this in osd_put_bufs() */
838 if (unlikely(rc != 0)) {
839 /* if write fails, we should drop pages from the cache */
840 for (i = 0; i < npages; i++) {
841 if (lnb[i].page == NULL)
843 LASSERT(PageLocked(lnb[i].page));
844 generic_error_remove_page(inode->i_mapping,lnb[i].page);
851 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
852 struct niobuf_local *lnb, int npages)
854 struct osd_thread_info *oti = osd_oti_get(env);
855 struct osd_iobuf *iobuf = &oti->oti_iobuf;
856 struct inode *inode = osd_dt_obj(dt)->oo_inode;
857 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
858 struct timeval start, end;
859 unsigned long timediff;
860 int rc = 0, i, m = 0, cache = 0;
864 rc = osd_init_iobuf(osd, iobuf, 0, npages);
865 if (unlikely(rc != 0))
868 if (osd->od_read_cache)
870 if (i_size_read(inode) > osd->od_readcache_max_filesize)
873 do_gettimeofday(&start);
874 for (i = 0; i < npages; i++) {
876 if (i_size_read(inode) <= lnb[i].lnb_file_offset)
877 /* If there's no more data, abort early.
878 * lnb->rc == 0, so it's easy to detect later. */
881 if (i_size_read(inode) <
882 lnb[i].lnb_file_offset + lnb[i].len - 1)
883 lnb[i].rc = i_size_read(inode) - lnb[i].lnb_file_offset;
885 lnb[i].rc = lnb[i].len;
888 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
889 if (PageUptodate(lnb[i].page)) {
890 lprocfs_counter_add(osd->od_stats,
891 LPROC_OSD_CACHE_HIT, 1);
893 lprocfs_counter_add(osd->od_stats,
894 LPROC_OSD_CACHE_MISS, 1);
895 osd_iobuf_add_page(iobuf, lnb[i].page);
898 generic_error_remove_page(inode->i_mapping,lnb[i].page);
900 do_gettimeofday(&end);
901 timediff = cfs_timeval_sub(&end, &start, NULL);
902 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
904 if (iobuf->dr_npages) {
905 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
909 rc = osd_do_bio(osd, inode, iobuf);
911 /* IO stats will be done in osd_bufs_put() */
918 * XXX: Another layering violation for now.
920 * We don't want to use ->f_op->read methods, because generic file write
922 * - serializes on ->i_sem, and
924 * - does a lot of extra work like balance_dirty_pages(),
926 * which doesn't work for globally shared files like /last_rcvd.
928 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
930 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
932 memcpy(buffer, (char *)ei->i_data, buflen);
937 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
939 struct buffer_head *bh;
947 /* prevent reading after eof */
948 spin_lock(&inode->i_lock);
949 if (i_size_read(inode) < *offs + size) {
950 loff_t diff = i_size_read(inode) - *offs;
951 spin_unlock(&inode->i_lock);
953 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
954 i_size_read(inode), *offs);
956 } else if (diff == 0) {
962 spin_unlock(&inode->i_lock);
965 blocksize = 1 << inode->i_blkbits;
968 block = *offs >> inode->i_blkbits;
969 boffs = *offs & (blocksize - 1);
970 csize = min(blocksize - boffs, size);
971 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
973 CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
974 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
975 csize, *offs, inode->i_ino, err);
979 memcpy(buf, bh->b_data + boffs, csize);
989 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
990 struct lu_buf *buf, loff_t *pos,
991 struct lustre_capa *capa)
993 struct inode *inode = osd_dt_obj(dt)->oo_inode;
996 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
999 /* Read small symlink from inode body as we need to maintain correct
1000 * on-disk symlinks for ldiskfs.
1002 if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
1003 (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1004 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
1006 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1011 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1012 const loff_t size, loff_t pos,
1013 struct thandle *handle)
1015 struct osd_thandle *oh;
1017 struct inode *inode;
1021 LASSERT(handle != NULL);
1023 oh = container_of0(handle, struct osd_thandle, ot_super);
1024 LASSERT(oh->ot_handle == NULL);
1026 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
1028 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1030 inode = osd_dt_obj(dt)->oo_inode;
1032 /* we may declare write to non-exist llog */
1036 /* dt_declare_write() is usually called for system objects, such
1037 * as llog or last_rcvd files. We needn't enforce quota on those
1038 * objects, so always set the lqi_space as 0. */
1039 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1040 true, true, NULL, false);
1044 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1046 /* LU-2634: clear the extent format for fast symlink */
1047 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1049 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1050 LDISKFS_I(inode)->i_disksize = buflen;
1051 i_size_write(inode, buflen);
1052 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1057 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1058 int write_NUL, loff_t *offs, handle_t *handle)
1060 struct buffer_head *bh = NULL;
1061 loff_t offset = *offs;
1062 loff_t new_size = i_size_read(inode);
1063 unsigned long block;
1064 int blocksize = 1 << inode->i_blkbits;
1068 int dirty_inode = 0;
1072 * long symlink write does not count the NUL terminator in
1073 * bufsize, we write it, and the inode's file size does not
1074 * count the NUL terminator as well.
1076 ((char *)buf)[bufsize] = '\0';
1079 while (bufsize > 0) {
1083 block = offset >> inode->i_blkbits;
1084 boffs = offset & (blocksize - 1);
1085 size = min(blocksize - boffs, bufsize);
1086 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1088 CERROR("%s: error reading offset %llu (block %lu): "
1090 inode->i_sb->s_id, offset, block, err);
1094 err = ldiskfs_journal_get_write_access(handle, bh);
1096 CERROR("journal_get_write_access() returned error %d\n",
1100 LASSERTF(boffs + size <= bh->b_size,
1101 "boffs %d size %d bh->b_size %lu",
1102 boffs, size, (unsigned long)bh->b_size);
1103 memcpy(bh->b_data + boffs, buf, size);
1104 err = ldiskfs_journal_dirty_metadata(handle, bh);
1108 if (offset + size > new_size)
1109 new_size = offset + size;
1119 /* correct in-core and on-disk sizes */
1120 if (new_size > i_size_read(inode)) {
1121 spin_lock(&inode->i_lock);
1122 if (new_size > i_size_read(inode))
1123 i_size_write(inode, new_size);
1124 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1125 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1128 spin_unlock(&inode->i_lock);
1130 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1138 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1139 const struct lu_buf *buf, loff_t *pos,
1140 struct thandle *handle, struct lustre_capa *capa,
1143 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1144 struct osd_thandle *oh;
1148 LASSERT(dt_object_exists(dt));
1150 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1153 LASSERT(handle != NULL);
1154 LASSERT(inode != NULL);
1155 ll_vfs_dq_init(inode);
1157 /* XXX: don't check: one declared chunk can be used many times */
1158 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
1160 oh = container_of(handle, struct osd_thandle, ot_super);
1161 LASSERT(oh->ot_handle->h_transaction != NULL);
1162 /* Write small symlink to inode body as we need to maintain correct
1163 * on-disk symlinks for ldiskfs.
1164 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1165 * does not count it in.
1167 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1168 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1169 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1171 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1172 buf->lb_len, is_link, pos,
1175 result = buf->lb_len;
1179 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1180 __u64 start, __u64 end, struct thandle *th)
1182 struct osd_thandle *oh;
1183 struct inode *inode;
1188 oh = container_of(th, struct osd_thandle, ot_super);
1191 * we don't need to reserve credits for whole truncate
1192 * it's not possible as truncate may need to free too many
1193 * blocks and that won't fit a single transaction. instead
1194 * we reserve credits to change i_size and put inode onto
1195 * orphan list. if needed truncate will extend or restart
1198 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
1199 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
1201 inode = osd_dt_obj(dt)->oo_inode;
1204 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1205 true, true, NULL, false);
1209 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1210 __u64 start, __u64 end, struct thandle *th,
1211 struct lustre_capa *capa)
1213 struct osd_thandle *oh;
1214 struct osd_object *obj = osd_dt_obj(dt);
1215 struct inode *inode = obj->oo_inode;
1219 int rc = 0, rc2 = 0;
1222 LASSERT(end == OBD_OBJECT_EOF);
1223 LASSERT(dt_object_exists(dt));
1224 LASSERT(osd_invariant(obj));
1225 LASSERT(inode != NULL);
1226 ll_vfs_dq_init(inode);
1229 oh = container_of(th, struct osd_thandle, ot_super);
1230 LASSERT(oh->ot_handle->h_transaction != NULL);
1232 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
1234 tid = oh->ot_handle->h_transaction->t_tid;
1236 oldsize=inode->i_size;
1237 i_size_write(inode, start);
1238 truncate_pagecache(inode, oldsize, start);
1239 if (inode->i_op->truncate)
1240 inode->i_op->truncate(inode);
1243 * For a partial-page truncate, flush the page to disk immediately to
1244 * avoid data corruption during direct disk write. b=17397
1246 if ((start & ~CFS_PAGE_MASK) != 0)
1247 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1249 h = journal_current_handle();
1251 LASSERT(h == oh->ot_handle);
1253 if (tid != h->h_transaction->t_tid) {
1254 int credits = oh->ot_credits;
1256 * transaction has changed during truncate
1257 * we need to restart the handle with our credits
1259 if (h->h_buffer_credits < credits) {
1260 if (ldiskfs_journal_extend(h, credits))
1261 rc2 = ldiskfs_journal_restart(h, credits);
1265 RETURN(rc == 0 ? rc2 : rc);
1268 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1269 struct ll_user_fiemap *fm)
1271 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1272 struct osd_thread_info *info = osd_oti_get(env);
1273 struct dentry *dentry = &info->oti_obj_dentry;
1274 struct file *file = &info->oti_file;
1275 mm_segment_t saved_fs;
1279 dentry->d_inode = inode;
1280 dentry->d_sb = inode->i_sb;
1281 file->f_dentry = dentry;
1282 file->f_mapping = inode->i_mapping;
1283 file->f_op = inode->i_fop;
1285 saved_fs = get_fs();
1287 /* ldiskfs_ioctl does not have a inode argument */
1288 if (inode->i_fop->unlocked_ioctl)
1289 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1298 * in some cases we may need declare methods for objects being created
1299 * e.g., when we create symlink
1301 const struct dt_body_operations osd_body_ops_new = {
1302 .dbo_declare_write = osd_declare_write,
1305 const struct dt_body_operations osd_body_ops = {
1306 .dbo_read = osd_read,
1307 .dbo_declare_write = osd_declare_write,
1308 .dbo_write = osd_write,
1309 .dbo_bufs_get = osd_bufs_get,
1310 .dbo_bufs_put = osd_bufs_put,
1311 .dbo_write_prep = osd_write_prep,
1312 .dbo_declare_write_commit = osd_declare_write_commit,
1313 .dbo_write_commit = osd_write_commit,
1314 .dbo_read_prep = osd_read_prep,
1315 .dbo_declare_punch = osd_declare_punch,
1316 .dbo_punch = osd_punch,
1317 .dbo_fiemap_get = osd_fiemap_get,