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>
61 static inline bool osd_use_page_cache(struct osd_device *d)
63 /* do not use pagecache if write and read caching are disabled */
64 if (d->od_writethrough_cache + d->od_read_cache == 0)
66 /* use pagecache by default */
70 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
71 int rw, int line, int pages)
75 LASSERTF(iobuf->dr_elapsed_valid == 0,
76 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
77 atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
79 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
81 init_waitqueue_head(&iobuf->dr_wait);
82 atomic_set(&iobuf->dr_numreqs, 0);
87 iobuf->dr_elapsed = ktime_set(0, 0);
88 /* must be counted before, so assert */
90 iobuf->dr_init_at = line;
92 blocks = pages * (PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
93 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
94 LASSERT(iobuf->dr_pg_buf.lb_len >=
95 pages * sizeof(iobuf->dr_pages[0]));
99 /* start with 1MB for 4K blocks */
101 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
104 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
105 (unsigned int)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
107 blocks = pages * (PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
108 iobuf->dr_max_pages = 0;
109 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
110 (unsigned int)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
112 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
113 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
114 if (unlikely(iobuf->dr_blocks == NULL))
117 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
118 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
119 if (unlikely(iobuf->dr_pages == NULL))
122 lu_buf_realloc(&iobuf->dr_lnb_buf,
123 pages * sizeof(iobuf->dr_lnbs[0]));
124 iobuf->dr_lnbs = iobuf->dr_lnb_buf.lb_buf;
125 if (unlikely(iobuf->dr_lnbs == NULL))
128 iobuf->dr_max_pages = pages;
132 #define osd_init_iobuf(dev, iobuf, rw, pages) \
133 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
135 static void osd_iobuf_add_page(struct osd_iobuf *iobuf,
136 struct niobuf_local *lnb)
138 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
139 iobuf->dr_pages[iobuf->dr_npages] = lnb->lnb_page;
140 iobuf->dr_lnbs[iobuf->dr_npages] = lnb;
144 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
146 int rw = iobuf->dr_rw;
148 if (iobuf->dr_elapsed_valid) {
149 iobuf->dr_elapsed_valid = 0;
150 LASSERT(iobuf->dr_dev == d);
151 LASSERT(iobuf->dr_frags > 0);
152 lprocfs_oh_tally(&d->od_brw_stats.hist[BRW_R_DIO_FRAGS+rw],
154 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
155 ktime_to_ms(iobuf->dr_elapsed));
159 #ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
160 static void dio_complete_routine(struct bio *bio)
162 int error = blk_status_to_errno(bio->bi_status);
164 static void dio_complete_routine(struct bio *bio, int error)
167 struct osd_iobuf *iobuf = bio->bi_private;
170 /* CAVEAT EMPTOR: possibly in IRQ context
171 * DO NOT record procfs stats here!!!
174 if (unlikely(iobuf == NULL)) {
175 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");
176 CERROR("bi_next: %p, bi_flags: %lx, " __stringify(bi_opf)
177 ": %x, bi_vcnt: %d, bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, bi_private: %p\n",
178 bio->bi_next, (unsigned long)bio->bi_flags,
179 (unsigned int)bio->bi_opf, bio->bi_vcnt, bio_idx(bio),
180 bio_sectors(bio) << 9, bio->bi_end_io,
181 atomic_read(&bio->__bi_cnt),
186 /* the check is outside of the cycle for performance reason -bzzz */
187 if (!bio_data_dir(bio)) {
188 DECLARE_BVEC_ITER_ALL(iter_all);
190 bio_for_each_segment_all(bvl, bio, iter_all) {
191 if (likely(error == 0))
192 SetPageUptodate(bvl_to_page(bvl));
193 LASSERT(PageLocked(bvl_to_page(bvl)));
195 atomic_dec(&iobuf->dr_dev->od_r_in_flight);
197 atomic_dec(&iobuf->dr_dev->od_w_in_flight);
200 /* any real error is good enough -bzzz */
201 if (error != 0 && iobuf->dr_error == 0)
202 iobuf->dr_error = error;
205 * set dr_elapsed before dr_numreqs turns to 0, otherwise
206 * it's possible that service thread will see dr_numreqs
207 * is zero, but dr_elapsed is not set yet, leading to lost
208 * data in this processing and an assertion in a subsequent
211 if (atomic_read(&iobuf->dr_numreqs) == 1) {
212 ktime_t now = ktime_get();
214 iobuf->dr_elapsed = ktime_sub(now, iobuf->dr_start_time);
215 iobuf->dr_elapsed_valid = 1;
217 if (atomic_dec_and_test(&iobuf->dr_numreqs))
218 wake_up(&iobuf->dr_wait);
220 /* Completed bios used to be chained off iobuf->dr_bios and freed in
221 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
222 * mempool when serious on-disk fragmentation was encountered,
223 * deadlocking the OST. The bios are now released as soon as complete
224 * so the pool cannot be exhausted while IOs are competing. b=10076
229 static void record_start_io(struct osd_iobuf *iobuf, int size)
231 struct osd_device *osd = iobuf->dr_dev;
232 struct obd_histogram *h = osd->od_brw_stats.hist;
235 atomic_inc(&iobuf->dr_numreqs);
237 if (iobuf->dr_rw == 0) {
238 atomic_inc(&osd->od_r_in_flight);
239 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
240 atomic_read(&osd->od_r_in_flight));
241 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
242 } else if (iobuf->dr_rw == 1) {
243 atomic_inc(&osd->od_w_in_flight);
244 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
245 atomic_read(&osd->od_w_in_flight));
246 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
252 static void osd_submit_bio(int rw, struct bio *bio)
254 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
255 #ifdef HAVE_SUBMIT_BIO_2ARGS
256 submit_bio(rw ? WRITE : READ, bio);
263 static int can_be_merged(struct bio *bio, sector_t sector)
268 return bio_end_sector(bio) == sector ? 1 : 0;
271 #if IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)
273 * This function will change the data written, thus it should only be
274 * used when checking data integrity feature
276 static void bio_integrity_fault_inject(struct bio *bio)
278 struct bio_vec *bvec;
279 DECLARE_BVEC_ITER_ALL(iter_all);
283 bio_for_each_segment_all(bvec, bio, iter_all) {
284 struct page *page = bvec->bv_page;
294 static int bio_dif_compare(__u16 *expected_guard_buf, void *bio_prot_buf,
295 unsigned int sectors, int tuple_size)
297 __u16 *expected_guard;
301 expected_guard = expected_guard_buf;
302 for (i = 0; i < sectors; i++) {
303 bio_guard = (__u16 *)bio_prot_buf;
304 if (*bio_guard != *expected_guard) {
306 "unexpected guard tags on sector %d expected guard %u, bio guard %u, sectors %u, tuple size %d\n",
307 i, *expected_guard, *bio_guard, sectors,
312 bio_prot_buf += tuple_size;
317 static int osd_bio_integrity_compare(struct bio *bio, struct block_device *bdev,
318 struct osd_iobuf *iobuf, int index)
320 struct blk_integrity *bi = bdev_get_integrity(bdev);
321 struct bio_integrity_payload *bip = bio->bi_integrity;
322 struct niobuf_local *lnb;
323 unsigned short sector_size = blk_integrity_interval(bi);
324 void *bio_prot_buf = page_address(bip->bip_vec->bv_page) +
325 bip->bip_vec->bv_offset;
327 sector_t sector = bio_start_sector(bio);
328 unsigned int sectors, total;
329 DECLARE_BVEC_ITER_ALL(iter_all);
330 __u16 *expected_guard;
334 bio_for_each_segment_all(bv, bio, iter_all) {
335 lnb = iobuf->dr_lnbs[index];
336 expected_guard = lnb->lnb_guards;
337 sectors = bv->bv_len / sector_size;
338 if (lnb->lnb_guard_rpc) {
339 rc = bio_dif_compare(expected_guard, bio_prot_buf,
340 sectors, bi->tuple_size);
346 bio_prot_buf += sectors * bi->tuple_size;
347 total += sectors * bi->tuple_size;
348 LASSERT(total <= bip_size(bio->bi_integrity));
354 static int osd_bio_integrity_handle(struct osd_device *osd, struct bio *bio,
355 struct osd_iobuf *iobuf,
356 int start_page_idx, bool fault_inject,
357 bool integrity_enabled)
359 struct super_block *sb = osd_sb(osd);
360 integrity_gen_fn *generate_fn = NULL;
361 integrity_vrfy_fn *verify_fn = NULL;
366 if (!integrity_enabled)
369 rc = osd_get_integrity_profile(osd, &generate_fn, &verify_fn);
373 rc = bio_integrity_prep_fn(bio, generate_fn, verify_fn);
377 /* Verify and inject fault only when writing */
378 if (iobuf->dr_rw == 1) {
379 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_CMP))) {
380 rc = osd_bio_integrity_compare(bio, sb->s_bdev, iobuf,
386 if (unlikely(fault_inject))
387 bio_integrity_fault_inject(bio);
393 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
394 # ifdef HAVE_BIO_ENDIO_USES_ONE_ARG
395 static void dio_integrity_complete_routine(struct bio *bio)
397 static void dio_integrity_complete_routine(struct bio *bio, int error)
400 struct osd_bio_private *bio_private = bio->bi_private;
402 bio->bi_private = bio_private->obp_iobuf;
403 osd_dio_complete_routine(bio, error);
405 OBD_FREE_PTR(bio_private);
407 #endif /* HAVE_BIO_INTEGRITY_PREP_FN */
408 #else /* !CONFIG_BLK_DEV_INTEGRITY */
409 #define osd_bio_integrity_handle(osd, bio, iobuf, start_page_idx, \
410 fault_inject, integrity_enabled) 0
411 #endif /* CONFIG_BLK_DEV_INTEGRITY */
413 static int osd_bio_init(struct bio *bio, struct osd_iobuf *iobuf,
414 bool integrity_enabled, int start_page_idx,
415 struct osd_bio_private **pprivate)
421 #ifdef HAVE_BIO_INTEGRITY_PREP_FN
422 if (integrity_enabled) {
423 struct osd_bio_private *bio_private = NULL;
425 OBD_ALLOC_GFP(bio_private, sizeof(*bio_private), GFP_NOIO);
426 if (bio_private == NULL)
428 bio->bi_end_io = dio_integrity_complete_routine;
429 bio->bi_private = bio_private;
430 bio_private->obp_start_page_idx = start_page_idx;
431 bio_private->obp_iobuf = iobuf;
432 *pprivate = bio_private;
436 bio->bi_end_io = dio_complete_routine;
437 bio->bi_private = iobuf;
443 static void osd_mark_page_io_done(struct osd_iobuf *iobuf,
445 sector_t start_blocks,
448 struct niobuf_local *lnb;
449 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
450 pgoff_t pg_start, pg_end;
452 pg_start = start_blocks / blocks_per_page;
453 if (start_blocks % blocks_per_page)
455 if (count >= blocks_per_page)
456 pg_end = (start_blocks + count -
457 blocks_per_page) / blocks_per_page;
459 return; /* nothing to mark */
460 for ( ; pg_start <= pg_end; pg_start++) {
461 lnb = iobuf->dr_lnbs[pg_start];
462 lnb->lnb_flags |= OBD_BRW_DONE;
466 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
467 struct osd_iobuf *iobuf, sector_t start_blocks,
470 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
471 struct page **pages = iobuf->dr_pages;
472 int npages = iobuf->dr_npages;
473 sector_t *blocks = iobuf->dr_blocks;
474 struct super_block *sb = inode->i_sb;
475 int sector_bits = sb->s_blocksize_bits - 9;
476 unsigned int blocksize = sb->s_blocksize;
477 struct block_device *bdev = sb->s_bdev;
478 struct osd_bio_private *bio_private = NULL;
479 struct bio *bio = NULL;
480 int bio_start_page_idx;
482 unsigned int page_offset;
485 int block_idx, block_idx_end;
486 int page_idx, page_idx_start;
490 bool integrity_enabled;
491 struct blk_plug plug;
492 int blocks_left_page;
496 fault_inject = OBD_FAIL_CHECK(OBD_FAIL_OST_INTEGRITY_FAULT);
497 LASSERT(iobuf->dr_npages == npages);
499 integrity_enabled = bdev_integrity_enabled(bdev, iobuf->dr_rw);
501 osd_brw_stats_update(osd, iobuf);
502 iobuf->dr_start_time = ktime_get();
505 count = npages * blocks_per_page;
506 block_idx_end = start_blocks + count;
508 blk_start_plug(&plug);
510 page_idx_start = start_blocks / blocks_per_page;
511 for (page_idx = page_idx_start, block_idx = start_blocks;
512 block_idx < block_idx_end; page_idx++,
513 block_idx += blocks_left_page) {
514 page = pages[page_idx];
515 LASSERT(page_idx < iobuf->dr_npages);
517 i = block_idx % blocks_per_page;
518 blocks_left_page = blocks_per_page - i;
519 for (page_offset = i * blocksize; i < blocks_left_page;
520 i += nblocks, page_offset += blocksize * nblocks) {
523 if (blocks[block_idx + i] == 0) { /* hole */
524 LASSERTF(iobuf->dr_rw == 0,
525 "page_idx %u, block_idx %u, i %u,"
526 "start_blocks: %llu, count: %llu, npages: %d\n",
527 page_idx, block_idx, i,
528 (unsigned long long)start_blocks,
529 (unsigned long long)count, npages);
530 memset(kmap(page) + page_offset, 0, blocksize);
535 sector = (sector_t)blocks[block_idx + i] << sector_bits;
537 /* Additional contiguous file blocks? */
538 while (i + nblocks < blocks_left_page &&
539 (sector + (nblocks << sector_bits)) ==
540 ((sector_t)blocks[block_idx + i + nblocks] <<
544 if (bio && can_be_merged(bio, sector) &&
545 bio_add_page(bio, page, blocksize * nblocks,
547 continue; /* added this frag OK */
550 struct request_queue *q = bio_get_queue(bio);
551 unsigned int bi_size = bio_sectors(bio) << 9;
553 /* Dang! I have to fragment this I/O */
555 "bio++ sz %d vcnt %d(%d) sectors %d(%d) psg %d(%d)\n",
556 bi_size, bio->bi_vcnt, bio->bi_max_vecs,
558 queue_max_sectors(q),
559 osd_bio_nr_segs(bio),
560 queue_max_segments(q));
561 rc = osd_bio_integrity_handle(osd, bio,
562 iobuf, bio_start_page_idx,
563 fault_inject, integrity_enabled);
569 record_start_io(iobuf, bi_size);
570 osd_submit_bio(iobuf->dr_rw, bio);
573 bio_start_page_idx = page_idx;
574 /* allocate new bio */
575 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
576 (block_idx_end - block_idx +
577 blocks_left_page - 1)));
579 CERROR("Can't allocate bio %u pages\n",
580 block_idx_end - block_idx +
581 blocks_left_page - 1);
586 bio_set_dev(bio, bdev);
587 bio_set_sector(bio, sector);
588 bio->bi_opf = iobuf->dr_rw ? WRITE : READ;
589 rc = osd_bio_init(bio, iobuf, integrity_enabled,
590 bio_start_page_idx, &bio_private);
596 rc = bio_add_page(bio, page,
597 blocksize * nblocks, page_offset);
603 rc = osd_bio_integrity_handle(osd, bio, iobuf,
612 record_start_io(iobuf, bio_sectors(bio) << 9);
613 osd_submit_bio(iobuf->dr_rw, bio);
618 blk_finish_plug(&plug);
620 /* in order to achieve better IO throughput, we don't wait for writes
621 * completion here. instead we proceed with transaction commit in
622 * parallel and wait for IO completion once transaction is stopped
623 * see osd_trans_stop() for more details -bzzz
625 if (iobuf->dr_rw == 0 || fault_inject) {
626 wait_event(iobuf->dr_wait,
627 atomic_read(&iobuf->dr_numreqs) == 0);
628 osd_fini_iobuf(osd, iobuf);
632 rc = iobuf->dr_error;
635 OBD_FREE_PTR(bio_private);
639 if (rc == 0 && iobuf->dr_rw)
640 osd_mark_page_io_done(iobuf, inode,
641 start_blocks, count);
646 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
647 struct niobuf_local *lnb, int maxlnb)
655 int poff = offset & (PAGE_SIZE - 1);
656 int plen = PAGE_SIZE - poff;
658 if (*nrpages >= maxlnb) {
665 lnb->lnb_file_offset = offset;
666 lnb->lnb_page_offset = poff;
668 /* lnb->lnb_flags = rnb->rnb_flags; */
670 lnb->lnb_page = NULL;
672 lnb->lnb_guard_rpc = 0;
673 lnb->lnb_guard_disk = 0;
676 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
687 static struct page *osd_get_page(const struct lu_env *env, struct dt_object *dt,
688 loff_t offset, gfp_t gfp_mask, bool cache)
690 struct osd_thread_info *oti = osd_oti_get(env);
691 struct inode *inode = osd_dt_obj(dt)->oo_inode;
692 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
699 page = find_or_create_page(inode->i_mapping,
700 offset >> PAGE_SHIFT, gfp_mask);
703 LASSERT(!PagePrivate2(page));
704 wait_on_page_writeback(page);
706 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
712 if (inode->i_mapping->nrpages) {
713 /* consult with pagecache, but do not create new pages */
714 /* this is normally used once */
715 page = find_lock_page(inode->i_mapping, offset >> PAGE_SHIFT);
717 wait_on_page_writeback(page);
722 LASSERT(oti->oti_dio_pages);
723 cur = oti->oti_dio_pages_used;
724 page = oti->oti_dio_pages[cur];
726 if (unlikely(!page)) {
727 LASSERT(cur < PTLRPC_MAX_BRW_PAGES);
728 page = alloc_page(gfp_mask);
731 oti->oti_dio_pages[cur] = page;
732 SetPagePrivate2(page);
736 ClearPageUptodate(page);
737 page->index = offset >> PAGE_SHIFT;
738 oti->oti_dio_pages_used++;
744 * there are following "locks":
755 * - lock pages, unlock
757 * - lock partial page
763 * Unlock and release pages loaded by osd_bufs_get()
765 * Unlock \a npages pages from \a lnb and drop the refcount on them.
767 * \param env thread execution environment
768 * \param dt dt object undergoing IO (OSD object + methods)
769 * \param lnb array of pages undergoing IO
770 * \param npages number of pages in \a lnb
774 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
775 struct niobuf_local *lnb, int npages)
777 struct osd_thread_info *oti = osd_oti_get(env);
781 ll_pagevec_init(&pvec, 0);
783 for (i = 0; i < npages; i++) {
784 struct page *page = lnb[i].lnb_page;
789 /* if the page isn't cached, then reset uptodate
792 if (PagePrivate2(page)) {
793 oti->oti_dio_pages_used--;
795 if (lnb[i].lnb_locked)
797 if (pagevec_add(&pvec, page) == 0)
798 pagevec_release(&pvec);
801 lnb[i].lnb_page = NULL;
804 LASSERTF(oti->oti_dio_pages_used == 0, "%d\n", oti->oti_dio_pages_used);
806 /* Release any partial pagevec */
807 pagevec_release(&pvec);
813 * Load and lock pages undergoing IO
815 * Pages as described in the \a lnb array are fetched (from disk or cache)
816 * and locked for IO by the caller.
818 * DLM locking protects us from write and truncate competing for same region,
819 * but partial-page truncate can leave dirty pages in the cache for ldiskfs.
820 * It's possible the writeout on a such a page is in progress when we access
821 * it. It's also possible that during this writeout we put new (partial) data
822 * into the page, but won't be able to proceed in filter_commitrw_write().
823 * Therefore, just wait for writeout completion as it should be rare enough.
825 * \param env thread execution environment
826 * \param dt dt object undergoing IO (OSD object + methods)
827 * \param pos byte offset of IO start
828 * \param len number of bytes of IO
829 * \param lnb array of extents undergoing IO
830 * \param rw read or write operation, and other flags
831 * \param capa capabilities
833 * \retval pages (zero or more) loaded successfully
834 * \retval -ENOMEM on memory/page allocation error
836 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
837 loff_t pos, ssize_t len, struct niobuf_local *lnb,
838 int maxlnb, enum dt_bufs_type rw)
840 struct osd_thread_info *oti = osd_oti_get(env);
841 struct osd_object *obj = osd_dt_obj(dt);
842 struct osd_device *osd = osd_obj2dev(obj);
843 int npages, i, iosize, rc = 0;
848 LASSERT(obj->oo_inode);
850 rc = osd_map_remote_to_local(pos, len, &npages, lnb, maxlnb);
854 write = rw & DT_BUFS_TYPE_WRITE;
856 fsize = lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len;
857 iosize = fsize - lnb[0].lnb_file_offset;
858 fsize = max(fsize, i_size_read(obj->oo_inode));
860 cache = rw & DT_BUFS_TYPE_READAHEAD;
864 cache = osd_use_page_cache(osd);
867 if (!osd->od_writethrough_cache) {
871 if (iosize > osd->od_writethrough_max_iosize) {
876 if (!osd->od_read_cache) {
880 if (iosize > osd->od_readcache_max_iosize) {
885 /* don't use cache on large files */
886 if (osd->od_readcache_max_filesize &&
887 fsize > osd->od_readcache_max_filesize)
893 if (!cache && unlikely(!oti->oti_dio_pages)) {
894 OBD_ALLOC_PTR_ARRAY_LARGE(oti->oti_dio_pages,
895 PTLRPC_MAX_BRW_PAGES);
896 if (!oti->oti_dio_pages)
900 /* this could also try less hard for DT_BUFS_TYPE_READAHEAD pages */
901 gfp_mask = rw & DT_BUFS_TYPE_LOCAL ? (GFP_NOFS | __GFP_HIGHMEM) :
903 for (i = 0; i < npages; i++, lnb++) {
904 lnb->lnb_page = osd_get_page(env, dt, lnb->lnb_file_offset,
906 if (lnb->lnb_page == NULL)
907 GOTO(cleanup, rc = -ENOMEM);
913 /* XXX: this version doesn't invalidate cached pages, but use them */
914 if (!cache && write && obj->oo_inode->i_mapping->nrpages) {
915 /* do not allow data aliasing, invalidate pagecache */
916 /* XXX: can be quite expensive in mixed case */
917 invalidate_mapping_pages(obj->oo_inode->i_mapping,
918 lnb[0].lnb_file_offset >> PAGE_SHIFT,
919 lnb[npages - 1].lnb_file_offset >> PAGE_SHIFT);
927 osd_bufs_put(env, dt, lnb - i, i);
930 /* Borrow @ext4_chunk_trans_blocks */
931 static int osd_chunk_trans_blocks(struct inode *inode, int nrblocks)
933 ldiskfs_group_t groups;
939 depth = ext_depth(inode);
940 idxblocks = depth * 2;
943 * Now let's see how many group bitmaps and group descriptors need
946 groups = idxblocks + 1;
948 if (groups > LDISKFS_SB(inode->i_sb)->s_groups_count)
949 groups = LDISKFS_SB(inode->i_sb)->s_groups_count;
950 if (gdpblocks > LDISKFS_SB(inode->i_sb)->s_gdb_count)
951 gdpblocks = LDISKFS_SB(inode->i_sb)->s_gdb_count;
953 /* bitmaps and block group descriptor blocks */
954 ret = idxblocks + groups + gdpblocks;
956 /* Blocks for super block, inode, quota and xattr blocks */
957 ret += LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
962 #ifdef HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS
963 static int osd_extend_restart_trans(handle_t *handle, int needed,
968 rc = ldiskfs_journal_ensure_credits(handle, needed,
969 ldiskfs_trans_default_revoke_credits(inode->i_sb));
970 /* this means journal has been restarted */
977 static int osd_extend_restart_trans(handle_t *handle, int needed,
982 if (ldiskfs_handle_has_enough_credits(handle, needed))
984 rc = ldiskfs_journal_extend(handle,
985 needed - handle->h_buffer_credits);
989 return ldiskfs_journal_restart(handle, needed);
991 #endif /* HAVE_LDISKFS_JOURNAL_ENSURE_CREDITS */
993 static int osd_ldiskfs_map_write(struct inode *inode, struct osd_iobuf *iobuf,
994 struct osd_device *osd, sector_t start_blocks,
995 sector_t count, loff_t *disk_size,
998 /* if file has grown, take user_size into account */
999 if (user_size && *disk_size > user_size)
1000 *disk_size = user_size;
1002 spin_lock(&inode->i_lock);
1003 if (*disk_size > i_size_read(inode)) {
1004 i_size_write(inode, *disk_size);
1005 LDISKFS_I(inode)->i_disksize = *disk_size;
1006 spin_unlock(&inode->i_lock);
1007 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
1009 spin_unlock(&inode->i_lock);
1013 * We don't do stats here as in read path because
1014 * write is async: we'll do this in osd_put_bufs()
1016 return osd_do_bio(osd, inode, iobuf, start_blocks, count);
1019 static unsigned int osd_extent_bytes(const struct osd_device *o)
1021 unsigned int *extent_bytes_ptr =
1022 raw_cpu_ptr(o->od_extent_bytes_percpu);
1024 if (likely(*extent_bytes_ptr))
1025 return *extent_bytes_ptr;
1027 /* initialize on first access or CPU hotplug */
1028 if (!ldiskfs_has_feature_extents(osd_sb(o)))
1029 *extent_bytes_ptr = 1 << osd_sb(o)->s_blocksize_bits;
1031 *extent_bytes_ptr = OSD_DEFAULT_EXTENT_BYTES;
1033 return *extent_bytes_ptr;
1036 #define EXTENT_BYTES_DECAY 64
1037 static void osd_decay_extent_bytes(struct osd_device *osd,
1038 unsigned int new_bytes)
1040 unsigned int old_bytes;
1042 if (!ldiskfs_has_feature_extents(osd_sb(osd)))
1045 old_bytes = osd_extent_bytes(osd);
1046 *raw_cpu_ptr(osd->od_extent_bytes_percpu) =
1047 (old_bytes * (EXTENT_BYTES_DECAY - 1) +
1048 min(new_bytes, OSD_DEFAULT_EXTENT_BYTES) +
1049 EXTENT_BYTES_DECAY - 1) / EXTENT_BYTES_DECAY;
1052 static int osd_ldiskfs_map_inode_pages(struct inode *inode,
1053 struct osd_iobuf *iobuf,
1054 struct osd_device *osd,
1055 int create, __u64 user_size,
1057 struct thandle *thandle)
1059 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1060 int rc = 0, i = 0, mapped_index = 0;
1061 struct page *fp = NULL;
1063 pgoff_t max_page_index;
1064 handle_t *handle = NULL;
1065 sector_t start_blocks = 0, count = 0;
1066 loff_t disk_size = 0;
1067 struct page **page = iobuf->dr_pages;
1068 int pages = iobuf->dr_npages;
1069 sector_t *blocks = iobuf->dr_blocks;
1070 struct niobuf_local *lnb1, *lnb2;
1071 loff_t size1, size2;
1073 max_page_index = inode->i_sb->s_maxbytes >> PAGE_SHIFT;
1075 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1076 inode->i_ino, pages, (*page)->index);
1079 create = LDISKFS_GET_BLOCKS_CREATE;
1080 handle = ldiskfs_journal_current_handle();
1081 LASSERT(handle != NULL);
1082 rc = osd_attach_jinode(inode);
1085 disk_size = i_size_read(inode);
1086 /* if disk_size is already bigger than specified user_size,
1089 if (disk_size > user_size)
1092 /* pages are sorted already. so, we just have to find
1093 * contig. space and process them properly
1096 long blen, total = 0, previous_total = 0;
1097 struct ldiskfs_map_blocks map = { 0 };
1099 if (fp == NULL) { /* start new extent */
1104 } else if (fp->index + clen == (*page)->index) {
1105 /* continue the extent */
1111 if (fp->index + clen >= max_page_index)
1112 GOTO(cleanup, rc = -EFBIG);
1113 /* process found extent */
1114 map.m_lblk = fp->index * blocks_per_page;
1115 map.m_len = blen = clen * blocks_per_page;
1118 * We might restart transaction for block allocations,
1119 * in order to make sure data ordered mode, issue IO, disk
1120 * size update and block allocations need be within same
1121 * transaction to make sure consistency.
1123 if (handle && check_credits) {
1124 struct osd_thandle *oh;
1126 LASSERT(thandle != NULL);
1127 oh = container_of(thandle, struct osd_thandle,
1130 * only issue IO if restart transaction needed,
1131 * as update disk size need hold inode lock, we
1132 * want to avoid that as much as possible.
1134 if (oh->oh_declared_ext <= 0) {
1135 rc = osd_ldiskfs_map_write(inode,
1136 iobuf, osd, start_blocks,
1137 count, &disk_size, user_size);
1140 thandle->th_restart_tran = 1;
1141 GOTO(cleanup, rc = -EAGAIN);
1144 if (OBD_FAIL_CHECK(OBD_FAIL_OST_RESTART_IO))
1145 oh->oh_declared_ext = 0;
1147 oh->oh_declared_ext--;
1149 rc = ldiskfs_map_blocks(handle, inode, &map, create);
1153 for (; total < blen && c < map.m_len; c++, total++) {
1155 *(blocks + total) = 0;
1159 if ((map.m_flags & LDISKFS_MAP_UNWRITTEN) &&
1161 /* don't try to read allocated, but
1162 * unwritten blocks, instead fill the
1163 * patches with zeros in osd_do_bio() */
1164 *(blocks + total) = 0;
1167 *(blocks + total) = map.m_pblk + c;
1168 /* unmap any possible underlying
1169 * metadata from the block device
1172 if ((map.m_flags & LDISKFS_MAP_NEW) &&
1174 clean_bdev_aliases(inode->i_sb->s_bdev,
1180 if (rc == 0 && create) {
1181 count += (total - previous_total);
1182 mapped_index = (count + blocks_per_page -
1183 1) / blocks_per_page - 1;
1184 lnb1 = iobuf->dr_lnbs[i - clen];
1185 lnb2 = iobuf->dr_lnbs[mapped_index];
1186 size1 = lnb1->lnb_file_offset -
1187 (lnb1->lnb_file_offset % PAGE_SIZE) +
1188 (total << inode->i_blkbits);
1189 size2 = lnb2->lnb_file_offset + lnb2->lnb_len;
1193 if (size1 > disk_size)
1197 if (rc == 0 && total < blen) {
1199 * decay extent blocks if we could not
1200 * allocate extent once.
1202 osd_decay_extent_bytes(osd,
1203 (total - previous_total) << inode->i_blkbits);
1204 map.m_lblk = fp->index * blocks_per_page + total;
1205 map.m_len = blen - total;
1206 previous_total = total;
1212 * decay extent blocks if we could allocate
1213 * good large(1M) extent.
1215 if (previous_total == 0 &&
1216 total >= OSD_DEFAULT_EXTENT_BYTES >> inode->i_blkbits)
1217 osd_decay_extent_bytes(osd,
1218 total << inode->i_blkbits);
1219 /* look for next extent */
1221 blocks += blocks_per_page * clen;
1224 if (rc == 0 && create &&
1225 start_blocks < pages * blocks_per_page) {
1226 rc = osd_ldiskfs_map_write(inode, iobuf, osd, start_blocks,
1227 count, &disk_size, user_size);
1228 LASSERT(start_blocks + count == pages * blocks_per_page);
1233 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
1234 struct niobuf_local *lnb, int npages)
1236 struct osd_thread_info *oti = osd_oti_get(env);
1237 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1238 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1239 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1248 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1249 if (unlikely(rc != 0))
1252 isize = i_size_read(inode);
1253 maxidx = ((isize + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1;
1255 start = ktime_get();
1256 for (i = 0; i < npages; i++) {
1259 * till commit the content of the page is undefined
1260 * we'll set it uptodate once bulk is done. otherwise
1261 * subsequent reads can access non-stable data
1263 ClearPageUptodate(lnb[i].lnb_page);
1265 if (lnb[i].lnb_len == PAGE_SIZE)
1268 if (maxidx >= lnb[i].lnb_page->index) {
1269 osd_iobuf_add_page(iobuf, &lnb[i]);
1272 char *p = kmap(lnb[i].lnb_page);
1274 off = lnb[i].lnb_page_offset;
1277 off = (lnb[i].lnb_page_offset + lnb[i].lnb_len) &
1280 memset(p + off, 0, PAGE_SIZE - off);
1281 kunmap(lnb[i].lnb_page);
1285 timediff = ktime_us_delta(end, start);
1286 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1288 if (iobuf->dr_npages) {
1289 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1291 if (likely(rc == 0)) {
1292 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1293 /* do IO stats for preparation reads */
1294 osd_fini_iobuf(osd, iobuf);
1300 struct osd_fextent {
1303 unsigned int mapped:1;
1306 static int osd_is_mapped(struct dt_object *dt, __u64 offset,
1307 struct osd_fextent *cached_extent)
1309 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1310 sector_t block = offset >> inode->i_blkbits;
1312 struct fiemap_extent_info fei = { 0 };
1313 struct fiemap_extent fe = { 0 };
1314 mm_segment_t saved_fs;
1317 if (block >= cached_extent->start && block < cached_extent->end)
1318 return cached_extent->mapped;
1320 if (i_size_read(inode) == 0)
1323 /* Beyond EOF, must not be mapped */
1324 if (((i_size_read(inode) - 1) >> inode->i_blkbits) < block)
1327 fei.fi_extents_max = 1;
1328 fei.fi_extents_start = &fe;
1330 saved_fs = get_fs();
1332 rc = inode->i_op->fiemap(inode, &fei, offset, FIEMAP_MAX_OFFSET-offset);
1337 start = fe.fe_logical >> inode->i_blkbits;
1338 if (fei.fi_extents_mapped == 0) {
1339 /* a special case - no extent found at this offset and forward.
1340 * we can consider this as a hole to EOF. it's safe to cache
1341 * as other threads can not allocate/punch blocks this thread
1342 * is working on (LDLM). */
1343 cached_extent->start = block;
1344 cached_extent->end = i_size_read(inode) >> inode->i_blkbits;
1345 cached_extent->mapped = 0;
1349 if (start > block) {
1350 cached_extent->start = block;
1351 cached_extent->end = start;
1352 cached_extent->mapped = 0;
1354 cached_extent->start = start;
1355 cached_extent->end = (fe.fe_logical + fe.fe_length) >>
1357 cached_extent->mapped = 1;
1360 return cached_extent->mapped;
1363 static int osd_declare_write_commit(const struct lu_env *env,
1364 struct dt_object *dt,
1365 struct niobuf_local *lnb, int npages,
1366 struct thandle *handle)
1368 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1369 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1370 struct osd_thandle *oh;
1377 long long quota_space = 0;
1378 struct osd_fextent mapped = { 0 }, extent = { 0 };
1379 enum osd_quota_local_flags local_flags = 0;
1380 enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
1381 unsigned int extent_bytes;
1384 LASSERT(handle != NULL);
1385 oh = container_of(handle, struct osd_thandle, ot_super);
1386 LASSERT(oh->ot_handle == NULL);
1389 * We track a decaying average extent blocks per filesystem,
1390 * for most of time, it will be 1M, with filesystem becoming
1391 * heavily-fragmented, it will be reduced to 4K at the worst.
1393 extent_bytes = osd_extent_bytes(osd);
1394 LASSERT(extent_bytes >= (1 << osd_sb(osd)->s_blocksize));
1396 /* calculate number of extents (probably better to pass nb) */
1397 for (i = 0; i < npages; i++) {
1398 /* ignore quota for the whole request if any page is from
1399 * client cache or written by root.
1401 * XXX once we drop the 1.8 client support, the checking
1402 * for whether page is from cache can be simplified as:
1403 * !(lnb[i].flags & OBD_BRW_SYNC)
1405 * XXX we could handle this on per-lnb basis as done by
1408 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
1409 (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
1411 declare_flags |= OSD_QID_FORCE;
1413 if (osd_is_mapped(dt, lnb[i].lnb_file_offset, &mapped)) {
1414 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1418 if (lnb[i].lnb_flags & OBD_BRW_DONE) {
1419 lnb[i].lnb_flags |= OBD_BRW_MAPPED;
1423 /* count only unmapped changes */
1425 if (lnb[i].lnb_file_offset != extent.end || extent.end == 0) {
1426 if (extent.end != 0)
1427 extents += (extent.end - extent.start +
1428 extent_bytes - 1) / extent_bytes;
1429 extent.start = lnb[i].lnb_file_offset;
1430 extent.end = lnb[i].lnb_file_offset + lnb[i].lnb_len;
1432 extent.end += lnb[i].lnb_len;
1435 quota_space += PAGE_SIZE;
1438 credits++; /* inode */
1440 * overwrite case, no need to modify tree and
1446 extents += (extent.end - extent.start +
1447 extent_bytes - 1) / extent_bytes;
1449 * each extent can go into new leaf causing a split
1450 * 5 is max tree depth: inode + 4 index blocks
1451 * with blockmaps, depth is 3 at most
1453 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
1455 * many concurrent threads may grow tree by the time
1456 * our transaction starts. so, consider 2 is a min depth
1458 depth = ext_depth(inode);
1459 depth = max(depth, 1) + 1;
1461 credits += depth * 2 * extents;
1465 credits += depth * extents;
1468 oh->oh_declared_ext = extents;
1470 /* quota space for metadata blocks */
1471 quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
1473 /* quota space should be reported in 1K blocks */
1474 quota_space = toqb(quota_space);
1476 /* each new block can go in different group (bitmap + gd) */
1478 /* we can't dirty more bitmap blocks than exist */
1479 if (extents > LDISKFS_SB(osd_sb(osd))->s_groups_count)
1480 credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
1484 /* we can't dirty more gd blocks than exist */
1485 if (extents > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
1486 credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
1491 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1493 /* make sure the over quota flags were not set */
1494 lnb[0].lnb_flags &= ~OBD_BRW_OVER_ALLQUOTA;
1496 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
1497 i_projid_read(inode), quota_space, oh,
1498 osd_dt_obj(dt), &local_flags, declare_flags);
1500 /* we need only to store the overquota flags in the first lnb for
1501 * now, once we support multiple objects BRW, this code needs be
1504 if (local_flags & QUOTA_FL_OVER_USRQUOTA)
1505 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
1506 if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
1507 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
1508 if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
1509 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
1512 rc = osd_trunc_lock(osd_dt_obj(dt), oh, true);
1517 /* Check if a block is allocated or not */
1518 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
1519 struct niobuf_local *lnb, int npages,
1520 struct thandle *thandle, __u64 user_size)
1522 struct osd_thread_info *oti = osd_oti_get(env);
1523 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1524 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1525 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1526 int rc = 0, i, check_credits = 0;
1530 rc = osd_init_iobuf(osd, iobuf, 1, npages);
1531 if (unlikely(rc != 0))
1534 dquot_initialize(inode);
1536 for (i = 0; i < npages; i++) {
1537 if (lnb[i].lnb_rc == -ENOSPC &&
1538 (lnb[i].lnb_flags & OBD_BRW_MAPPED)) {
1539 /* Allow the write to proceed if overwriting an
1545 if (lnb[i].lnb_rc) { /* ENOSPC, network RPC error, etc. */
1546 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
1548 LASSERT(lnb[i].lnb_page);
1549 generic_error_remove_page(inode->i_mapping,
1554 if (lnb[i].lnb_flags & OBD_BRW_DONE)
1557 if (!(lnb[i].lnb_flags & OBD_BRW_MAPPED))
1560 LASSERT(PageLocked(lnb[i].lnb_page));
1561 LASSERT(!PageWriteback(lnb[i].lnb_page));
1564 * Since write and truncate are serialized by oo_sem, even
1565 * partial-page truncate should not leave dirty pages in the
1568 LASSERT(!PageDirty(lnb[i].lnb_page));
1570 SetPageUptodate(lnb[i].lnb_page);
1572 osd_iobuf_add_page(iobuf, &lnb[i]);
1575 osd_trans_exec_op(env, thandle, OSD_OT_WRITE);
1577 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
1579 } else if (iobuf->dr_npages > 0) {
1580 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd,
1585 /* no pages to write, no transno is needed */
1586 thandle->th_local = 1;
1589 if (rc != 0 && !thandle->th_restart_tran)
1590 osd_fini_iobuf(osd, iobuf);
1592 osd_trans_exec_check(env, thandle, OSD_OT_WRITE);
1594 if (unlikely(rc != 0 && !thandle->th_restart_tran)) {
1595 /* if write fails, we should drop pages from the cache */
1596 for (i = 0; i < npages; i++) {
1597 if (lnb[i].lnb_page == NULL)
1599 if (!PagePrivate2(lnb[i].lnb_page)) {
1600 LASSERT(PageLocked(lnb[i].lnb_page));
1601 generic_error_remove_page(inode->i_mapping,
1610 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
1611 struct niobuf_local *lnb, int npages)
1613 struct osd_thread_info *oti = osd_oti_get(env);
1614 struct osd_iobuf *iobuf = &oti->oti_iobuf;
1615 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1616 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1617 int rc = 0, i, cache_hits = 0, cache_misses = 0;
1624 rc = osd_init_iobuf(osd, iobuf, 0, npages);
1625 if (unlikely(rc != 0))
1628 isize = i_size_read(inode);
1630 start = ktime_get();
1631 for (i = 0; i < npages; i++) {
1633 if (isize <= lnb[i].lnb_file_offset)
1634 /* If there's no more data, abort early.
1635 * lnb->lnb_rc == 0, so it's easy to detect later.
1639 /* instead of looking if we go beyong isize, send complete
1640 * pages all the time
1642 lnb[i].lnb_rc = lnb[i].lnb_len;
1644 /* Bypass disk read if fail_loc is set properly */
1645 if (OBD_FAIL_CHECK(OBD_FAIL_OST_FAKE_RW))
1646 SetPageUptodate(lnb[i].lnb_page);
1648 if (PageUptodate(lnb[i].lnb_page)) {
1650 unlock_page(lnb[i].lnb_page);
1653 osd_iobuf_add_page(iobuf, &lnb[i]);
1655 /* no need to unlock in osd_bufs_put(), the sooner page is
1656 * unlocked, the earlier another client can access it.
1657 * notice real unlock_page() can be called few lines
1658 * below after osd_do_bio(). lnb is a per-thread, so it's
1659 * fine to have PG_locked and lnb_locked inconsistent here
1661 lnb[i].lnb_locked = 0;
1664 timediff = ktime_us_delta(end, start);
1665 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
1667 if (cache_hits != 0)
1668 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_HIT,
1670 if (cache_misses != 0)
1671 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_MISS,
1673 if (cache_hits + cache_misses != 0)
1674 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
1675 cache_hits + cache_misses);
1677 if (iobuf->dr_npages) {
1678 rc = osd_ldiskfs_map_inode_pages(inode, iobuf, osd, 0,
1681 rc = osd_do_bio(osd, inode, iobuf, 0, 0);
1683 /* IO stats will be done in osd_bufs_put() */
1685 /* early release to let others read data during the bulk */
1686 for (i = 0; i < iobuf->dr_npages; i++) {
1687 LASSERT(PageLocked(iobuf->dr_pages[i]));
1688 if (!PagePrivate2(iobuf->dr_pages[i]))
1689 unlock_page(iobuf->dr_pages[i]);
1697 * XXX: Another layering violation for now.
1699 * We don't want to use ->f_op->read methods, because generic file write
1701 * - serializes on ->i_sem, and
1703 * - does a lot of extra work like balance_dirty_pages(),
1705 * which doesn't work for globally shared files like /last_rcvd.
1707 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
1709 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
1711 memcpy(buffer, (char *)ei->i_data, buflen);
1716 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
1718 struct buffer_head *bh;
1719 unsigned long block;
1725 /* prevent reading after eof */
1726 spin_lock(&inode->i_lock);
1727 if (i_size_read(inode) < *offs + size) {
1728 loff_t diff = i_size_read(inode) - *offs;
1730 spin_unlock(&inode->i_lock);
1733 "size %llu is too short to read @%llu\n",
1734 i_size_read(inode), *offs);
1736 } else if (diff == 0) {
1742 spin_unlock(&inode->i_lock);
1745 blocksize = 1 << inode->i_blkbits;
1748 block = *offs >> inode->i_blkbits;
1749 boffs = *offs & (blocksize - 1);
1750 csize = min(blocksize - boffs, size);
1751 bh = __ldiskfs_bread(NULL, inode, block, 0);
1753 CERROR("%s: can't read %u@%llu on ino %lu: rc = %ld\n",
1754 osd_ino2name(inode), csize, *offs, inode->i_ino,
1760 memcpy(buf, bh->b_data + boffs, csize);
1763 memset(buf, 0, csize);
1773 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1774 struct lu_buf *buf, loff_t *pos)
1776 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1779 /* Read small symlink from inode body as we need to maintain correct
1780 * on-disk symlinks for ldiskfs.
1782 if (S_ISLNK(dt->do_lu.lo_header->loh_attr)) {
1783 loff_t size = i_size_read(inode);
1785 if (buf->lb_len < size)
1788 if (size < sizeof(LDISKFS_I(inode)->i_data))
1789 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, size);
1791 rc = osd_ldiskfs_read(inode, buf->lb_buf, size, pos);
1793 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1799 static inline int osd_extents_enabled(struct super_block *sb,
1800 struct inode *inode)
1802 if (inode != NULL) {
1803 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL)
1805 } else if (ldiskfs_has_feature_extents(sb)) {
1811 int osd_calc_bkmap_credits(struct super_block *sb, struct inode *inode,
1812 const loff_t size, const loff_t pos,
1815 int credits, bits, bs, i;
1817 bits = sb->s_blocksize_bits;
1820 /* legacy blockmap: 3 levels * 3 (bitmap,gd,itself)
1821 * we do not expect blockmaps on the large files,
1822 * so let's shrink it to 2 levels (4GB files)
1825 /* this is default reservation: 2 levels */
1826 credits = (blocks + 2) * 3;
1828 /* actual offset is unknown, hard to optimize */
1832 /* now check for few specific cases to optimize */
1833 if (pos + size <= LDISKFS_NDIR_BLOCKS * bs) {
1836 /* allocate if not allocated */
1837 if (inode == NULL) {
1838 credits += blocks * 2;
1841 for (i = (pos >> bits); i < (pos >> bits) + blocks; i++) {
1842 LASSERT(i < LDISKFS_NDIR_BLOCKS);
1843 if (LDISKFS_I(inode)->i_data[i] == 0)
1846 } else if (pos + size <= (LDISKFS_NDIR_BLOCKS + 1024) * bs) {
1847 /* single indirect */
1848 credits = blocks * 3;
1849 if (inode == NULL ||
1850 LDISKFS_I(inode)->i_data[LDISKFS_IND_BLOCK] == 0)
1853 /* The indirect block may be modified. */
1860 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1861 const struct lu_buf *buf, loff_t _pos,
1862 struct thandle *handle)
1864 struct osd_object *obj = osd_dt_obj(dt);
1865 struct inode *inode = obj->oo_inode;
1866 struct super_block *sb = osd_sb(osd_obj2dev(obj));
1867 struct osd_thandle *oh;
1868 int rc = 0, est = 0, credits, blocks, allocated = 0;
1874 LASSERT(buf != NULL);
1875 LASSERT(handle != NULL);
1877 oh = container_of(handle, struct osd_thandle, ot_super);
1878 LASSERT(oh->ot_handle == NULL);
1881 bits = sb->s_blocksize_bits;
1885 /* if this is an append, then we
1886 * should expect cross-block record
1893 /* blocks to modify */
1894 blocks = ((pos + size + bs - 1) >> bits) - (pos >> bits);
1895 LASSERT(blocks > 0);
1897 if (inode != NULL && _pos != -1) {
1898 /* object size in blocks */
1899 est = (i_size_read(inode) + bs - 1) >> bits;
1900 allocated = inode->i_blocks >> (bits - 9);
1901 if (pos + size <= i_size_read(inode) && est <= allocated) {
1902 /* looks like an overwrite, no need to modify tree */
1904 /* no need to modify i_size */
1909 if (osd_extents_enabled(sb, inode)) {
1911 * many concurrent threads may grow tree by the time
1912 * our transaction starts. so, consider 2 is a min depth
1913 * for every level we may need to allocate a new block
1914 * and take some entries from the old one. so, 3 blocks
1915 * to allocate (bitmap, gd, itself) + old block - 4 per
1918 depth = inode != NULL ? ext_depth(inode) : 0;
1919 depth = max(depth, 1) + 1;
1921 /* if not append, then split may need to modify
1922 * existing blocks moving entries into the new ones
1926 /* blocks to store data: bitmap,gd,itself */
1927 credits += blocks * 3;
1929 credits = osd_calc_bkmap_credits(sb, inode, size, _pos, blocks);
1931 /* if inode is created as part of the transaction,
1932 * then it's counted already by the creation method
1939 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1941 /* dt_declare_write() is usually called for system objects, such
1942 * as llog or last_rcvd files. We needn't enforce quota on those
1943 * objects, so always set the lqi_space as 0.
1946 rc = osd_declare_inode_qid(env, i_uid_read(inode),
1948 i_projid_read(inode), 0,
1949 oh, obj, NULL, OSD_QID_BLK);
1952 rc = osd_trunc_lock(obj, oh, true);
1957 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1959 /* LU-2634: clear the extent format for fast symlink */
1960 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1962 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1963 spin_lock(&inode->i_lock);
1964 LDISKFS_I(inode)->i_disksize = buflen;
1965 i_size_write(inode, buflen);
1966 spin_unlock(&inode->i_lock);
1967 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
1972 static int osd_ldiskfs_write_record(struct dt_object *dt, void *buf,
1973 int bufsize, int write_NUL, loff_t *offs,
1976 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1977 struct buffer_head *bh = NULL;
1978 loff_t offset = *offs;
1979 loff_t new_size = i_size_read(inode);
1980 unsigned long block;
1981 int blocksize = 1 << inode->i_blkbits;
1982 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
1986 int dirty_inode = 0;
1987 bool create, sparse, sync = false;
1991 * long symlink write does not count the NUL terminator in
1992 * bufsize, we write it, and the inode's file size does not
1993 * count the NUL terminator as well.
1995 ((char *)buf)[bufsize] = '\0';
1999 /* only the first flag-set matters */
2000 dirty_inode = !test_and_set_bit(LDISKFS_INODE_JOURNAL_DATA,
2003 /* sparse checking is racy, but sparse is very rare case, leave as is */
2004 sparse = (new_size > 0 && (inode->i_blocks >> (inode->i_blkbits - 9)) <
2005 ((new_size - 1) >> inode->i_blkbits) + 1);
2007 while (bufsize > 0) {
2008 int credits = handle->h_buffer_credits;
2009 unsigned long last_block = (new_size == 0) ? 0 :
2010 (new_size - 1) >> inode->i_blkbits;
2015 block = offset >> inode->i_blkbits;
2016 boffs = offset & (blocksize - 1);
2017 size = min(blocksize - boffs, bufsize);
2018 sync = (block > last_block || new_size == 0 || sparse);
2021 down(&ei->i_append_sem);
2023 bh = __ldiskfs_bread(handle, inode, block, 0);
2025 if (unlikely(IS_ERR_OR_NULL(bh) && !sync))
2027 "%s: adding bh without locking off %llu (block %lu, size %d, offs %llu)\n",
2028 osd_ino2name(inode),
2029 offset, block, bufsize, *offs);
2031 if (IS_ERR_OR_NULL(bh)) {
2032 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2033 int flags = LDISKFS_GET_BLOCKS_CREATE;
2035 /* while the file system is being mounted, avoid
2036 * preallocation otherwise mount can take a long
2037 * time as mballoc cache is cold.
2038 * XXX: this is a workaround until we have a proper
2040 * XXX: works with extent-based files only */
2041 if (!osd->od_cl_seq)
2042 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2043 bh = __ldiskfs_bread(handle, inode, block, flags);
2047 up(&ei->i_append_sem);
2052 if (IS_ERR_OR_NULL(bh)) {
2061 "%s: error reading offset %llu (block %lu, size %d, offs %llu), credits %d/%d: rc = %d\n",
2062 osd_ino2name(inode), offset, block, bufsize,
2063 *offs, credits, handle->h_buffer_credits, err);
2067 err = ldiskfs_journal_get_write_access(handle, bh);
2069 CERROR("journal_get_write_access() returned error %d\n",
2073 LASSERTF(boffs + size <= bh->b_size,
2074 "boffs %d size %d bh->b_size %lu\n",
2075 boffs, size, (unsigned long)bh->b_size);
2077 memset(bh->b_data, 0, bh->b_size);
2079 up(&ei->i_append_sem);
2083 memcpy(bh->b_data + boffs, buf, size);
2084 err = ldiskfs_handle_dirty_metadata(handle, NULL, bh);
2088 if (offset + size > new_size)
2089 new_size = offset + size;
2095 up(&ei->i_append_sem);
2102 /* correct in-core and on-disk sizes */
2103 if (new_size > i_size_read(inode)) {
2104 spin_lock(&inode->i_lock);
2105 if (new_size > i_size_read(inode))
2106 i_size_write(inode, new_size);
2107 if (i_size_read(inode) > ei->i_disksize) {
2108 ei->i_disksize = i_size_read(inode);
2111 spin_unlock(&inode->i_lock);
2114 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2121 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
2122 const struct lu_buf *buf, loff_t *pos,
2123 struct thandle *handle)
2125 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2126 struct osd_thandle *oh;
2130 LASSERT(dt_object_exists(dt));
2132 LASSERT(handle != NULL);
2133 LASSERT(inode != NULL);
2134 dquot_initialize(inode);
2136 /* XXX: don't check: one declared chunk can be used many times */
2137 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
2139 oh = container_of(handle, struct osd_thandle, ot_super);
2140 LASSERT(oh->ot_handle->h_transaction != NULL);
2141 osd_trans_exec_op(env, handle, OSD_OT_WRITE);
2143 /* Write small symlink to inode body as we need to maintain correct
2144 * on-disk symlinks for ldiskfs.
2145 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
2146 * does not count it in.
2148 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
2149 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
2150 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
2152 result = osd_ldiskfs_write_record(dt, buf->lb_buf, buf->lb_len,
2153 is_link, pos, oh->ot_handle);
2155 result = buf->lb_len;
2157 osd_trans_exec_check(env, handle, OSD_OT_WRITE);
2162 static int osd_declare_fallocate(const struct lu_env *env,
2163 struct dt_object *dt, __u64 start, __u64 end,
2164 int mode, struct thandle *th)
2166 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2167 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
2168 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2169 long long quota_space = 0;
2170 /* 5 is max tree depth. (inode + 4 index blocks) */
2177 * Only mode == 0 (which is standard prealloc) is supported now.
2178 * Rest of mode options is not supported yet.
2180 if (mode & ~FALLOC_FL_KEEP_SIZE)
2181 RETURN(-EOPNOTSUPP);
2183 /* disable fallocate completely */
2184 if (osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks < 0)
2185 RETURN(-EOPNOTSUPP);
2190 /* quota space for metadata blocks
2191 * approximate metadata estimate should be good enough.
2193 quota_space += PAGE_SIZE;
2194 quota_space += depth * LDISKFS_BLOCK_SIZE(osd_sb(osd));
2196 /* quota space should be reported in 1K blocks */
2197 quota_space = toqb(quota_space) + toqb(end - start) +
2198 LDISKFS_META_TRANS_BLOCKS(inode->i_sb);
2200 /* We don't need to reserve credits for whole fallocate here.
2201 * We reserve space only for metadata. Fallocate credits are
2202 * extended as required
2204 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2205 i_projid_read(inode), quota_space, oh,
2206 osd_dt_obj(dt), NULL, OSD_QID_BLK);
2210 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
2211 __u64 start, __u64 end, int mode, struct thandle *th)
2213 struct osd_thandle *oh = container_of(th, struct osd_thandle, ot_super);
2214 handle_t *handle = ldiskfs_journal_current_handle();
2215 unsigned int save_credits = oh->ot_credits;
2216 struct osd_object *obj = osd_dt_obj(dt);
2217 struct inode *inode = obj->oo_inode;
2218 struct ldiskfs_map_blocks map;
2219 unsigned int credits;
2220 ldiskfs_lblk_t blen;
2221 ldiskfs_lblk_t boff;
2222 loff_t new_size = 0;
2229 LASSERT(dt_object_exists(dt));
2230 LASSERT(osd_invariant(obj));
2231 LASSERT(inode != NULL);
2233 CDEBUG(D_INODE, "fallocate: inode #%lu: start %llu end %llu mode %d\n",
2234 inode->i_ino, start, end, mode);
2236 dquot_initialize(inode);
2240 boff = start >> inode->i_blkbits;
2241 blen = (ALIGN(end, 1 << inode->i_blkbits) >> inode->i_blkbits) - boff;
2243 /* Create and mark new extents as either zero or unwritten */
2244 flags = osd_dev(dt->do_lu.lo_dev)->od_fallocate_zero_blocks ?
2245 LDISKFS_GET_BLOCKS_CREATE_ZERO :
2246 LDISKFS_GET_BLOCKS_CREATE_UNWRIT_EXT;
2247 if (mode & FALLOC_FL_KEEP_SIZE)
2248 flags |= LDISKFS_GET_BLOCKS_KEEP_SIZE;
2253 * We only support preallocation for extent-based file only.
2255 if (!(ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS)))
2256 GOTO(out, rc = -EOPNOTSUPP);
2258 if (!(mode & FALLOC_FL_KEEP_SIZE) && (end > i_size_read(inode) ||
2259 end > LDISKFS_I(inode)->i_disksize)) {
2261 rc = inode_newsize_ok(inode, new_size);
2266 inode_dio_wait(inode);
2271 /* Don't normalize the request if it can fit in one extent so
2272 * that it doesn't get unnecessarily split into multiple extents.
2274 if (blen <= EXT_UNWRITTEN_MAX_LEN)
2275 flags |= LDISKFS_GET_BLOCKS_NO_NORMALIZE;
2278 * credits to insert 1 extent into extent tree.
2280 credits = osd_chunk_trans_blocks(inode, blen);
2281 depth = ext_depth(inode);
2283 while (rc >= 0 && blen) {
2287 * Recalculate credits when extent tree depth changes.
2289 if (depth != ext_depth(inode)) {
2290 credits = osd_chunk_trans_blocks(inode, blen);
2291 depth = ext_depth(inode);
2294 /* TODO: quota check */
2295 rc = osd_extend_restart_trans(handle, credits, inode);
2299 rc = ldiskfs_map_blocks(handle, inode, &map, flags);
2302 "inode #%lu: block %u: len %u: ldiskfs_map_blocks returned %d\n",
2303 inode->i_ino, map.m_lblk, map.m_len, rc);
2304 ldiskfs_mark_inode_dirty(handle, inode);
2309 map.m_len = blen = blen - rc;
2310 epos = (loff_t)map.m_lblk << inode->i_blkbits;
2311 inode->i_ctime = current_time(inode);
2315 if (ldiskfs_update_inode_size(inode, epos) & 0x1)
2316 inode->i_mtime = inode->i_ctime;
2318 if (epos > inode->i_size)
2319 ldiskfs_set_inode_flag(inode,
2320 LDISKFS_INODE_EOFBLOCKS);
2323 ldiskfs_mark_inode_dirty(handle, inode);
2327 /* extand credits if needed for operations such as attribute set */
2329 rc = osd_extend_restart_trans(handle, save_credits, inode);
2331 inode_unlock(inode);
2336 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
2337 __u64 start, __u64 end, struct thandle *th)
2339 struct osd_thandle *oh;
2340 struct inode *inode;
2345 oh = container_of(th, struct osd_thandle, ot_super);
2348 * we don't need to reserve credits for whole truncate
2349 * it's not possible as truncate may need to free too many
2350 * blocks and that won't fit a single transaction. instead
2351 * we reserve credits to change i_size and put inode onto
2352 * orphan list. if needed truncate will extend or restart
2355 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
2356 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
2358 inode = osd_dt_obj(dt)->oo_inode;
2361 rc = osd_declare_inode_qid(env, i_uid_read(inode), i_gid_read(inode),
2362 i_projid_read(inode), 0, oh, osd_dt_obj(dt),
2366 rc = osd_trunc_lock(osd_dt_obj(dt), oh, false);
2371 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
2372 __u64 start, __u64 end, struct thandle *th)
2374 struct osd_object *obj = osd_dt_obj(dt);
2375 struct osd_device *osd = osd_obj2dev(obj);
2376 struct inode *inode = obj->oo_inode;
2377 struct osd_access_lock *al;
2378 struct osd_thandle *oh;
2379 int rc = 0, found = 0;
2383 LASSERT(dt_object_exists(dt));
2384 LASSERT(osd_invariant(obj));
2385 LASSERT(inode != NULL);
2386 dquot_initialize(inode);
2389 oh = container_of(th, struct osd_thandle, ot_super);
2390 LASSERT(oh->ot_handle->h_transaction != NULL);
2392 /* we used to skip truncate to current size to
2393 * optimize truncates on OST. with DoM we can
2394 * get attr_set to set specific size (MDS_REINT)
2395 * and then get truncate RPC which essentially
2396 * would be skipped. this is bad.. so, disable
2397 * this optimization on MDS till the client stop
2398 * to sent MDS_REINT (LU-11033) -bzzz
2400 if (osd->od_is_ost && i_size_read(inode) == start)
2403 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
2405 spin_lock(&inode->i_lock);
2406 if (i_size_read(inode) < start)
2408 i_size_write(inode, start);
2409 spin_unlock(&inode->i_lock);
2410 /* if object holds encrypted content, we need to make sure we truncate
2411 * on an encryption unit boundary, or subsequent reads will get
2414 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2415 start & ~LUSTRE_ENCRYPTION_MASK)
2416 start = (start & LUSTRE_ENCRYPTION_MASK) +
2417 LUSTRE_ENCRYPTION_UNIT_SIZE;
2418 ll_truncate_pagecache(inode, start);
2420 /* optimize grow case */
2422 osd_execute_truncate(obj);
2427 /* add to orphan list to ensure truncate completion
2428 * if this transaction succeed. ldiskfs_truncate()
2429 * will take the inode out of the list
2431 rc = ldiskfs_orphan_add(oh->ot_handle, inode);
2432 inode_unlock(inode);
2436 list_for_each_entry(al, &oh->ot_trunc_locks, tl_list) {
2437 if (obj != al->tl_obj)
2439 LASSERT(al->tl_shared == 0);
2441 /* do actual truncate in osd_trans_stop() */
2442 al->tl_truncate = 1;
2451 static int fiemap_check_ranges(struct inode *inode,
2452 u64 start, u64 len, u64 *new_len)
2461 if (ldiskfs_test_inode_flag(inode, LDISKFS_INODE_EXTENTS))
2462 maxbytes = inode->i_sb->s_maxbytes;
2464 maxbytes = LDISKFS_SB(inode->i_sb)->s_bitmap_maxbytes;
2466 if (start > maxbytes)
2470 * Shrink request scope to what the fs can actually handle.
2472 if (len > maxbytes || (maxbytes - len) < start)
2473 *new_len = maxbytes - start;
2478 /* So that the fiemap access checks can't overflow on 32 bit machines. */
2479 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
2481 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
2484 struct fiemap_extent_info fieinfo = {0, };
2485 struct inode *inode = osd_dt_obj(dt)->oo_inode;
2488 mm_segment_t cur_fs;
2491 if (inode->i_op->fiemap == NULL)
2494 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS)
2497 rc = fiemap_check_ranges(inode, fm->fm_start, fm->fm_length, &len);
2501 fieinfo.fi_flags = fm->fm_flags;
2502 fieinfo.fi_extents_max = fm->fm_extent_count;
2503 fieinfo.fi_extents_start = fm->fm_extents;
2505 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
2506 filemap_write_and_wait(inode->i_mapping);
2508 /* Save previous value address limit */
2510 /* Set the address limit of the kernel */
2513 rc = inode->i_op->fiemap(inode, &fieinfo, fm->fm_start, len);
2514 fm->fm_flags = fieinfo.fi_flags;
2515 fm->fm_mapped_extents = fieinfo.fi_extents_mapped;
2517 /* Restore the previous address limt */
2523 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
2524 __u64 start, __u64 end, enum lu_ladvise_type advice)
2526 struct osd_object *obj = osd_dt_obj(dt);
2531 case LU_LADVISE_DONTNEED:
2533 invalidate_mapping_pages(obj->oo_inode->i_mapping,
2534 start >> PAGE_SHIFT,
2535 (end - 1) >> PAGE_SHIFT);
2545 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
2546 loff_t offset, int whence)
2548 struct osd_object *obj = osd_dt_obj(dt);
2549 struct inode *inode = obj->oo_inode;
2555 LASSERT(dt_object_exists(dt));
2556 LASSERT(osd_invariant(obj));
2558 LASSERT(offset >= 0);
2560 file = osd_quasi_file(env, inode);
2561 result = file->f_op->llseek(file, offset, whence);
2564 * If 'offset' is beyond end of object file then treat it as not error
2565 * but valid case for SEEK_HOLE and return 'offset' as result.
2566 * LOV will decide if it is beyond real end of file or not.
2568 if (whence == SEEK_HOLE && result == -ENXIO)
2571 CDEBUG(D_INFO, "seek %s from %lld: %lld\n", whence == SEEK_HOLE ?
2572 "hole" : "data", offset, result);
2577 * in some cases we may need declare methods for objects being created
2578 * e.g., when we create symlink
2580 const struct dt_body_operations osd_body_ops_new = {
2581 .dbo_declare_write = osd_declare_write,
2584 const struct dt_body_operations osd_body_ops = {
2585 .dbo_read = osd_read,
2586 .dbo_declare_write = osd_declare_write,
2587 .dbo_write = osd_write,
2588 .dbo_bufs_get = osd_bufs_get,
2589 .dbo_bufs_put = osd_bufs_put,
2590 .dbo_write_prep = osd_write_prep,
2591 .dbo_declare_write_commit = osd_declare_write_commit,
2592 .dbo_write_commit = osd_write_commit,
2593 .dbo_read_prep = osd_read_prep,
2594 .dbo_declare_punch = osd_declare_punch,
2595 .dbo_punch = osd_punch,
2596 .dbo_fiemap_get = osd_fiemap_get,
2597 .dbo_ladvise = osd_ladvise,
2598 .dbo_declare_fallocate = osd_declare_fallocate,
2599 .dbo_fallocate = osd_fallocate,
2600 .dbo_lseek = osd_lseek,
2604 * Get a truncate lock
2606 * In order to take multi-transaction truncate out of main transaction we let
2607 * the caller grab a lock on the object passed. the lock can be shared (for
2608 * writes) and exclusive (for truncate). It's not allowed to mix truncate
2609 * and write in the same transaction handle (do not confuse with big ldiskfs
2610 * transaction containing lots of handles).
2611 * The lock must be taken at declaration.
2613 * \param obj object to lock
2615 * \shared shared or exclusive
2617 * \retval 0 lock is granted
2618 * \retval -NOMEM no memory to allocate lock
2620 int osd_trunc_lock(struct osd_object *obj, struct osd_thandle *oh, bool shared)
2622 struct osd_access_lock *al, *tmp;
2627 list_for_each_entry(tmp, &oh->ot_trunc_locks, tl_list) {
2628 if (tmp->tl_obj != obj)
2630 LASSERT(tmp->tl_shared == shared);
2631 /* found same lock */
2636 if (unlikely(al == NULL))
2639 al->tl_truncate = false;
2641 down_read(&obj->oo_ext_idx_sem);
2643 down_write(&obj->oo_ext_idx_sem);
2644 al->tl_shared = shared;
2645 lu_object_get(&obj->oo_dt.do_lu);
2647 list_add(&al->tl_list, &oh->ot_trunc_locks);
2652 void osd_trunc_unlock_all(const struct lu_env *env, struct list_head *list)
2654 struct osd_access_lock *al, *tmp;
2656 list_for_each_entry_safe(al, tmp, list, tl_list) {
2658 up_read(&al->tl_obj->oo_ext_idx_sem);
2660 up_write(&al->tl_obj->oo_ext_idx_sem);
2661 osd_object_put(env, al->tl_obj);
2662 list_del(&al->tl_list);
2667 void osd_execute_truncate(struct osd_object *obj)
2669 struct osd_device *d = osd_obj2dev(obj);
2670 struct inode *inode = obj->oo_inode;
2673 /* simulate crash before (in the middle) of delayed truncate */
2674 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_FAIL_AT_TRUNCATE)) {
2675 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2676 struct ldiskfs_sb_info *sbi = LDISKFS_SB(inode->i_sb);
2678 mutex_lock(&sbi->s_orphan_lock);
2679 list_del_init(&ei->i_orphan);
2680 mutex_unlock(&sbi->s_orphan_lock);
2684 size = i_size_read(inode);
2686 /* if object holds encrypted content, we need to make sure we truncate
2687 * on an encryption unit boundary, or block content will get corrupted
2689 if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
2690 size & ~LUSTRE_ENCRYPTION_MASK)
2691 inode->i_size = (size & LUSTRE_ENCRYPTION_MASK) +
2692 LUSTRE_ENCRYPTION_UNIT_SIZE;
2693 ldiskfs_truncate(inode);
2694 inode_unlock(inode);
2695 if (inode->i_size != size) {
2696 spin_lock(&inode->i_lock);
2697 i_size_write(inode, size);
2698 LDISKFS_I(inode)->i_disksize = size;
2699 spin_unlock(&inode->i_lock);
2700 osd_dirty_inode(inode, I_DIRTY_DATASYNC);
2704 * For a partial-page truncate, flush the page to disk immediately to
2705 * avoid data corruption during direct disk write. b=17397
2707 if ((size & ~PAGE_MASK) == 0)
2709 if (osd_use_page_cache(d)) {
2710 filemap_fdatawrite_range(inode->i_mapping, size, size + 1);
2712 /* Notice we use "wait" version to ensure I/O is complete */
2713 filemap_write_and_wait_range(inode->i_mapping, size, size + 1);
2714 invalidate_mapping_pages(inode->i_mapping, size >> PAGE_SHIFT,
2715 size >> PAGE_SHIFT);
2719 void osd_process_truncates(struct list_head *list)
2721 struct osd_access_lock *al;
2723 LASSERT(journal_current_handle() == NULL);
2725 list_for_each_entry(al, list, tl_list) {
2728 if (!al->tl_truncate)
2730 osd_execute_truncate(al->tl_obj);