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, Whamcloud, Inc.
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 void osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,int rw)
98 cfs_waitq_init(&iobuf->dr_wait);
99 cfs_atomic_set(&iobuf->dr_numreqs, 0);
100 iobuf->dr_max_pages = PTLRPC_MAX_BRW_PAGES;
101 iobuf->dr_npages = 0;
105 iobuf->dr_elapsed = 0;
106 /* must be counted before, so assert */
107 LASSERT(iobuf->dr_elapsed_valid == 0);
111 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
113 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
114 iobuf->dr_pages[iobuf->dr_npages++] = page;
117 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
119 int rw = iobuf->dr_rw;
121 if (iobuf->dr_elapsed_valid) {
122 iobuf->dr_elapsed_valid = 0;
123 LASSERT(iobuf->dr_dev == d);
124 LASSERT(iobuf->dr_frags > 0);
125 lprocfs_oh_tally(&d->od_brw_stats.
126 hist[BRW_R_DIO_FRAGS+rw],
128 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
133 #ifdef HAVE_BIO_ENDIO_2ARG
134 #define DIO_RETURN(a)
135 static void dio_complete_routine(struct bio *bio, int error)
137 #define DIO_RETURN(a) return(a)
138 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
141 struct osd_iobuf *iobuf = bio->bi_private;
145 /* CAVEAT EMPTOR: possibly in IRQ context
146 * DO NOT record procfs stats here!!! */
148 if (unlikely(iobuf == NULL)) {
149 CERROR("***** bio->bi_private is NULL! This should never "
150 "happen. Normally, I would crash here, but instead I "
151 "will dump the bio contents to the console. Please "
152 "report this to <http://jira.whamcloud.com/> , along "
153 "with any interesting messages leading up to this point "
154 "(like SCSI errors, perhaps). Because bi_private is "
155 "NULL, I can't wake up the thread that initiated this "
156 "IO - you will probably have to reboot this node.\n");
157 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
158 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
159 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
160 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
161 bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
166 /* the check is outside of the cycle for performance reason -bzzz */
167 if (!cfs_test_bit(BIO_RW, &bio->bi_rw)) {
168 bio_for_each_segment(bvl, bio, i) {
169 if (likely(error == 0))
170 SetPageUptodate(bvl->bv_page);
171 LASSERT(PageLocked(bvl->bv_page));
172 ClearPageConstant(bvl->bv_page);
174 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
176 struct page *p = iobuf->dr_pages[0];
178 if (mapping_cap_page_constant_write(p->mapping)) {
179 bio_for_each_segment(bvl, bio, i) {
180 ClearPageConstant(bvl->bv_page);
184 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
187 /* any real error is good enough -bzzz */
188 if (error != 0 && iobuf->dr_error == 0)
189 iobuf->dr_error = error;
191 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs)) {
192 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
193 iobuf->dr_elapsed_valid = 1;
194 cfs_waitq_signal(&iobuf->dr_wait);
197 /* Completed bios used to be chained off iobuf->dr_bios and freed in
198 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
199 * mempool when serious on-disk fragmentation was encountered,
200 * deadlocking the OST. The bios are now released as soon as complete
201 * so the pool cannot be exhausted while IOs are competing. bug 10076 */
206 static void record_start_io(struct osd_iobuf *iobuf, int size)
208 struct osd_device *osd = iobuf->dr_dev;
209 struct obd_histogram *h = osd->od_brw_stats.hist;
212 cfs_atomic_inc(&iobuf->dr_numreqs);
214 if (iobuf->dr_rw == 0) {
215 cfs_atomic_inc(&osd->od_r_in_flight);
216 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
217 cfs_atomic_read(&osd->od_r_in_flight));
218 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
219 } else if (iobuf->dr_rw == 1) {
220 cfs_atomic_inc(&osd->od_w_in_flight);
221 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
222 cfs_atomic_read(&osd->od_w_in_flight));
223 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
229 static void osd_submit_bio(int rw, struct bio *bio)
231 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
233 submit_bio(READ, bio);
235 submit_bio(WRITE, bio);
238 static int can_be_merged(struct bio *bio, sector_t sector)
245 size = bio->bi_size >> 9;
246 return bio->bi_sector + size == sector ? 1 : 0;
249 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
250 struct osd_iobuf *iobuf)
252 int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
253 struct page **pages = iobuf->dr_pages;
254 int npages = iobuf->dr_npages;
255 unsigned long *blocks = iobuf->dr_blocks;
256 int total_blocks = npages * blocks_per_page;
257 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
258 unsigned int blocksize = inode->i_sb->s_blocksize;
259 struct bio *bio = NULL;
261 unsigned int page_offset;
270 LASSERT(iobuf->dr_npages == npages);
272 osd_brw_stats_update(osd, iobuf);
273 iobuf->dr_start_time = cfs_time_current();
275 for (page_idx = 0, block_idx = 0;
277 page_idx++, block_idx += blocks_per_page) {
279 page = pages[page_idx];
280 LASSERT(block_idx + blocks_per_page <= total_blocks);
282 for (i = 0, page_offset = 0;
284 i += nblocks, page_offset += blocksize * nblocks) {
288 if (blocks[block_idx + i] == 0) { /* hole */
289 LASSERTF(iobuf->dr_rw == 0,
290 "page_idx %u, block_idx %u, i %u\n",
291 page_idx, block_idx, i);
292 memset(kmap(page) + page_offset, 0, blocksize);
297 sector = (sector_t)blocks[block_idx + i] << sector_bits;
299 /* Additional contiguous file blocks? */
300 while (i + nblocks < blocks_per_page &&
301 (sector + (nblocks << sector_bits)) ==
302 ((sector_t)blocks[block_idx + i + nblocks] <<
306 /* I only set the page to be constant only if it
307 * is mapped to a contiguous underlying disk block(s).
308 * It will then make sure the corresponding device
309 * cache of raid5 will be overwritten by this page.
311 if (iobuf->dr_rw && (nblocks == blocks_per_page) &&
312 mapping_cap_page_constant_write(inode->i_mapping))
313 SetPageConstant(page);
316 can_be_merged(bio, sector) &&
317 bio_add_page(bio, page,
318 blocksize * nblocks, page_offset) != 0)
319 continue; /* added this frag OK */
322 struct request_queue *q =
323 bdev_get_queue(bio->bi_bdev);
325 /* Dang! I have to fragment this I/O */
326 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
327 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
329 bio->bi_vcnt, bio->bi_max_vecs,
330 bio->bi_size >> 9, queue_max_sectors(q),
331 bio_phys_segments(q, bio),
332 queue_max_phys_segments(q),
333 bio_hw_segments(q, bio),
334 queue_max_hw_segments(q));
336 record_start_io(iobuf, bio->bi_size);
337 osd_submit_bio(iobuf->dr_rw, bio);
340 /* allocate new bio, limited by max BIO size, b=9945 */
341 bio = bio_alloc(GFP_NOIO, max(BIO_MAX_PAGES,
342 (npages - page_idx) *
345 CERROR("Can't allocate bio %u*%u = %u pages\n",
346 (npages - page_idx), blocks_per_page,
347 (npages - page_idx) * blocks_per_page);
352 bio->bi_bdev = inode->i_sb->s_bdev;
353 bio->bi_sector = sector;
354 bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
355 bio->bi_end_io = dio_complete_routine;
356 bio->bi_private = iobuf;
358 rc = bio_add_page(bio, page,
359 blocksize * nblocks, page_offset);
365 record_start_io(iobuf, bio->bi_size);
366 osd_submit_bio(iobuf->dr_rw, bio);
371 /* in order to achieve better IO throughput, we don't wait for writes
372 * completion here. instead we proceed with transaction commit in
373 * parallel and wait for IO completion once transaction is stopped
374 * see osd_trans_stop() for more details -bzzz */
375 if (iobuf->dr_rw == 0) {
376 cfs_wait_event(iobuf->dr_wait,
377 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
381 rc = iobuf->dr_error;
385 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
386 struct niobuf_local *lnb)
393 int poff = offset & (CFS_PAGE_SIZE - 1);
394 int plen = CFS_PAGE_SIZE - poff;
398 lnb->offset = offset;
399 /* lnb->lnb_page_offset = poff; */
401 /* lb->flags = rnb->flags; */
406 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
417 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
419 struct inode *inode = osd_dt_obj(dt)->oo_inode;
420 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
425 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
426 GFP_NOFS | __GFP_HIGHMEM);
427 if (unlikely(page == NULL))
428 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
434 * there are following "locks":
452 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
453 ssize_t len, struct niobuf_local *lnb, int rw,
454 struct lustre_capa *capa)
456 struct osd_object *obj = osd_dt_obj(d);
457 int npages, i, rc = 0;
459 LASSERT(obj->oo_inode);
461 osd_map_remote_to_local(pos, len, &npages, lnb);
463 for (i = 0; i < npages; i++, lnb++) {
465 /* We still set up for ungranted pages so that granted pages
466 * can be written to disk as they were promised, and portals
467 * needs to keep the pages all aligned properly. */
468 lnb->dentry = (void *) obj;
470 lnb->page = osd_get_page(d, lnb->offset, rw);
471 if (lnb->page == NULL)
472 GOTO(cleanup, rc = -ENOMEM);
474 /* DLM locking protects us from write and truncate competing
475 * for same region, but truncate can leave dirty page in the
476 * cache. it's possible the writeout on a such a page is in
477 * progress when we access it. it's also possible that during
478 * this writeout we put new (partial) data, but then won't
479 * be able to proceed in filter_commitrw_write(). thus let's
480 * just wait for writeout completion, should be rare enough.
482 wait_on_page_writeback(lnb->page);
483 BUG_ON(PageWriteback(lnb->page));
485 lu_object_get(&d->do_lu);
493 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
494 struct niobuf_local *lnb, int npages)
496 struct osd_thread_info *oti = osd_oti_get(env);
497 struct osd_iobuf *iobuf = &oti->oti_iobuf;
498 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
501 /* to do IO stats, notice we do this here because
502 * osd_do_bio() doesn't wait for write to complete */
503 osd_fini_iobuf(d, iobuf);
505 for (i = 0; i < npages; i++) {
506 if (lnb[i].page == NULL)
508 LASSERT(PageLocked(lnb[i].page));
509 unlock_page(lnb[i].page);
510 page_cache_release(lnb[i].page);
511 lu_object_put(env, &dt->do_lu);
517 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
518 struct niobuf_local *lnb, int npages)
520 struct osd_thread_info *oti = osd_oti_get(env);
521 struct osd_iobuf *iobuf = &oti->oti_iobuf;
522 struct inode *inode = osd_dt_obj(dt)->oo_inode;
523 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
524 struct timeval start;
526 unsigned long timediff;
535 osd_init_iobuf(osd, iobuf, 0);
537 isize = i_size_read(inode);
538 maxidx = ((isize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
540 if (osd->od_writethrough_cache)
542 if (isize > osd->od_readcache_max_filesize)
545 cfs_gettimeofday(&start);
546 for (i = 0; i < npages; i++) {
549 generic_error_remove_page(inode->i_mapping,
553 * till commit the content of the page is undefined
554 * we'll set it uptodate once bulk is done. otherwise
555 * subsequent reads can access non-stable data
557 ClearPageUptodate(lnb[i].page);
559 if (lnb[i].len == CFS_PAGE_SIZE)
562 if (maxidx >= lnb[i].page->index) {
563 osd_iobuf_add_page(iobuf, lnb[i].page);
566 char *p = kmap(lnb[i].page);
571 off = lnb[i].offset + lnb[i].len;
572 off &= ~CFS_PAGE_MASK;
574 memset(p + off, 0, CFS_PAGE_SIZE - off);
578 cfs_gettimeofday(&end);
579 timediff = cfs_timeval_sub(&end, &start, NULL);
580 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
582 if (iobuf->dr_npages) {
583 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
588 if (likely(rc == 0)) {
589 rc = osd_do_bio(osd, inode, iobuf);
590 /* do IO stats for preparation reads */
591 osd_fini_iobuf(osd, iobuf);
597 static int osd_declare_write_commit(const struct lu_env *env,
598 struct dt_object *dt,
599 struct niobuf_local *lnb, int npages,
600 struct thandle *handle)
602 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
603 struct inode *inode = osd_dt_obj(dt)->oo_inode;
604 struct osd_thandle *oh;
611 LASSERT(handle != NULL);
612 oh = container_of0(handle, struct osd_thandle, ot_super);
613 LASSERT(oh->ot_handle == NULL);
615 old = oh->ot_credits;
618 /* calculate number of extents (probably better to pass nb) */
619 for (i = 1; i < npages; i++)
621 lnb[i - 1].offset + lnb[i - 1].len)
625 * each extent can go into new leaf causing a split
626 * 5 is max tree depth: inode + 4 index blocks
627 * with blockmaps, depth is 3 at most
629 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
631 * many concurrent threads may grow tree by the time
632 * our transaction starts. so, consider 2 is a min depth
634 depth = ext_depth(inode);
635 depth = max(depth, 1) + 1;
637 oh->ot_credits++; /* inode */
638 oh->ot_credits += depth * 2 * extents;
642 oh->ot_credits++; /* inode */
643 oh->ot_credits += depth * extents;
646 /* each new block can go in different group (bitmap + gd) */
648 /* we can't dirty more bitmap blocks than exist */
649 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
650 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
652 oh->ot_credits += newblocks;
654 /* we can't dirty more gd blocks than exist */
655 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
656 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
658 oh->ot_credits += newblocks;
663 /* Check if a block is allocated or not */
664 static int osd_is_mapped(struct inode *inode, obd_size offset)
666 sector_t (*fs_bmap)(struct address_space *, sector_t);
668 fs_bmap = inode->i_mapping->a_ops->bmap;
670 /* We can't know if we are overwriting or not */
674 if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
680 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
681 struct niobuf_local *lnb, int npages,
682 struct thandle *thandle)
684 struct osd_thread_info *oti = osd_oti_get(env);
685 struct osd_iobuf *iobuf = &oti->oti_iobuf;
686 struct inode *inode = osd_dt_obj(dt)->oo_inode;
687 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
693 osd_init_iobuf(osd, iobuf, 1);
694 isize = i_size_read(inode);
695 ll_vfs_dq_init(inode);
697 for (i = 0; i < npages; i++) {
698 if (lnb[i].rc == -ENOSPC &&
699 osd_is_mapped(inode, lnb[i].offset)) {
700 /* Allow the write to proceed if overwriting an
705 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
706 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
708 LASSERT(lnb[i].page);
709 generic_error_remove_page(inode->i_mapping,lnb[i].page);
713 LASSERT(PageLocked(lnb[i].page));
714 LASSERT(!PageWriteback(lnb[i].page));
716 if (lnb[i].offset + lnb[i].len > isize)
717 isize = lnb[i].offset + lnb[i].len;
720 * Since write and truncate are serialized by oo_sem, even
721 * partial-page truncate should not leave dirty pages in the
724 LASSERT(!PageDirty(lnb[i].page));
726 SetPageUptodate(lnb[i].page);
728 osd_iobuf_add_page(iobuf, lnb[i].page);
731 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
733 } else if (iobuf->dr_npages > 0) {
734 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
740 /* no pages to write, no transno is needed */
741 thandle->th_local = 1;
744 if (likely(rc == 0)) {
745 if (isize > i_size_read(inode)) {
746 i_size_write(inode, isize);
747 LDISKFS_I(inode)->i_disksize = isize;
748 inode->i_sb->s_op->dirty_inode(inode);
751 rc = osd_do_bio(osd, inode, iobuf);
752 /* we don't do stats here as in read path because
753 * write is async: we'll do this in osd_put_bufs() */
756 if (unlikely(rc != 0)) {
757 /* if write fails, we should drop pages from the cache */
758 for (i = 0; i < npages; i++) {
759 if (lnb[i].page == NULL)
761 LASSERT(PageLocked(lnb[i].page));
762 generic_error_remove_page(inode->i_mapping,lnb[i].page);
769 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
770 struct niobuf_local *lnb, int npages)
772 struct osd_thread_info *oti = osd_oti_get(env);
773 struct osd_iobuf *iobuf = &oti->oti_iobuf;
774 struct inode *inode = osd_dt_obj(dt)->oo_inode;
775 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
776 struct timeval start, end;
777 unsigned long timediff;
778 int rc = 0, i, m = 0, cache = 0;
782 osd_init_iobuf(osd, iobuf, 0);
784 if (osd->od_read_cache)
786 if (i_size_read(inode) > osd->od_readcache_max_filesize)
789 cfs_gettimeofday(&start);
790 for (i = 0; i < npages; i++) {
792 if (i_size_read(inode) <= lnb[i].offset)
793 /* If there's no more data, abort early.
794 * lnb->rc == 0, so it's easy to detect later. */
797 if (i_size_read(inode) <
798 lnb[i].offset + lnb[i].len - 1)
799 lnb[i].rc = i_size_read(inode) - lnb[i].offset;
801 lnb[i].rc = lnb[i].len;
804 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
805 if (PageUptodate(lnb[i].page)) {
806 lprocfs_counter_add(osd->od_stats,
807 LPROC_OSD_CACHE_HIT, 1);
809 lprocfs_counter_add(osd->od_stats,
810 LPROC_OSD_CACHE_MISS, 1);
811 osd_iobuf_add_page(iobuf, lnb[i].page);
814 generic_error_remove_page(inode->i_mapping,lnb[i].page);
816 cfs_gettimeofday(&end);
817 timediff = cfs_timeval_sub(&end, &start, NULL);
818 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
820 if (iobuf->dr_npages) {
821 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
826 rc = osd_do_bio(osd, inode, iobuf);
828 /* IO stats will be done in osd_bufs_put() */
835 * XXX: Another layering violation for now.
837 * We don't want to use ->f_op->read methods, because generic file write
839 * - serializes on ->i_sem, and
841 * - does a lot of extra work like balance_dirty_pages(),
843 * which doesn't work for globally shared files like /last_rcvd.
845 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
847 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
849 memcpy(buffer, (char *)ei->i_data, buflen);
854 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
856 struct buffer_head *bh;
864 /* prevent reading after eof */
865 cfs_spin_lock(&inode->i_lock);
866 if (i_size_read(inode) < *offs + size) {
867 loff_t diff = i_size_read(inode) - *offs;
868 cfs_spin_unlock(&inode->i_lock);
870 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
871 i_size_read(inode), *offs);
873 } else if (diff == 0) {
879 cfs_spin_unlock(&inode->i_lock);
882 blocksize = 1 << inode->i_blkbits;
885 block = *offs >> inode->i_blkbits;
886 boffs = *offs & (blocksize - 1);
887 csize = min(blocksize - boffs, size);
888 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
890 CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
891 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
892 csize, *offs, inode->i_ino, err);
896 memcpy(buf, bh->b_data + boffs, csize);
906 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
907 struct lu_buf *buf, loff_t *pos,
908 struct lustre_capa *capa)
910 struct inode *inode = osd_dt_obj(dt)->oo_inode;
913 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
916 /* Read small symlink from inode body as we need to maintain correct
917 * on-disk symlinks for ldiskfs.
919 if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
920 (buf->lb_len <= sizeof(LDISKFS_I(inode)->i_data)))
921 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
923 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
928 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
929 const loff_t size, loff_t pos,
930 struct thandle *handle)
932 struct osd_thandle *oh;
935 LASSERT(handle != NULL);
937 oh = container_of0(handle, struct osd_thandle, ot_super);
938 LASSERT(oh->ot_handle == NULL);
940 /* XXX: size == 0 or INT_MAX indicating a catalog header update or
941 * llog write, see comment in mdd_declare_llog_record().
943 * This hack will be removed with llog over OSD landing
945 if (size == DECLARE_LLOG_REWRITE)
947 else if (size == DECLARE_LLOG_WRITE)
950 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
952 OSD_DECLARE_OP(oh, write);
953 oh->ot_credits += credits;
955 if (osd_dt_obj(dt)->oo_inode == NULL)
958 osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
959 osd_dt_obj(dt)->oo_inode);
960 osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
961 osd_dt_obj(dt)->oo_inode);
965 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
968 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
969 LDISKFS_I(inode)->i_disksize = buflen;
970 i_size_write(inode, buflen);
971 inode->i_sb->s_op->dirty_inode(inode);
976 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
977 int write_NUL, loff_t *offs, handle_t *handle)
979 struct buffer_head *bh = NULL;
980 loff_t offset = *offs;
981 loff_t new_size = i_size_read(inode);
983 int blocksize = 1 << inode->i_blkbits;
991 * long symlink write does not count the NUL terminator in
992 * bufsize, we write it, and the inode's file size does not
993 * count the NUL terminator as well.
995 ((char *)buf)[bufsize] = '\0';
998 while (bufsize > 0) {
1002 block = offset >> inode->i_blkbits;
1003 boffs = offset & (blocksize - 1);
1004 size = min(blocksize - boffs, bufsize);
1005 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1007 CERROR("%s: error reading offset %llu (block %lu): "
1009 inode->i_sb->s_id, offset, block, err);
1013 err = ldiskfs_journal_get_write_access(handle, bh);
1015 CERROR("journal_get_write_access() returned error %d\n",
1019 LASSERTF(boffs + size <= bh->b_size,
1020 "boffs %d size %d bh->b_size %lu",
1021 boffs, size, (unsigned long)bh->b_size);
1022 memcpy(bh->b_data + boffs, buf, size);
1023 err = ldiskfs_journal_dirty_metadata(handle, bh);
1027 if (offset + size > new_size)
1028 new_size = offset + size;
1038 /* correct in-core and on-disk sizes */
1039 if (new_size > i_size_read(inode)) {
1040 cfs_spin_lock(&inode->i_lock);
1041 if (new_size > i_size_read(inode))
1042 i_size_write(inode, new_size);
1043 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1044 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1047 cfs_spin_unlock(&inode->i_lock);
1049 inode->i_sb->s_op->dirty_inode(inode);
1057 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1058 const struct lu_buf *buf, loff_t *pos,
1059 struct thandle *handle, struct lustre_capa *capa,
1062 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1063 struct osd_thandle *oh;
1065 #ifdef HAVE_QUOTA_SUPPORT
1066 cfs_cap_t save = cfs_curproc_cap_pack();
1070 LASSERT(dt_object_exists(dt));
1072 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1075 LASSERT(handle != NULL);
1077 /* XXX: don't check: one declared chunk can be used many times */
1078 /* OSD_EXEC_OP(handle, write); */
1080 oh = container_of(handle, struct osd_thandle, ot_super);
1081 LASSERT(oh->ot_handle->h_transaction != NULL);
1082 #ifdef HAVE_QUOTA_SUPPORT
1084 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1086 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1088 /* Write small symlink to inode body as we need to maintain correct
1089 * on-disk symlinks for ldiskfs.
1090 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1091 * does not count it in.
1093 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1094 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1095 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1097 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1098 buf->lb_len, is_link, pos,
1100 #ifdef HAVE_QUOTA_SUPPORT
1101 cfs_curproc_cap_unpack(save);
1104 result = buf->lb_len;
1108 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1109 __u64 start, __u64 end, struct thandle *th)
1111 struct osd_thandle *oh;
1115 oh = container_of(th, struct osd_thandle, ot_super);
1117 OSD_DECLARE_OP(oh, punch);
1120 * we don't need to reserve credits for whole truncate
1121 * it's not possible as truncate may need to free too many
1122 * blocks and that won't fit a single transaction. instead
1123 * we reserve credits to change i_size and put inode onto
1124 * orphan list. if needed truncate will extend or restart
1127 oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1128 oh->ot_credits += 3;
1133 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1134 __u64 start, __u64 end, struct thandle *th,
1135 struct lustre_capa *capa)
1137 struct osd_thandle *oh;
1138 struct osd_object *obj = osd_dt_obj(dt);
1139 struct inode *inode = obj->oo_inode;
1145 LASSERT(end == OBD_OBJECT_EOF);
1146 LASSERT(dt_object_exists(dt));
1147 LASSERT(osd_invariant(obj));
1150 oh = container_of(th, struct osd_thandle, ot_super);
1151 LASSERT(oh->ot_handle->h_transaction != NULL);
1153 OSD_EXEC_OP(th, punch);
1155 tid = oh->ot_handle->h_transaction->t_tid;
1157 rc = vmtruncate(inode, start);
1160 * For a partial-page truncate, flush the page to disk immediately to
1161 * avoid data corruption during direct disk write. b=17397
1163 if (rc == 0 && (start & ~CFS_PAGE_MASK) != 0)
1164 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1166 h = journal_current_handle();
1168 LASSERT(h == oh->ot_handle);
1170 if (tid != h->h_transaction->t_tid) {
1171 int credits = oh->ot_credits;
1173 * transaction has changed during truncate
1174 * we need to restart the handle with our credits
1176 if (h->h_buffer_credits < credits) {
1177 if (ldiskfs_journal_extend(h, credits))
1178 rc2 = ldiskfs_journal_restart(h, credits);
1182 RETURN(rc == 0 ? rc2 : rc);
1185 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1186 struct ll_user_fiemap *fm)
1188 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1189 struct osd_thread_info *info = osd_oti_get(env);
1190 struct dentry *dentry = &info->oti_obj_dentry;
1191 struct file *file = &info->oti_file;
1192 mm_segment_t saved_fs;
1196 dentry->d_inode = inode;
1197 file->f_dentry = dentry;
1198 file->f_mapping = inode->i_mapping;
1199 file->f_op = inode->i_fop;
1201 saved_fs = get_fs();
1203 /* ldiskfs_ioctl does not have a inode argument */
1204 if (inode->i_fop->unlocked_ioctl)
1205 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1214 * in some cases we may need declare methods for objects being created
1215 * e.g., when we create symlink
1217 const struct dt_body_operations osd_body_ops_new = {
1218 .dbo_declare_write = osd_declare_write,
1221 const struct dt_body_operations osd_body_ops = {
1222 .dbo_read = osd_read,
1223 .dbo_declare_write = osd_declare_write,
1224 .dbo_write = osd_write,
1225 .dbo_bufs_get = osd_bufs_get,
1226 .dbo_bufs_put = osd_bufs_put,
1227 .dbo_write_prep = osd_write_prep,
1228 .dbo_declare_write_commit = osd_declare_write_commit,
1229 .dbo_write_commit = osd_write_commit,
1230 .dbo_read_prep = osd_read_prep,
1231 .do_declare_punch = osd_declare_punch,
1232 .do_punch = osd_punch,
1233 .dbo_fiemap_get = osd_fiemap_get,