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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
35 * Author: Nikita Danilov <nikita@clusterfs.com>
36 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40 #define DEBUG_SUBSYSTEM S_OSD
42 /* prerequisite for linux/xattr.h */
43 #include <linux/types.h>
44 /* prerequisite for linux/xattr.h */
47 #include <linux/swap.h>
48 #include <linux/pagevec.h>
51 * struct OBD_{ALLOC,FREE}*()
54 #include <obd_support.h>
56 #include "osd_internal.h"
59 #include <ldiskfs/ldiskfs_extents.h>
60 #include <ldiskfs/ldiskfs.h>
62 static inline bool osd_use_page_cache(struct osd_device *d)
64 /* do not use pagecache if write and read caching are disabled */
65 if (d->od_writethrough_cache + d->od_read_cache == 0)
67 /* use pagecache by default */
71 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
72 int rw, int line, int pages)
76 LASSERTF(iobuf->dr_elapsed_valid == 0,
77 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
78 atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
80 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
82 init_waitqueue_head(&iobuf->dr_wait);
83 atomic_set(&iobuf->dr_numreqs, 0);
88 iobuf->dr_elapsed = ktime_set(0, 0);
89 /* must be counted before, so assert */
91 iobuf->dr_init_at = line;
93 blocks = pages * (PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
94 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
95 LASSERT(iobuf->dr_pg_buf.lb_len >=
96 pages * sizeof(iobuf->dr_pages[0]));
100 /* start with 1MB for 4K blocks */
102 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
105 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
106 (unsigned int)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
108 blocks = pages * (PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
109 iobuf->dr_max_pages = 0;
110 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
111 (unsigned int)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
113 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
114 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
115 if (unlikely(iobuf->dr_blocks == NULL))
118 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
119 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
120 if (unlikely(iobuf->dr_pages == NULL))
123 lu_buf_realloc(&iobuf->dr_lnb_buf,
124 pages * sizeof(iobuf->dr_lnbs[0]));
125 iobuf->dr_lnbs = iobuf->dr_lnb_buf.lb_buf;
126 if (unlikely(iobuf->dr_lnbs == NULL))
129 iobuf->dr_max_pages = pages;
133 #define osd_init_iobuf(dev, iobuf, rw, pages) \
134 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
136 static void osd_iobuf_add_page(struct osd_iobuf *iobuf,
137 struct niobuf_local *lnb)
139 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
140 iobuf->dr_pages[iobuf->dr_npages] = lnb->lnb_page;
141 iobuf->dr_lnbs[iobuf->dr_npages] = lnb;
145 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
147 int rw = iobuf->dr_rw;
149 if (iobuf->dr_elapsed_valid) {
150 iobuf->dr_elapsed_valid = 0;
151 LASSERT(iobuf->dr_dev == d);
152 LASSERT(iobuf->dr_frags > 0);
153 lprocfs_oh_tally(&d->od_brw_stats.hist[BRW_R_DIO_FRAGS+rw],
155 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
156 ktime_to_ms(iobuf->dr_elapsed));
160 #ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
161 static void dio_complete_routine(struct bio *bio)
163 int error = blk_status_to_errno(bio->bi_status);
165 static void dio_complete_routine(struct bio *bio, int error)
168 struct osd_iobuf *iobuf = bio->bi_private;
171 /* CAVEAT EMPTOR: possibly in IRQ context
172 * DO NOT record procfs stats here!!!
175 if (unlikely(iobuf == NULL)) {
176 CERROR("***** bio->bi_private is NULL! Dump the bio contents to the console. Please report this to <https://jira.whamcloud.com/>, and probably have to reboot this node.\n");
177 CERROR("bi_next: %p, bi_flags: %lx, " __stringify(bi_opf)
178 ": %x, bi_vcnt: %d, bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, bi_private: %p\n",
179 bio->bi_next, (unsigned long)bio->bi_flags,
180 (unsigned int)bio->bi_opf, bio->bi_vcnt, bio_idx(bio),
181 bio_sectors(bio) << 9, bio->bi_end_io,
182 atomic_read(&bio->__bi_cnt),
187 /* the check is outside of the cycle for performance reason -bzzz */
188 if (!bio_data_dir(bio)) {
189 DECLARE_BVEC_ITER_ALL(iter_all);
191 bio_for_each_segment_all(bvl, bio, iter_all) {
192 if (likely(error == 0))
193 SetPageUptodate(bvl_to_page(bvl));
194 LASSERT(PageLocked(bvl_to_page(bvl)));
196 atomic_dec(&iobuf->dr_dev->od_r_in_flight);
198 atomic_dec(&iobuf->dr_dev->od_w_in_flight);
201 /* any real error is good enough -bzzz */
202 if (error != 0 && iobuf->dr_error == 0)
203 iobuf->dr_error = error;
206 * set dr_elapsed before dr_numreqs turns to 0, otherwise
207 * it's possible that service thread will see dr_numreqs
208 * is zero, but dr_elapsed is not set yet, leading to lost
209 * data in this processing and an assertion in a subsequent
212 if (atomic_read(&iobuf->dr_numreqs) == 1) {
213 ktime_t now = ktime_get();
215 iobuf->dr_elapsed = ktime_sub(now, iobuf->dr_start_time);
216 iobuf->dr_elapsed_valid = 1;
218 if (atomic_dec_and_test(&iobuf->dr_numreqs))
219 wake_up(&iobuf->dr_wait);
221 /* Completed bios used to be chained off iobuf->dr_bios and freed in
222 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
223 * mempool when serious on-disk fragmentation was encountered,
224 * deadlocking the OST. The bios are now released as soon as complete
225 * so the pool cannot be exhausted while IOs are competing. b=10076
230 static void record_start_io(struct osd_iobuf *iobuf, int size)
232 struct osd_device *osd = iobuf->dr_dev;
233 struct obd_histogram *h = osd->od_brw_stats.hist;
236 atomic_inc(&iobuf->dr_numreqs);
238 if (iobuf->dr_rw == 0) {
239 atomic_inc(&osd->od_r_in_flight);
240 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
241 atomic_read(&osd->od_r_in_flight));
242 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
243 } else if (iobuf->dr_rw == 1) {
244 atomic_inc(&osd->od_w_in_flight);
245 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
246 atomic_read(&osd->od_w_in_flight));
247 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
253 static void osd_submit_bio(int rw, struct bio *bio)
255 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
256 #ifdef HAVE_SUBMIT_BIO_2ARGS
257 submit_bio(rw ? WRITE : READ, bio);
264 static int can_be_merged(struct bio *bio, sector_t sector)
269 return bio_end_sector(bio) == sector ? 1 : 0;
272 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
274 * This function will change the data written, thus it should only be
275 * used when checking data integrity feature
277 static void bio_integrity_fault_inject(struct bio *bio)
279 struct bio_vec *bvec;
280 DECLARE_BVEC_ITER_ALL(iter_all);
284 bio_for_each_segment_all(bvec, bio, iter_all) {
285 struct page *page = bvec->bv_page;
295 static int bio_dif_compare(__u16 *expected_guard_buf, void *bio_prot_buf,
296 unsigned int sectors, int tuple_size)
298 __u16 *expected_guard;
302 expected_guard = expected_guard_buf;
303 for (i = 0; i < sectors; i++) {
304 bio_guard = (__u16 *)bio_prot_buf;
305 if (*bio_guard != *expected_guard) {
307 "unexpected guard tags on sector %d expected guard %u, bio guard %u, sectors %u, tuple size %d\n",
308 i, *expected_guard, *bio_guard, sectors,
313 bio_prot_buf += tuple_size;
318 static int osd_bio_integrity_compare(struct bio *bio, struct block_device *bdev,
319 struct osd_iobuf *iobuf, int index)
321 struct blk_integrity *bi = bdev_get_integrity(bdev);
322 struct bio_integrity_payload *bip = bio->bi_integrity;
323 struct niobuf_local *lnb = NULL;
324 unsigned short sector_size = blk_integrity_interval(bi);
325 void *bio_prot_buf = page_address(bip->bip_vec->bv_page) +
326 bip->bip_vec->bv_offset;
328 sector_t sector = bio_start_sector(bio);
329 unsigned int i, sectors, total;
330 DECLARE_BVEC_ITER_ALL(iter_all);
331 __u16 *expected_guard;
335 bio_for_each_segment_all(bv, bio, iter_all) {
336 for (i = index; i < iobuf->dr_npages; i++) {
337 if (iobuf->dr_pages[i] == bv->bv_page) {
338 lnb = iobuf->dr_lnbs[i];
344 expected_guard = lnb->lnb_guards;
345 sectors = bv->bv_len / sector_size;
346 if (lnb->lnb_guard_rpc) {
347 rc = bio_dif_compare(expected_guard, bio_prot_buf,
348 sectors, bi->tuple_size);
354 bio_prot_buf += sectors * bi->tuple_size;
355 total += sectors * bi->tuple_size;
356 LASSERT(total <= bip_size(bio->bi_integrity));
363 static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
364 struct osd_iobuf *iobuf,
365 int start_page_idx, bool fault_inject,
366 bool integrity_enabled)
368 struct super_block *sb = osd_sb(osd);
369 integrity_gen_fn *generate_fn = NULL;
370 integrity_vrfy_fn *verify_fn = NULL;
375 if (!integrity_enabled)
378 rc = osd_get_integrity_profile(osd, &generate_fn, &verify_fn);
382 rc = bio_integrity_prep_fn(bio, generate_fn, verify_fn);
386 /* Verify and inject fault only when writing */
387 if (iobuf->dr_rw == 1) {
388 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_CMP))) {
389 rc = osd_bio_integrity_compare(bio, sb->s_bdev, iobuf,
395 if (unlikely(fault_inject))
396 bio_integrity_fault_inject(bio);
402 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
403 # ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
404 static void dio_integrity_complete_routine(struct bio *bio)
406 static void dio_integrity_complete_routine(struct bio *bio, int error)
409 struct osd_bio_private *bio_private = bio->bi_private;
411 bio->bi_private = bio_private->obp_iobuf;
412 osd_dio_complete_routine(bio, error);
414 OBD_FREE_PTR(bio_private);
416 #endif /* HAVE_BIO_INTEGRITY_PREP_FN */
417 #else /* !CONFIG_BLK_DEV_INTEGRITY */
418 #define osd_bio_integrity_handle(osd, bio, iobuf, start_page_idx, \
419 fault_inject, integrity_enabled) 0
420 #endif /* CONFIG_BLK_DEV_INTEGRITY */
422 static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
423 bool integrity_enabled, int start_page_idx,
424 struct osd_bio_private **pprivate)
430 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
431 if (integrity_enabled) {
432 struct osd_bio_private *bio_private = NULL;
434 OBD_ALLOC_GFP(bio_private, sizeof(*bio_private), GFP_NOIO);
435 if (bio_private == NULL)
437 bio->bi_end_io = dio_integrity_complete_routine;
438 bio->bi_private = bio_private;
439 bio_private->obp_start_page_idx = start_page_idx;
440 bio_private->obp_iobuf = iobuf;
441 *pprivate = bio_private;
445 bio->bi_end_io = dio_complete_routine;
446 bio->bi_private = iobuf;
452 static void osd_mark_page_io_done(struct osd_iobuf *iobuf,
454 sector_t start_blocks,
457 struct niobuf_local *lnb;
458 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
459 pgoff_t pg_start, pg_end;
461 pg_start = start_blocks / blocks_per_page;
462 if (start_blocks % blocks_per_page)
464 if (count >= blocks_per_page)
465 pg_end = (start_blocks + count -
466 blocks_per_page) / blocks_per_page;
468 return; /* nothing to mark */
469 for ( ; pg_start <= pg_end; pg_start++) {
470 lnb = iobuf->dr_lnbs[pg_start];
471 lnb->lnb_flags |= OBD_BRW_DONE;
475 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
476 struct osd_iobuf *iobuf, sector_t start_blocks,
479 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
480 struct page **pages = iobuf->dr_pages;
481 int npages = iobuf->dr_npages;
482 sector_t *blocks = iobuf->dr_blocks;
483 struct super_block *sb = inode->i_sb;
484 int sector_bits = sb->s_blocksize_bits - 9;
485 unsigned int blocksize = sb->s_blocksize;
486 struct block_device *bdev = sb->s_bdev;
487 struct osd_bio_private *bio_private = NULL;
488 struct bio *bio = NULL;
489 int bio_start_page_idx;
491 unsigned int page_offset;
494 int block_idx, block_idx_end;
495 int page_idx, page_idx_start;
499 bool integrity_enabled;
500 struct blk_plug plug;
501 int blocks_left_page;
505 fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
506 LASSERT(iobuf->dr_npages == npages);
508 integrity_enabled = bdev_integrity_enabled(bdev, iobuf->dr_rw);
510 osd_brw_stats_update(osd, iobuf);
511 iobuf->dr_start_time = ktime_get();
514 count = npages * blocks_per_page;
515 block_idx_end = start_blocks + count;
517 blk_start_plug(&plug);
519 page_idx_start = start_blocks / blocks_per_page;
520 for (page_idx = page_idx_start, block_idx = start_blocks;
521 block_idx < block_idx_end; page_idx++,
522 block_idx += blocks_left_page) {
523 page = pages[page_idx];
524 LASSERT(page_idx < iobuf->dr_npages);
526 i = block_idx % blocks_per_page;
527 blocks_left_page = blocks_per_page - i;
528 for (page_offset = i * blocksize; i < blocks_left_page;
529 i += nblocks, page_offset += blocksize * nblocks) {
532 if (blocks[block_idx + i] == 0) { /* hole */
533 LASSERTF(iobuf->dr_rw == 0,
534 "page_idx %u, block_idx %u, i %u,"
535 "start_blocks: %llu, count: %llu, npages: %d\n",
536 page_idx, block_idx, i,
537 (unsigned long long)start_blocks,
538 (unsigned long long)count, npages);
539 memset(kmap(page) + page_offset, 0, blocksize);
544 sector = (sector_t)blocks[block_idx + i] << sector_bits;
546 /* Additional contiguous file blocks? */
547 while (i + nblocks < blocks_left_page &&
548 (sector + (nblocks << sector_bits)) ==
549 ((sector_t)blocks[block_idx + i + nblocks] <<
553 if (bio && can_be_merged(bio, sector) &&
554 bio_add_page(bio, page, blocksize * nblocks,
556 continue; /* added this frag OK */
559 struct request_queue *q = bio_get_queue(bio);
560 unsigned int bi_size = bio_sectors(bio) << 9;
562 /* Dang! I have to fragment this I/O */
564 "bio++ sz %d vcnt %d(%d) sectors %d(%d) psg %d(%d)\n",
565 bi_size, bio->bi_vcnt, bio->bi_max_vecs,
567 queue_max_sectors(q),
568 osd_bio_nr_segs(bio),
569 queue_max_segments(q));
570 rc = osd_bio_integrity_handle(osd, bio,
571 iobuf, bio_start_page_idx,
572 fault_inject, integrity_enabled);
578 record_start_io(iobuf, bi_size);
579 osd_submit_bio(iobuf->dr_rw, bio);
582 bio_start_page_idx = page_idx;
583 /* allocate new bio */
584 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
585 (block_idx_end - block_idx +
586 blocks_left_page - 1)));
588 CERROR("Can't allocate bio %u pages\n",
589 block_idx_end - block_idx +
590 blocks_left_page - 1);
595 bio_set_dev(bio, bdev);
596 bio_set_sector(bio, sector);
597 bio->bi_opf = iobuf->dr_rw ? WRITE : READ;
598 rc = osd_bio_init(bio, iobuf, integrity_enabled,
599 bio_start_page_idx, &bio_private);
605 rc = bio_add_page(bio, page,
606 blocksize * nblocks, page_offset);
612 rc = osd_bio_integrity_handle(osd, bio, iobuf,
621 record_start_io(iobuf, bio_sectors(bio) << 9);
622 osd_submit_bio(iobuf->dr_rw, bio);
627 blk_finish_plug(&plug);
629 /* in order to achieve better IO throughput, we don't wait for writes
630 * completion here. instead we proceed with transaction commit in
631 * parallel and wait for IO completion once transaction is stopped
632 * see osd_trans_stop() for more details -bzzz
634 if (iobuf->dr_rw == 0 || fault_inject) {
635 wait_event(iobuf->dr_wait,
636 atomic_read(&iobuf->dr_numreqs) == 0);
637 osd_fini_iobuf(osd, iobuf);
641 rc = iobuf->dr_error;
644 OBD_FREE_PTR(bio_private);
648 if (rc == 0 && iobuf->dr_rw)
649 osd_mark_page_io_done(iobuf, inode,
650 start_blocks, count);
655 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
656 struct niobuf_local *lnb, int maxlnb)
664 int poff = offset & (PAGE_SIZE - 1);
665 int plen = PAGE_SIZE - poff;
667 if (*nrpages >= maxlnb) {
674 lnb->lnb_file_offset = offset;
675 lnb->lnb_page_offset = poff;
677 /* lnb->lnb_flags = rnb->rnb_flags; */
679 lnb->lnb_page = NULL;
681 lnb->lnb_guard_rpc = 0;
682 lnb->lnb_guard_disk = 0;
685 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
696 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
697 loff_t offset, gfp_t gfp_mask, bool cache)
699 struct osd_thread_info *oti = osd_oti_get(env);
700 struct inode *inode = osd_dt_obj(dt)->oo_inode;
701 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
708 page = find_or_create_page(inode->i_mapping,
709 offset >> PAGE_SHIFT, gfp_mask);
712 LASSERT(!PagePrivate2(page));
713 wait_on_page_writeback(page);
715 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
721 if (inode->i_mapping->nrpages) {
722 /* consult with pagecache, but do not create new pages */
723 /* this is normally used once */
724 page = find_lock_page(inode->i_mapping, offset >> PAGE_SHIFT);
726 wait_on_page_writeback(page);
731 LASSERT(oti->oti_dio_pages);
732 cur = oti->oti_dio_pages_used;
733 page = oti->oti_dio_pages[cur];
735 if (unlikely(!page)) {
736 LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
737 page = alloc_page(gfp_mask);
740 oti->oti_dio_pages[cur] = page;
741 SetPagePrivate2(page);
745 ClearPageUptodate(page);
746 page->index = offset >> PAGE_SHIFT;
747 oti->oti_dio_pages_used++;
753 * there are following "locks":
764 * - lock pages, unlock
766 * - lock partial page
772 * Unlock and release pages loaded by osd_bufs_get()
774 * Unlock \a npages pages from \a lnb and drop the refcount on them.
776 * \param env thread execution environment
777 * \param dt dt object undergoing IO (OSD object + methods)
778 * \param lnb array of pages undergoing IO
779 * \param npages number of pages in \a lnb
783 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
784 struct niobuf_local *lnb, int npages)
786 struct osd_thread_info *oti = osd_oti_get(env);
790 ll_pagevec_init(&pvec, 0);
792 for (i = 0; i < npages; i++) {
793 struct page *page = lnb[i].lnb_page;
798 /* if the page isn't cached, then reset uptodate
801 if (PagePrivate2(page)) {
802 oti->oti_dio_pages_used--;
804 if (lnb[i].lnb_locked)
806 if (pagevec_add(&pvec, page) == 0)
807 pagevec_release(&pvec);
810 lnb[i].lnb_page = NULL;
813 LASSERTF(oti->oti_dio_pages_used == 0, "%d\n", oti->oti_dio_pages_used);
815 /* Release any partial pagevec */
816 pagevec_release(&pvec);
822 * Load and lock pages undergoing IO
824 * Pages as described in the \a lnb array are fetched (from disk or cache)
825 * and locked for IO by the caller.
827 * DLM locking protects us from write and truncate competing for same region,
828 * but partial-page truncate can leave dirty pages in the cache for ldiskfs.
829 * It's possible the writeout on a such a page is in progress when we access
830 * it. It's also possible that during this writeout we put new (partial) data
831 * into the page, but won't be able to proceed in filter_commitrw_write().
832 * Therefore, just wait for writeout completion as it should be rare enough.
834 * \param env thread execution environment
835 * \param dt dt object undergoing IO (OSD object + methods)
836 * \param pos byte offset of IO start
837 * \param len number of bytes of IO
838 * \param lnb array of extents undergoing IO
839 * \param rw read or write operation, and other flags
840 * \param capa capabilities
842 * \retval pages (zero or more) loaded successfully
843 * \retval -ENOMEM on memory/page allocation error
845 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
846 loff_t pos, ssize_t len, struct niobuf_local *lnb,
847 int maxlnb, enum dt_bufs_type rw)
849 struct osd_thread_info *oti = osd_oti_get(env);
850 struct osd_object *obj = osd_dt_obj(dt);
851 struct osd_device *osd = osd_obj2dev(obj);
852 int npages, i, iosize, rc = 0;
857 LASSERT(obj->oo_inode);
859 rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
863 write = rw & DT_BUFS_TYPE_WRITE;
865 fsize = lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len;
866 iosize = fsize - lnb[0].lnb_file_offset;
867 fsize = max(fsize, i_size_read(obj->oo_inode));
869 cache = rw & DT_BUFS_TYPE_READAHEAD;
873 cache = osd_use_page_cache(osd);
876 if (!osd->od_writethrough_cache) {
880 if (iosize > osd->od_writethrough_max_iosize) {
885 if (!osd->od_read_cache) {
889 if (iosize > osd->od_readcache_max_iosize) {
894 /* don't use cache on large files */
895 if (osd->od_readcache_max_filesize &&
896 fsize > osd->od_readcache_max_filesize)
902 if (!cache && unlikely(!oti->oti_dio_pages)) {
903 OBD_ALLOC_PTR_ARRAY_LARGE(oti->oti_dio_pages,
904 PTLRPC_MAX_BRW_PAGES);
905 if (!oti->oti_dio_pages)
909 /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
910 gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
912 for (i = 0; i < npages; i++, lnb++) {
913 lnb->lnb_page = osd_get_page(env, dt, lnb->lnb_file_offset,
915 if (lnb->lnb_page == NULL)
916 GOTO(cleanup, rc = -ENOMEM);
920 mark_page_accessed(lnb->lnb_page);
924 /* XXX: this version doesn't invalidate cached pages, but use them */
925 if (!cache && write && obj->oo_inode->i_mapping->nrpages) {
926 /* do not allow data aliasing, invalidate pagecache */
927 /* XXX: can be quite expensive in mixed case */
928 invalidate_mapping_pages(obj->oo_inode->i_mapping,
929 lnb[0].lnb_file_offset >> PAGE_SHIFT,
930 lnb[npages - 1].lnb_file_offset >> PAGE_SHIFT);
938 osd_bufs_put(env, dt, lnb - i, i);
941 /* Borrow @ext4_chunk_trans_blocks */
942 static int osd_chunk_trans_blocks(struct inode *inode, int nrblocks)
944 ldiskfs_group_t groups;
950 depth = ext_depth(inode);
951 idxblocks = depth * 2;
954 * Now let's see how many group bitmaps and group descriptors need
957 groups = idxblocks + 1;
959 if (groups > LDISKFS_SB(inode->i_sb)->s_groups_count)
960 groups = LDISKFS_SB(inode->i_sb)->s_groups_count;
961 if (gdpblocks > LDISKFS_SB(inode->i_sb)->s_gdb_count)
962 gdpblocks = LDISKFS_SB(inode->i_sb)->s_gdb_count;
964 /* bitmaps and block group descriptor blocks */
965 ret = idxblocks + groups + gdpblocks;
967 /* Blocks for super block, inode, quota and xattr blocks */
968 ret += LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
973 #ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
974 static int osd_extend_restart_trans(handle_t *handle, int needed,
979 rc = ldiskfs_journal_ensure_credits(handle, needed,
980 ldiskfs_trans_default_revoke_credits(inode->i_sb));
981 /* this means journal has been restarted */
988 static int osd_extend_restart_trans(handle_t *handle, int needed,
993 if (ldiskfs_handle_has_enough_credits(handle, needed))
995 rc = ldiskfs_journal_extend(handle,
996 needed - handle->h_buffer_credits);
1000 return ldiskfs_journal_restart(handle, needed);
1002 #endif /* HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS */
1004 static int osd_ldiskfs_map_write(struct inode *inode, struct osd_iobuf *iobuf,
1005 struct osd_device *osd, sector_t start_blocks,
1006 sector_t count, loff_t *disk_size,
1009 /* if file has grown, take user_size into account */
1010 if (user_size && *disk_size > user_size)
1011 *disk_size = user_size;
1013 spin_lock(&inode->i_lock);
1014 if (*disk_size > i_size_read(inode)) {
1015 i_size_write(inode, *disk_size);
1016 LDISKFS_I(inode)->i_disksize = *disk_size;
1017 spin_unlock(&inode->i_lock);
1018 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
1020 spin_unlock(&inode->i_lock);
1024 * We don't do stats here as in read path because
1025 * write is async: we'll do this in osd_put_bufs()
1027 return osd_do_bio(osd, inode, iobuf, start_blocks, count);
1030 static unsigned int osd_extent_bytes(const struct osd_device *o)
1032 unsigned int *extent_bytes_ptr =
1033 raw_cpu_ptr(o->od_extent_bytes_percpu);
1035 if (likely(*extent_bytes_ptr))
1036 return *extent_bytes_ptr;
1038 /* initialize on first access or CPU hotplug */
1039 if (!ldiskfs_has_feature_extents(osd_sb(o)))
1040 *extent_bytes_ptr = 1 << osd_sb(o)->s_blocksize_bits;
1042 *extent_bytes_ptr = OSD_DEFAULT_EXTENT_BYTES;
1044 return *extent_bytes_ptr;
1047 #define EXTENT_BYTES_DECAY 64
1048 static void osd_decay_extent_bytes(struct osd_device *osd,
1049 unsigned int new_bytes)
1051 unsigned int old_bytes;
1053 if (!ldiskfs_has_feature_extents(osd_sb(osd)))
1056 old_bytes = osd_extent_bytes(osd);
1057 *raw_cpu_ptr(osd->od_extent_bytes_percpu) =
1058 (old_bytes * (EXTENT_BYTES_DECAY - 1) +
1059 min(new_bytes, OSD_DEFAULT_EXTENT_BYTES) +
1060 EXTENT_BYTES_DECAY - 1) / EXTENT_BYTES_DECAY;
1063 static int osd_ldiskfs_map_inode_pages(struct inode *inode,
1064 struct osd_iobuf *iobuf,
1065 struct osd_device *osd,
1066 int create, __u64 user_size,
1068 struct thandle *thandle)
1070 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1071 int rc = 0, i = 0, mapped_index = 0;
1072 struct page *fp = NULL;
1074 pgoff_t max_page_index;
1075 handle_t *handle = NULL;
1076 sector_t start_blocks = 0, count = 0;
1077 loff_t disk_size = 0;
1078 struct page **page = iobuf->dr_pages;
1079 int pages = iobuf->dr_npages;
1080 sector_t *blocks = iobuf->dr_blocks;
1081 struct niobuf_local *lnb1, *lnb2;
1082 loff_t size1, size2;
1084 max_page_index = inode->i_sb->s_maxbytes >> PAGE_SHIFT;
1086 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1087 inode->i_ino, pages, (*page)->index);
1090 create = LDISKFS_GET_BLOCKS_CREATE;
1091 handle = ldiskfs_journal_current_handle();
1092 LASSERT(handle != NULL);
1093 rc = osd_attach_jinode(inode);
1096 disk_size = i_size_read(inode);
1097 /* if disk_size is already bigger than specified user_size,
1100 if (disk_size > user_size)
1103 /* pages are sorted already. so, we just have to find
1104 * contig. space and process them properly
1107 long blen, total = 0, previous_total = 0;
1108 struct ldiskfs_map_blocks map = { 0 };
1110 if (fp == NULL) { /* start new extent */
1115 } else if (fp->index + clen == (*page)->index) {
1116 /* continue the extent */
1122 if (fp->index + clen >= max_page_index)
1123 GOTO(cleanup, rc = -EFBIG);
1124 /* process found extent */
1125 map.m_lblk = fp->index * blocks_per_page;
1126 map.m_len = blen = clen * blocks_per_page;
1129 * We might restart transaction for block allocations,
1130 * in order to make sure data ordered mode, issue IO, disk
1131 * size update and block allocations need be within same
1132 * transaction to make sure consistency.
1134 if (handle && check_credits) {
1135 struct osd_thandle *oh;
1137 LASSERT(thandle != NULL);
1138 oh = container_of(thandle, struct osd_thandle,
1141 * only issue IO if restart transaction needed,
1142 * as update disk size need hold inode lock, we
1143 * want to avoid that as much as possible.
1145 if (oh->oh_declared_ext <= 0) {
1146 rc = osd_ldiskfs_map_write(inode,
1147 iobuf, osd, start_blocks,
1148 count, &disk_size, user_size);
1151 thandle->th_restart_tran = 1;
1152 GOTO(cleanup, rc = -EAGAIN);
1155 if (OBD_FAIL_CHECK(OBD_FAIL_OST_RESTART_IO))
1156 oh->oh_declared_ext = 0;
1158 oh->oh_declared_ext--;
1160 rc = ldiskfs_map_blocks(handle, inode, &map, create);
1164 for (; total < blen && c < map.m_len; c++, total++) {
1166 *(blocks + total) = 0;
1170 if ((map.m_flags & LDISKFS_MAP_UNWRITTEN) &&
1172 /* don't try to read allocated, but
1173 * unwritten blocks, instead fill the
1174 * patches with zeros in osd_do_bio() */
1175 *(blocks + total) = 0;
1178 *(blocks + total) = map.m_pblk + c;
1179 /* unmap any possible underlying
1180 * metadata from the block device
1183 if ((map.m_flags & LDISKFS_MAP_NEW) &&
1185 clean_bdev_aliases(inode->i_sb->s_bdev,
1191 if (rc == 0 && create) {
1192 count += (total - previous_total);
1193 mapped_index = (count + blocks_per_page -
1194 1) / blocks_per_page - 1;
1195 lnb1 = iobuf->dr_lnbs[i - clen];
1196 lnb2 = iobuf->dr_lnbs[mapped_index];
1197 size1 = lnb1->lnb_file_offset -
1198 (lnb1->lnb_file_offset % PAGE_SIZE) +
1199 (total << inode->i_blkbits);
1200 size2 = lnb2->lnb_file_offset + lnb2->lnb_len;
1204 if (size1 > disk_size)
1208 if (rc == 0 && total < blen) {
1210 * decay extent blocks if we could not
1211 * allocate extent once.
1213 osd_decay_extent_bytes(osd,
1214 (total - previous_total) << inode->i_blkbits);
1215 map.m_lblk = fp->index * blocks_per_page + total;
1216 map.m_len = blen - total;
1217 previous_total = total;
1223 * decay extent blocks if we could allocate
1224 * good large extent.
1226 if (total - previous_total >=
1227 osd_extent_bytes(osd) >> inode->i_blkbits)
1228 osd_decay_extent_bytes(osd,
1229 (total - previous_total) << inode->i_blkbits);
1230 /* look for next extent */
1232 blocks += blocks_per_page * clen;
1235 if (rc == 0 && create &&
1236 start_blocks < pages * blocks_per_page) {
1237 rc = osd_ldiskfs_map_write(inode, iobuf, osd, start_blocks,
1238 count, &disk_size, user_size);
1239 LASSERT(start_blocks + count == pages * blocks_per_page);
1244 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
1245 struct niobuf_local *lnb, int npages)
1247 struct osd_thread_info *oti = osd_oti_get(env);
1248 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1249 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1250 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1259 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1260 if (unlikely(rc != 0))
1263 isize = i_size_read(inode);
1264 maxidx = ((isize + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1;
1266 start = ktime_get();
1267 for (i = 0; i < npages; i++) {
1270 * till commit the content of the page is undefined
1271 * we'll set it uptodate once bulk is done. otherwise
1272 * subsequent reads can access non-stable data
1274 ClearPageUptodate(lnb[i].lnb_page);
1276 if (lnb[i].lnb_len == PAGE_SIZE)
1279 if (maxidx >= lnb[i].lnb_page->index) {
1280 osd_iobuf_add_page(iobuf, &lnb[i]);
1283 char *p = kmap(lnb[i].lnb_page);
1285 off = lnb[i].lnb_page_offset;
1288 off = (lnb[i].lnb_page_offset + lnb[i].lnb_len) &
1291 memset(p + off, 0, PAGE_SIZE - off);
1292 kunmap(lnb[i].lnb_page);
1296 timediff = ktime_us_delta(end, start);
1297 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1299 if (iobuf->dr_npages) {
1300 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1302 if (likely(rc == 0)) {
1303 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1304 /* do IO stats for preparation reads */
1305 osd_fini_iobuf(osd, iobuf);
1311 struct osd_fextent {
1315 unsigned int mapped:1;
1318 static int osd_is_mapped(struct dt_object *dt, __u64 offset,
1319 struct osd_fextent *cached_extent)
1321 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1322 sector_t block = offset >> inode->i_blkbits;
1324 struct fiemap_extent_info fei = { 0 };
1325 struct fiemap_extent fe = { 0 };
1328 if (block >= cached_extent->start && block < cached_extent->end)
1329 return cached_extent->mapped;
1331 if (i_size_read(inode) == 0)
1334 /* Beyond EOF, must not be mapped */
1335 if (((i_size_read(inode) - 1) >> inode->i_blkbits) < block)
1338 fei.fi_extents_max = 1;
1339 fei.fi_extents_start = &fe;
1341 rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
1345 start = fe.fe_logical >> inode->i_blkbits;
1346 cached_extent->flags = fe.fe_flags;
1347 if (fei.fi_extents_mapped == 0) {
1348 /* a special case - no extent found at this offset and forward.
1349 * we can consider this as a hole to EOF. it's safe to cache
1350 * as other threads can not allocate/punch blocks this thread
1351 * is working on (LDLM). */
1352 cached_extent->start = block;
1353 cached_extent->end = i_size_read(inode) >> inode->i_blkbits;
1354 cached_extent->mapped = 0;
1358 if (start > block) {
1359 cached_extent->start = block;
1360 cached_extent->end = start;
1361 cached_extent->mapped = 0;
1363 cached_extent->start = start;
1364 cached_extent->end = (fe.fe_logical + fe.fe_length) >>
1366 cached_extent->mapped = 1;
1369 return cached_extent->mapped;
1372 #define MAX_EXTENTS_PER_WRITE 100
1373 static int osd_declare_write_commit(const struct lu_env *env,
1374 struct dt_object *dt,
1375 struct niobuf_local *lnb, int npages,
1376 struct thandle *handle)
1378 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1379 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1380 struct osd_thandle *oh;
1381 int extents = 0, new_meta = 0;
1382 int depth, new_blocks = 0;
1384 int dirty_groups = 0;
1387 long long quota_space = 0;
1388 struct osd_fextent mapped = { 0 }, extent = { 0 };
1389 enum osd_quota_local_flags local_flags = 0;
1390 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
1391 unsigned int extent_bytes;
1394 LASSERT(handle != NULL);
1395 oh = container_of(handle, struct osd_thandle, ot_super);
1396 LASSERT(oh->ot_handle == NULL);
1399 * We track a decaying average extent blocks per filesystem,
1400 * for most of time, it will be 1M, with filesystem becoming
1401 * heavily-fragmented, it will be reduced to 4K at the worst.
1403 extent_bytes = osd_extent_bytes(osd);
1404 LASSERT(extent_bytes >= (1 << osd_sb(osd)->s_blocksize));
1406 /* calculate number of extents (probably better to pass nb) */
1407 for (i = 0; i < npages; i++) {
1408 /* ignore quota for the whole request if any page is from
1409 * client cache or written by root.
1411 * XXX once we drop the 1.8 client support, the checking
1412 * for whether page is from cache can be simplified as:
1413 * !(lnb[i].flags & OBD_BRW_SYNC)
1415 * XXX we could handle this on per-lnb basis as done by
1418 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
1419 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
1421 declare_flags |= OSD_QID_FORCE;
1424 * Convert unwritten extent might need split extents, could
1427 if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &mapped) &&
1428 !(mapped.flags & FIEMAP_EXTENT_UNWRITTEN)) {
1429 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1433 if (lnb[i].lnb_flags & OBD_BRW_DONE) {
1434 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1438 /* count only unmapped changes */
1440 if (lnb[i].lnb_file_offset != extent.end || extent.end == 0) {
1441 if (extent.end != 0)
1442 extents += (extent.end - extent.start +
1443 extent_bytes - 1) / extent_bytes;
1444 extent.start = lnb[i].lnb_file_offset;
1445 extent.end = lnb[i].lnb_file_offset + lnb[i].lnb_len;
1447 extent.end += lnb[i].lnb_len;
1450 quota_space += PAGE_SIZE;
1453 credits++; /* inode */
1455 * overwrite case, no need to modify tree and
1461 extents += (extent.end - extent.start +
1462 extent_bytes - 1) / extent_bytes;
1464 * with system space usage growing up, mballoc codes won't
1465 * try best to scan block group to align best free extent as
1466 * we can. So extent bytes per extent could be decayed to a
1467 * very small value, this could make us reserve too many credits.
1468 * We could be more optimistic in the credit reservations, even
1469 * in a case where the filesystem is nearly full, it is extremely
1470 * unlikely that the worst case would ever be hit.
1472 if (extents > MAX_EXTENTS_PER_WRITE)
1473 extents = MAX_EXTENTS_PER_WRITE;
1476 * If we add a single extent, then in the worse case, each tree
1477 * level index/leaf need to be changed in case of the tree split.
1478 * If more extents are inserted, they could cause the whole tree
1479 * split more than once, but this is really rare.
1481 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
1483 * many concurrent threads may grow tree by the time
1484 * our transaction starts. so, consider 2 is a min depth.
1486 depth = ext_depth(inode);
1487 depth = min(max(depth, 1) + 1, LDISKFS_MAX_EXTENT_DEPTH);
1489 credits += depth * 2 * extents;
1492 credits += depth * 3 * extents;
1493 new_meta = depth * 2 * extents;
1497 * With N contiguous data blocks, we need at most
1498 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
1499 * 2 dindirect blocks, and 1 tindirect block
1501 new_meta = DIV_ROUND_UP(new_blocks,
1502 LDISKFS_ADDR_PER_BLOCK(inode->i_sb)) + 4;
1503 credits += new_meta;
1505 dirty_groups += (extents + new_meta);
1507 oh->oh_declared_ext = extents;
1509 /* quota space for metadata blocks */
1510 quota_space += new_meta * LDISKFS_BLOCK_SIZE(osd_sb(osd));
1512 /* quota space should be reported in 1K blocks */
1513 quota_space = toqb(quota_space);
1515 /* each new block can go in different group (bitmap + gd) */
1517 /* we can't dirty more bitmap blocks than exist */
1518 if (dirty_groups > LDISKFS_SB(osd_sb(osd))->s_groups_count)
1519 credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
1521 credits += dirty_groups;
1523 /* we can't dirty more gd blocks than exist */
1524 if (dirty_groups > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
1525 credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
1527 credits += dirty_groups;
1530 "%s: inode #%lu extent_bytes %u extents %d credits %d\n",
1531 osd_ino2name(inode), inode->i_ino, extent_bytes, extents,
1535 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1537 /* make sure the over quota flags were not set */
1538 lnb[0].lnb_flags &= ~OBD_BRW_OVER_ALLQUOTA;
1540 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
1541 i_projid_read(inode), quota_space, oh,
1542 osd_dt_obj(dt), &local_flags, declare_flags);
1544 /* we need only to store the overquota flags in the first lnb for
1545 * now, once we support multiple objects BRW, this code needs be
1548 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
1549 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
1550 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
1551 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
1552 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
1553 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
1556 rc = osd_trunc_lock(osd_dt_obj(dt), oh, true);
1561 /* Check if a block is allocated or not */
1562 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
1563 struct niobuf_local *lnb, int npages,
1564 struct thandle *thandle, __u64 user_size)
1566 struct osd_thread_info *oti = osd_oti_get(env);
1567 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1568 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1569 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1570 int rc = 0, i, check_credits = 0;
1574 rc = osd_init_iobuf(osd, iobuf, 1, npages);
1575 if (unlikely(rc != 0))
1578 dquot_initialize(inode);
1580 for (i = 0; i < npages; i++) {
1581 if (lnb[i].lnb_rc == -ENOSPC &&
1582 (lnb[i].lnb_flags & OBD_BRW_MAPPED)) {
1583 /* Allow the write to proceed if overwriting an
1589 if (lnb[i].lnb_rc) { /* ENOSPC, network RPC error, etc. */
1590 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
1592 LASSERT(lnb[i].lnb_page);
1593 generic_error_remove_page(inode->i_mapping,
1598 if (lnb[i].lnb_flags & OBD_BRW_DONE)
1601 if (!(lnb[i].lnb_flags & OBD_BRW_MAPPED))
1604 LASSERT(PageLocked(lnb[i].lnb_page));
1605 LASSERT(!PageWriteback(lnb[i].lnb_page));
1608 * Since write and truncate are serialized by oo_sem, even
1609 * partial-page truncate should not leave dirty pages in the
1612 LASSERT(!PageDirty(lnb[i].lnb_page));
1614 SetPageUptodate(lnb[i].lnb_page);
1616 osd_iobuf_add_page(iobuf, &lnb[i]);
1619 osd_trans_exec_op(env, thandle, OSD_OT_WRITE);
1621 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
1623 } else if (iobuf->dr_npages > 0) {
1624 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd,
1629 /* no pages to write, no transno is needed */
1630 thandle->th_local = 1;
1633 if (rc != 0 && !thandle->th_restart_tran)
1634 osd_fini_iobuf(osd, iobuf);
1636 osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
1638 if (unlikely(rc != 0 && !thandle->th_restart_tran)) {
1639 /* if write fails, we should drop pages from the cache */
1640 for (i = 0; i < npages; i++) {
1641 if (lnb[i].lnb_page == NULL)
1643 if (!PagePrivate2(lnb[i].lnb_page)) {
1644 LASSERT(PageLocked(lnb[i].lnb_page));
1645 generic_error_remove_page(inode->i_mapping,
1654 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
1655 struct niobuf_local *lnb, int npages)
1657 struct osd_thread_info *oti = osd_oti_get(env);
1658 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1659 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1660 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1661 int rc = 0, i, cache_hits = 0, cache_misses = 0;
1668 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1669 if (unlikely(rc != 0))
1672 isize = i_size_read(inode);
1674 start = ktime_get();
1675 for (i = 0; i < npages; i++) {
1677 if (isize <= lnb[i].lnb_file_offset)
1678 /* If there's no more data, abort early.
1679 * lnb->lnb_rc == 0, so it's easy to detect later.
1683 /* instead of looking if we go beyong isize, send complete
1684 * pages all the time
1686 lnb[i].lnb_rc = lnb[i].lnb_len;
1688 /* Bypass disk read if fail_loc is set properly */
1689 if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW))
1690 SetPageUptodate(lnb[i].lnb_page);
1692 if (PageUptodate(lnb[i].lnb_page)) {
1694 unlock_page(lnb[i].lnb_page);
1697 osd_iobuf_add_page(iobuf, &lnb[i]);
1699 /* no need to unlock in osd_bufs_put(), the sooner page is
1700 * unlocked, the earlier another client can access it.
1701 * notice real unlock_page() can be called few lines
1702 * below after osd_do_bio(). lnb is a per-thread, so it's
1703 * fine to have PG_locked and lnb_locked inconsistent here
1705 lnb[i].lnb_locked = 0;
1708 timediff = ktime_us_delta(end, start);
1709 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1711 if (cache_hits != 0)
1712 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_HIT,
1714 if (cache_misses != 0)
1715 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_MISS,
1717 if (cache_hits + cache_misses != 0)
1718 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
1719 cache_hits + cache_misses);
1721 if (iobuf->dr_npages) {
1722 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1725 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1727 /* IO stats will be done in osd_bufs_put() */
1729 /* early release to let others read data during the bulk */
1730 for (i = 0; i < iobuf->dr_npages; i++) {
1731 LASSERT(PageLocked(iobuf->dr_pages[i]));
1732 if (!PagePrivate2(iobuf->dr_pages[i]))
1733 unlock_page(iobuf->dr_pages[i]);
1741 * XXX: Another layering violation for now.
1743 * We don't want to use ->f_op->read methods, because generic file write
1745 * - serializes on ->i_sem, and
1747 * - does a lot of extra work like balance_dirty_pages(),
1749 * which doesn't work for globally shared files like /last_rcvd.
1751 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
1753 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
1755 memcpy(buffer, (char *)ei->i_data, buflen);
1760 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
1762 struct buffer_head *bh;
1763 unsigned long block;
1769 /* prevent reading after eof */
1770 spin_lock(&inode->i_lock);
1771 if (i_size_read(inode) < *offs + size) {
1772 loff_t diff = i_size_read(inode) - *offs;
1774 spin_unlock(&inode->i_lock);
1777 "size %llu is too short to read @%llu\n",
1778 i_size_read(inode), *offs);
1780 } else if (diff == 0) {
1786 spin_unlock(&inode->i_lock);
1789 blocksize = 1 << inode->i_blkbits;
1792 block = *offs >> inode->i_blkbits;
1793 boffs = *offs & (blocksize - 1);
1794 csize = min(blocksize - boffs, size);
1795 bh = __ldiskfs_bread(NULL, inode, block, 0);
1797 CERROR("%s: can't read %u@%llu on ino %lu: rc = %ld\n",
1798 osd_ino2name(inode), csize, *offs, inode->i_ino,
1804 memcpy(buf, bh->b_data + boffs, csize);
1807 memset(buf, 0, csize);
1817 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1818 struct lu_buf *buf, loff_t *pos)
1820 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1823 /* Read small symlink from inode body as we need to maintain correct
1824 * on-disk symlinks for ldiskfs.
1826 if (S_ISLNK(dt->do_lu.lo_header->loh_attr)) {
1827 loff_t size = i_size_read(inode);
1829 if (buf->lb_len < size)
1832 if (size < sizeof(LDISKFS_I(inode)->i_data))
1833 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, size);
1835 rc = osd_ldiskfs_read(inode, buf->lb_buf, size, pos);
1837 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1843 static inline int osd_extents_enabled(struct super_block *sb,
1844 struct inode *inode)
1846 if (inode != NULL) {
1847 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL)
1849 } else if (ldiskfs_has_feature_extents(sb)) {
1855 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1856 const loff_t size, const loff_t pos,
1859 int credits, bits, bs, i;
1861 bits = sb->s_blocksize_bits;
1864 /* legacy blockmap: 3 levels * 3 (bitmap,gd,itself)
1865 * we do not expect blockmaps on the large files,
1866 * so let's shrink it to 2 levels (4GB files)
1869 /* this is default reservation: 2 levels */
1870 credits = (blocks + 2) * 3;
1872 /* actual offset is unknown, hard to optimize */
1876 /* now check for few specific cases to optimize */
1877 if (pos + size <= LDISKFS_NDIR_BLOCKS * bs) {
1880 /* allocate if not allocated */
1881 if (inode == NULL) {
1882 credits += blocks * 2;
1885 for (i = (pos >> bits); i < (pos >> bits) + blocks; i++) {
1886 LASSERT(i < LDISKFS_NDIR_BLOCKS);
1887 if (LDISKFS_I(inode)->i_data[i] == 0)
1890 } else if (pos + size <= (LDISKFS_NDIR_BLOCKS + 1024) * bs) {
1891 /* single indirect */
1892 credits = blocks * 3;
1893 if (inode == NULL ||
1894 LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK] == 0)
1897 /* The indirect block may be modified. */
1904 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1905 const struct lu_buf *buf, loff_t _pos,
1906 struct thandle *handle)
1908 struct osd_object *obj = osd_dt_obj(dt);
1909 struct inode *inode = obj->oo_inode;
1910 struct super_block *sb = osd_sb(osd_obj2dev(obj));
1911 struct osd_thandle *oh;
1912 int rc = 0, est = 0, credits, blocks, allocated = 0;
1918 LASSERT(buf != NULL);
1919 LASSERT(handle != NULL);
1921 oh = container_of(handle, struct osd_thandle, ot_super);
1922 LASSERT(oh->ot_handle == NULL);
1925 bits = sb->s_blocksize_bits;
1929 /* if this is an append, then we
1930 * should expect cross-block record
1937 /* blocks to modify */
1938 blocks = ((pos + size + bs - 1) >> bits) - (pos >> bits);
1939 LASSERT(blocks > 0);
1941 if (inode != NULL && _pos != -1) {
1942 /* object size in blocks */
1943 est = (i_size_read(inode) + bs - 1) >> bits;
1944 allocated = inode->i_blocks >> (bits - 9);
1945 if (pos + size <= i_size_read(inode) && est <= allocated) {
1946 /* looks like an overwrite, no need to modify tree */
1948 /* no need to modify i_size */
1953 if (osd_extents_enabled(sb, inode)) {
1955 * many concurrent threads may grow tree by the time
1956 * our transaction starts. so, consider 2 is a min depth
1957 * for every level we may need to allocate a new block
1958 * and take some entries from the old one. so, 3 blocks
1959 * to allocate (bitmap, gd, itself) + old block - 4 per
1962 depth = inode != NULL ? ext_depth(inode) : 0;
1963 depth = min(max(depth, 1) + 1, LDISKFS_MAX_EXTENT_DEPTH);
1965 /* if not append, then split may need to modify
1966 * existing blocks moving entries into the new ones
1970 /* blocks to store data: bitmap,gd,itself */
1971 credits += blocks * 3;
1973 credits = osd_calc_bkmap_credits(sb, inode, size, _pos, blocks);
1975 /* if inode is created as part of the transaction,
1976 * then it's counted already by the creation method
1983 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1985 /* dt_declare_write() is usually called for system objects, such
1986 * as llog or last_rcvd files. We needn't enforce quota on those
1987 * objects, so always set the lqi_space as 0.
1990 rc = osd_declare_inode_qid(env, i_uid_read(inode),
1992 i_projid_read(inode), 0,
1993 oh, obj, NULL, OSD_QID_BLK);
1996 rc = osd_trunc_lock(obj, oh, true);
2001 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2003 /* LU-2634: clear the extent format for fast symlink */
2004 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
2006 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
2007 spin_lock(&inode->i_lock);
2008 LDISKFS_I(inode)->i_disksize = buflen;
2009 i_size_write(inode, buflen);
2010 spin_unlock(&inode->i_lock);
2011 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2016 static int osd_ldiskfs_write_record(struct dt_object *dt, void *buf,
2017 int bufsize, int write_NUL, loff_t *offs,
2020 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2021 struct buffer_head *bh = NULL;
2022 loff_t offset = *offs;
2023 loff_t new_size = i_size_read(inode);
2024 unsigned long block;
2025 int blocksize = 1 << inode->i_blkbits;
2026 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2030 int dirty_inode = 0;
2031 bool create, sparse, sync = false;
2035 * long symlink write does not count the NUL terminator in
2036 * bufsize, we write it, and the inode's file size does not
2037 * count the NUL terminator as well.
2039 ((char *)buf)[bufsize] = '\0';
2043 /* only the first flag-set matters */
2044 dirty_inode = !test_and_set_bit(LDISKFS_INODE_JOURNAL_DATA,
2047 /* sparse checking is racy, but sparse is very rare case, leave as is */
2048 sparse = (new_size > 0 && (inode->i_blocks >> (inode->i_blkbits - 9)) <
2049 ((new_size - 1) >> inode->i_blkbits) + 1);
2051 while (bufsize > 0) {
2052 int credits = handle->h_buffer_credits;
2053 unsigned long last_block = (new_size == 0) ? 0 :
2054 (new_size - 1) >> inode->i_blkbits;
2059 block = offset >> inode->i_blkbits;
2060 boffs = offset & (blocksize - 1);
2061 size = min(blocksize - boffs, bufsize);
2062 sync = (block > last_block || new_size == 0 || sparse);
2065 down(&ei->i_append_sem);
2067 bh = __ldiskfs_bread(handle, inode, block, 0);
2069 if (unlikely(IS_ERR_OR_NULL(bh) && !sync))
2071 "%s: adding bh without locking off %llu (block %lu, size %d, offs %llu)\n",
2072 osd_ino2name(inode),
2073 offset, block, bufsize, *offs);
2075 if (IS_ERR_OR_NULL(bh)) {
2076 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2077 int flags = LDISKFS_GET_BLOCKS_CREATE;
2079 /* while the file system is being mounted, avoid
2080 * preallocation otherwise mount can take a long
2081 * time as mballoc cache is cold.
2082 * XXX: this is a workaround until we have a proper
2084 * XXX: works with extent-based files only */
2085 if (!osd->od_cl_seq)
2086 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2087 bh = __ldiskfs_bread(handle, inode, block, flags);
2091 up(&ei->i_append_sem);
2096 if (IS_ERR_OR_NULL(bh)) {
2105 "%s: error reading offset %llu (block %lu, size %d, offs %llu), credits %d/%d: rc = %d\n",
2106 osd_ino2name(inode), offset, block, bufsize,
2107 *offs, credits, handle->h_buffer_credits, err);
2111 err = ldiskfs_journal_get_write_access(handle, bh);
2113 CERROR("journal_get_write_access() returned error %d\n",
2117 LASSERTF(boffs + size <= bh->b_size,
2118 "boffs %d size %d bh->b_size %lu\n",
2119 boffs, size, (unsigned long)bh->b_size);
2121 memset(bh->b_data, 0, bh->b_size);
2123 up(&ei->i_append_sem);
2127 memcpy(bh->b_data + boffs, buf, size);
2128 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2132 if (offset + size > new_size)
2133 new_size = offset + size;
2139 up(&ei->i_append_sem);
2146 /* correct in-core and on-disk sizes */
2147 if (new_size > i_size_read(inode)) {
2148 spin_lock(&inode->i_lock);
2149 if (new_size > i_size_read(inode))
2150 i_size_write(inode, new_size);
2151 if (i_size_read(inode) > ei->i_disksize) {
2152 ei->i_disksize = i_size_read(inode);
2155 spin_unlock(&inode->i_lock);
2158 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2165 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2166 const struct lu_buf *buf, loff_t *pos,
2167 struct thandle *handle)
2169 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2170 struct osd_thandle *oh;
2174 LASSERT(dt_object_exists(dt));
2176 LASSERT(handle != NULL);
2177 LASSERT(inode != NULL);
2178 dquot_initialize(inode);
2180 /* XXX: don't check: one declared chunk can be used many times */
2181 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
2183 oh = container_of(handle, struct osd_thandle, ot_super);
2184 LASSERT(oh->ot_handle->h_transaction != NULL);
2185 osd_trans_exec_op(env, handle, OSD_OT_WRITE);
2187 /* Write small symlink to inode body as we need to maintain correct
2188 * on-disk symlinks for ldiskfs.
2189 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
2190 * does not count it in.
2192 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
2193 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
2194 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2196 result = osd_ldiskfs_write_record(dt, buf->lb_buf, buf->lb_len,
2197 is_link, pos, oh->ot_handle);
2199 result = buf->lb_len;
2201 osd_trans_exec_check(env, handle, OSD_OT_WRITE);
2206 static int osd_declare_fallocate(const struct lu_env *env,
2207 struct dt_object *dt, __u64 start, __u64 end,
2208 int mode, struct thandle *th)
2210 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2211 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2212 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2213 long long quota_space = 0;
2214 /* 5 is max tree depth. (inode + 4 index blocks) */
2221 * mode == 0 (which is standard prealloc) and PUNCH is supported
2222 * Rest of mode options is not supported yet.
2224 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2225 RETURN(-EOPNOTSUPP);
2227 /* disable fallocate completely */
2228 if (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks < 0)
2229 RETURN(-EOPNOTSUPP);
2234 if (mode & FALLOC_FL_PUNCH_HOLE) {
2235 rc = osd_declare_inode_qid(env, i_uid_read(inode),
2237 i_projid_read(inode), 0, oh,
2238 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2240 rc = osd_trunc_lock(osd_dt_obj(dt), oh, false);
2244 /* quota space for metadata blocks
2245 * approximate metadata estimate should be good enough.
2247 quota_space += PAGE_SIZE;
2248 quota_space += depth * LDISKFS_BLOCK_SIZE(osd_sb(osd));
2250 /* quota space should be reported in 1K blocks */
2251 quota_space = toqb(quota_space) + toqb(end - start) +
2252 LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
2254 /* We don't need to reserve credits for whole fallocate here.
2255 * We reserve space only for metadata. Fallocate credits are
2256 * extended as required
2258 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2259 i_projid_read(inode), quota_space, oh,
2260 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2264 static int osd_fallocate_preallocate(const struct lu_env *env,
2265 struct dt_object *dt,
2266 __u64 start, __u64 end, int mode,
2269 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2270 handle_t *handle = ldiskfs_journal_current_handle();
2271 unsigned int save_credits = oh->ot_credits;
2272 struct osd_object *obj = osd_dt_obj(dt);
2273 struct inode *inode = obj->oo_inode;
2274 struct ldiskfs_map_blocks map;
2275 unsigned int credits;
2276 ldiskfs_lblk_t blen;
2277 ldiskfs_lblk_t boff;
2278 loff_t new_size = 0;
2285 LASSERT(dt_object_exists(dt));
2286 LASSERT(osd_invariant(obj));
2287 LASSERT(inode != NULL);
2289 CDEBUG(D_INODE, "fallocate: inode #%lu: start %llu end %llu mode %d\n",
2290 inode->i_ino, start, end, mode);
2292 dquot_initialize(inode);
2296 boff = start >> inode->i_blkbits;
2297 blen = (ALIGN(end, 1 << inode->i_blkbits) >> inode->i_blkbits) - boff;
2299 /* Create and mark new extents as either zero or unwritten */
2300 flags = (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks ||
2301 !ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS)) ?
2302 LDISKFS_GET_BLOCKS_CREATE_ZERO :
2303 LDISKFS_GET_BLOCKS_CREATE_UNWRIT_EXT;
2304 #ifndef HAVE_LDISKFS_GET_BLOCKS_KEEP_SIZE
2305 if (mode & FALLOC_FL_KEEP_SIZE)
2306 flags |= LDISKFS_GET_BLOCKS_KEEP_SIZE;
2310 if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > i_size_read(inode) ||
2311 end > LDISKFS_I(inode)->i_disksize)) {
2313 rc = inode_newsize_ok(inode, new_size);
2318 inode_dio_wait(inode);
2323 /* Don't normalize the request if it can fit in one extent so
2324 * that it doesn't get unnecessarily split into multiple extents.
2326 if (blen <= EXT_UNWRITTEN_MAX_LEN)
2327 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2330 * credits to insert 1 extent into extent tree.
2332 credits = osd_chunk_trans_blocks(inode, blen);
2333 depth = ext_depth(inode);
2335 while (rc >= 0 && blen) {
2339 * Recalculate credits when extent tree depth changes.
2341 if (depth != ext_depth(inode)) {
2342 credits = osd_chunk_trans_blocks(inode, blen);
2343 depth = ext_depth(inode);
2346 /* TODO: quota check */
2347 rc = osd_extend_restart_trans(handle, credits, inode);
2351 rc = ldiskfs_map_blocks(handle, inode, &map, flags);
2354 "inode #%lu: block %u: len %u: ldiskfs_map_blocks returned %d\n",
2355 inode->i_ino, map.m_lblk, map.m_len, rc);
2356 ldiskfs_mark_inode_dirty(handle, inode);
2361 map.m_len = blen = blen - rc;
2362 epos = (loff_t)map.m_lblk << inode->i_blkbits;
2363 inode->i_ctime = current_time(inode);
2367 if (ldiskfs_update_inode_size(inode, epos) & 0x1)
2368 inode->i_mtime = inode->i_ctime;
2369 #ifndef HAVE_LDISKFS_GET_BLOCKS_KEEP_SIZE
2371 if (epos > inode->i_size)
2372 ldiskfs_set_inode_flag(inode,
2373 LDISKFS_INODE_EOFBLOCKS);
2377 ldiskfs_mark_inode_dirty(handle, inode);
2381 /* extand credits if needed for operations such as attribute set */
2383 rc = osd_extend_restart_trans(handle, save_credits, inode);
2385 inode_unlock(inode);
2390 static int osd_fallocate_punch(const struct lu_env *env, struct dt_object *dt,
2391 __u64 start, __u64 end, int mode,
2394 struct osd_object *obj = osd_dt_obj(dt);
2395 struct inode *inode = obj->oo_inode;
2396 struct osd_access_lock *al;
2397 struct osd_thandle *oh;
2398 int rc = 0, found = 0;
2402 LASSERT(dt_object_exists(dt));
2403 LASSERT(osd_invariant(obj));
2404 LASSERT(inode != NULL);
2406 dquot_initialize(inode);
2409 oh = container_of(th, struct osd_thandle, ot_super);
2410 LASSERT(oh->ot_handle->h_transaction != NULL);
2412 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2413 if (obj != al->tl_obj)
2415 LASSERT(al->tl_shared == 0);
2417 /* do actual punch in osd_trans_stop() */
2418 al->tl_start = start;
2421 al->tl_punch = true;
2428 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
2429 __u64 start, __u64 end, int mode, struct thandle *th)
2435 if (mode & FALLOC_FL_PUNCH_HOLE) {
2437 rc = osd_fallocate_punch(env, dt, start, end, mode, th);
2439 /* standard preallocate */
2440 rc = osd_fallocate_preallocate(env, dt, start, end, mode, th);
2445 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
2446 __u64 start, __u64 end, struct thandle *th)
2448 struct osd_thandle *oh;
2449 struct inode *inode;
2454 oh = container_of(th, struct osd_thandle, ot_super);
2457 * we don't need to reserve credits for whole truncate
2458 * it's not possible as truncate may need to free too many
2459 * blocks and that won't fit a single transaction. instead
2460 * we reserve credits to change i_size and put inode onto
2461 * orphan list. if needed truncate will extend or restart
2464 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
2465 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
2467 inode = osd_dt_obj(dt)->oo_inode;
2470 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2471 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
2475 rc = osd_trunc_lock(osd_dt_obj(dt), oh, false);
2480 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
2481 __u64 start, __u64 end, struct thandle *th)
2483 struct osd_object *obj = osd_dt_obj(dt);
2484 struct osd_device *osd = osd_obj2dev(obj);
2485 struct inode *inode = obj->oo_inode;
2486 struct osd_access_lock *al;
2487 struct osd_thandle *oh;
2488 int rc = 0, found = 0;
2492 LASSERT(dt_object_exists(dt));
2493 LASSERT(osd_invariant(obj));
2494 LASSERT(inode != NULL);
2495 dquot_initialize(inode);
2498 oh = container_of(th, struct osd_thandle, ot_super);
2499 LASSERT(oh->ot_handle->h_transaction != NULL);
2501 /* we used to skip truncate to current size to
2502 * optimize truncates on OST. with DoM we can
2503 * get attr_set to set specific size (MDS_REINT)
2504 * and then get truncate RPC which essentially
2505 * would be skipped. this is bad.. so, disable
2506 * this optimization on MDS till the client stop
2507 * to sent MDS_REINT (LU-11033) -bzzz
2509 if (osd->od_is_ost && i_size_read(inode) == start)
2512 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
2514 spin_lock(&inode->i_lock);
2515 if (i_size_read(inode) < start)
2517 i_size_write(inode, start);
2518 spin_unlock(&inode->i_lock);
2519 /* if object holds encrypted content, we need to make sure we truncate
2520 * on an encryption unit boundary, or subsequent reads will get
2523 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2524 start & ~LUSTRE_ENCRYPTION_MASK)
2525 start = (start & LUSTRE_ENCRYPTION_MASK) +
2526 LUSTRE_ENCRYPTION_UNIT_SIZE;
2527 ll_truncate_pagecache(inode, start);
2529 /* optimize grow case */
2531 osd_execute_truncate(obj);
2536 /* add to orphan list to ensure truncate completion
2537 * if this transaction succeed. ldiskfs_truncate()
2538 * will take the inode out of the list
2540 rc = ldiskfs_orphan_add(oh->ot_handle, inode);
2541 inode_unlock(inode);
2545 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2546 if (obj != al->tl_obj)
2548 LASSERT(al->tl_shared == 0);
2550 /* do actual truncate in osd_trans_stop() */
2551 al->tl_truncate = 1;
2560 static int fiemap_check_ranges(struct inode *inode,
2561 u64 start, u64 len, u64 *new_len)
2570 if (ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS))
2571 maxbytes = inode->i_sb->s_maxbytes;
2573 maxbytes = LDISKFS_SB(inode->i_sb)->s_bitmap_maxbytes;
2575 if (start > maxbytes)
2579 * Shrink request scope to what the fs can actually handle.
2581 if (len > maxbytes || (maxbytes - len) < start)
2582 *new_len = maxbytes - start;
2587 /* So that the fiemap access checks can't overflow on 32 bit machines. */
2588 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
2590 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
2593 struct fiemap_extent_info fieinfo = {0, };
2594 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2599 if (inode->i_op->fiemap == NULL)
2602 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS)
2605 rc = fiemap_check_ranges(inode, fm->fm_start, fm->fm_length, &len);
2609 fieinfo.fi_flags = fm->fm_flags;
2610 fieinfo.fi_extents_max = fm->fm_extent_count;
2611 fieinfo.fi_extents_start = fm->fm_extents;
2613 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
2614 filemap_write_and_wait(inode->i_mapping);
2616 rc = inode->i_op->fiemap(inode, &fieinfo, fm->fm_start, len);
2617 fm->fm_flags = fieinfo.fi_flags;
2618 fm->fm_mapped_extents = fieinfo.fi_extents_mapped;
2623 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
2624 __u64 start, __u64 end, enum lu_ladvise_type advice)
2626 struct osd_object *obj = osd_dt_obj(dt);
2631 case LU_LADVISE_DONTNEED:
2633 invalidate_mapping_pages(obj->oo_inode->i_mapping,
2634 start >> PAGE_SHIFT,
2635 (end - 1) >> PAGE_SHIFT);
2645 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
2646 loff_t offset, int whence)
2648 struct osd_object *obj = osd_dt_obj(dt);
2649 struct inode *inode = obj->oo_inode;
2655 LASSERT(dt_object_exists(dt));
2656 LASSERT(osd_invariant(obj));
2658 LASSERT(offset >= 0);
2660 file = osd_quasi_file(env, inode);
2661 result = file->f_op->llseek(file, offset, whence);
2664 * If 'offset' is beyond end of object file then treat it as not error
2665 * but valid case for SEEK_HOLE and return 'offset' as result.
2666 * LOV will decide if it is beyond real end of file or not.
2668 if (whence == SEEK_HOLE && result == -ENXIO)
2671 CDEBUG(D_INFO, "seek %s from %lld: %lld\n", whence == SEEK_HOLE ?
2672 "hole" : "data", offset, result);
2677 * in some cases we may need declare methods for objects being created
2678 * e.g., when we create symlink
2680 const struct dt_body_operations osd_body_ops_new = {
2681 .dbo_declare_write = osd_declare_write,
2684 const struct dt_body_operations osd_body_ops = {
2685 .dbo_read = osd_read,
2686 .dbo_declare_write = osd_declare_write,
2687 .dbo_write = osd_write,
2688 .dbo_bufs_get = osd_bufs_get,
2689 .dbo_bufs_put = osd_bufs_put,
2690 .dbo_write_prep = osd_write_prep,
2691 .dbo_declare_write_commit = osd_declare_write_commit,
2692 .dbo_write_commit = osd_write_commit,
2693 .dbo_read_prep = osd_read_prep,
2694 .dbo_declare_punch = osd_declare_punch,
2695 .dbo_punch = osd_punch,
2696 .dbo_fiemap_get = osd_fiemap_get,
2697 .dbo_ladvise = osd_ladvise,
2698 .dbo_declare_fallocate = osd_declare_fallocate,
2699 .dbo_fallocate = osd_fallocate,
2700 .dbo_lseek = osd_lseek,
2704 * Get a truncate lock
2706 * In order to take multi-transaction truncate out of main transaction we let
2707 * the caller grab a lock on the object passed. the lock can be shared (for
2708 * writes) and exclusive (for truncate). It's not allowed to mix truncate
2709 * and write in the same transaction handle (do not confuse with big ldiskfs
2710 * transaction containing lots of handles).
2711 * The lock must be taken at declaration.
2713 * \param obj object to lock
2715 * \shared shared or exclusive
2717 * \retval 0 lock is granted
2718 * \retval -NOMEM no memory to allocate lock
2720 int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh, bool shared)
2722 struct osd_access_lock *al, *tmp;
2727 list_for_each_entry(tmp, &oh->ot_trunc_locks, tl_list) {
2728 if (tmp->tl_obj != obj)
2730 LASSERT(tmp->tl_shared == shared);
2731 /* found same lock */
2736 if (unlikely(al == NULL))
2739 al->tl_truncate = false;
2741 down_read(&obj->oo_ext_idx_sem);
2743 down_write(&obj->oo_ext_idx_sem);
2744 al->tl_shared = shared;
2745 lu_object_get(&obj->oo_dt.do_lu);
2747 list_add(&al->tl_list, &oh->ot_trunc_locks);
2752 void osd_trunc_unlock_all(const struct lu_env *env, struct list_head *list)
2754 struct osd_access_lock *al, *tmp;
2756 list_for_each_entry_safe(al, tmp, list, tl_list) {
2758 up_read(&al->tl_obj->oo_ext_idx_sem);
2760 up_write(&al->tl_obj->oo_ext_idx_sem);
2761 osd_object_put(env, al->tl_obj);
2762 list_del(&al->tl_list);
2767 /* For a partial-page punch, flush punch range to disk immediately */
2768 static void osd_partial_page_flush_punch(struct osd_device *d,
2769 struct inode *inode, loff_t start,
2772 if (osd_use_page_cache(d)) {
2773 filemap_fdatawrite_range(inode->i_mapping, start, end);
2775 /* Notice we use "wait" version to ensure I/O is complete */
2776 filemap_write_and_wait_range(inode->i_mapping, start,
2778 invalidate_mapping_pages(inode->i_mapping, start >> PAGE_SHIFT,
2784 * For a partial-page truncate, flush the page to disk immediately to
2785 * avoid data corruption during direct disk write. b=17397
2787 static void osd_partial_page_flush(struct osd_device *d, struct inode *inode,
2790 if (!(offset & ~PAGE_MASK))
2793 if (osd_use_page_cache(d)) {
2794 filemap_fdatawrite_range(inode->i_mapping, offset, offset + 1);
2796 /* Notice we use "wait" version to ensure I/O is complete */
2797 filemap_write_and_wait_range(inode->i_mapping, offset,
2799 invalidate_mapping_pages(inode->i_mapping, offset >> PAGE_SHIFT,
2800 offset >> PAGE_SHIFT);
2804 void osd_execute_truncate(struct osd_object *obj)
2806 struct osd_device *d = osd_obj2dev(obj);
2807 struct inode *inode = obj->oo_inode;
2810 /* simulate crash before (in the middle) of delayed truncate */
2811 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FAIL_AT_TRUNCATE)) {
2812 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2813 struct ldiskfs_sb_info *sbi = LDISKFS_SB(inode->i_sb);
2815 mutex_lock(&sbi->s_orphan_lock);
2816 list_del_init(&ei->i_orphan);
2817 mutex_unlock(&sbi->s_orphan_lock);
2821 size = i_size_read(inode);
2823 /* if object holds encrypted content, we need to make sure we truncate
2824 * on an encryption unit boundary, or block content will get corrupted
2826 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2827 size & ~LUSTRE_ENCRYPTION_MASK)
2828 inode->i_size = (size & LUSTRE_ENCRYPTION_MASK) +
2829 LUSTRE_ENCRYPTION_UNIT_SIZE;
2830 ldiskfs_truncate(inode);
2831 inode_unlock(inode);
2832 if (inode->i_size != size) {
2833 spin_lock(&inode->i_lock);
2834 i_size_write(inode, size);
2835 LDISKFS_I(inode)->i_disksize = size;
2836 spin_unlock(&inode->i_lock);
2837 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2839 osd_partial_page_flush(d, inode, size);
2842 void osd_execute_punch(const struct lu_env *env, struct osd_object *obj,
2843 loff_t start, loff_t end, int mode)
2845 struct osd_device *d = osd_obj2dev(obj);
2846 struct inode *inode = obj->oo_inode;
2847 struct file *file = osd_quasi_file(env, inode);
2849 file->f_op->fallocate(file, mode, start, end - start);
2850 osd_partial_page_flush_punch(d, inode, start, end - 1);
2853 void osd_process_truncates(const struct lu_env *env, struct list_head *list)
2855 struct osd_access_lock *al;
2857 LASSERT(journal_current_handle() == NULL);
2859 list_for_each_entry(al, list, tl_list) {
2862 if (al->tl_truncate)
2863 osd_execute_truncate(al->tl_obj);
2864 else if (al->tl_punch)
2865 osd_execute_punch(env, al->tl_obj, al->tl_start,
2866 al->tl_end, al->tl_mode);