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 struct brw_stats *h = &d->od_brw_stats;
152 iobuf->dr_elapsed_valid = 0;
153 LASSERT(iobuf->dr_dev == d);
154 LASSERT(iobuf->dr_frags > 0);
155 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_R_DIO_FRAGS+rw],
157 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_R_IO_TIME+rw],
158 ktime_to_ms(iobuf->dr_elapsed));
162 #ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
163 static void dio_complete_routine(struct bio *bio)
165 int error = blk_status_to_errno(bio->bi_status);
167 static void dio_complete_routine(struct bio *bio, int error)
170 struct osd_iobuf *iobuf = bio->bi_private;
173 /* CAVEAT EMPTOR: possibly in IRQ context
174 * DO NOT record procfs stats here!!!
177 if (unlikely(iobuf == NULL)) {
178 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");
179 CERROR("bi_next: %p, bi_flags: %lx, " __stringify(bi_opf)
180 ": %x, bi_vcnt: %d, bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, bi_private: %p\n",
181 bio->bi_next, (unsigned long)bio->bi_flags,
182 (unsigned int)bio->bi_opf, bio->bi_vcnt, bio_idx(bio),
183 bio_sectors(bio) << 9, bio->bi_end_io,
184 atomic_read(&bio->__bi_cnt),
189 /* the check is outside of the cycle for performance reason -bzzz */
190 if (!bio_data_dir(bio)) {
191 DECLARE_BVEC_ITER_ALL(iter_all);
193 bio_for_each_segment_all(bvl, bio, iter_all) {
194 if (likely(error == 0))
195 SetPageUptodate(bvl_to_page(bvl));
196 LASSERT(PageLocked(bvl_to_page(bvl)));
198 atomic_dec(&iobuf->dr_dev->od_r_in_flight);
200 atomic_dec(&iobuf->dr_dev->od_w_in_flight);
203 /* any real error is good enough -bzzz */
204 if (error != 0 && iobuf->dr_error == 0)
205 iobuf->dr_error = error;
208 * set dr_elapsed before dr_numreqs turns to 0, otherwise
209 * it's possible that service thread will see dr_numreqs
210 * is zero, but dr_elapsed is not set yet, leading to lost
211 * data in this processing and an assertion in a subsequent
214 if (atomic_read(&iobuf->dr_numreqs) == 1) {
215 ktime_t now = ktime_get();
217 iobuf->dr_elapsed = ktime_sub(now, iobuf->dr_start_time);
218 iobuf->dr_elapsed_valid = 1;
220 if (atomic_dec_and_test(&iobuf->dr_numreqs))
221 wake_up(&iobuf->dr_wait);
223 /* Completed bios used to be chained off iobuf->dr_bios and freed in
224 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
225 * mempool when serious on-disk fragmentation was encountered,
226 * deadlocking the OST. The bios are now released as soon as complete
227 * so the pool cannot be exhausted while IOs are competing. b=10076
232 static void record_start_io(struct osd_iobuf *iobuf, int size)
234 struct osd_device *osd = iobuf->dr_dev;
235 struct brw_stats *h = &osd->od_brw_stats;
238 atomic_inc(&iobuf->dr_numreqs);
240 if (iobuf->dr_rw == 0) {
241 atomic_inc(&osd->od_r_in_flight);
242 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_R_RPC_HIST],
243 atomic_read(&osd->od_r_in_flight));
244 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_R_DISK_IOSIZE],
246 } else if (iobuf->dr_rw == 1) {
247 atomic_inc(&osd->od_w_in_flight);
248 lprocfs_oh_tally_pcpu(&h->bs_hist[BRW_W_RPC_HIST],
249 atomic_read(&osd->od_w_in_flight));
250 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[BRW_W_DISK_IOSIZE],
257 static void osd_submit_bio(int rw, struct bio *bio)
259 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
260 #ifdef HAVE_SUBMIT_BIO_2ARGS
261 submit_bio(rw ? WRITE : READ, bio);
268 static int can_be_merged(struct bio *bio, sector_t sector)
273 return bio_end_sector(bio) == sector ? 1 : 0;
276 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
278 * This function will change the data written, thus it should only be
279 * used when checking data integrity feature
281 static void bio_integrity_fault_inject(struct bio *bio)
283 struct bio_vec *bvec;
284 DECLARE_BVEC_ITER_ALL(iter_all);
288 bio_for_each_segment_all(bvec, bio, iter_all) {
289 struct page *page = bvec->bv_page;
299 static int bio_dif_compare(__u16 *expected_guard_buf, void *bio_prot_buf,
300 unsigned int sectors, int tuple_size)
302 __u16 *expected_guard;
306 expected_guard = expected_guard_buf;
307 for (i = 0; i < sectors; i++) {
308 bio_guard = (__u16 *)bio_prot_buf;
309 if (*bio_guard != *expected_guard) {
311 "unexpected guard tags on sector %d expected guard %u, bio guard %u, sectors %u, tuple size %d\n",
312 i, *expected_guard, *bio_guard, sectors,
317 bio_prot_buf += tuple_size;
322 static int osd_bio_integrity_compare(struct bio *bio, struct block_device *bdev,
323 struct osd_iobuf *iobuf, int index)
325 struct blk_integrity *bi = bdev_get_integrity(bdev);
326 struct bio_integrity_payload *bip = bio->bi_integrity;
327 struct niobuf_local *lnb = NULL;
328 unsigned short sector_size = blk_integrity_interval(bi);
329 void *bio_prot_buf = page_address(bip->bip_vec->bv_page) +
330 bip->bip_vec->bv_offset;
332 sector_t sector = bio_start_sector(bio);
333 unsigned int i, sectors, total;
334 DECLARE_BVEC_ITER_ALL(iter_all);
335 __u16 *expected_guard;
339 bio_for_each_segment_all(bv, bio, iter_all) {
340 for (i = index; i < iobuf->dr_npages; i++) {
341 if (iobuf->dr_pages[i] == bv->bv_page) {
342 lnb = iobuf->dr_lnbs[i];
348 expected_guard = lnb->lnb_guards;
349 sectors = bv->bv_len / sector_size;
350 if (lnb->lnb_guard_rpc) {
351 rc = bio_dif_compare(expected_guard, bio_prot_buf,
352 sectors, bi->tuple_size);
358 bio_prot_buf += sectors * bi->tuple_size;
359 total += sectors * bi->tuple_size;
360 LASSERT(total <= bip_size(bio->bi_integrity));
367 static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
368 struct osd_iobuf *iobuf,
369 int start_page_idx, bool fault_inject,
370 bool integrity_enabled)
372 struct super_block *sb = osd_sb(osd);
373 integrity_gen_fn *generate_fn = NULL;
374 integrity_vrfy_fn *verify_fn = NULL;
379 if (!integrity_enabled)
382 rc = osd_get_integrity_profile(osd, &generate_fn, &verify_fn);
386 rc = bio_integrity_prep_fn(bio, generate_fn, verify_fn);
390 /* Verify and inject fault only when writing */
391 if (iobuf->dr_rw == 1) {
392 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_CMP))) {
393 rc = osd_bio_integrity_compare(bio, sb->s_bdev, iobuf,
399 if (unlikely(fault_inject))
400 bio_integrity_fault_inject(bio);
406 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
407 # ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
408 static void dio_integrity_complete_routine(struct bio *bio)
410 static void dio_integrity_complete_routine(struct bio *bio, int error)
413 struct osd_bio_private *bio_private = bio->bi_private;
415 bio->bi_private = bio_private->obp_iobuf;
416 osd_dio_complete_routine(bio, error);
418 OBD_FREE_PTR(bio_private);
420 #endif /* HAVE_BIO_INTEGRITY_PREP_FN */
421 #else /* !CONFIG_BLK_DEV_INTEGRITY */
422 #define osd_bio_integrity_handle(osd, bio, iobuf, start_page_idx, \
423 fault_inject, integrity_enabled) 0
424 #endif /* CONFIG_BLK_DEV_INTEGRITY */
426 static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
427 bool integrity_enabled, int start_page_idx,
428 struct osd_bio_private **pprivate)
434 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
435 if (integrity_enabled) {
436 struct osd_bio_private *bio_private = NULL;
438 OBD_ALLOC_GFP(bio_private, sizeof(*bio_private), GFP_NOIO);
439 if (bio_private == NULL)
441 bio->bi_end_io = dio_integrity_complete_routine;
442 bio->bi_private = bio_private;
443 bio_private->obp_start_page_idx = start_page_idx;
444 bio_private->obp_iobuf = iobuf;
445 *pprivate = bio_private;
449 bio->bi_end_io = dio_complete_routine;
450 bio->bi_private = iobuf;
456 static void osd_mark_page_io_done(struct osd_iobuf *iobuf,
458 sector_t start_blocks,
461 struct niobuf_local *lnb;
462 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
463 pgoff_t pg_start, pg_end;
465 pg_start = start_blocks / blocks_per_page;
466 if (start_blocks % blocks_per_page)
468 if (count >= blocks_per_page)
469 pg_end = (start_blocks + count -
470 blocks_per_page) / blocks_per_page;
472 return; /* nothing to mark */
473 for ( ; pg_start <= pg_end; pg_start++) {
474 lnb = iobuf->dr_lnbs[pg_start];
475 lnb->lnb_flags |= OBD_BRW_DONE;
480 * Linux v5.12-rc1-20-ga8affc03a9b3
481 * block: rename BIO_MAX_PAGES to BIO_MAX_VECS
484 #define BIO_MAX_VECS BIO_MAX_PAGES
487 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
488 struct osd_iobuf *iobuf, sector_t start_blocks,
491 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
492 struct page **pages = iobuf->dr_pages;
493 int npages = iobuf->dr_npages;
494 sector_t *blocks = iobuf->dr_blocks;
495 struct super_block *sb = inode->i_sb;
496 int sector_bits = sb->s_blocksize_bits - 9;
497 unsigned int blocksize = sb->s_blocksize;
498 struct block_device *bdev = sb->s_bdev;
499 struct osd_bio_private *bio_private = NULL;
500 struct bio *bio = NULL;
501 int bio_start_page_idx;
503 unsigned int page_offset;
506 int block_idx, block_idx_end;
507 int page_idx, page_idx_start;
511 bool integrity_enabled;
512 struct blk_plug plug;
513 int blocks_left_page;
517 fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
518 LASSERT(iobuf->dr_npages == npages);
520 integrity_enabled = bdev_integrity_enabled(bdev, iobuf->dr_rw);
522 osd_brw_stats_update(osd, iobuf);
523 iobuf->dr_start_time = ktime_get();
526 count = npages * blocks_per_page;
527 block_idx_end = start_blocks + count;
529 blk_start_plug(&plug);
531 page_idx_start = start_blocks / blocks_per_page;
532 for (page_idx = page_idx_start, block_idx = start_blocks;
533 block_idx < block_idx_end; page_idx++,
534 block_idx += blocks_left_page) {
535 /* For cases where the filesystems blocksize is not the
536 * same as PAGE_SIZE (e.g. ARM with PAGE_SIZE=64KB and
537 * blocksize=4KB), there will be multiple blocks to
538 * read/write per page. Also, the start and end block may
539 * not be aligned to the start and end of the page, so the
540 * first page may skip some blocks at the start ("i != 0",
541 * "blocks_left_page" is reduced), and the last page may
542 * skip some blocks at the end (limited by "count").
544 page = pages[page_idx];
545 LASSERT(page_idx < iobuf->dr_npages);
547 i = block_idx % blocks_per_page;
548 blocks_left_page = blocks_per_page - i;
549 if (block_idx + blocks_left_page > block_idx_end)
550 blocks_left_page = block_idx_end - block_idx;
551 page_offset = i * blocksize;
552 for (i = 0; i < blocks_left_page;
553 i += nblocks, page_offset += blocksize * nblocks) {
556 if (blocks[block_idx + i] == 0) { /* hole */
557 LASSERTF(iobuf->dr_rw == 0,
558 "page_idx %u, block_idx %u, i %u,"
559 "start_blocks: %llu, count: %llu, npages: %d\n",
560 page_idx, block_idx, i,
561 (unsigned long long)start_blocks,
562 (unsigned long long)count, npages);
563 memset(kmap(page) + page_offset, 0, blocksize);
568 sector = (sector_t)blocks[block_idx + i] << sector_bits;
570 /* Additional contiguous file blocks? */
571 while (i + nblocks < blocks_left_page &&
572 (sector + (nblocks << sector_bits)) ==
573 ((sector_t)blocks[block_idx + i + nblocks] <<
577 if (bio && can_be_merged(bio, sector) &&
578 bio_add_page(bio, page, blocksize * nblocks,
580 continue; /* added this frag OK */
583 struct request_queue *q = bio_get_queue(bio);
584 unsigned int bi_size = bio_sectors(bio) << 9;
586 /* Dang! I have to fragment this I/O */
588 "bio++ sz %d vcnt %d(%d) sectors %d(%d) psg %d(%d)\n",
589 bi_size, bio->bi_vcnt, bio->bi_max_vecs,
591 queue_max_sectors(q),
592 osd_bio_nr_segs(bio),
593 queue_max_segments(q));
594 rc = osd_bio_integrity_handle(osd, bio,
595 iobuf, bio_start_page_idx,
596 fault_inject, integrity_enabled);
602 record_start_io(iobuf, bi_size);
603 osd_submit_bio(iobuf->dr_rw, bio);
606 bio_start_page_idx = page_idx;
607 /* allocate new bio */
608 bio = bio_alloc(GFP_NOIO, min_t(unsigned short,
610 (block_idx_end - block_idx +
611 blocks_left_page - 1)));
613 CERROR("Can't allocate bio %u pages\n",
614 block_idx_end - block_idx +
615 blocks_left_page - 1);
620 bio_set_dev(bio, bdev);
621 bio_set_sector(bio, sector);
622 bio->bi_opf = iobuf->dr_rw ? WRITE : READ;
623 rc = osd_bio_init(bio, iobuf, integrity_enabled,
624 bio_start_page_idx, &bio_private);
630 rc = bio_add_page(bio, page,
631 blocksize * nblocks, page_offset);
637 rc = osd_bio_integrity_handle(osd, bio, iobuf,
646 record_start_io(iobuf, bio_sectors(bio) << 9);
647 osd_submit_bio(iobuf->dr_rw, bio);
652 blk_finish_plug(&plug);
654 /* in order to achieve better IO throughput, we don't wait for writes
655 * completion here. instead we proceed with transaction commit in
656 * parallel and wait for IO completion once transaction is stopped
657 * see osd_trans_stop() for more details -bzzz
659 if (iobuf->dr_rw == 0 || fault_inject) {
660 wait_event(iobuf->dr_wait,
661 atomic_read(&iobuf->dr_numreqs) == 0);
662 osd_fini_iobuf(osd, iobuf);
666 rc = iobuf->dr_error;
669 OBD_FREE_PTR(bio_private);
673 if (rc == 0 && iobuf->dr_rw)
674 osd_mark_page_io_done(iobuf, inode,
675 start_blocks, count);
680 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
681 struct niobuf_local *lnb, int maxlnb)
689 int poff = offset & (PAGE_SIZE - 1);
690 int plen = PAGE_SIZE - poff;
692 if (*nrpages >= maxlnb) {
699 lnb->lnb_file_offset = offset;
700 lnb->lnb_page_offset = poff;
702 /* lnb->lnb_flags = rnb->rnb_flags; */
704 lnb->lnb_page = NULL;
706 lnb->lnb_guard_rpc = 0;
707 lnb->lnb_guard_disk = 0;
710 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
721 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
722 loff_t offset, gfp_t gfp_mask, bool cache)
724 struct osd_thread_info *oti = osd_oti_get(env);
725 struct inode *inode = osd_dt_obj(dt)->oo_inode;
726 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
733 page = find_or_create_page(inode->i_mapping,
734 offset >> PAGE_SHIFT, gfp_mask);
737 LASSERT(!PagePrivate2(page));
738 wait_on_page_writeback(page);
740 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
746 if (inode->i_mapping->nrpages) {
747 /* consult with pagecache, but do not create new pages */
748 /* this is normally used once */
749 page = find_lock_page(inode->i_mapping, offset >> PAGE_SHIFT);
751 wait_on_page_writeback(page);
756 LASSERT(oti->oti_dio_pages);
757 cur = oti->oti_dio_pages_used;
758 page = oti->oti_dio_pages[cur];
760 if (unlikely(!page)) {
761 LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
762 page = alloc_page(gfp_mask);
765 oti->oti_dio_pages[cur] = page;
766 SetPagePrivate2(page);
770 ClearPageUptodate(page);
771 page->index = offset >> PAGE_SHIFT;
772 oti->oti_dio_pages_used++;
778 * there are following "locks":
789 * - lock pages, unlock
791 * - lock partial page
797 * Unlock and release pages loaded by osd_bufs_get()
799 * Unlock \a npages pages from \a lnb and drop the refcount on them.
801 * \param env thread execution environment
802 * \param dt dt object undergoing IO (OSD object + methods)
803 * \param lnb array of pages undergoing IO
804 * \param npages number of pages in \a lnb
808 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
809 struct niobuf_local *lnb, int npages)
811 struct osd_thread_info *oti = osd_oti_get(env);
815 ll_pagevec_init(&pvec, 0);
817 for (i = 0; i < npages; i++) {
818 struct page *page = lnb[i].lnb_page;
823 /* if the page isn't cached, then reset uptodate
826 if (PagePrivate2(page)) {
827 oti->oti_dio_pages_used--;
829 if (lnb[i].lnb_locked)
831 if (pagevec_add(&pvec, page) == 0)
832 pagevec_release(&pvec);
835 lnb[i].lnb_page = NULL;
838 LASSERTF(oti->oti_dio_pages_used == 0, "%d\n", oti->oti_dio_pages_used);
840 /* Release any partial pagevec */
841 pagevec_release(&pvec);
847 * Load and lock pages undergoing IO
849 * Pages as described in the \a lnb array are fetched (from disk or cache)
850 * and locked for IO by the caller.
852 * DLM locking protects us from write and truncate competing for same region,
853 * but partial-page truncate can leave dirty pages in the cache for ldiskfs.
854 * It's possible the writeout on a such a page is in progress when we access
855 * it. It's also possible that during this writeout we put new (partial) data
856 * into the page, but won't be able to proceed in filter_commitrw_write().
857 * Therefore, just wait for writeout completion as it should be rare enough.
859 * \param env thread execution environment
860 * \param dt dt object undergoing IO (OSD object + methods)
861 * \param pos byte offset of IO start
862 * \param len number of bytes of IO
863 * \param lnb array of extents undergoing IO
864 * \param rw read or write operation, and other flags
865 * \param capa capabilities
867 * \retval pages (zero or more) loaded successfully
868 * \retval -ENOMEM on memory/page allocation error
870 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
871 loff_t pos, ssize_t len, struct niobuf_local *lnb,
872 int maxlnb, enum dt_bufs_type rw)
874 struct osd_thread_info *oti = osd_oti_get(env);
875 struct osd_object *obj = osd_dt_obj(dt);
876 struct osd_device *osd = osd_obj2dev(obj);
877 int npages, i, iosize, rc = 0;
882 LASSERT(obj->oo_inode);
884 if (unlikely(obj->oo_destroyed))
887 rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
891 write = rw & DT_BUFS_TYPE_WRITE;
893 fsize = lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len;
894 iosize = fsize - lnb[0].lnb_file_offset;
895 fsize = max(fsize, i_size_read(obj->oo_inode));
897 cache = rw & DT_BUFS_TYPE_READAHEAD;
901 cache = osd_use_page_cache(osd);
904 if (!osd->od_writethrough_cache) {
908 if (iosize > osd->od_writethrough_max_iosize) {
913 if (!osd->od_read_cache) {
917 if (iosize > osd->od_readcache_max_iosize) {
922 /* don't use cache on large files */
923 if (osd->od_readcache_max_filesize &&
924 fsize > osd->od_readcache_max_filesize)
930 if (!cache && unlikely(!oti->oti_dio_pages)) {
931 OBD_ALLOC_PTR_ARRAY_LARGE(oti->oti_dio_pages,
932 PTLRPC_MAX_BRW_PAGES);
933 if (!oti->oti_dio_pages)
937 /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
938 gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
940 for (i = 0; i < npages; i++, lnb++) {
941 lnb->lnb_page = osd_get_page(env, dt, lnb->lnb_file_offset,
943 if (lnb->lnb_page == NULL)
944 GOTO(cleanup, rc = -ENOMEM);
948 mark_page_accessed(lnb->lnb_page);
952 /* XXX: this version doesn't invalidate cached pages, but use them */
953 if (!cache && write && obj->oo_inode->i_mapping->nrpages) {
954 /* do not allow data aliasing, invalidate pagecache */
955 /* XXX: can be quite expensive in mixed case */
956 invalidate_mapping_pages(obj->oo_inode->i_mapping,
957 lnb[0].lnb_file_offset >> PAGE_SHIFT,
958 lnb[npages - 1].lnb_file_offset >> PAGE_SHIFT);
966 osd_bufs_put(env, dt, lnb - i, i);
969 /* Borrow @ext4_chunk_trans_blocks */
970 static int osd_chunk_trans_blocks(struct inode *inode, int nrblocks)
972 ldiskfs_group_t groups;
978 depth = ext_depth(inode);
979 idxblocks = depth * 2;
982 * Now let's see how many group bitmaps and group descriptors need
985 groups = idxblocks + 1;
987 if (groups > LDISKFS_SB(inode->i_sb)->s_groups_count)
988 groups = LDISKFS_SB(inode->i_sb)->s_groups_count;
989 if (gdpblocks > LDISKFS_SB(inode->i_sb)->s_gdb_count)
990 gdpblocks = LDISKFS_SB(inode->i_sb)->s_gdb_count;
992 /* bitmaps and block group descriptor blocks */
993 ret = idxblocks + groups + gdpblocks;
995 /* Blocks for super block, inode, quota and xattr blocks */
996 ret += LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
1001 #ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
1002 static int osd_extend_restart_trans(handle_t *handle, int needed,
1003 struct inode *inode)
1007 rc = ldiskfs_journal_ensure_credits(handle, needed,
1008 ldiskfs_trans_default_revoke_credits(inode->i_sb));
1009 /* this means journal has been restarted */
1016 static int osd_extend_restart_trans(handle_t *handle, int needed,
1017 struct inode *inode)
1021 if (ldiskfs_handle_has_enough_credits(handle, needed))
1023 rc = ldiskfs_journal_extend(handle,
1024 needed - handle->h_buffer_credits);
1028 return ldiskfs_journal_restart(handle, needed);
1030 #endif /* HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS */
1032 static int osd_ldiskfs_map_write(struct inode *inode, struct osd_iobuf *iobuf,
1033 struct osd_device *osd, sector_t start_blocks,
1034 sector_t count, loff_t *disk_size,
1037 /* if file has grown, take user_size into account */
1038 if (user_size && *disk_size > user_size)
1039 *disk_size = user_size;
1041 spin_lock(&inode->i_lock);
1042 if (*disk_size > i_size_read(inode)) {
1043 i_size_write(inode, *disk_size);
1044 LDISKFS_I(inode)->i_disksize = *disk_size;
1045 spin_unlock(&inode->i_lock);
1046 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
1048 spin_unlock(&inode->i_lock);
1052 * We don't do stats here as in read path because
1053 * write is async: we'll do this in osd_put_bufs()
1055 return osd_do_bio(osd, inode, iobuf, start_blocks, count);
1058 static unsigned int osd_extent_bytes(const struct osd_device *o)
1060 unsigned int *extent_bytes_ptr =
1061 raw_cpu_ptr(o->od_extent_bytes_percpu);
1063 if (likely(*extent_bytes_ptr))
1064 return *extent_bytes_ptr;
1066 /* initialize on first access or CPU hotplug */
1067 if (!ldiskfs_has_feature_extents(osd_sb(o)))
1068 *extent_bytes_ptr = 1 << osd_sb(o)->s_blocksize_bits;
1070 *extent_bytes_ptr = OSD_DEFAULT_EXTENT_BYTES;
1072 return *extent_bytes_ptr;
1075 #define EXTENT_BYTES_DECAY 64
1076 static void osd_decay_extent_bytes(struct osd_device *osd,
1077 unsigned int new_bytes)
1079 unsigned int old_bytes;
1081 if (!ldiskfs_has_feature_extents(osd_sb(osd)))
1084 old_bytes = osd_extent_bytes(osd);
1085 *raw_cpu_ptr(osd->od_extent_bytes_percpu) =
1086 (old_bytes * (EXTENT_BYTES_DECAY - 1) +
1087 min(new_bytes, OSD_DEFAULT_EXTENT_BYTES) +
1088 EXTENT_BYTES_DECAY - 1) / EXTENT_BYTES_DECAY;
1091 static int osd_ldiskfs_map_inode_pages(struct inode *inode,
1092 struct osd_iobuf *iobuf,
1093 struct osd_device *osd,
1094 int create, __u64 user_size,
1096 struct thandle *thandle)
1098 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1099 int blocksize = 1 << inode->i_blkbits;
1100 int rc = 0, i = 0, mapped_index = 0;
1101 struct page *fp = NULL;
1103 pgoff_t max_page_index;
1104 handle_t *handle = NULL;
1105 sector_t start_blocks = 0, count = 0;
1106 loff_t disk_size = 0;
1107 struct page **page = iobuf->dr_pages;
1108 int pages = iobuf->dr_npages;
1109 sector_t *blocks = iobuf->dr_blocks;
1110 struct niobuf_local *lnb1, *lnb2;
1111 loff_t size1, size2;
1113 max_page_index = inode->i_sb->s_maxbytes >> PAGE_SHIFT;
1115 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1116 inode->i_ino, pages, (*page)->index);
1119 create = LDISKFS_GET_BLOCKS_CREATE;
1120 handle = ldiskfs_journal_current_handle();
1121 LASSERT(handle != NULL);
1122 rc = osd_attach_jinode(inode);
1125 disk_size = i_size_read(inode);
1126 /* if disk_size is already bigger than specified user_size,
1129 if (disk_size > user_size)
1132 /* pages are sorted already. so, we just have to find
1133 * contig. space and process them properly
1136 long blen, total = 0, previous_total = 0;
1137 struct ldiskfs_map_blocks map = { 0 };
1140 if (fp == NULL) { /* start new extent */
1145 } else if (fp->index + clen == (*page)->index) {
1146 /* continue the extent */
1152 if (fp->index + clen >= max_page_index)
1153 GOTO(cleanup, rc = -EFBIG);
1154 /* process found extent */
1155 map.m_lblk = fp->index * blocks_per_page;
1156 map.m_len = blen = clen * blocks_per_page;
1159 * For PAGE_SIZE > blocksize block allocation mapping, the
1160 * ldiskfs_map_blocks() aims at looking up already mapped
1161 * blocks, recording them to iobuf->dr_blocks and fixing up
1162 * m_lblk, m_len for un-allocated blocks to be created/mapped
1163 * in the second ldiskfs_map_blocks().
1165 * M_lblk should be the first un-allocated block if m_lblk
1166 * points at an already allocated block when create = 1,
1167 * ldiskfs_map_blocks() will just return with already
1168 * allocated blocks and without allocating any requested
1169 * new blocks for the extent. For PAGE_SIZE = blocksize
1170 * case, if m_lblk points at an already allocated block it
1171 * will point at an un-allocated block in next restart
1172 * transaction, because the already mapped block/page will
1173 * be filtered out in next restart transaction via flag
1174 * OBD_BRW_DONE in osd_declare_write_commit().
1176 if (create && PAGE_SIZE > blocksize) {
1177 /* With flags=0 just for already mapped blocks lookup */
1178 rc = ldiskfs_map_blocks(handle, inode, &map, 0);
1179 if (rc > 0 && map.m_flags & LDISKFS_MAP_MAPPED) {
1180 for (; total < blen && total < map.m_len;
1182 *(blocks + total) = map.m_pblk + total;
1184 /* The extent is already full mapped */
1185 if (total == blen) {
1187 goto ext_already_mapped;
1191 * Fixup or reset m_lblk and m_len for un-mapped blocks.
1192 * The second ldiskfs_map_blocks() will create and map
1195 map.m_lblk = fp->index * blocks_per_page + total;
1196 map.m_len = blen - total;
1201 * We might restart transaction for block allocations,
1202 * in order to make sure data ordered mode, issue IO, disk
1203 * size update and block allocations need be within same
1204 * transaction to make sure consistency.
1206 if (handle && check_credits) {
1207 struct osd_thandle *oh;
1209 LASSERT(thandle != NULL);
1210 oh = container_of(thandle, struct osd_thandle,
1213 * only issue IO if restart transaction needed,
1214 * as update disk size need hold inode lock, we
1215 * want to avoid that as much as possible.
1217 if (oh->oh_declared_ext <= 0) {
1218 rc = osd_ldiskfs_map_write(inode,
1219 iobuf, osd, start_blocks,
1220 count, &disk_size, user_size);
1223 thandle->th_restart_tran = 1;
1224 GOTO(cleanup, rc = -EAGAIN);
1227 if (OBD_FAIL_CHECK(OBD_FAIL_OST_RESTART_IO))
1228 oh->oh_declared_ext = 0;
1230 oh->oh_declared_ext--;
1234 rc = ldiskfs_map_blocks(handle, inode, &map, create);
1235 time = ktime_sub(ktime_get(), time);
1238 struct brw_stats *h = &osd->od_brw_stats;
1241 idx = map.m_flags & LDISKFS_MAP_NEW ?
1242 BRW_ALLOC_TIME : BRW_MAP_TIME;
1243 lprocfs_oh_tally_log2_pcpu(&h->bs_hist[idx],
1246 for (; total < blen && c < map.m_len; c++, total++) {
1248 *(blocks + total) = 0;
1252 if ((map.m_flags & LDISKFS_MAP_UNWRITTEN) &&
1254 /* don't try to read allocated, but
1255 * unwritten blocks, instead fill the
1256 * patches with zeros in osd_do_bio() */
1257 *(blocks + total) = 0;
1260 *(blocks + total) = map.m_pblk + c;
1261 /* unmap any possible underlying
1262 * metadata from the block device
1265 if ((map.m_flags & LDISKFS_MAP_NEW) &&
1267 clean_bdev_aliases(inode->i_sb->s_bdev,
1274 if (rc == 0 && create) {
1275 count += (total - previous_total);
1276 mapped_index = (count + blocks_per_page -
1277 1) / blocks_per_page - 1;
1278 lnb1 = iobuf->dr_lnbs[i - clen];
1279 lnb2 = iobuf->dr_lnbs[mapped_index];
1280 size1 = lnb1->lnb_file_offset -
1281 (lnb1->lnb_file_offset % PAGE_SIZE) +
1282 (total << inode->i_blkbits);
1283 size2 = lnb2->lnb_file_offset + lnb2->lnb_len;
1287 if (size1 > disk_size)
1291 if (rc == 0 && total < blen) {
1293 * decay extent blocks if we could not
1294 * allocate extent once.
1296 osd_decay_extent_bytes(osd,
1297 (total - previous_total) << inode->i_blkbits);
1298 map.m_lblk = fp->index * blocks_per_page + total;
1299 map.m_len = blen - total;
1300 previous_total = total;
1306 * decay extent blocks if we could allocate
1307 * good large extent.
1309 if (total - previous_total >=
1310 osd_extent_bytes(osd) >> inode->i_blkbits)
1311 osd_decay_extent_bytes(osd,
1312 (total - previous_total) << inode->i_blkbits);
1313 /* look for next extent */
1315 blocks += blocks_per_page * clen;
1318 if (rc == 0 && create &&
1319 start_blocks < pages * blocks_per_page) {
1320 rc = osd_ldiskfs_map_write(inode, iobuf, osd, start_blocks,
1321 count, &disk_size, user_size);
1322 LASSERT(start_blocks + count == pages * blocks_per_page);
1327 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
1328 struct niobuf_local *lnb, int npages)
1330 struct osd_thread_info *oti = osd_oti_get(env);
1331 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1332 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1333 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1342 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1343 if (unlikely(rc != 0))
1346 isize = i_size_read(inode);
1347 maxidx = ((isize + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1;
1349 start = ktime_get();
1350 for (i = 0; i < npages; i++) {
1353 * till commit the content of the page is undefined
1354 * we'll set it uptodate once bulk is done. otherwise
1355 * subsequent reads can access non-stable data
1357 ClearPageUptodate(lnb[i].lnb_page);
1359 if (lnb[i].lnb_len == PAGE_SIZE)
1362 if (maxidx >= lnb[i].lnb_page->index) {
1363 osd_iobuf_add_page(iobuf, &lnb[i]);
1366 char *p = kmap(lnb[i].lnb_page);
1368 off = lnb[i].lnb_page_offset;
1371 off = (lnb[i].lnb_page_offset + lnb[i].lnb_len) &
1374 memset(p + off, 0, PAGE_SIZE - off);
1375 kunmap(lnb[i].lnb_page);
1379 timediff = ktime_us_delta(end, start);
1380 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1382 if (iobuf->dr_npages) {
1383 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1385 if (likely(rc == 0)) {
1386 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1387 /* do IO stats for preparation reads */
1388 osd_fini_iobuf(osd, iobuf);
1394 struct osd_fextent {
1398 unsigned int mapped:1;
1401 static int osd_is_mapped(struct dt_object *dt, __u64 offset,
1402 struct osd_fextent *cached_extent)
1404 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1405 sector_t block = offset >> inode->i_blkbits;
1407 struct fiemap_extent_info fei = { 0 };
1408 struct fiemap_extent fe = { 0 };
1411 if (block >= cached_extent->start && block < cached_extent->end)
1412 return cached_extent->mapped;
1414 if (i_size_read(inode) == 0)
1417 /* Beyond EOF, must not be mapped */
1418 if (((i_size_read(inode) - 1) >> inode->i_blkbits) < block)
1421 fei.fi_extents_max = 1;
1422 fei.fi_extents_start = &fe;
1424 rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
1428 start = fe.fe_logical >> inode->i_blkbits;
1429 cached_extent->flags = fe.fe_flags;
1430 if (fei.fi_extents_mapped == 0) {
1431 /* a special case - no extent found at this offset and forward.
1432 * we can consider this as a hole to EOF. it's safe to cache
1433 * as other threads can not allocate/punch blocks this thread
1434 * is working on (LDLM). */
1435 cached_extent->start = block;
1436 cached_extent->end = i_size_read(inode) >> inode->i_blkbits;
1437 cached_extent->mapped = 0;
1441 if (start > block) {
1442 cached_extent->start = block;
1443 cached_extent->end = start;
1444 cached_extent->mapped = 0;
1446 cached_extent->start = start;
1447 cached_extent->end = (fe.fe_logical + fe.fe_length) >>
1449 cached_extent->mapped = 1;
1452 return cached_extent->mapped;
1455 #define MAX_EXTENTS_PER_WRITE 100
1456 static int osd_declare_write_commit(const struct lu_env *env,
1457 struct dt_object *dt,
1458 struct niobuf_local *lnb, int npages,
1459 struct thandle *handle)
1461 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1462 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1463 struct osd_thandle *oh;
1464 int extents = 0, new_meta = 0;
1465 int depth, new_blocks = 0;
1467 int dirty_groups = 0;
1470 long long quota_space = 0;
1471 struct osd_fextent mapped = { 0 }, extent = { 0 };
1472 enum osd_quota_local_flags local_flags = 0;
1473 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
1474 unsigned int extent_bytes;
1477 LASSERT(handle != NULL);
1478 oh = container_of(handle, struct osd_thandle, ot_super);
1479 LASSERT(oh->ot_handle == NULL);
1482 * We track a decaying average extent blocks per filesystem,
1483 * for most of time, it will be 1M, with filesystem becoming
1484 * heavily-fragmented, it will be reduced to 4K at the worst.
1486 extent_bytes = osd_extent_bytes(osd);
1487 LASSERT(extent_bytes >= osd_sb(osd)->s_blocksize);
1489 /* calculate number of extents (probably better to pass nb) */
1490 for (i = 0; i < npages; i++) {
1491 /* ignore quota for the whole request if any page is from
1492 * client cache or written by root.
1494 * XXX we could handle this on per-lnb basis as done by
1497 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
1498 (lnb[i].lnb_flags & OBD_BRW_SYS_RESOURCE) ||
1499 !(lnb[i].lnb_flags & OBD_BRW_SYNC))
1500 declare_flags |= OSD_QID_FORCE;
1503 * Convert unwritten extent might need split extents, could
1506 if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &mapped) &&
1507 !(mapped.flags & FIEMAP_EXTENT_UNWRITTEN)) {
1508 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1512 if (lnb[i].lnb_flags & OBD_BRW_DONE) {
1513 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1517 /* count only unmapped changes */
1519 if (lnb[i].lnb_file_offset != extent.end || extent.end == 0) {
1520 if (extent.end != 0)
1521 extents += (extent.end - extent.start +
1522 extent_bytes - 1) / extent_bytes;
1523 extent.start = lnb[i].lnb_file_offset;
1524 extent.end = lnb[i].lnb_file_offset + lnb[i].lnb_len;
1526 extent.end += lnb[i].lnb_len;
1529 quota_space += PAGE_SIZE;
1532 credits++; /* inode */
1534 * overwrite case, no need to modify tree and
1540 extents += (extent.end - extent.start +
1541 extent_bytes - 1) / extent_bytes;
1543 * with system space usage growing up, mballoc codes won't
1544 * try best to scan block group to align best free extent as
1545 * we can. So extent bytes per extent could be decayed to a
1546 * very small value, this could make us reserve too many credits.
1547 * We could be more optimistic in the credit reservations, even
1548 * in a case where the filesystem is nearly full, it is extremely
1549 * unlikely that the worst case would ever be hit.
1551 if (extents > MAX_EXTENTS_PER_WRITE)
1552 extents = MAX_EXTENTS_PER_WRITE;
1555 * If we add a single extent, then in the worse case, each tree
1556 * level index/leaf need to be changed in case of the tree split.
1557 * If more extents are inserted, they could cause the whole tree
1558 * split more than once, but this is really rare.
1560 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
1562 * many concurrent threads may grow tree by the time
1563 * our transaction starts. so, consider 2 is a min depth.
1565 depth = ext_depth(inode);
1566 depth = min(max(depth, 1) + 1, LDISKFS_MAX_EXTENT_DEPTH);
1568 credits += depth * 2 * extents;
1571 credits += depth * 3 * extents;
1572 new_meta = depth * 2 * extents;
1576 * With N contiguous data blocks, we need at most
1577 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
1578 * 2 dindirect blocks, and 1 tindirect block
1580 new_meta = DIV_ROUND_UP(new_blocks,
1581 LDISKFS_ADDR_PER_BLOCK(inode->i_sb)) + 4;
1582 credits += new_meta;
1584 dirty_groups += (extents + new_meta);
1586 oh->oh_declared_ext = extents;
1588 /* quota space for metadata blocks */
1589 quota_space += new_meta * LDISKFS_BLOCK_SIZE(osd_sb(osd));
1591 /* quota space should be reported in 1K blocks */
1592 quota_space = toqb(quota_space);
1594 /* each new block can go in different group (bitmap + gd) */
1596 /* we can't dirty more bitmap blocks than exist */
1597 if (dirty_groups > LDISKFS_SB(osd_sb(osd))->s_groups_count)
1598 credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
1600 credits += dirty_groups;
1602 /* we can't dirty more gd blocks than exist */
1603 if (dirty_groups > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
1604 credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
1606 credits += dirty_groups;
1609 "%s: inode #%lu extent_bytes %u extents %d credits %d\n",
1610 osd_ino2name(inode), inode->i_ino, extent_bytes, extents,
1614 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1616 /* make sure the over quota flags were not set */
1617 lnb[0].lnb_flags &= ~OBD_BRW_OVER_ALLQUOTA;
1619 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
1620 i_projid_read(inode), quota_space, oh,
1621 osd_dt_obj(dt), &local_flags, declare_flags);
1623 /* we need only to store the overquota flags in the first lnb for
1624 * now, once we support multiple objects BRW, this code needs be
1627 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
1628 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
1629 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
1630 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
1631 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
1632 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
1635 rc = osd_trunc_lock(osd_dt_obj(dt), oh, true);
1640 /* Check if a block is allocated or not */
1641 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
1642 struct niobuf_local *lnb, int npages,
1643 struct thandle *thandle, __u64 user_size)
1645 struct osd_thread_info *oti = osd_oti_get(env);
1646 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1647 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1648 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1649 int rc = 0, i, check_credits = 0;
1653 rc = osd_init_iobuf(osd, iobuf, 1, npages);
1654 if (unlikely(rc != 0))
1657 dquot_initialize(inode);
1659 for (i = 0; i < npages; i++) {
1660 if (lnb[i].lnb_rc == -ENOSPC &&
1661 (lnb[i].lnb_flags & OBD_BRW_MAPPED)) {
1662 /* Allow the write to proceed if overwriting an
1668 if (lnb[i].lnb_rc) { /* ENOSPC, network RPC error, etc. */
1669 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
1671 LASSERT(lnb[i].lnb_page);
1672 generic_error_remove_page(inode->i_mapping,
1677 if (lnb[i].lnb_flags & OBD_BRW_DONE)
1680 if (!(lnb[i].lnb_flags & OBD_BRW_MAPPED))
1683 LASSERT(PageLocked(lnb[i].lnb_page));
1684 LASSERT(!PageWriteback(lnb[i].lnb_page));
1687 * Since write and truncate are serialized by oo_sem, even
1688 * partial-page truncate should not leave dirty pages in the
1691 LASSERT(!PageDirty(lnb[i].lnb_page));
1693 SetPageUptodate(lnb[i].lnb_page);
1695 osd_iobuf_add_page(iobuf, &lnb[i]);
1698 osd_trans_exec_op(env, thandle, OSD_OT_WRITE);
1700 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
1702 } else if (iobuf->dr_npages > 0) {
1703 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd,
1708 /* no pages to write, no transno is needed */
1709 thandle->th_local = 1;
1712 if (rc != 0 && !thandle->th_restart_tran)
1713 osd_fini_iobuf(osd, iobuf);
1715 osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
1717 if (unlikely(rc != 0 && !thandle->th_restart_tran)) {
1718 /* if write fails, we should drop pages from the cache */
1719 for (i = 0; i < npages; i++) {
1720 if (lnb[i].lnb_page == NULL)
1722 if (!PagePrivate2(lnb[i].lnb_page)) {
1723 LASSERT(PageLocked(lnb[i].lnb_page));
1724 generic_error_remove_page(inode->i_mapping,
1733 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
1734 struct niobuf_local *lnb, int npages)
1736 struct osd_thread_info *oti = osd_oti_get(env);
1737 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1738 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1739 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1740 int rc = 0, i, cache_hits = 0, cache_misses = 0;
1747 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1748 if (unlikely(rc != 0))
1751 isize = i_size_read(inode);
1753 start = ktime_get();
1754 for (i = 0; i < npages; i++) {
1756 if (isize <= lnb[i].lnb_file_offset)
1757 /* If there's no more data, abort early.
1758 * lnb->lnb_rc == 0, so it's easy to detect later.
1762 /* instead of looking if we go beyong isize, send complete
1763 * pages all the time
1765 lnb[i].lnb_rc = lnb[i].lnb_len;
1767 /* Bypass disk read if fail_loc is set properly */
1768 if (OBD_FAIL_CHECK_QUIET(OBD_FAIL_OST_FAKE_RW))
1769 SetPageUptodate(lnb[i].lnb_page);
1771 if (PageUptodate(lnb[i].lnb_page)) {
1773 unlock_page(lnb[i].lnb_page);
1776 osd_iobuf_add_page(iobuf, &lnb[i]);
1778 /* no need to unlock in osd_bufs_put(), the sooner page is
1779 * unlocked, the earlier another client can access it.
1780 * notice real unlock_page() can be called few lines
1781 * below after osd_do_bio(). lnb is a per-thread, so it's
1782 * fine to have PG_locked and lnb_locked inconsistent here
1784 lnb[i].lnb_locked = 0;
1787 timediff = ktime_us_delta(end, start);
1788 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1790 if (cache_hits != 0)
1791 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_HIT,
1793 if (cache_misses != 0)
1794 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_MISS,
1796 if (cache_hits + cache_misses != 0)
1797 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
1798 cache_hits + cache_misses);
1800 if (iobuf->dr_npages) {
1801 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1804 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1806 /* IO stats will be done in osd_bufs_put() */
1808 /* early release to let others read data during the bulk */
1809 for (i = 0; i < iobuf->dr_npages; i++) {
1810 LASSERT(PageLocked(iobuf->dr_pages[i]));
1811 if (!PagePrivate2(iobuf->dr_pages[i]))
1812 unlock_page(iobuf->dr_pages[i]);
1820 * XXX: Another layering violation for now.
1822 * We don't want to use ->f_op->read methods, because generic file write
1824 * - serializes on ->i_sem, and
1826 * - does a lot of extra work like balance_dirty_pages(),
1828 * which doesn't work for globally shared files like /last_rcvd.
1830 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
1832 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
1834 memcpy(buffer, (char *)ei->i_data, buflen);
1839 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
1841 struct buffer_head *bh;
1842 unsigned long block;
1848 /* prevent reading after eof */
1849 spin_lock(&inode->i_lock);
1850 if (i_size_read(inode) < *offs + size) {
1851 loff_t diff = i_size_read(inode) - *offs;
1853 spin_unlock(&inode->i_lock);
1856 "size %llu is too short to read @%llu\n",
1857 i_size_read(inode), *offs);
1859 } else if (diff == 0) {
1865 spin_unlock(&inode->i_lock);
1868 blocksize = 1 << inode->i_blkbits;
1871 block = *offs >> inode->i_blkbits;
1872 boffs = *offs & (blocksize - 1);
1873 csize = min(blocksize - boffs, size);
1874 bh = __ldiskfs_bread(NULL, inode, block, 0);
1876 CERROR("%s: can't read %u@%llu on ino %lu: rc = %ld\n",
1877 osd_ino2name(inode), csize, *offs, inode->i_ino,
1883 memcpy(buf, bh->b_data + boffs, csize);
1886 memset(buf, 0, csize);
1896 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1897 struct lu_buf *buf, loff_t *pos)
1899 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1902 /* Read small symlink from inode body as we need to maintain correct
1903 * on-disk symlinks for ldiskfs.
1905 if (S_ISLNK(dt->do_lu.lo_header->loh_attr)) {
1906 loff_t size = i_size_read(inode);
1908 if (buf->lb_len < size)
1911 if (size < sizeof(LDISKFS_I(inode)->i_data))
1912 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, size);
1914 rc = osd_ldiskfs_read(inode, buf->lb_buf, size, pos);
1916 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1922 static inline int osd_extents_enabled(struct super_block *sb,
1923 struct inode *inode)
1925 if (inode != NULL) {
1926 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL)
1928 } else if (ldiskfs_has_feature_extents(sb)) {
1934 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1935 const loff_t size, const loff_t pos,
1938 int credits, bits, bs, i;
1940 bits = sb->s_blocksize_bits;
1943 /* legacy blockmap: 3 levels * 3 (bitmap,gd,itself)
1944 * we do not expect blockmaps on the large files,
1945 * so let's shrink it to 2 levels (4GB files)
1948 /* this is default reservation: 2 levels */
1949 credits = (blocks + 2) * 3;
1951 /* actual offset is unknown, hard to optimize */
1955 /* now check for few specific cases to optimize */
1956 if (pos + size <= LDISKFS_NDIR_BLOCKS * bs) {
1959 /* allocate if not allocated */
1960 if (inode == NULL) {
1961 credits += blocks * 2;
1964 for (i = (pos >> bits); i < (pos >> bits) + blocks; i++) {
1965 LASSERT(i < LDISKFS_NDIR_BLOCKS);
1966 if (LDISKFS_I(inode)->i_data[i] == 0)
1969 } else if (pos + size <= (LDISKFS_NDIR_BLOCKS + 1024) * bs) {
1970 /* single indirect */
1971 credits = blocks * 3;
1972 if (inode == NULL ||
1973 LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK] == 0)
1976 /* The indirect block may be modified. */
1983 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1984 const struct lu_buf *buf, loff_t _pos,
1985 struct thandle *handle)
1987 struct osd_object *obj = osd_dt_obj(dt);
1988 struct inode *inode = obj->oo_inode;
1989 struct super_block *sb = osd_sb(osd_obj2dev(obj));
1990 struct osd_thandle *oh;
1991 int rc = 0, est = 0, credits, blocks, allocated = 0;
1997 LASSERT(buf != NULL);
1998 LASSERT(handle != NULL);
2000 oh = container_of(handle, struct osd_thandle, ot_super);
2001 LASSERT(oh->ot_handle == NULL);
2004 bits = sb->s_blocksize_bits;
2008 /* if this is an append, then we
2009 * should expect cross-block record
2016 /* blocks to modify */
2017 blocks = ((pos + size + bs - 1) >> bits) - (pos >> bits);
2018 LASSERT(blocks > 0);
2020 if (inode != NULL && _pos != -1) {
2021 /* object size in blocks */
2022 est = (i_size_read(inode) + bs - 1) >> bits;
2023 allocated = inode->i_blocks >> (bits - 9);
2024 if (pos + size <= i_size_read(inode) && est <= allocated) {
2025 /* looks like an overwrite, no need to modify tree */
2027 /* no need to modify i_size */
2032 if (osd_extents_enabled(sb, inode)) {
2034 * many concurrent threads may grow tree by the time
2035 * our transaction starts. so, consider 2 is a min depth
2036 * for every level we may need to allocate a new block
2037 * and take some entries from the old one. so, 3 blocks
2038 * to allocate (bitmap, gd, itself) + old block - 4 per
2041 depth = inode != NULL ? ext_depth(inode) : 0;
2042 depth = min(max(depth, 1) + 3, LDISKFS_MAX_EXTENT_DEPTH);
2044 /* if not append, then split may need to modify
2045 * existing blocks moving entries into the new ones
2049 /* blocks to store data: bitmap,gd,itself */
2050 credits += blocks * 3;
2052 credits = osd_calc_bkmap_credits(sb, inode, size, _pos, blocks);
2054 /* if inode is created as part of the transaction,
2055 * then it's counted already by the creation method
2062 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
2064 /* dt_declare_write() is usually called for system objects, such
2065 * as llog or last_rcvd files. We needn't enforce quota on those
2066 * objects, so always set the lqi_space as 0.
2069 rc = osd_declare_inode_qid(env, i_uid_read(inode),
2071 i_projid_read(inode), 0,
2072 oh, obj, NULL, OSD_QID_BLK);
2075 rc = osd_trunc_lock(obj, oh, true);
2080 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2082 /* LU-2634: clear the extent format for fast symlink */
2083 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
2085 /* Copying the NUL byte terminating the link target as well */
2086 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen + 1);
2087 spin_lock(&inode->i_lock);
2088 LDISKFS_I(inode)->i_disksize = buflen;
2089 i_size_write(inode, buflen);
2090 spin_unlock(&inode->i_lock);
2091 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2096 static int osd_ldiskfs_write_record(struct dt_object *dt, void *buf,
2097 int bufsize, int write_NUL, loff_t *offs,
2100 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2101 struct buffer_head *bh = NULL;
2102 loff_t offset = *offs;
2103 loff_t new_size = i_size_read(inode);
2104 unsigned long block;
2105 int blocksize = 1 << inode->i_blkbits;
2106 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2110 int dirty_inode = 0;
2111 bool create, sparse, sync = false;
2115 * long symlink write does not count the NUL terminator in
2116 * bufsize, we write it, and the inode's file size does not
2117 * count the NUL terminator as well.
2119 ((char *)buf)[bufsize] = '\0';
2123 /* only the first flag-set matters */
2124 dirty_inode = !test_and_set_bit(LDISKFS_INODE_JOURNAL_DATA,
2127 /* sparse checking is racy, but sparse is very rare case, leave as is */
2128 sparse = (new_size > 0 && (inode->i_blocks >> (inode->i_blkbits - 9)) <
2129 ((new_size - 1) >> inode->i_blkbits) + 1);
2131 while (bufsize > 0) {
2132 int credits = handle->h_buffer_credits;
2133 unsigned long last_block = (new_size == 0) ? 0 :
2134 (new_size - 1) >> inode->i_blkbits;
2139 block = offset >> inode->i_blkbits;
2140 boffs = offset & (blocksize - 1);
2141 size = min(blocksize - boffs, bufsize);
2142 sync = (block > last_block || new_size == 0 || sparse);
2145 down(&ei->i_append_sem);
2147 bh = __ldiskfs_bread(handle, inode, block, 0);
2149 if (unlikely(IS_ERR_OR_NULL(bh) && !sync))
2151 "%s: adding bh without locking off %llu (block %lu, size %d, offs %llu)\n",
2152 osd_ino2name(inode),
2153 offset, block, bufsize, *offs);
2155 if (IS_ERR_OR_NULL(bh)) {
2156 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2157 int flags = LDISKFS_GET_BLOCKS_CREATE;
2159 /* while the file system is being mounted, avoid
2160 * preallocation otherwise mount can take a long
2161 * time as mballoc cache is cold.
2162 * XXX: this is a workaround until we have a proper
2164 * XXX: works with extent-based files only */
2165 if (!osd->od_cl_seq)
2166 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2167 bh = __ldiskfs_bread(handle, inode, block, flags);
2171 up(&ei->i_append_sem);
2176 if (IS_ERR_OR_NULL(bh)) {
2185 "%s: error reading offset %llu (block %lu, size %d, offs %llu), credits %d/%d: rc = %d\n",
2186 osd_ino2name(inode), offset, block, bufsize,
2187 *offs, credits, handle->h_buffer_credits, err);
2191 err = osd_ldiskfs_journal_get_write_access(handle, inode->i_sb,
2195 CERROR("journal_get_write_access() returned error %d\n",
2199 LASSERTF(boffs + size <= bh->b_size,
2200 "boffs %d size %d bh->b_size %lu\n",
2201 boffs, size, (unsigned long)bh->b_size);
2203 memset(bh->b_data, 0, bh->b_size);
2205 up(&ei->i_append_sem);
2209 memcpy(bh->b_data + boffs, buf, size);
2210 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2214 if (offset + size > new_size)
2215 new_size = offset + size;
2221 up(&ei->i_append_sem);
2228 /* correct in-core and on-disk sizes */
2229 if (new_size > i_size_read(inode)) {
2230 spin_lock(&inode->i_lock);
2231 if (new_size > i_size_read(inode))
2232 i_size_write(inode, new_size);
2233 if (i_size_read(inode) > ei->i_disksize) {
2234 ei->i_disksize = i_size_read(inode);
2237 spin_unlock(&inode->i_lock);
2240 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2247 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2248 const struct lu_buf *buf, loff_t *pos,
2249 struct thandle *handle)
2251 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2252 struct osd_thandle *oh;
2256 LASSERT(dt_object_exists(dt));
2258 LASSERT(handle != NULL);
2259 LASSERT(inode != NULL);
2260 dquot_initialize(inode);
2262 /* XXX: don't check: one declared chunk can be used many times */
2263 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
2265 oh = container_of(handle, struct osd_thandle, ot_super);
2266 LASSERT(oh->ot_handle->h_transaction != NULL);
2267 osd_trans_exec_op(env, handle, OSD_OT_WRITE);
2269 /* Write small symlink to inode body as we need to maintain correct
2270 * on-disk symlinks for ldiskfs.
2271 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
2272 * does not count it in.
2274 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
2275 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
2276 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2278 result = osd_ldiskfs_write_record(dt, buf->lb_buf, buf->lb_len,
2279 is_link, pos, oh->ot_handle);
2281 result = buf->lb_len;
2283 osd_trans_exec_check(env, handle, OSD_OT_WRITE);
2288 static int osd_declare_fallocate(const struct lu_env *env,
2289 struct dt_object *dt, __u64 start, __u64 end,
2290 int mode, struct thandle *th)
2292 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2293 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2294 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2295 long long quota_space = 0;
2296 /* 5 is max tree depth. (inode + 4 index blocks) */
2303 * mode == 0 (which is standard prealloc) and PUNCH is supported
2304 * Rest of mode options is not supported yet.
2306 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2307 RETURN(-EOPNOTSUPP);
2309 /* disable fallocate completely */
2310 if (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks < 0)
2311 RETURN(-EOPNOTSUPP);
2316 if (mode & FALLOC_FL_PUNCH_HOLE) {
2317 rc = osd_declare_inode_qid(env, i_uid_read(inode),
2319 i_projid_read(inode), 0, oh,
2320 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2322 rc = osd_trunc_lock(osd_dt_obj(dt), oh, false);
2326 /* quota space for metadata blocks
2327 * approximate metadata estimate should be good enough.
2329 quota_space += PAGE_SIZE;
2330 quota_space += depth * LDISKFS_BLOCK_SIZE(osd_sb(osd));
2332 /* quota space should be reported in 1K blocks */
2333 quota_space = toqb(quota_space) + toqb(end - start) +
2334 LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
2336 /* We don't need to reserve credits for whole fallocate here.
2337 * We reserve space only for metadata. Fallocate credits are
2338 * extended as required
2340 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2341 i_projid_read(inode), quota_space, oh,
2342 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2346 static int osd_fallocate_preallocate(const struct lu_env *env,
2347 struct dt_object *dt,
2348 __u64 start, __u64 end, int mode,
2351 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2352 handle_t *handle = ldiskfs_journal_current_handle();
2353 unsigned int save_credits = oh->ot_credits;
2354 struct osd_object *obj = osd_dt_obj(dt);
2355 struct inode *inode = obj->oo_inode;
2356 struct ldiskfs_map_blocks map;
2357 unsigned int credits;
2358 ldiskfs_lblk_t blen;
2359 ldiskfs_lblk_t boff;
2360 loff_t new_size = 0;
2367 LASSERT(dt_object_exists(dt));
2368 LASSERT(osd_invariant(obj));
2369 LASSERT(inode != NULL);
2371 CDEBUG(D_INODE, "fallocate: inode #%lu: start %llu end %llu mode %d\n",
2372 inode->i_ino, start, end, mode);
2374 dquot_initialize(inode);
2378 boff = start >> inode->i_blkbits;
2379 blen = (ALIGN(end, 1 << inode->i_blkbits) >> inode->i_blkbits) - boff;
2381 /* Create and mark new extents as either zero or unwritten */
2382 flags = (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks ||
2383 !ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS)) ?
2384 LDISKFS_GET_BLOCKS_CREATE_ZERO :
2385 LDISKFS_GET_BLOCKS_CREATE_UNWRIT_EXT;
2386 #ifdef LDISKFS_GET_BLOCKS_KEEP_SIZE
2387 if (mode & FALLOC_FL_KEEP_SIZE)
2388 flags |= LDISKFS_GET_BLOCKS_KEEP_SIZE;
2392 if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > i_size_read(inode) ||
2393 end > LDISKFS_I(inode)->i_disksize)) {
2395 rc = inode_newsize_ok(inode, new_size);
2400 inode_dio_wait(inode);
2405 /* Don't normalize the request if it can fit in one extent so
2406 * that it doesn't get unnecessarily split into multiple extents.
2408 if (blen <= EXT_UNWRITTEN_MAX_LEN)
2409 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2412 * credits to insert 1 extent into extent tree.
2414 credits = osd_chunk_trans_blocks(inode, blen);
2415 depth = ext_depth(inode);
2417 while (rc >= 0 && blen) {
2421 * Recalculate credits when extent tree depth changes.
2423 if (depth != ext_depth(inode)) {
2424 credits = osd_chunk_trans_blocks(inode, blen);
2425 depth = ext_depth(inode);
2428 /* TODO: quota check */
2429 rc = osd_extend_restart_trans(handle, credits, inode);
2433 rc = ldiskfs_map_blocks(handle, inode, &map, flags);
2436 "inode #%lu: block %u: len %u: ldiskfs_map_blocks returned %d\n",
2437 inode->i_ino, map.m_lblk, map.m_len, rc);
2438 ldiskfs_mark_inode_dirty(handle, inode);
2443 map.m_len = blen = blen - rc;
2444 epos = (loff_t)map.m_lblk << inode->i_blkbits;
2445 inode->i_ctime = current_time(inode);
2449 if (ldiskfs_update_inode_size(inode, epos) & 0x1)
2450 inode->i_mtime = inode->i_ctime;
2451 #ifdef LDISKFS_EOFBLOCKS_FL
2453 if (epos > inode->i_size)
2454 ldiskfs_set_inode_flag(inode,
2455 LDISKFS_INODE_EOFBLOCKS);
2459 ldiskfs_mark_inode_dirty(handle, inode);
2463 /* extand credits if needed for operations such as attribute set */
2465 rc = osd_extend_restart_trans(handle, save_credits, inode);
2467 inode_unlock(inode);
2472 static int osd_fallocate_punch(const struct lu_env *env, struct dt_object *dt,
2473 __u64 start, __u64 end, int mode,
2476 struct osd_object *obj = osd_dt_obj(dt);
2477 struct inode *inode = obj->oo_inode;
2478 struct osd_access_lock *al;
2479 struct osd_thandle *oh;
2480 int rc = 0, found = 0;
2484 LASSERT(dt_object_exists(dt));
2485 LASSERT(osd_invariant(obj));
2486 LASSERT(inode != NULL);
2488 dquot_initialize(inode);
2491 oh = container_of(th, struct osd_thandle, ot_super);
2492 LASSERT(oh->ot_handle->h_transaction != NULL);
2494 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2495 if (obj != al->tl_obj)
2497 LASSERT(al->tl_shared == 0);
2499 /* do actual punch in osd_trans_stop() */
2500 al->tl_start = start;
2503 al->tl_punch = true;
2510 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
2511 __u64 start, __u64 end, int mode, struct thandle *th)
2517 if (mode & FALLOC_FL_PUNCH_HOLE) {
2519 rc = osd_fallocate_punch(env, dt, start, end, mode, th);
2521 /* standard preallocate */
2522 rc = osd_fallocate_preallocate(env, dt, start, end, mode, th);
2527 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
2528 __u64 start, __u64 end, struct thandle *th)
2530 struct osd_thandle *oh;
2531 struct osd_object *obj = osd_dt_obj(dt);
2532 struct inode *inode;
2537 oh = container_of(th, struct osd_thandle, ot_super);
2540 * we don't need to reserve credits for whole truncate
2541 * it's not possible as truncate may need to free too many
2542 * blocks and that won't fit a single transaction. instead
2543 * we reserve credits to change i_size and put inode onto
2544 * orphan list. if needed truncate will extend or restart
2547 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
2548 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
2550 inode = obj->oo_inode;
2553 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2554 i_projid_read(inode), 0, oh, obj,
2557 /* if object holds encrypted content, we need to make sure we truncate
2558 * on an encryption unit boundary, or subsequent reads will get
2562 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2563 start & ~LUSTRE_ENCRYPTION_MASK)
2564 start = (start & LUSTRE_ENCRYPTION_MASK) +
2565 LUSTRE_ENCRYPTION_UNIT_SIZE;
2566 ll_truncate_pagecache(inode, start);
2567 rc = osd_trunc_lock(obj, oh, false);
2573 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
2574 __u64 start, __u64 end, struct thandle *th)
2576 struct osd_object *obj = osd_dt_obj(dt);
2577 struct osd_device *osd = osd_obj2dev(obj);
2578 struct inode *inode = obj->oo_inode;
2579 struct osd_access_lock *al;
2580 struct osd_thandle *oh;
2581 int rc = 0, found = 0;
2585 LASSERT(dt_object_exists(dt));
2586 LASSERT(osd_invariant(obj));
2587 LASSERT(inode != NULL);
2588 dquot_initialize(inode);
2591 oh = container_of(th, struct osd_thandle, ot_super);
2592 LASSERT(oh->ot_handle->h_transaction != NULL);
2594 /* we used to skip truncate to current size to
2595 * optimize truncates on OST. with DoM we can
2596 * get attr_set to set specific size (MDS_REINT)
2597 * and then get truncate RPC which essentially
2598 * would be skipped. this is bad.. so, disable
2599 * this optimization on MDS till the client stop
2600 * to sent MDS_REINT (LU-11033) -bzzz
2602 if (osd->od_is_ost && i_size_read(inode) == start)
2605 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
2607 spin_lock(&inode->i_lock);
2608 if (i_size_read(inode) < start)
2610 i_size_write(inode, start);
2611 spin_unlock(&inode->i_lock);
2613 /* optimize grow case */
2615 osd_execute_truncate(obj);
2620 /* add to orphan list to ensure truncate completion
2621 * if this transaction succeed. ldiskfs_truncate()
2622 * will take the inode out of the list
2624 rc = ldiskfs_orphan_add(oh->ot_handle, inode);
2625 inode_unlock(inode);
2629 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2630 if (obj != al->tl_obj)
2632 LASSERT(al->tl_shared == 0);
2634 /* do actual truncate in osd_trans_stop() */
2635 al->tl_truncate = 1;
2644 static int fiemap_check_ranges(struct inode *inode,
2645 u64 start, u64 len, u64 *new_len)
2654 if (ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS))
2655 maxbytes = inode->i_sb->s_maxbytes;
2657 maxbytes = LDISKFS_SB(inode->i_sb)->s_bitmap_maxbytes;
2659 if (start > maxbytes)
2663 * Shrink request scope to what the fs can actually handle.
2665 if (len > maxbytes || (maxbytes - len) < start)
2666 *new_len = maxbytes - start;
2671 /* So that the fiemap access checks can't overflow on 32 bit machines. */
2672 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
2674 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
2677 struct fiemap_extent_info fieinfo = {0, };
2678 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2683 if (inode->i_op->fiemap == NULL)
2686 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS)
2689 rc = fiemap_check_ranges(inode, fm->fm_start, fm->fm_length, &len);
2693 fieinfo.fi_flags = fm->fm_flags;
2694 fieinfo.fi_extents_max = fm->fm_extent_count;
2695 fieinfo.fi_extents_start = fm->fm_extents;
2697 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
2698 filemap_write_and_wait(inode->i_mapping);
2700 rc = inode->i_op->fiemap(inode, &fieinfo, fm->fm_start, len);
2701 fm->fm_flags = fieinfo.fi_flags;
2702 fm->fm_mapped_extents = fieinfo.fi_extents_mapped;
2707 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
2708 __u64 start, __u64 end, enum lu_ladvise_type advice)
2710 struct osd_object *obj = osd_dt_obj(dt);
2715 case LU_LADVISE_DONTNEED:
2717 invalidate_mapping_pages(obj->oo_inode->i_mapping,
2718 start >> PAGE_SHIFT,
2719 (end - 1) >> PAGE_SHIFT);
2729 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
2730 loff_t offset, int whence)
2732 struct osd_object *obj = osd_dt_obj(dt);
2733 struct osd_device *dev = osd_obj2dev(obj);
2734 struct inode *inode = obj->oo_inode;
2739 LASSERT(dt_object_exists(dt));
2740 LASSERT(osd_invariant(obj));
2742 LASSERT(offset >= 0);
2744 file = alloc_file_pseudo(inode, dev->od_mnt, "/", O_NOATIME,
2747 RETURN(PTR_ERR(file));
2749 file->f_mode |= FMODE_64BITHASH;
2750 result = file->f_op->llseek(file, offset, whence);
2754 * If 'offset' is beyond end of object file then treat it as not error
2755 * but valid case for SEEK_HOLE and return 'offset' as result.
2756 * LOV will decide if it is beyond real end of file or not.
2758 if (whence == SEEK_HOLE && result == -ENXIO)
2761 CDEBUG(D_INFO, "seek %s from %lld: %lld\n", whence == SEEK_HOLE ?
2762 "hole" : "data", offset, result);
2767 * in some cases we may need declare methods for objects being created
2768 * e.g., when we create symlink
2770 const struct dt_body_operations osd_body_ops_new = {
2771 .dbo_declare_write = osd_declare_write,
2774 const struct dt_body_operations osd_body_ops = {
2775 .dbo_read = osd_read,
2776 .dbo_declare_write = osd_declare_write,
2777 .dbo_write = osd_write,
2778 .dbo_bufs_get = osd_bufs_get,
2779 .dbo_bufs_put = osd_bufs_put,
2780 .dbo_write_prep = osd_write_prep,
2781 .dbo_declare_write_commit = osd_declare_write_commit,
2782 .dbo_write_commit = osd_write_commit,
2783 .dbo_read_prep = osd_read_prep,
2784 .dbo_declare_punch = osd_declare_punch,
2785 .dbo_punch = osd_punch,
2786 .dbo_fiemap_get = osd_fiemap_get,
2787 .dbo_ladvise = osd_ladvise,
2788 .dbo_declare_fallocate = osd_declare_fallocate,
2789 .dbo_fallocate = osd_fallocate,
2790 .dbo_lseek = osd_lseek,
2794 * Get a truncate lock
2796 * In order to take multi-transaction truncate out of main transaction we let
2797 * the caller grab a lock on the object passed. the lock can be shared (for
2798 * writes) and exclusive (for truncate). It's not allowed to mix truncate
2799 * and write in the same transaction handle (do not confuse with big ldiskfs
2800 * transaction containing lots of handles).
2801 * The lock must be taken at declaration.
2803 * \param obj object to lock
2805 * \shared shared or exclusive
2807 * \retval 0 lock is granted
2808 * \retval -NOMEM no memory to allocate lock
2810 int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh, bool shared)
2812 struct osd_access_lock *al, *tmp;
2817 list_for_each_entry(tmp, &oh->ot_trunc_locks, tl_list) {
2818 if (tmp->tl_obj != obj)
2820 LASSERT(tmp->tl_shared == shared);
2821 /* found same lock */
2826 if (unlikely(al == NULL))
2829 al->tl_truncate = false;
2831 down_read(&obj->oo_ext_idx_sem);
2833 down_write(&obj->oo_ext_idx_sem);
2834 al->tl_shared = shared;
2835 lu_object_get(&obj->oo_dt.do_lu);
2837 list_add(&al->tl_list, &oh->ot_trunc_locks);
2842 void osd_trunc_unlock_all(const struct lu_env *env, struct list_head *list)
2844 struct osd_access_lock *al, *tmp;
2846 list_for_each_entry_safe(al, tmp, list, tl_list) {
2848 up_read(&al->tl_obj->oo_ext_idx_sem);
2850 up_write(&al->tl_obj->oo_ext_idx_sem);
2851 osd_object_put(env, al->tl_obj);
2852 list_del(&al->tl_list);
2857 /* For a partial-page punch, flush punch range to disk immediately */
2858 static void osd_partial_page_flush_punch(struct osd_device *d,
2859 struct inode *inode, loff_t start,
2862 if (osd_use_page_cache(d)) {
2863 filemap_fdatawrite_range(inode->i_mapping, start, end);
2865 /* Notice we use "wait" version to ensure I/O is complete */
2866 filemap_write_and_wait_range(inode->i_mapping, start,
2868 invalidate_mapping_pages(inode->i_mapping, start >> PAGE_SHIFT,
2874 * For a partial-page truncate, flush the page to disk immediately to
2875 * avoid data corruption during direct disk write. b=17397
2877 static void osd_partial_page_flush(struct osd_device *d, struct inode *inode,
2880 if (!(offset & ~PAGE_MASK))
2883 if (osd_use_page_cache(d)) {
2884 filemap_fdatawrite_range(inode->i_mapping, offset, offset + 1);
2886 /* Notice we use "wait" version to ensure I/O is complete */
2887 filemap_write_and_wait_range(inode->i_mapping, offset,
2889 invalidate_mapping_pages(inode->i_mapping, offset >> PAGE_SHIFT,
2890 offset >> PAGE_SHIFT);
2894 void osd_execute_truncate(struct osd_object *obj)
2896 struct osd_device *d = osd_obj2dev(obj);
2897 struct inode *inode = obj->oo_inode;
2900 /* simulate crash before (in the middle) of delayed truncate */
2901 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FAIL_AT_TRUNCATE)) {
2902 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2903 struct ldiskfs_sb_info *sbi = LDISKFS_SB(inode->i_sb);
2905 mutex_lock(&sbi->s_orphan_lock);
2906 list_del_init(&ei->i_orphan);
2907 mutex_unlock(&sbi->s_orphan_lock);
2911 size = i_size_read(inode);
2913 /* if object holds encrypted content, we need to make sure we truncate
2914 * on an encryption unit boundary, or block content will get corrupted
2916 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2917 size & ~LUSTRE_ENCRYPTION_MASK)
2918 inode->i_size = (size & LUSTRE_ENCRYPTION_MASK) +
2919 LUSTRE_ENCRYPTION_UNIT_SIZE;
2920 ldiskfs_truncate(inode);
2921 inode_unlock(inode);
2922 if (inode->i_size != size) {
2923 spin_lock(&inode->i_lock);
2924 i_size_write(inode, size);
2925 LDISKFS_I(inode)->i_disksize = size;
2926 spin_unlock(&inode->i_lock);
2927 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2929 osd_partial_page_flush(d, inode, size);
2932 static int osd_execute_punch(const struct lu_env *env, struct osd_object *obj,
2933 loff_t start, loff_t end, int mode)
2935 struct osd_device *d = osd_obj2dev(obj);
2936 struct inode *inode = obj->oo_inode;
2940 file = alloc_file_pseudo(inode, d->od_mnt, "/", O_NOATIME,
2943 RETURN(PTR_ERR(file));
2945 file->f_mode |= FMODE_64BITHASH;
2946 rc = file->f_op->fallocate(file, mode, start, end - start);
2950 osd_partial_page_flush_punch(d, inode, start, end - 1);
2954 int osd_process_truncates(const struct lu_env *env, struct list_head *list)
2956 struct osd_access_lock *al;
2959 LASSERT(!journal_current_handle());
2961 list_for_each_entry(al, list, tl_list) {
2964 if (al->tl_truncate)
2965 osd_execute_truncate(al->tl_obj);
2966 else if (al->tl_punch)
2967 rc = osd_execute_punch(env, al->tl_obj, al->tl_start,
2968 al->tl_end, al->tl_mode);