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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Author: Nikita Danilov <nikita@clusterfs.com>
37 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41 #define DEBUG_SUBSYSTEM S_OSD
43 /* prerequisite for linux/xattr.h */
44 #include <linux/types.h>
45 /* prerequisite for linux/xattr.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! This should never happen. Normally, I would crash here, but instead I will dump the bio contents to the console. Please report this to <https://jira.whamcloud.com/> , along with any interesting messages leading up to this point (like SCSI errors, perhaps). Because bi_private is NULL, I can't wake up the thread that initiated this IO - you will 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;
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 sectors, total;
330 DECLARE_BVEC_ITER_ALL(iter_all);
331 __u16 *expected_guard;
335 bio_for_each_segment_all(bv, bio, iter_all) {
336 lnb = iobuf->dr_lnbs[index];
337 expected_guard = lnb->lnb_guards;
338 sectors = bv->bv_len / sector_size;
339 if (lnb->lnb_guard_rpc) {
340 rc = bio_dif_compare(expected_guard, bio_prot_buf,
341 sectors, bi->tuple_size);
347 bio_prot_buf += sectors * bi->tuple_size;
348 total += sectors * bi->tuple_size;
349 LASSERT(total <= bip_size(bio->bi_integrity));
355 static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
356 struct osd_iobuf *iobuf,
357 int start_page_idx, bool fault_inject,
358 bool integrity_enabled)
360 struct super_block *sb = osd_sb(osd);
361 integrity_gen_fn *generate_fn = NULL;
362 integrity_vrfy_fn *verify_fn = NULL;
367 if (!integrity_enabled)
370 rc = osd_get_integrity_profile(osd, &generate_fn, &verify_fn);
374 rc = bio_integrity_prep_fn(bio, generate_fn, verify_fn);
378 /* Verify and inject fault only when writing */
379 if (iobuf->dr_rw == 1) {
380 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_CMP))) {
381 rc = osd_bio_integrity_compare(bio, sb->s_bdev, iobuf,
387 if (unlikely(fault_inject))
388 bio_integrity_fault_inject(bio);
394 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
395 # ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
396 static void dio_integrity_complete_routine(struct bio *bio)
398 static void dio_integrity_complete_routine(struct bio *bio, int error)
401 struct osd_bio_private *bio_private = bio->bi_private;
403 bio->bi_private = bio_private->obp_iobuf;
404 osd_dio_complete_routine(bio, error);
406 OBD_FREE_PTR(bio_private);
408 #endif /* HAVE_BIO_INTEGRITY_PREP_FN */
409 #else /* !CONFIG_BLK_DEV_INTEGRITY */
410 #define osd_bio_integrity_handle(osd, bio, iobuf, start_page_idx, \
411 fault_inject, integrity_enabled) 0
412 #endif /* CONFIG_BLK_DEV_INTEGRITY */
414 static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
415 bool integrity_enabled, int start_page_idx,
416 struct osd_bio_private **pprivate)
422 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
423 if (integrity_enabled) {
424 struct osd_bio_private *bio_private = NULL;
426 OBD_ALLOC_GFP(bio_private, sizeof(*bio_private), GFP_NOIO);
427 if (bio_private == NULL)
429 bio->bi_end_io = dio_integrity_complete_routine;
430 bio->bi_private = bio_private;
431 bio_private->obp_start_page_idx = start_page_idx;
432 bio_private->obp_iobuf = iobuf;
433 *pprivate = bio_private;
437 bio->bi_end_io = dio_complete_routine;
438 bio->bi_private = iobuf;
444 static void osd_mark_page_io_done(struct osd_iobuf *iobuf,
446 sector_t start_blocks,
449 struct niobuf_local *lnb;
450 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
451 pgoff_t pg_start, pg_end;
453 pg_start = start_blocks / blocks_per_page;
454 if (start_blocks % blocks_per_page)
456 if (count >= blocks_per_page)
457 pg_end = (start_blocks + count -
458 blocks_per_page) / blocks_per_page;
460 return; /* nothing to mark */
461 for ( ; pg_start <= pg_end; pg_start++) {
462 lnb = iobuf->dr_lnbs[pg_start];
463 lnb->lnb_flags |= OBD_BRW_DONE;
467 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
468 struct osd_iobuf *iobuf, sector_t start_blocks,
471 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
472 struct page **pages = iobuf->dr_pages;
473 int npages = iobuf->dr_npages;
474 sector_t *blocks = iobuf->dr_blocks;
475 struct super_block *sb = inode->i_sb;
476 int sector_bits = sb->s_blocksize_bits - 9;
477 unsigned int blocksize = sb->s_blocksize;
478 struct block_device *bdev = sb->s_bdev;
479 struct osd_bio_private *bio_private = NULL;
480 struct bio *bio = NULL;
481 int bio_start_page_idx;
483 unsigned int page_offset;
486 int block_idx, block_idx_end;
487 int page_idx, page_idx_start;
491 bool integrity_enabled;
492 struct blk_plug plug;
493 int blocks_left_page;
497 fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
498 LASSERT(iobuf->dr_npages == npages);
500 integrity_enabled = bdev_integrity_enabled(bdev, iobuf->dr_rw);
502 osd_brw_stats_update(osd, iobuf);
503 iobuf->dr_start_time = ktime_get();
506 count = npages * blocks_per_page;
507 block_idx_end = start_blocks + count;
509 blk_start_plug(&plug);
511 page_idx_start = start_blocks / blocks_per_page;
512 for (page_idx = page_idx_start, block_idx = start_blocks;
513 block_idx < block_idx_end; page_idx++,
514 block_idx += blocks_left_page) {
515 page = pages[page_idx];
516 LASSERT(page_idx < iobuf->dr_npages);
518 i = block_idx % blocks_per_page;
519 blocks_left_page = blocks_per_page - i;
520 for (page_offset = i * blocksize; i < blocks_left_page;
521 i += nblocks, page_offset += blocksize * nblocks) {
524 if (blocks[block_idx + i] == 0) { /* hole */
525 LASSERTF(iobuf->dr_rw == 0,
526 "page_idx %u, block_idx %u, i %u,"
527 "start_blocks: %llu, count: %llu, npages: %d\n",
528 page_idx, block_idx, i,
529 (unsigned long long)start_blocks,
530 (unsigned long long)count, npages);
531 memset(kmap(page) + page_offset, 0, blocksize);
536 sector = (sector_t)blocks[block_idx + i] << sector_bits;
538 /* Additional contiguous file blocks? */
539 while (i + nblocks < blocks_left_page &&
540 (sector + (nblocks << sector_bits)) ==
541 ((sector_t)blocks[block_idx + i + nblocks] <<
545 if (bio && can_be_merged(bio, sector) &&
546 bio_add_page(bio, page, blocksize * nblocks,
548 continue; /* added this frag OK */
551 struct request_queue *q = bio_get_queue(bio);
552 unsigned int bi_size = bio_sectors(bio) << 9;
554 /* Dang! I have to fragment this I/O */
556 "bio++ sz %d vcnt %d(%d) sectors %d(%d) psg %d(%d)\n",
557 bi_size, bio->bi_vcnt, bio->bi_max_vecs,
559 queue_max_sectors(q),
560 osd_bio_nr_segs(bio),
561 queue_max_segments(q));
562 rc = osd_bio_integrity_handle(osd, bio,
563 iobuf, bio_start_page_idx,
564 fault_inject, integrity_enabled);
570 record_start_io(iobuf, bi_size);
571 osd_submit_bio(iobuf->dr_rw, bio);
574 bio_start_page_idx = page_idx;
575 /* allocate new bio */
576 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
577 (block_idx_end - block_idx +
578 blocks_left_page - 1)));
580 CERROR("Can't allocate bio %u pages\n",
581 block_idx_end - block_idx +
582 blocks_left_page - 1);
587 bio_set_dev(bio, bdev);
588 bio_set_sector(bio, sector);
589 bio->bi_opf = iobuf->dr_rw ? WRITE : READ;
590 rc = osd_bio_init(bio, iobuf, integrity_enabled,
591 bio_start_page_idx, &bio_private);
597 rc = bio_add_page(bio, page,
598 blocksize * nblocks, page_offset);
604 rc = osd_bio_integrity_handle(osd, bio, iobuf,
613 record_start_io(iobuf, bio_sectors(bio) << 9);
614 osd_submit_bio(iobuf->dr_rw, bio);
619 blk_finish_plug(&plug);
621 /* in order to achieve better IO throughput, we don't wait for writes
622 * completion here. instead we proceed with transaction commit in
623 * parallel and wait for IO completion once transaction is stopped
624 * see osd_trans_stop() for more details -bzzz
626 if (iobuf->dr_rw == 0 || fault_inject) {
627 wait_event(iobuf->dr_wait,
628 atomic_read(&iobuf->dr_numreqs) == 0);
629 osd_fini_iobuf(osd, iobuf);
633 rc = iobuf->dr_error;
636 OBD_FREE_PTR(bio_private);
640 if (rc == 0 && iobuf->dr_rw)
641 osd_mark_page_io_done(iobuf, inode,
642 start_blocks, count);
647 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
648 struct niobuf_local *lnb, int maxlnb)
656 int poff = offset & (PAGE_SIZE - 1);
657 int plen = PAGE_SIZE - poff;
659 if (*nrpages >= maxlnb) {
666 lnb->lnb_file_offset = offset;
667 lnb->lnb_page_offset = poff;
669 /* lnb->lnb_flags = rnb->rnb_flags; */
671 lnb->lnb_page = NULL;
673 lnb->lnb_guard_rpc = 0;
674 lnb->lnb_guard_disk = 0;
677 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
688 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
689 loff_t offset, gfp_t gfp_mask, bool cache)
691 struct osd_thread_info *oti = osd_oti_get(env);
692 struct inode *inode = osd_dt_obj(dt)->oo_inode;
693 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
700 page = find_or_create_page(inode->i_mapping,
701 offset >> PAGE_SHIFT, gfp_mask);
704 LASSERT(!PagePrivate2(page));
705 wait_on_page_writeback(page);
707 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
713 if (inode->i_mapping->nrpages) {
714 /* consult with pagecache, but do not create new pages */
715 /* this is normally used once */
716 page = find_lock_page(inode->i_mapping, offset >> PAGE_SHIFT);
718 wait_on_page_writeback(page);
723 LASSERT(oti->oti_dio_pages);
724 cur = oti->oti_dio_pages_used;
725 page = oti->oti_dio_pages[cur];
727 if (unlikely(!page)) {
728 LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
729 page = alloc_page(gfp_mask);
732 oti->oti_dio_pages[cur] = page;
733 SetPagePrivate2(page);
737 ClearPageUptodate(page);
738 page->index = offset >> PAGE_SHIFT;
739 oti->oti_dio_pages_used++;
745 * there are following "locks":
756 * - lock pages, unlock
758 * - lock partial page
764 * Unlock and release pages loaded by osd_bufs_get()
766 * Unlock \a npages pages from \a lnb and drop the refcount on them.
768 * \param env thread execution environment
769 * \param dt dt object undergoing IO (OSD object + methods)
770 * \param lnb array of pages undergoing IO
771 * \param npages number of pages in \a lnb
775 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
776 struct niobuf_local *lnb, int npages)
778 struct osd_thread_info *oti = osd_oti_get(env);
782 ll_pagevec_init(&pvec, 0);
784 for (i = 0; i < npages; i++) {
785 struct page *page = lnb[i].lnb_page;
790 /* if the page isn't cached, then reset uptodate
793 if (PagePrivate2(page)) {
794 oti->oti_dio_pages_used--;
796 if (lnb[i].lnb_locked)
798 if (pagevec_add(&pvec, page) == 0)
799 pagevec_release(&pvec);
802 lnb[i].lnb_page = NULL;
805 LASSERTF(oti->oti_dio_pages_used == 0, "%d\n", oti->oti_dio_pages_used);
807 /* Release any partial pagevec */
808 pagevec_release(&pvec);
814 * Load and lock pages undergoing IO
816 * Pages as described in the \a lnb array are fetched (from disk or cache)
817 * and locked for IO by the caller.
819 * DLM locking protects us from write and truncate competing for same region,
820 * but partial-page truncate can leave dirty pages in the cache for ldiskfs.
821 * It's possible the writeout on a such a page is in progress when we access
822 * it. It's also possible that during this writeout we put new (partial) data
823 * into the page, but won't be able to proceed in filter_commitrw_write().
824 * Therefore, just wait for writeout completion as it should be rare enough.
826 * \param env thread execution environment
827 * \param dt dt object undergoing IO (OSD object + methods)
828 * \param pos byte offset of IO start
829 * \param len number of bytes of IO
830 * \param lnb array of extents undergoing IO
831 * \param rw read or write operation, and other flags
832 * \param capa capabilities
834 * \retval pages (zero or more) loaded successfully
835 * \retval -ENOMEM on memory/page allocation error
837 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
838 loff_t pos, ssize_t len, struct niobuf_local *lnb,
839 int maxlnb, enum dt_bufs_type rw)
841 struct osd_thread_info *oti = osd_oti_get(env);
842 struct osd_object *obj = osd_dt_obj(dt);
843 struct osd_device *osd = osd_obj2dev(obj);
844 int npages, i, iosize, rc = 0;
849 LASSERT(obj->oo_inode);
851 rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
855 write = rw & DT_BUFS_TYPE_WRITE;
857 fsize = lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len;
858 iosize = fsize - lnb[0].lnb_file_offset;
859 fsize = max(fsize, i_size_read(obj->oo_inode));
861 cache = rw & DT_BUFS_TYPE_READAHEAD;
865 cache = osd_use_page_cache(osd);
868 if (!osd->od_writethrough_cache) {
872 if (iosize > osd->od_writethrough_max_iosize) {
877 if (!osd->od_read_cache) {
881 if (iosize > osd->od_readcache_max_iosize) {
886 /* don't use cache on large files */
887 if (osd->od_readcache_max_filesize &&
888 fsize > osd->od_readcache_max_filesize)
894 if (!cache && unlikely(!oti->oti_dio_pages)) {
895 OBD_ALLOC_PTR_ARRAY_LARGE(oti->oti_dio_pages,
896 PTLRPC_MAX_BRW_PAGES);
897 if (!oti->oti_dio_pages)
901 /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
902 gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
904 for (i = 0; i < npages; i++, lnb++) {
905 lnb->lnb_page = osd_get_page(env, dt, lnb->lnb_file_offset,
907 if (lnb->lnb_page == NULL)
908 GOTO(cleanup, rc = -ENOMEM);
914 /* XXX: this version doesn't invalidate cached pages, but use them */
915 if (!cache && write && obj->oo_inode->i_mapping->nrpages) {
916 /* do not allow data aliasing, invalidate pagecache */
917 /* XXX: can be quite expensive in mixed case */
918 invalidate_mapping_pages(obj->oo_inode->i_mapping,
919 lnb[0].lnb_file_offset >> PAGE_SHIFT,
920 lnb[npages - 1].lnb_file_offset >> PAGE_SHIFT);
928 osd_bufs_put(env, dt, lnb - i, i);
931 /* Borrow @ext4_chunk_trans_blocks */
932 static int osd_chunk_trans_blocks(struct inode *inode, int nrblocks)
934 ldiskfs_group_t groups;
940 depth = ext_depth(inode);
941 idxblocks = depth * 2;
944 * Now let's see how many group bitmaps and group descriptors need
947 groups = idxblocks + 1;
949 if (groups > LDISKFS_SB(inode->i_sb)->s_groups_count)
950 groups = LDISKFS_SB(inode->i_sb)->s_groups_count;
951 if (gdpblocks > LDISKFS_SB(inode->i_sb)->s_gdb_count)
952 gdpblocks = LDISKFS_SB(inode->i_sb)->s_gdb_count;
954 /* bitmaps and block group descriptor blocks */
955 ret = idxblocks + groups + gdpblocks;
957 /* Blocks for super block, inode, quota and xattr blocks */
958 ret += LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
963 #ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
964 static int osd_extend_restart_trans(handle_t *handle, int needed,
969 rc = ldiskfs_journal_ensure_credits(handle, needed,
970 ldiskfs_trans_default_revoke_credits(inode->i_sb));
971 /* this means journal has been restarted */
978 static int osd_extend_restart_trans(handle_t *handle, int needed,
983 if (ldiskfs_handle_has_enough_credits(handle, needed))
985 rc = ldiskfs_journal_extend(handle,
986 needed - handle->h_buffer_credits);
990 return ldiskfs_journal_restart(handle, needed);
992 #endif /* HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS */
994 static int osd_ldiskfs_map_write(struct inode *inode, struct osd_iobuf *iobuf,
995 struct osd_device *osd, sector_t start_blocks,
996 sector_t count, loff_t *disk_size,
999 /* if file has grown, take user_size into account */
1000 if (user_size && *disk_size > user_size)
1001 *disk_size = user_size;
1003 spin_lock(&inode->i_lock);
1004 if (*disk_size > i_size_read(inode)) {
1005 i_size_write(inode, *disk_size);
1006 LDISKFS_I(inode)->i_disksize = *disk_size;
1007 spin_unlock(&inode->i_lock);
1008 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
1010 spin_unlock(&inode->i_lock);
1014 * We don't do stats here as in read path because
1015 * write is async: we'll do this in osd_put_bufs()
1017 return osd_do_bio(osd, inode, iobuf, start_blocks, count);
1020 static unsigned int osd_extent_bytes(const struct osd_device *o)
1022 unsigned int *extent_bytes_ptr =
1023 raw_cpu_ptr(o->od_extent_bytes_percpu);
1025 if (likely(*extent_bytes_ptr))
1026 return *extent_bytes_ptr;
1028 /* initialize on first access or CPU hotplug */
1029 if (!ldiskfs_has_feature_extents(osd_sb(o)))
1030 *extent_bytes_ptr = 1 << osd_sb(o)->s_blocksize_bits;
1032 *extent_bytes_ptr = OSD_DEFAULT_EXTENT_BYTES;
1034 return *extent_bytes_ptr;
1037 #define EXTENT_BYTES_DECAY 64
1038 static void osd_decay_extent_bytes(struct osd_device *osd,
1039 unsigned int new_bytes)
1041 unsigned int old_bytes;
1043 if (!ldiskfs_has_feature_extents(osd_sb(osd)))
1046 old_bytes = osd_extent_bytes(osd);
1047 *raw_cpu_ptr(osd->od_extent_bytes_percpu) =
1048 (old_bytes * (EXTENT_BYTES_DECAY - 1) +
1049 min(new_bytes, OSD_DEFAULT_EXTENT_BYTES) +
1050 EXTENT_BYTES_DECAY - 1) / EXTENT_BYTES_DECAY;
1053 static int osd_ldiskfs_map_inode_pages(struct inode *inode,
1054 struct osd_iobuf *iobuf,
1055 struct osd_device *osd,
1056 int create, __u64 user_size,
1058 struct thandle *thandle)
1060 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1061 int rc = 0, i = 0, mapped_index = 0;
1062 struct page *fp = NULL;
1064 pgoff_t max_page_index;
1065 handle_t *handle = NULL;
1066 sector_t start_blocks = 0, count = 0;
1067 loff_t disk_size = 0;
1068 struct page **page = iobuf->dr_pages;
1069 int pages = iobuf->dr_npages;
1070 sector_t *blocks = iobuf->dr_blocks;
1071 struct niobuf_local *lnb1, *lnb2;
1072 loff_t size1, size2;
1074 max_page_index = inode->i_sb->s_maxbytes >> PAGE_SHIFT;
1076 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1077 inode->i_ino, pages, (*page)->index);
1080 create = LDISKFS_GET_BLOCKS_CREATE;
1081 handle = ldiskfs_journal_current_handle();
1082 LASSERT(handle != NULL);
1083 rc = osd_attach_jinode(inode);
1086 disk_size = i_size_read(inode);
1087 /* if disk_size is already bigger than specified user_size,
1090 if (disk_size > user_size)
1093 /* pages are sorted already. so, we just have to find
1094 * contig. space and process them properly
1097 long blen, total = 0, previous_total = 0;
1098 struct ldiskfs_map_blocks map = { 0 };
1100 if (fp == NULL) { /* start new extent */
1105 } else if (fp->index + clen == (*page)->index) {
1106 /* continue the extent */
1112 if (fp->index + clen >= max_page_index)
1113 GOTO(cleanup, rc = -EFBIG);
1114 /* process found extent */
1115 map.m_lblk = fp->index * blocks_per_page;
1116 map.m_len = blen = clen * blocks_per_page;
1119 * We might restart transaction for block allocations,
1120 * in order to make sure data ordered mode, issue IO, disk
1121 * size update and block allocations need be within same
1122 * transaction to make sure consistency.
1124 if (handle && check_credits) {
1125 struct osd_thandle *oh;
1127 LASSERT(thandle != NULL);
1128 oh = container_of(thandle, struct osd_thandle,
1131 * only issue IO if restart transaction needed,
1132 * as update disk size need hold inode lock, we
1133 * want to avoid that as much as possible.
1135 if (oh->oh_declared_ext <= 0) {
1136 rc = osd_ldiskfs_map_write(inode,
1137 iobuf, osd, start_blocks,
1138 count, &disk_size, user_size);
1141 thandle->th_restart_tran = 1;
1142 GOTO(cleanup, rc = -EAGAIN);
1145 if (OBD_FAIL_CHECK(OBD_FAIL_OST_RESTART_IO))
1146 oh->oh_declared_ext = 0;
1148 oh->oh_declared_ext--;
1150 rc = ldiskfs_map_blocks(handle, inode, &map, create);
1154 for (; total < blen && c < map.m_len; c++, total++) {
1156 *(blocks + total) = 0;
1160 if ((map.m_flags & LDISKFS_MAP_UNWRITTEN) &&
1162 /* don't try to read allocated, but
1163 * unwritten blocks, instead fill the
1164 * patches with zeros in osd_do_bio() */
1165 *(blocks + total) = 0;
1168 *(blocks + total) = map.m_pblk + c;
1169 /* unmap any possible underlying
1170 * metadata from the block device
1173 if ((map.m_flags & LDISKFS_MAP_NEW) &&
1175 clean_bdev_aliases(inode->i_sb->s_bdev,
1181 if (rc == 0 && create) {
1182 count += (total - previous_total);
1183 mapped_index = (count + blocks_per_page -
1184 1) / blocks_per_page - 1;
1185 lnb1 = iobuf->dr_lnbs[i - clen];
1186 lnb2 = iobuf->dr_lnbs[mapped_index];
1187 size1 = lnb1->lnb_file_offset -
1188 (lnb1->lnb_file_offset % PAGE_SIZE) +
1189 (total << inode->i_blkbits);
1190 size2 = lnb2->lnb_file_offset + lnb2->lnb_len;
1194 if (size1 > disk_size)
1198 if (rc == 0 && total < blen) {
1200 * decay extent blocks if we could not
1201 * allocate extent once.
1203 osd_decay_extent_bytes(osd,
1204 (total - previous_total) << inode->i_blkbits);
1205 map.m_lblk = fp->index * blocks_per_page + total;
1206 map.m_len = blen - total;
1207 previous_total = total;
1213 * decay extent blocks if we could allocate
1214 * good large extent.
1216 if (total - previous_total >=
1217 osd_extent_bytes(osd) >> inode->i_blkbits)
1218 osd_decay_extent_bytes(osd,
1219 (total - previous_total) << inode->i_blkbits);
1220 /* look for next extent */
1222 blocks += blocks_per_page * clen;
1225 if (rc == 0 && create &&
1226 start_blocks < pages * blocks_per_page) {
1227 rc = osd_ldiskfs_map_write(inode, iobuf, osd, start_blocks,
1228 count, &disk_size, user_size);
1229 LASSERT(start_blocks + count == pages * blocks_per_page);
1234 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
1235 struct niobuf_local *lnb, int npages)
1237 struct osd_thread_info *oti = osd_oti_get(env);
1238 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1239 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1240 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1249 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1250 if (unlikely(rc != 0))
1253 isize = i_size_read(inode);
1254 maxidx = ((isize + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1;
1256 start = ktime_get();
1257 for (i = 0; i < npages; i++) {
1260 * till commit the content of the page is undefined
1261 * we'll set it uptodate once bulk is done. otherwise
1262 * subsequent reads can access non-stable data
1264 ClearPageUptodate(lnb[i].lnb_page);
1266 if (lnb[i].lnb_len == PAGE_SIZE)
1269 if (maxidx >= lnb[i].lnb_page->index) {
1270 osd_iobuf_add_page(iobuf, &lnb[i]);
1273 char *p = kmap(lnb[i].lnb_page);
1275 off = lnb[i].lnb_page_offset;
1278 off = (lnb[i].lnb_page_offset + lnb[i].lnb_len) &
1281 memset(p + off, 0, PAGE_SIZE - off);
1282 kunmap(lnb[i].lnb_page);
1286 timediff = ktime_us_delta(end, start);
1287 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1289 if (iobuf->dr_npages) {
1290 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1292 if (likely(rc == 0)) {
1293 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1294 /* do IO stats for preparation reads */
1295 osd_fini_iobuf(osd, iobuf);
1301 struct osd_fextent {
1305 unsigned int mapped:1;
1308 static int osd_is_mapped(struct dt_object *dt, __u64 offset,
1309 struct osd_fextent *cached_extent)
1311 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1312 sector_t block = offset >> inode->i_blkbits;
1314 struct fiemap_extent_info fei = { 0 };
1315 struct fiemap_extent fe = { 0 };
1316 mm_segment_t saved_fs;
1319 if (block >= cached_extent->start && block < cached_extent->end)
1320 return cached_extent->mapped;
1322 if (i_size_read(inode) == 0)
1325 /* Beyond EOF, must not be mapped */
1326 if (((i_size_read(inode) - 1) >> inode->i_blkbits) < block)
1329 fei.fi_extents_max = 1;
1330 fei.fi_extents_start = &fe;
1332 saved_fs = get_fs();
1334 rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
1339 start = fe.fe_logical >> inode->i_blkbits;
1340 cached_extent->flags = fe.fe_flags;
1341 if (fei.fi_extents_mapped == 0) {
1342 /* a special case - no extent found at this offset and forward.
1343 * we can consider this as a hole to EOF. it's safe to cache
1344 * as other threads can not allocate/punch blocks this thread
1345 * is working on (LDLM). */
1346 cached_extent->start = block;
1347 cached_extent->end = i_size_read(inode) >> inode->i_blkbits;
1348 cached_extent->mapped = 0;
1352 if (start > block) {
1353 cached_extent->start = block;
1354 cached_extent->end = start;
1355 cached_extent->mapped = 0;
1357 cached_extent->start = start;
1358 cached_extent->end = (fe.fe_logical + fe.fe_length) >>
1360 cached_extent->mapped = 1;
1363 return cached_extent->mapped;
1366 #define MAX_EXTENTS_PER_WRITE 100
1367 static int osd_declare_write_commit(const struct lu_env *env,
1368 struct dt_object *dt,
1369 struct niobuf_local *lnb, int npages,
1370 struct thandle *handle)
1372 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1373 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1374 struct osd_thandle *oh;
1375 int extents = 0, new_meta = 0;
1376 int depth, new_blocks = 0;
1378 int dirty_groups = 0;
1381 long long quota_space = 0;
1382 struct osd_fextent mapped = { 0 }, extent = { 0 };
1383 enum osd_quota_local_flags local_flags = 0;
1384 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
1385 unsigned int extent_bytes;
1388 LASSERT(handle != NULL);
1389 oh = container_of(handle, struct osd_thandle, ot_super);
1390 LASSERT(oh->ot_handle == NULL);
1393 * We track a decaying average extent blocks per filesystem,
1394 * for most of time, it will be 1M, with filesystem becoming
1395 * heavily-fragmented, it will be reduced to 4K at the worst.
1397 extent_bytes = osd_extent_bytes(osd);
1398 LASSERT(extent_bytes >= (1 << osd_sb(osd)->s_blocksize));
1400 /* calculate number of extents (probably better to pass nb) */
1401 for (i = 0; i < npages; i++) {
1402 /* ignore quota for the whole request if any page is from
1403 * client cache or written by root.
1405 * XXX once we drop the 1.8 client support, the checking
1406 * for whether page is from cache can be simplified as:
1407 * !(lnb[i].flags & OBD_BRW_SYNC)
1409 * XXX we could handle this on per-lnb basis as done by
1412 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
1413 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
1415 declare_flags |= OSD_QID_FORCE;
1418 * Convert unwritten extent might need split extents, could
1421 if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &mapped) &&
1422 !(mapped.flags & FIEMAP_EXTENT_UNWRITTEN)) {
1423 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1427 if (lnb[i].lnb_flags & OBD_BRW_DONE) {
1428 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1432 /* count only unmapped changes */
1434 if (lnb[i].lnb_file_offset != extent.end || extent.end == 0) {
1435 if (extent.end != 0)
1436 extents += (extent.end - extent.start +
1437 extent_bytes - 1) / extent_bytes;
1438 extent.start = lnb[i].lnb_file_offset;
1439 extent.end = lnb[i].lnb_file_offset + lnb[i].lnb_len;
1441 extent.end += lnb[i].lnb_len;
1444 quota_space += PAGE_SIZE;
1447 credits++; /* inode */
1449 * overwrite case, no need to modify tree and
1455 extents += (extent.end - extent.start +
1456 extent_bytes - 1) / extent_bytes;
1458 * with system space usage growing up, mballoc codes won't
1459 * try best to scan block group to align best free extent as
1460 * we can. So extent bytes per extent could be decayed to a
1461 * very small value, this could make us reserve too many credits.
1462 * We could be more optimistic in the credit reservations, even
1463 * in a case where the filesystem is nearly full, it is extremely
1464 * unlikely that the worst case would ever be hit.
1466 if (extents > MAX_EXTENTS_PER_WRITE)
1467 extents = MAX_EXTENTS_PER_WRITE;
1470 * If we add a single extent, then in the worse case, each tree
1471 * level index/leaf need to be changed in case of the tree split.
1472 * If more extents are inserted, they could cause the whole tree
1473 * split more than once, but this is really rare.
1475 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
1476 depth = ext_depth(inode);
1478 credits += depth * 2 * extents;
1481 credits += depth * 3 * extents;
1482 new_meta = depth * 2 * extents;
1486 * With N contiguous data blocks, we need at most
1487 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
1488 * 2 dindirect blocks, and 1 tindirect block
1490 new_meta = DIV_ROUND_UP(new_blocks,
1491 LDISKFS_ADDR_PER_BLOCK(inode->i_sb)) + 4;
1492 credits += new_meta;
1495 dirty_groups += (extents + new_meta);
1497 oh->oh_declared_ext = extents;
1499 /* quota space for metadata blocks */
1500 quota_space += new_meta * LDISKFS_BLOCK_SIZE(osd_sb(osd));
1502 /* quota space should be reported in 1K blocks */
1503 quota_space = toqb(quota_space);
1505 /* each new block can go in different group (bitmap + gd) */
1507 /* we can't dirty more bitmap blocks than exist */
1508 if (dirty_groups > LDISKFS_SB(osd_sb(osd))->s_groups_count)
1509 credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
1511 credits += dirty_groups;
1513 /* we can't dirty more gd blocks than exist */
1514 if (extents > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
1515 credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
1517 credits += dirty_groups;
1520 "%s: inode #%lu extent_bytes %u extents %d credits %d\n",
1521 osd_ino2name(inode), inode->i_ino, extent_bytes, extents,
1525 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1527 /* make sure the over quota flags were not set */
1528 lnb[0].lnb_flags &= ~OBD_BRW_OVER_ALLQUOTA;
1530 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
1531 i_projid_read(inode), quota_space, oh,
1532 osd_dt_obj(dt), &local_flags, declare_flags);
1534 /* we need only to store the overquota flags in the first lnb for
1535 * now, once we support multiple objects BRW, this code needs be
1538 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
1539 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
1540 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
1541 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
1542 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
1543 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
1546 rc = osd_trunc_lock(osd_dt_obj(dt), oh, true);
1551 /* Check if a block is allocated or not */
1552 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
1553 struct niobuf_local *lnb, int npages,
1554 struct thandle *thandle, __u64 user_size)
1556 struct osd_thread_info *oti = osd_oti_get(env);
1557 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1558 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1559 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1560 int rc = 0, i, check_credits = 0;
1564 rc = osd_init_iobuf(osd, iobuf, 1, npages);
1565 if (unlikely(rc != 0))
1568 dquot_initialize(inode);
1570 for (i = 0; i < npages; i++) {
1571 if (lnb[i].lnb_rc == -ENOSPC &&
1572 (lnb[i].lnb_flags & OBD_BRW_MAPPED)) {
1573 /* Allow the write to proceed if overwriting an
1579 if (lnb[i].lnb_rc) { /* ENOSPC, network RPC error, etc. */
1580 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
1582 LASSERT(lnb[i].lnb_page);
1583 generic_error_remove_page(inode->i_mapping,
1588 if (lnb[i].lnb_flags & OBD_BRW_DONE)
1591 if (!(lnb[i].lnb_flags & OBD_BRW_MAPPED))
1594 LASSERT(PageLocked(lnb[i].lnb_page));
1595 LASSERT(!PageWriteback(lnb[i].lnb_page));
1598 * Since write and truncate are serialized by oo_sem, even
1599 * partial-page truncate should not leave dirty pages in the
1602 LASSERT(!PageDirty(lnb[i].lnb_page));
1604 SetPageUptodate(lnb[i].lnb_page);
1606 osd_iobuf_add_page(iobuf, &lnb[i]);
1609 osd_trans_exec_op(env, thandle, OSD_OT_WRITE);
1611 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
1613 } else if (iobuf->dr_npages > 0) {
1614 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd,
1619 /* no pages to write, no transno is needed */
1620 thandle->th_local = 1;
1623 if (rc != 0 && !thandle->th_restart_tran)
1624 osd_fini_iobuf(osd, iobuf);
1626 osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
1628 if (unlikely(rc != 0 && !thandle->th_restart_tran)) {
1629 /* if write fails, we should drop pages from the cache */
1630 for (i = 0; i < npages; i++) {
1631 if (lnb[i].lnb_page == NULL)
1633 if (!PagePrivate2(lnb[i].lnb_page)) {
1634 LASSERT(PageLocked(lnb[i].lnb_page));
1635 generic_error_remove_page(inode->i_mapping,
1644 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
1645 struct niobuf_local *lnb, int npages)
1647 struct osd_thread_info *oti = osd_oti_get(env);
1648 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1649 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1650 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1651 int rc = 0, i, cache_hits = 0, cache_misses = 0;
1658 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1659 if (unlikely(rc != 0))
1662 isize = i_size_read(inode);
1664 start = ktime_get();
1665 for (i = 0; i < npages; i++) {
1667 if (isize <= lnb[i].lnb_file_offset)
1668 /* If there's no more data, abort early.
1669 * lnb->lnb_rc == 0, so it's easy to detect later.
1673 /* instead of looking if we go beyong isize, send complete
1674 * pages all the time
1676 lnb[i].lnb_rc = lnb[i].lnb_len;
1678 /* Bypass disk read if fail_loc is set properly */
1679 if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW))
1680 SetPageUptodate(lnb[i].lnb_page);
1682 if (PageUptodate(lnb[i].lnb_page)) {
1684 unlock_page(lnb[i].lnb_page);
1687 osd_iobuf_add_page(iobuf, &lnb[i]);
1689 /* no need to unlock in osd_bufs_put(), the sooner page is
1690 * unlocked, the earlier another client can access it.
1691 * notice real unlock_page() can be called few lines
1692 * below after osd_do_bio(). lnb is a per-thread, so it's
1693 * fine to have PG_locked and lnb_locked inconsistent here
1695 lnb[i].lnb_locked = 0;
1698 timediff = ktime_us_delta(end, start);
1699 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1701 if (cache_hits != 0)
1702 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_HIT,
1704 if (cache_misses != 0)
1705 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_MISS,
1707 if (cache_hits + cache_misses != 0)
1708 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
1709 cache_hits + cache_misses);
1711 if (iobuf->dr_npages) {
1712 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1715 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1717 /* IO stats will be done in osd_bufs_put() */
1719 /* early release to let others read data during the bulk */
1720 for (i = 0; i < iobuf->dr_npages; i++) {
1721 LASSERT(PageLocked(iobuf->dr_pages[i]));
1722 if (!PagePrivate2(iobuf->dr_pages[i]))
1723 unlock_page(iobuf->dr_pages[i]);
1731 * XXX: Another layering violation for now.
1733 * We don't want to use ->f_op->read methods, because generic file write
1735 * - serializes on ->i_sem, and
1737 * - does a lot of extra work like balance_dirty_pages(),
1739 * which doesn't work for globally shared files like /last_rcvd.
1741 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
1743 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
1745 memcpy(buffer, (char *)ei->i_data, buflen);
1750 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
1752 struct buffer_head *bh;
1753 unsigned long block;
1759 /* prevent reading after eof */
1760 spin_lock(&inode->i_lock);
1761 if (i_size_read(inode) < *offs + size) {
1762 loff_t diff = i_size_read(inode) - *offs;
1764 spin_unlock(&inode->i_lock);
1767 "size %llu is too short to read @%llu\n",
1768 i_size_read(inode), *offs);
1770 } else if (diff == 0) {
1776 spin_unlock(&inode->i_lock);
1779 blocksize = 1 << inode->i_blkbits;
1782 block = *offs >> inode->i_blkbits;
1783 boffs = *offs & (blocksize - 1);
1784 csize = min(blocksize - boffs, size);
1785 bh = __ldiskfs_bread(NULL, inode, block, 0);
1787 CERROR("%s: can't read %u@%llu on ino %lu: rc = %ld\n",
1788 osd_ino2name(inode), csize, *offs, inode->i_ino,
1794 memcpy(buf, bh->b_data + boffs, csize);
1797 memset(buf, 0, csize);
1807 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1808 struct lu_buf *buf, loff_t *pos)
1810 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1813 /* Read small symlink from inode body as we need to maintain correct
1814 * on-disk symlinks for ldiskfs.
1816 if (S_ISLNK(dt->do_lu.lo_header->loh_attr)) {
1817 loff_t size = i_size_read(inode);
1819 if (buf->lb_len < size)
1822 if (size < sizeof(LDISKFS_I(inode)->i_data))
1823 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, size);
1825 rc = osd_ldiskfs_read(inode, buf->lb_buf, size, pos);
1827 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1833 static inline int osd_extents_enabled(struct super_block *sb,
1834 struct inode *inode)
1836 if (inode != NULL) {
1837 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL)
1839 } else if (ldiskfs_has_feature_extents(sb)) {
1845 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1846 const loff_t size, const loff_t pos,
1849 int credits, bits, bs, i;
1851 bits = sb->s_blocksize_bits;
1854 /* legacy blockmap: 3 levels * 3 (bitmap,gd,itself)
1855 * we do not expect blockmaps on the large files,
1856 * so let's shrink it to 2 levels (4GB files)
1859 /* this is default reservation: 2 levels */
1860 credits = (blocks + 2) * 3;
1862 /* actual offset is unknown, hard to optimize */
1866 /* now check for few specific cases to optimize */
1867 if (pos + size <= LDISKFS_NDIR_BLOCKS * bs) {
1870 /* allocate if not allocated */
1871 if (inode == NULL) {
1872 credits += blocks * 2;
1875 for (i = (pos >> bits); i < (pos >> bits) + blocks; i++) {
1876 LASSERT(i < LDISKFS_NDIR_BLOCKS);
1877 if (LDISKFS_I(inode)->i_data[i] == 0)
1880 } else if (pos + size <= (LDISKFS_NDIR_BLOCKS + 1024) * bs) {
1881 /* single indirect */
1882 credits = blocks * 3;
1883 if (inode == NULL ||
1884 LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK] == 0)
1887 /* The indirect block may be modified. */
1894 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1895 const struct lu_buf *buf, loff_t _pos,
1896 struct thandle *handle)
1898 struct osd_object *obj = osd_dt_obj(dt);
1899 struct inode *inode = obj->oo_inode;
1900 struct super_block *sb = osd_sb(osd_obj2dev(obj));
1901 struct osd_thandle *oh;
1902 int rc = 0, est = 0, credits, blocks, allocated = 0;
1908 LASSERT(buf != NULL);
1909 LASSERT(handle != NULL);
1911 oh = container_of(handle, struct osd_thandle, ot_super);
1912 LASSERT(oh->ot_handle == NULL);
1915 bits = sb->s_blocksize_bits;
1919 /* if this is an append, then we
1920 * should expect cross-block record
1927 /* blocks to modify */
1928 blocks = ((pos + size + bs - 1) >> bits) - (pos >> bits);
1929 LASSERT(blocks > 0);
1931 if (inode != NULL && _pos != -1) {
1932 /* object size in blocks */
1933 est = (i_size_read(inode) + bs - 1) >> bits;
1934 allocated = inode->i_blocks >> (bits - 9);
1935 if (pos + size <= i_size_read(inode) && est <= allocated) {
1936 /* looks like an overwrite, no need to modify tree */
1938 /* no need to modify i_size */
1943 if (osd_extents_enabled(sb, inode)) {
1945 * many concurrent threads may grow tree by the time
1946 * our transaction starts. so, consider 2 is a min depth
1947 * for every level we may need to allocate a new block
1948 * and take some entries from the old one. so, 3 blocks
1949 * to allocate (bitmap, gd, itself) + old block - 4 per
1952 depth = inode != NULL ? ext_depth(inode) : 0;
1953 depth = max(depth, 1) + 1;
1955 /* if not append, then split may need to modify
1956 * existing blocks moving entries into the new ones
1960 /* blocks to store data: bitmap,gd,itself */
1961 credits += blocks * 3;
1963 credits = osd_calc_bkmap_credits(sb, inode, size, _pos, blocks);
1965 /* if inode is created as part of the transaction,
1966 * then it's counted already by the creation method
1973 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1975 /* dt_declare_write() is usually called for system objects, such
1976 * as llog or last_rcvd files. We needn't enforce quota on those
1977 * objects, so always set the lqi_space as 0.
1980 rc = osd_declare_inode_qid(env, i_uid_read(inode),
1982 i_projid_read(inode), 0,
1983 oh, obj, NULL, OSD_QID_BLK);
1986 rc = osd_trunc_lock(obj, oh, true);
1991 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1993 /* LU-2634: clear the extent format for fast symlink */
1994 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1996 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1997 spin_lock(&inode->i_lock);
1998 LDISKFS_I(inode)->i_disksize = buflen;
1999 i_size_write(inode, buflen);
2000 spin_unlock(&inode->i_lock);
2001 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2006 static int osd_ldiskfs_write_record(struct dt_object *dt, void *buf,
2007 int bufsize, int write_NUL, loff_t *offs,
2010 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2011 struct buffer_head *bh = NULL;
2012 loff_t offset = *offs;
2013 loff_t new_size = i_size_read(inode);
2014 unsigned long block;
2015 int blocksize = 1 << inode->i_blkbits;
2016 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2020 int dirty_inode = 0;
2021 bool create, sparse, sync = false;
2025 * long symlink write does not count the NUL terminator in
2026 * bufsize, we write it, and the inode's file size does not
2027 * count the NUL terminator as well.
2029 ((char *)buf)[bufsize] = '\0';
2033 /* only the first flag-set matters */
2034 dirty_inode = !test_and_set_bit(LDISKFS_INODE_JOURNAL_DATA,
2037 /* sparse checking is racy, but sparse is very rare case, leave as is */
2038 sparse = (new_size > 0 && (inode->i_blocks >> (inode->i_blkbits - 9)) <
2039 ((new_size - 1) >> inode->i_blkbits) + 1);
2041 while (bufsize > 0) {
2042 int credits = handle->h_buffer_credits;
2043 unsigned long last_block = (new_size == 0) ? 0 :
2044 (new_size - 1) >> inode->i_blkbits;
2049 block = offset >> inode->i_blkbits;
2050 boffs = offset & (blocksize - 1);
2051 size = min(blocksize - boffs, bufsize);
2052 sync = (block > last_block || new_size == 0 || sparse);
2055 down(&ei->i_append_sem);
2057 bh = __ldiskfs_bread(handle, inode, block, 0);
2059 if (unlikely(IS_ERR_OR_NULL(bh) && !sync))
2061 "%s: adding bh without locking off %llu (block %lu, size %d, offs %llu)\n",
2062 osd_ino2name(inode),
2063 offset, block, bufsize, *offs);
2065 if (IS_ERR_OR_NULL(bh)) {
2066 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2067 int flags = LDISKFS_GET_BLOCKS_CREATE;
2069 /* while the file system is being mounted, avoid
2070 * preallocation otherwise mount can take a long
2071 * time as mballoc cache is cold.
2072 * XXX: this is a workaround until we have a proper
2074 * XXX: works with extent-based files only */
2075 if (!osd->od_cl_seq)
2076 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2077 bh = __ldiskfs_bread(handle, inode, block, flags);
2081 up(&ei->i_append_sem);
2086 if (IS_ERR_OR_NULL(bh)) {
2095 "%s: error reading offset %llu (block %lu, size %d, offs %llu), credits %d/%d: rc = %d\n",
2096 osd_ino2name(inode), offset, block, bufsize,
2097 *offs, credits, handle->h_buffer_credits, err);
2101 err = ldiskfs_journal_get_write_access(handle, bh);
2103 CERROR("journal_get_write_access() returned error %d\n",
2107 LASSERTF(boffs + size <= bh->b_size,
2108 "boffs %d size %d bh->b_size %lu\n",
2109 boffs, size, (unsigned long)bh->b_size);
2111 memset(bh->b_data, 0, bh->b_size);
2113 up(&ei->i_append_sem);
2117 memcpy(bh->b_data + boffs, buf, size);
2118 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2122 if (offset + size > new_size)
2123 new_size = offset + size;
2129 up(&ei->i_append_sem);
2136 /* correct in-core and on-disk sizes */
2137 if (new_size > i_size_read(inode)) {
2138 spin_lock(&inode->i_lock);
2139 if (new_size > i_size_read(inode))
2140 i_size_write(inode, new_size);
2141 if (i_size_read(inode) > ei->i_disksize) {
2142 ei->i_disksize = i_size_read(inode);
2145 spin_unlock(&inode->i_lock);
2148 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2155 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2156 const struct lu_buf *buf, loff_t *pos,
2157 struct thandle *handle)
2159 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2160 struct osd_thandle *oh;
2164 LASSERT(dt_object_exists(dt));
2166 LASSERT(handle != NULL);
2167 LASSERT(inode != NULL);
2168 dquot_initialize(inode);
2170 /* XXX: don't check: one declared chunk can be used many times */
2171 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
2173 oh = container_of(handle, struct osd_thandle, ot_super);
2174 LASSERT(oh->ot_handle->h_transaction != NULL);
2175 osd_trans_exec_op(env, handle, OSD_OT_WRITE);
2177 /* Write small symlink to inode body as we need to maintain correct
2178 * on-disk symlinks for ldiskfs.
2179 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
2180 * does not count it in.
2182 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
2183 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
2184 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2186 result = osd_ldiskfs_write_record(dt, buf->lb_buf, buf->lb_len,
2187 is_link, pos, oh->ot_handle);
2189 result = buf->lb_len;
2191 osd_trans_exec_check(env, handle, OSD_OT_WRITE);
2196 static int osd_declare_fallocate(const struct lu_env *env,
2197 struct dt_object *dt, __u64 start, __u64 end,
2198 int mode, struct thandle *th)
2200 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2201 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2202 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2203 long long quota_space = 0;
2204 /* 5 is max tree depth. (inode + 4 index blocks) */
2211 * Only mode == 0 (which is standard prealloc) is supported now.
2212 * Rest of mode options is not supported yet.
2214 if (mode & ~FALLOC_FL_KEEP_SIZE)
2215 RETURN(-EOPNOTSUPP);
2217 /* disable fallocate completely */
2218 if (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks < 0)
2219 RETURN(-EOPNOTSUPP);
2224 /* quota space for metadata blocks
2225 * approximate metadata estimate should be good enough.
2227 quota_space += PAGE_SIZE;
2228 quota_space += depth * LDISKFS_BLOCK_SIZE(osd_sb(osd));
2230 /* quota space should be reported in 1K blocks */
2231 quota_space = toqb(quota_space) + toqb(end - start) +
2232 LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
2234 /* We don't need to reserve credits for whole fallocate here.
2235 * We reserve space only for metadata. Fallocate credits are
2236 * extended as required
2238 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2239 i_projid_read(inode), quota_space, oh,
2240 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2244 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
2245 __u64 start, __u64 end, int mode, struct thandle *th)
2247 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2248 handle_t *handle = ldiskfs_journal_current_handle();
2249 unsigned int save_credits = oh->ot_credits;
2250 struct osd_object *obj = osd_dt_obj(dt);
2251 struct inode *inode = obj->oo_inode;
2252 struct ldiskfs_map_blocks map;
2253 unsigned int credits;
2254 ldiskfs_lblk_t blen;
2255 ldiskfs_lblk_t boff;
2256 loff_t new_size = 0;
2263 LASSERT(dt_object_exists(dt));
2264 LASSERT(osd_invariant(obj));
2265 LASSERT(inode != NULL);
2267 CDEBUG(D_INODE, "fallocate: inode #%lu: start %llu end %llu mode %d\n",
2268 inode->i_ino, start, end, mode);
2270 dquot_initialize(inode);
2274 boff = start >> inode->i_blkbits;
2275 blen = (ALIGN(end, 1 << inode->i_blkbits) >> inode->i_blkbits) - boff;
2277 /* Create and mark new extents as either zero or unwritten */
2278 flags = osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks ?
2279 LDISKFS_GET_BLOCKS_CREATE_ZERO :
2280 LDISKFS_GET_BLOCKS_CREATE_UNWRIT_EXT;
2281 if (mode & FALLOC_FL_KEEP_SIZE)
2282 flags |= LDISKFS_GET_BLOCKS_KEEP_SIZE;
2287 * We only support preallocation for extent-based file only.
2289 if (!(ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS)))
2290 GOTO(out, rc = -EOPNOTSUPP);
2292 if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > i_size_read(inode) ||
2293 end > LDISKFS_I(inode)->i_disksize)) {
2295 rc = inode_newsize_ok(inode, new_size);
2300 inode_dio_wait(inode);
2305 /* Don't normalize the request if it can fit in one extent so
2306 * that it doesn't get unnecessarily split into multiple extents.
2308 if (blen <= EXT_UNWRITTEN_MAX_LEN)
2309 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2312 * credits to insert 1 extent into extent tree.
2314 credits = osd_chunk_trans_blocks(inode, blen);
2315 depth = ext_depth(inode);
2317 while (rc >= 0 && blen) {
2321 * Recalculate credits when extent tree depth changes.
2323 if (depth != ext_depth(inode)) {
2324 credits = osd_chunk_trans_blocks(inode, blen);
2325 depth = ext_depth(inode);
2328 /* TODO: quota check */
2329 rc = osd_extend_restart_trans(handle, credits, inode);
2333 rc = ldiskfs_map_blocks(handle, inode, &map, flags);
2336 "inode #%lu: block %u: len %u: ldiskfs_map_blocks returned %d\n",
2337 inode->i_ino, map.m_lblk, map.m_len, rc);
2338 ldiskfs_mark_inode_dirty(handle, inode);
2343 map.m_len = blen = blen - rc;
2344 epos = (loff_t)map.m_lblk << inode->i_blkbits;
2345 inode->i_ctime = current_time(inode);
2349 if (ldiskfs_update_inode_size(inode, epos) & 0x1)
2350 inode->i_mtime = inode->i_ctime;
2352 if (epos > inode->i_size)
2353 ldiskfs_set_inode_flag(inode,
2354 LDISKFS_INODE_EOFBLOCKS);
2357 ldiskfs_mark_inode_dirty(handle, inode);
2361 /* extand credits if needed for operations such as attribute set */
2363 rc = osd_extend_restart_trans(handle, save_credits, inode);
2365 inode_unlock(inode);
2370 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
2371 __u64 start, __u64 end, struct thandle *th)
2373 struct osd_thandle *oh;
2374 struct inode *inode;
2379 oh = container_of(th, struct osd_thandle, ot_super);
2382 * we don't need to reserve credits for whole truncate
2383 * it's not possible as truncate may need to free too many
2384 * blocks and that won't fit a single transaction. instead
2385 * we reserve credits to change i_size and put inode onto
2386 * orphan list. if needed truncate will extend or restart
2389 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
2390 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
2392 inode = osd_dt_obj(dt)->oo_inode;
2395 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2396 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
2400 rc = osd_trunc_lock(osd_dt_obj(dt), oh, false);
2405 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
2406 __u64 start, __u64 end, struct thandle *th)
2408 struct osd_object *obj = osd_dt_obj(dt);
2409 struct osd_device *osd = osd_obj2dev(obj);
2410 struct inode *inode = obj->oo_inode;
2411 struct osd_access_lock *al;
2412 struct osd_thandle *oh;
2413 int rc = 0, found = 0;
2417 LASSERT(dt_object_exists(dt));
2418 LASSERT(osd_invariant(obj));
2419 LASSERT(inode != NULL);
2420 dquot_initialize(inode);
2423 oh = container_of(th, struct osd_thandle, ot_super);
2424 LASSERT(oh->ot_handle->h_transaction != NULL);
2426 /* we used to skip truncate to current size to
2427 * optimize truncates on OST. with DoM we can
2428 * get attr_set to set specific size (MDS_REINT)
2429 * and then get truncate RPC which essentially
2430 * would be skipped. this is bad.. so, disable
2431 * this optimization on MDS till the client stop
2432 * to sent MDS_REINT (LU-11033) -bzzz
2434 if (osd->od_is_ost && i_size_read(inode) == start)
2437 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
2439 spin_lock(&inode->i_lock);
2440 if (i_size_read(inode) < start)
2442 i_size_write(inode, start);
2443 spin_unlock(&inode->i_lock);
2444 /* if object holds encrypted content, we need to make sure we truncate
2445 * on an encryption unit boundary, or subsequent reads will get
2448 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2449 start & ~LUSTRE_ENCRYPTION_MASK)
2450 start = (start & LUSTRE_ENCRYPTION_MASK) +
2451 LUSTRE_ENCRYPTION_UNIT_SIZE;
2452 ll_truncate_pagecache(inode, start);
2454 /* optimize grow case */
2456 osd_execute_truncate(obj);
2461 /* add to orphan list to ensure truncate completion
2462 * if this transaction succeed. ldiskfs_truncate()
2463 * will take the inode out of the list
2465 rc = ldiskfs_orphan_add(oh->ot_handle, inode);
2466 inode_unlock(inode);
2470 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2471 if (obj != al->tl_obj)
2473 LASSERT(al->tl_shared == 0);
2475 /* do actual truncate in osd_trans_stop() */
2476 al->tl_truncate = 1;
2485 static int fiemap_check_ranges(struct inode *inode,
2486 u64 start, u64 len, u64 *new_len)
2495 if (ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS))
2496 maxbytes = inode->i_sb->s_maxbytes;
2498 maxbytes = LDISKFS_SB(inode->i_sb)->s_bitmap_maxbytes;
2500 if (start > maxbytes)
2504 * Shrink request scope to what the fs can actually handle.
2506 if (len > maxbytes || (maxbytes - len) < start)
2507 *new_len = maxbytes - start;
2512 /* So that the fiemap access checks can't overflow on 32 bit machines. */
2513 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
2515 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
2518 struct fiemap_extent_info fieinfo = {0, };
2519 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2522 mm_segment_t cur_fs;
2525 if (inode->i_op->fiemap == NULL)
2528 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS)
2531 rc = fiemap_check_ranges(inode, fm->fm_start, fm->fm_length, &len);
2535 fieinfo.fi_flags = fm->fm_flags;
2536 fieinfo.fi_extents_max = fm->fm_extent_count;
2537 fieinfo.fi_extents_start = fm->fm_extents;
2539 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
2540 filemap_write_and_wait(inode->i_mapping);
2542 /* Save previous value address limit */
2544 /* Set the address limit of the kernel */
2547 rc = inode->i_op->fiemap(inode, &fieinfo, fm->fm_start, len);
2548 fm->fm_flags = fieinfo.fi_flags;
2549 fm->fm_mapped_extents = fieinfo.fi_extents_mapped;
2551 /* Restore the previous address limt */
2557 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
2558 __u64 start, __u64 end, enum lu_ladvise_type advice)
2560 struct osd_object *obj = osd_dt_obj(dt);
2565 case LU_LADVISE_DONTNEED:
2567 invalidate_mapping_pages(obj->oo_inode->i_mapping,
2568 start >> PAGE_SHIFT,
2569 (end - 1) >> PAGE_SHIFT);
2579 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
2580 loff_t offset, int whence)
2582 struct osd_object *obj = osd_dt_obj(dt);
2583 struct inode *inode = obj->oo_inode;
2589 LASSERT(dt_object_exists(dt));
2590 LASSERT(osd_invariant(obj));
2592 LASSERT(offset >= 0);
2594 file = osd_quasi_file(env, inode);
2595 result = file->f_op->llseek(file, offset, whence);
2598 * If 'offset' is beyond end of object file then treat it as not error
2599 * but valid case for SEEK_HOLE and return 'offset' as result.
2600 * LOV will decide if it is beyond real end of file or not.
2602 if (whence == SEEK_HOLE && result == -ENXIO)
2605 CDEBUG(D_INFO, "seek %s from %lld: %lld\n", whence == SEEK_HOLE ?
2606 "hole" : "data", offset, result);
2611 * in some cases we may need declare methods for objects being created
2612 * e.g., when we create symlink
2614 const struct dt_body_operations osd_body_ops_new = {
2615 .dbo_declare_write = osd_declare_write,
2618 const struct dt_body_operations osd_body_ops = {
2619 .dbo_read = osd_read,
2620 .dbo_declare_write = osd_declare_write,
2621 .dbo_write = osd_write,
2622 .dbo_bufs_get = osd_bufs_get,
2623 .dbo_bufs_put = osd_bufs_put,
2624 .dbo_write_prep = osd_write_prep,
2625 .dbo_declare_write_commit = osd_declare_write_commit,
2626 .dbo_write_commit = osd_write_commit,
2627 .dbo_read_prep = osd_read_prep,
2628 .dbo_declare_punch = osd_declare_punch,
2629 .dbo_punch = osd_punch,
2630 .dbo_fiemap_get = osd_fiemap_get,
2631 .dbo_ladvise = osd_ladvise,
2632 .dbo_declare_fallocate = osd_declare_fallocate,
2633 .dbo_fallocate = osd_fallocate,
2634 .dbo_lseek = osd_lseek,
2638 * Get a truncate lock
2640 * In order to take multi-transaction truncate out of main transaction we let
2641 * the caller grab a lock on the object passed. the lock can be shared (for
2642 * writes) and exclusive (for truncate). It's not allowed to mix truncate
2643 * and write in the same transaction handle (do not confuse with big ldiskfs
2644 * transaction containing lots of handles).
2645 * The lock must be taken at declaration.
2647 * \param obj object to lock
2649 * \shared shared or exclusive
2651 * \retval 0 lock is granted
2652 * \retval -NOMEM no memory to allocate lock
2654 int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh, bool shared)
2656 struct osd_access_lock *al, *tmp;
2661 list_for_each_entry(tmp, &oh->ot_trunc_locks, tl_list) {
2662 if (tmp->tl_obj != obj)
2664 LASSERT(tmp->tl_shared == shared);
2665 /* found same lock */
2670 if (unlikely(al == NULL))
2673 al->tl_truncate = false;
2675 down_read(&obj->oo_ext_idx_sem);
2677 down_write(&obj->oo_ext_idx_sem);
2678 al->tl_shared = shared;
2679 lu_object_get(&obj->oo_dt.do_lu);
2681 list_add(&al->tl_list, &oh->ot_trunc_locks);
2686 void osd_trunc_unlock_all(const struct lu_env *env, struct list_head *list)
2688 struct osd_access_lock *al, *tmp;
2690 list_for_each_entry_safe(al, tmp, list, tl_list) {
2692 up_read(&al->tl_obj->oo_ext_idx_sem);
2694 up_write(&al->tl_obj->oo_ext_idx_sem);
2695 osd_object_put(env, al->tl_obj);
2696 list_del(&al->tl_list);
2701 void osd_execute_truncate(struct osd_object *obj)
2703 struct osd_device *d = osd_obj2dev(obj);
2704 struct inode *inode = obj->oo_inode;
2707 /* simulate crash before (in the middle) of delayed truncate */
2708 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FAIL_AT_TRUNCATE)) {
2709 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2710 struct ldiskfs_sb_info *sbi = LDISKFS_SB(inode->i_sb);
2712 mutex_lock(&sbi->s_orphan_lock);
2713 list_del_init(&ei->i_orphan);
2714 mutex_unlock(&sbi->s_orphan_lock);
2718 size = i_size_read(inode);
2720 /* if object holds encrypted content, we need to make sure we truncate
2721 * on an encryption unit boundary, or block content will get corrupted
2723 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2724 size & ~LUSTRE_ENCRYPTION_MASK)
2725 inode->i_size = (size & LUSTRE_ENCRYPTION_MASK) +
2726 LUSTRE_ENCRYPTION_UNIT_SIZE;
2727 ldiskfs_truncate(inode);
2728 inode_unlock(inode);
2729 if (inode->i_size != size) {
2730 spin_lock(&inode->i_lock);
2731 i_size_write(inode, size);
2732 LDISKFS_I(inode)->i_disksize = size;
2733 spin_unlock(&inode->i_lock);
2734 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2738 * For a partial-page truncate, flush the page to disk immediately to
2739 * avoid data corruption during direct disk write. b=17397
2741 if ((size & ~PAGE_MASK) == 0)
2743 if (osd_use_page_cache(d)) {
2744 filemap_fdatawrite_range(inode->i_mapping, size, size + 1);
2746 /* Notice we use "wait" version to ensure I/O is complete */
2747 filemap_write_and_wait_range(inode->i_mapping, size, size + 1);
2748 invalidate_mapping_pages(inode->i_mapping, size >> PAGE_SHIFT,
2749 size >> PAGE_SHIFT);
2753 void osd_process_truncates(struct list_head *list)
2755 struct osd_access_lock *al;
2757 LASSERT(journal_current_handle() == NULL);
2759 list_for_each_entry(al, list, tl_list) {
2762 if (!al->tl_truncate)
2764 osd_execute_truncate(al->tl_obj);