4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45 /* LUSTRE_VERSION_CODE */
46 #include <lustre_ver.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/types.h>
49 /* prerequisite for linux/xattr.h */
53 * struct OBD_{ALLOC,FREE}*()
56 #include <obd_support.h>
58 #include "osd_internal.h"
61 #include <ldiskfs/ldiskfs_extents.h>
63 #ifndef HAVE_PAGE_CONSTANT
64 #define mapping_cap_page_constant_write(mapping) 0
65 #define SetPageConstant(page) do {} while (0)
66 #define ClearPageConstant(page) do {} while (0)
69 #ifndef HAS_GENERIC_ERROR_REMOVE_PAGE
70 int generic_error_remove_page(struct address_space *mapping, struct page *page)
75 if (mapping != page->mapping)
78 * Only punch for normal data pages for now.
79 * Handling other types like directories would need more auditing.
81 if (!S_ISREG(mapping->host->i_mode))
84 if (page_mapped(page)) {
85 unmap_mapping_range(mapping,
86 (loff_t)page->index << PAGE_CACHE_SHIFT,
89 truncate_complete_page(mapping, page);
94 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
95 int rw, int line, int pages)
99 LASSERTF(iobuf->dr_elapsed_valid == 0,
100 "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
101 cfs_atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
103 LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
105 cfs_waitq_init(&iobuf->dr_wait);
106 cfs_atomic_set(&iobuf->dr_numreqs, 0);
107 iobuf->dr_npages = 0;
111 iobuf->dr_elapsed = 0;
112 /* must be counted before, so assert */
114 iobuf->dr_init_at = line;
116 blocks = pages * (CFS_PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
117 if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
118 LASSERT(iobuf->dr_pg_buf.lb_len >=
119 pages * sizeof(iobuf->dr_pages[0]));
123 /* start with 1MB for 4K blocks */
125 while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
128 CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
129 (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
131 blocks = pages * (CFS_PAGE_SIZE >> osd_sb(d)->s_blocksize_bits);
132 iobuf->dr_max_pages = 0;
133 CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
134 (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
136 lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
137 iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
138 if (unlikely(iobuf->dr_blocks == NULL))
141 lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
142 iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
143 if (unlikely(iobuf->dr_pages == NULL))
146 iobuf->dr_max_pages = pages;
150 #define osd_init_iobuf(dev, iobuf, rw, pages) \
151 __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
153 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
155 LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
156 iobuf->dr_pages[iobuf->dr_npages++] = page;
159 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
161 int rw = iobuf->dr_rw;
163 if (iobuf->dr_elapsed_valid) {
164 iobuf->dr_elapsed_valid = 0;
165 LASSERT(iobuf->dr_dev == d);
166 LASSERT(iobuf->dr_frags > 0);
167 lprocfs_oh_tally(&d->od_brw_stats.
168 hist[BRW_R_DIO_FRAGS+rw],
170 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
175 #ifndef REQ_WRITE /* pre-2.6.35 */
176 #define __REQ_WRITE BIO_RW
179 #ifdef HAVE_BIO_ENDIO_2ARG
180 #define DIO_RETURN(a)
181 static void dio_complete_routine(struct bio *bio, int error)
183 #define DIO_RETURN(a) return(a)
184 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
187 struct osd_iobuf *iobuf = bio->bi_private;
191 /* CAVEAT EMPTOR: possibly in IRQ context
192 * DO NOT record procfs stats here!!! */
194 if (unlikely(iobuf == NULL)) {
195 CERROR("***** bio->bi_private is NULL! This should never "
196 "happen. Normally, I would crash here, but instead I "
197 "will dump the bio contents to the console. Please "
198 "report this to <http://jira.whamcloud.com/> , along "
199 "with any interesting messages leading up to this point "
200 "(like SCSI errors, perhaps). Because bi_private is "
201 "NULL, I can't wake up the thread that initiated this "
202 "IO - you will probably have to reboot this node.\n");
203 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
204 "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
205 "bi_private: %p\n", bio->bi_next, bio->bi_flags,
206 bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
207 bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
212 /* the check is outside of the cycle for performance reason -bzzz */
213 if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
214 bio_for_each_segment(bvl, bio, i) {
215 if (likely(error == 0))
216 SetPageUptodate(bvl->bv_page);
217 LASSERT(PageLocked(bvl->bv_page));
218 ClearPageConstant(bvl->bv_page);
220 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
222 struct page *p = iobuf->dr_pages[0];
224 if (mapping_cap_page_constant_write(p->mapping)) {
225 bio_for_each_segment(bvl, bio, i) {
226 ClearPageConstant(bvl->bv_page);
230 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
233 /* any real error is good enough -bzzz */
234 if (error != 0 && iobuf->dr_error == 0)
235 iobuf->dr_error = error;
238 * set dr_elapsed before dr_numreqs turns to 0, otherwise
239 * it's possible that service thread will see dr_numreqs
240 * is zero, but dr_elapsed is not set yet, leading to lost
241 * data in this processing and an assertion in a subsequent
244 if (cfs_atomic_read(&iobuf->dr_numreqs) == 1) {
245 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
246 iobuf->dr_elapsed_valid = 1;
248 if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
249 cfs_waitq_signal(&iobuf->dr_wait);
251 /* Completed bios used to be chained off iobuf->dr_bios and freed in
252 * filter_clear_dreq(). It was then possible to exhaust the biovec-256
253 * mempool when serious on-disk fragmentation was encountered,
254 * deadlocking the OST. The bios are now released as soon as complete
255 * so the pool cannot be exhausted while IOs are competing. bug 10076 */
260 static void record_start_io(struct osd_iobuf *iobuf, int size)
262 struct osd_device *osd = iobuf->dr_dev;
263 struct obd_histogram *h = osd->od_brw_stats.hist;
266 cfs_atomic_inc(&iobuf->dr_numreqs);
268 if (iobuf->dr_rw == 0) {
269 cfs_atomic_inc(&osd->od_r_in_flight);
270 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
271 cfs_atomic_read(&osd->od_r_in_flight));
272 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
273 } else if (iobuf->dr_rw == 1) {
274 cfs_atomic_inc(&osd->od_w_in_flight);
275 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
276 cfs_atomic_read(&osd->od_w_in_flight));
277 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
283 static void osd_submit_bio(int rw, struct bio *bio)
285 LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
287 submit_bio(READ, bio);
289 submit_bio(WRITE, bio);
292 static int can_be_merged(struct bio *bio, sector_t sector)
299 size = bio->bi_size >> 9;
300 return bio->bi_sector + size == sector ? 1 : 0;
303 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
304 struct osd_iobuf *iobuf)
306 int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
307 struct page **pages = iobuf->dr_pages;
308 int npages = iobuf->dr_npages;
309 unsigned long *blocks = iobuf->dr_blocks;
310 int total_blocks = npages * blocks_per_page;
311 int sector_bits = inode->i_sb->s_blocksize_bits - 9;
312 unsigned int blocksize = inode->i_sb->s_blocksize;
313 struct bio *bio = NULL;
315 unsigned int page_offset;
324 LASSERT(iobuf->dr_npages == npages);
326 osd_brw_stats_update(osd, iobuf);
327 iobuf->dr_start_time = cfs_time_current();
329 for (page_idx = 0, block_idx = 0;
331 page_idx++, block_idx += blocks_per_page) {
333 page = pages[page_idx];
334 LASSERT(block_idx + blocks_per_page <= total_blocks);
336 for (i = 0, page_offset = 0;
338 i += nblocks, page_offset += blocksize * nblocks) {
342 if (blocks[block_idx + i] == 0) { /* hole */
343 LASSERTF(iobuf->dr_rw == 0,
344 "page_idx %u, block_idx %u, i %u\n",
345 page_idx, block_idx, i);
346 memset(kmap(page) + page_offset, 0, blocksize);
351 sector = (sector_t)blocks[block_idx + i] << sector_bits;
353 /* Additional contiguous file blocks? */
354 while (i + nblocks < blocks_per_page &&
355 (sector + (nblocks << sector_bits)) ==
356 ((sector_t)blocks[block_idx + i + nblocks] <<
360 /* I only set the page to be constant only if it
361 * is mapped to a contiguous underlying disk block(s).
362 * It will then make sure the corresponding device
363 * cache of raid5 will be overwritten by this page.
365 if (iobuf->dr_rw && (nblocks == blocks_per_page) &&
366 mapping_cap_page_constant_write(inode->i_mapping))
367 SetPageConstant(page);
370 can_be_merged(bio, sector) &&
371 bio_add_page(bio, page,
372 blocksize * nblocks, page_offset) != 0)
373 continue; /* added this frag OK */
376 struct request_queue *q =
377 bdev_get_queue(bio->bi_bdev);
379 /* Dang! I have to fragment this I/O */
380 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
381 "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
383 bio->bi_vcnt, bio->bi_max_vecs,
384 bio->bi_size >> 9, queue_max_sectors(q),
385 bio_phys_segments(q, bio),
386 queue_max_phys_segments(q),
387 bio_hw_segments(q, bio),
388 queue_max_hw_segments(q));
390 record_start_io(iobuf, bio->bi_size);
391 osd_submit_bio(iobuf->dr_rw, bio);
394 /* allocate new bio */
395 bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
396 (npages - page_idx) *
399 CERROR("Can't allocate bio %u*%u = %u pages\n",
400 (npages - page_idx), blocks_per_page,
401 (npages - page_idx) * blocks_per_page);
406 bio->bi_bdev = inode->i_sb->s_bdev;
407 bio->bi_sector = sector;
408 bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
409 bio->bi_end_io = dio_complete_routine;
410 bio->bi_private = iobuf;
412 rc = bio_add_page(bio, page,
413 blocksize * nblocks, page_offset);
419 record_start_io(iobuf, bio->bi_size);
420 osd_submit_bio(iobuf->dr_rw, bio);
425 /* in order to achieve better IO throughput, we don't wait for writes
426 * completion here. instead we proceed with transaction commit in
427 * parallel and wait for IO completion once transaction is stopped
428 * see osd_trans_stop() for more details -bzzz */
429 if (iobuf->dr_rw == 0) {
430 cfs_wait_event(iobuf->dr_wait,
431 cfs_atomic_read(&iobuf->dr_numreqs) == 0);
435 rc = iobuf->dr_error;
439 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
440 struct niobuf_local *lnb)
447 int poff = offset & (CFS_PAGE_SIZE - 1);
448 int plen = CFS_PAGE_SIZE - poff;
452 lnb->lnb_file_offset = offset;
453 lnb->lnb_page_offset = poff;
455 /* lb->flags = rnb->flags; */
460 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
471 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
473 struct inode *inode = osd_dt_obj(dt)->oo_inode;
474 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
479 page = find_or_create_page(inode->i_mapping, offset >> CFS_PAGE_SHIFT,
480 GFP_NOFS | __GFP_HIGHMEM);
481 if (unlikely(page == NULL))
482 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
488 * there are following "locks":
505 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
506 ssize_t len, struct niobuf_local *lnb, int rw,
507 struct lustre_capa *capa)
509 struct osd_object *obj = osd_dt_obj(d);
510 int npages, i, rc = 0;
512 LASSERT(obj->oo_inode);
514 osd_map_remote_to_local(pos, len, &npages, lnb);
516 for (i = 0; i < npages; i++, lnb++) {
518 /* We still set up for ungranted pages so that granted pages
519 * can be written to disk as they were promised, and portals
520 * needs to keep the pages all aligned properly. */
521 lnb->dentry = (void *) obj;
523 lnb->page = osd_get_page(d, lnb->lnb_file_offset, rw);
524 if (lnb->page == NULL)
525 GOTO(cleanup, rc = -ENOMEM);
527 /* DLM locking protects us from write and truncate competing
528 * for same region, but truncate can leave dirty page in the
529 * cache. it's possible the writeout on a such a page is in
530 * progress when we access it. it's also possible that during
531 * this writeout we put new (partial) data, but then won't
532 * be able to proceed in filter_commitrw_write(). thus let's
533 * just wait for writeout completion, should be rare enough.
535 wait_on_page_writeback(lnb->page);
536 BUG_ON(PageWriteback(lnb->page));
538 lu_object_get(&d->do_lu);
546 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
547 struct niobuf_local *lnb, int npages)
549 struct osd_thread_info *oti = osd_oti_get(env);
550 struct osd_iobuf *iobuf = &oti->oti_iobuf;
551 struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
554 /* to do IO stats, notice we do this here because
555 * osd_do_bio() doesn't wait for write to complete */
556 osd_fini_iobuf(d, iobuf);
558 for (i = 0; i < npages; i++) {
559 if (lnb[i].page == NULL)
561 LASSERT(PageLocked(lnb[i].page));
562 unlock_page(lnb[i].page);
563 page_cache_release(lnb[i].page);
564 lu_object_put(env, &dt->do_lu);
570 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
571 struct niobuf_local *lnb, int npages)
573 struct osd_thread_info *oti = osd_oti_get(env);
574 struct osd_iobuf *iobuf = &oti->oti_iobuf;
575 struct inode *inode = osd_dt_obj(dt)->oo_inode;
576 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
577 struct timeval start;
579 unsigned long timediff;
588 rc = osd_init_iobuf(osd, iobuf, 0, npages);
589 if (unlikely(rc != 0))
592 isize = i_size_read(inode);
593 maxidx = ((isize + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT) - 1;
595 if (osd->od_writethrough_cache)
597 if (isize > osd->od_readcache_max_filesize)
600 cfs_gettimeofday(&start);
601 for (i = 0; i < npages; i++) {
604 generic_error_remove_page(inode->i_mapping,
608 * till commit the content of the page is undefined
609 * we'll set it uptodate once bulk is done. otherwise
610 * subsequent reads can access non-stable data
612 ClearPageUptodate(lnb[i].page);
614 if (lnb[i].len == CFS_PAGE_SIZE)
617 if (maxidx >= lnb[i].page->index) {
618 osd_iobuf_add_page(iobuf, lnb[i].page);
621 char *p = kmap(lnb[i].page);
623 off = lnb[i].lnb_page_offset;
626 off = (lnb[i].lnb_page_offset + lnb[i].len) &
629 memset(p + off, 0, CFS_PAGE_SIZE - off);
633 cfs_gettimeofday(&end);
634 timediff = cfs_timeval_sub(&end, &start, NULL);
635 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
637 if (iobuf->dr_npages) {
638 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
642 if (likely(rc == 0)) {
643 rc = osd_do_bio(osd, inode, iobuf);
644 /* do IO stats for preparation reads */
645 osd_fini_iobuf(osd, iobuf);
651 /* Check if a block is allocated or not */
652 static int osd_is_mapped(struct inode *inode, obd_size offset)
654 sector_t (*fs_bmap)(struct address_space *, sector_t);
656 fs_bmap = inode->i_mapping->a_ops->bmap;
658 /* We can't know if we are overwriting or not */
659 if (unlikely(fs_bmap == NULL))
662 if (i_size_read(inode) == 0)
665 /* Beyond EOF, must not be mapped */
666 if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
667 (offset >> inode->i_blkbits))
670 if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
676 static int osd_declare_write_commit(const struct lu_env *env,
677 struct dt_object *dt,
678 struct niobuf_local *lnb, int npages,
679 struct thandle *handle)
681 const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
682 struct inode *inode = osd_dt_obj(dt)->oo_inode;
683 struct osd_thandle *oh;
690 bool ignore_quota = false;
691 long long quota_space = 0;
694 LASSERT(handle != NULL);
695 oh = container_of0(handle, struct osd_thandle, ot_super);
696 LASSERT(oh->ot_handle == NULL);
700 /* calculate number of extents (probably better to pass nb) */
701 for (i = 0; i < npages; i++) {
702 if (i && lnb[i].lnb_file_offset !=
703 lnb[i - 1].lnb_file_offset + lnb[i - 1].len)
706 if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
707 quota_space += CFS_PAGE_SIZE;
709 /* ignore quota for the whole request if any page is from
710 * client cache or written by root.
712 * XXX once we drop the 1.8 client support, the checking
713 * for whether page is from cache can be simplified as:
714 * !(lnb[i].flags & OBD_BRW_SYNC)
716 * XXX we could handle this on per-lnb basis as done by
718 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
719 (lnb[i].flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
725 * each extent can go into new leaf causing a split
726 * 5 is max tree depth: inode + 4 index blocks
727 * with blockmaps, depth is 3 at most
729 if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
731 * many concurrent threads may grow tree by the time
732 * our transaction starts. so, consider 2 is a min depth
734 depth = ext_depth(inode);
735 depth = max(depth, 1) + 1;
737 oh->ot_credits++; /* inode */
738 oh->ot_credits += depth * 2 * extents;
742 oh->ot_credits++; /* inode */
743 oh->ot_credits += depth * extents;
746 /* quota space for metadata blocks */
747 quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
749 /* quota space should be reported in 1K blocks */
750 quota_space = toqb(quota_space);
752 /* each new block can go in different group (bitmap + gd) */
754 /* we can't dirty more bitmap blocks than exist */
755 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
756 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
758 oh->ot_credits += newblocks;
760 /* we can't dirty more gd blocks than exist */
761 if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
762 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
764 oh->ot_credits += newblocks;
766 /* make sure the over quota flags were not set */
767 lnb[0].flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
769 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid,
770 quota_space, oh, true, true, &flags,
773 /* we need only to store the overquota flags in the first lnb for
774 * now, once we support multiple objects BRW, this code needs be
776 if (flags & QUOTA_FL_OVER_USRQUOTA)
777 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
778 if (flags & QUOTA_FL_OVER_GRPQUOTA)
779 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
784 /* Check if a block is allocated or not */
785 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
786 struct niobuf_local *lnb, int npages,
787 struct thandle *thandle)
789 struct osd_thread_info *oti = osd_oti_get(env);
790 struct osd_iobuf *iobuf = &oti->oti_iobuf;
791 struct inode *inode = osd_dt_obj(dt)->oo_inode;
792 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
798 rc = osd_init_iobuf(osd, iobuf, 1, npages);
799 if (unlikely(rc != 0))
802 isize = i_size_read(inode);
803 ll_vfs_dq_init(inode);
805 for (i = 0; i < npages; i++) {
806 if (lnb[i].rc == -ENOSPC &&
807 osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
808 /* Allow the write to proceed if overwriting an
813 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
814 CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
816 LASSERT(lnb[i].page);
817 generic_error_remove_page(inode->i_mapping,lnb[i].page);
821 LASSERT(PageLocked(lnb[i].page));
822 LASSERT(!PageWriteback(lnb[i].page));
824 if (lnb[i].lnb_file_offset + lnb[i].len > isize)
825 isize = lnb[i].lnb_file_offset + lnb[i].len;
828 * Since write and truncate are serialized by oo_sem, even
829 * partial-page truncate should not leave dirty pages in the
832 LASSERT(!PageDirty(lnb[i].page));
834 SetPageUptodate(lnb[i].page);
836 osd_iobuf_add_page(iobuf, lnb[i].page);
839 if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
841 } else if (iobuf->dr_npages > 0) {
842 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
847 /* no pages to write, no transno is needed */
848 thandle->th_local = 1;
851 if (likely(rc == 0)) {
852 if (isize > i_size_read(inode)) {
853 i_size_write(inode, isize);
854 LDISKFS_I(inode)->i_disksize = isize;
855 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
858 rc = osd_do_bio(osd, inode, iobuf);
859 /* we don't do stats here as in read path because
860 * write is async: we'll do this in osd_put_bufs() */
863 if (unlikely(rc != 0)) {
864 /* if write fails, we should drop pages from the cache */
865 for (i = 0; i < npages; i++) {
866 if (lnb[i].page == NULL)
868 LASSERT(PageLocked(lnb[i].page));
869 generic_error_remove_page(inode->i_mapping,lnb[i].page);
876 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
877 struct niobuf_local *lnb, int npages)
879 struct osd_thread_info *oti = osd_oti_get(env);
880 struct osd_iobuf *iobuf = &oti->oti_iobuf;
881 struct inode *inode = osd_dt_obj(dt)->oo_inode;
882 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
883 struct timeval start, end;
884 unsigned long timediff;
885 int rc = 0, i, m = 0, cache = 0;
889 rc = osd_init_iobuf(osd, iobuf, 0, npages);
890 if (unlikely(rc != 0))
893 if (osd->od_read_cache)
895 if (i_size_read(inode) > osd->od_readcache_max_filesize)
898 cfs_gettimeofday(&start);
899 for (i = 0; i < npages; i++) {
901 if (i_size_read(inode) <= lnb[i].lnb_file_offset)
902 /* If there's no more data, abort early.
903 * lnb->rc == 0, so it's easy to detect later. */
906 if (i_size_read(inode) <
907 lnb[i].lnb_file_offset + lnb[i].len - 1)
908 lnb[i].rc = i_size_read(inode) - lnb[i].lnb_file_offset;
910 lnb[i].rc = lnb[i].len;
913 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
914 if (PageUptodate(lnb[i].page)) {
915 lprocfs_counter_add(osd->od_stats,
916 LPROC_OSD_CACHE_HIT, 1);
918 lprocfs_counter_add(osd->od_stats,
919 LPROC_OSD_CACHE_MISS, 1);
920 osd_iobuf_add_page(iobuf, lnb[i].page);
923 generic_error_remove_page(inode->i_mapping,lnb[i].page);
925 cfs_gettimeofday(&end);
926 timediff = cfs_timeval_sub(&end, &start, NULL);
927 lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
929 if (iobuf->dr_npages) {
930 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
934 rc = osd_do_bio(osd, inode, iobuf);
936 /* IO stats will be done in osd_bufs_put() */
943 * XXX: Another layering violation for now.
945 * We don't want to use ->f_op->read methods, because generic file write
947 * - serializes on ->i_sem, and
949 * - does a lot of extra work like balance_dirty_pages(),
951 * which doesn't work for globally shared files like /last_rcvd.
953 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
955 struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
957 memcpy(buffer, (char *)ei->i_data, buflen);
962 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
964 struct buffer_head *bh;
972 /* prevent reading after eof */
973 spin_lock(&inode->i_lock);
974 if (i_size_read(inode) < *offs + size) {
975 loff_t diff = i_size_read(inode) - *offs;
976 spin_unlock(&inode->i_lock);
978 CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
979 i_size_read(inode), *offs);
981 } else if (diff == 0) {
987 spin_unlock(&inode->i_lock);
990 blocksize = 1 << inode->i_blkbits;
993 block = *offs >> inode->i_blkbits;
994 boffs = *offs & (blocksize - 1);
995 csize = min(blocksize - boffs, size);
996 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
998 CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
999 LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
1000 csize, *offs, inode->i_ino, err);
1004 memcpy(buf, bh->b_data + boffs, csize);
1014 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
1015 struct lu_buf *buf, loff_t *pos,
1016 struct lustre_capa *capa)
1018 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1021 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
1024 /* Read small symlink from inode body as we need to maintain correct
1025 * on-disk symlinks for ldiskfs.
1027 if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
1028 (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1029 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
1031 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1036 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1037 const loff_t size, loff_t pos,
1038 struct thandle *handle)
1040 struct osd_thandle *oh;
1042 struct inode *inode;
1046 LASSERT(handle != NULL);
1048 oh = container_of0(handle, struct osd_thandle, ot_super);
1049 LASSERT(oh->ot_handle == NULL);
1051 credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
1053 osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1055 inode = osd_dt_obj(dt)->oo_inode;
1057 /* we may declare write to non-exist llog */
1061 /* dt_declare_write() is usually called for system objects, such
1062 * as llog or last_rcvd files. We needn't enforce quota on those
1063 * objects, so always set the lqi_space as 0. */
1064 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1065 true, true, NULL, false);
1069 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1071 /* LU-2634: clear the extent format for fast symlink */
1072 ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1074 memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1075 LDISKFS_I(inode)->i_disksize = buflen;
1076 i_size_write(inode, buflen);
1077 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1082 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1083 int write_NUL, loff_t *offs, handle_t *handle)
1085 struct buffer_head *bh = NULL;
1086 loff_t offset = *offs;
1087 loff_t new_size = i_size_read(inode);
1088 unsigned long block;
1089 int blocksize = 1 << inode->i_blkbits;
1093 int dirty_inode = 0;
1097 * long symlink write does not count the NUL terminator in
1098 * bufsize, we write it, and the inode's file size does not
1099 * count the NUL terminator as well.
1101 ((char *)buf)[bufsize] = '\0';
1104 while (bufsize > 0) {
1108 block = offset >> inode->i_blkbits;
1109 boffs = offset & (blocksize - 1);
1110 size = min(blocksize - boffs, bufsize);
1111 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1113 CERROR("%s: error reading offset %llu (block %lu): "
1115 inode->i_sb->s_id, offset, block, err);
1119 err = ldiskfs_journal_get_write_access(handle, bh);
1121 CERROR("journal_get_write_access() returned error %d\n",
1125 LASSERTF(boffs + size <= bh->b_size,
1126 "boffs %d size %d bh->b_size %lu",
1127 boffs, size, (unsigned long)bh->b_size);
1128 memcpy(bh->b_data + boffs, buf, size);
1129 err = ldiskfs_journal_dirty_metadata(handle, bh);
1133 if (offset + size > new_size)
1134 new_size = offset + size;
1144 /* correct in-core and on-disk sizes */
1145 if (new_size > i_size_read(inode)) {
1146 spin_lock(&inode->i_lock);
1147 if (new_size > i_size_read(inode))
1148 i_size_write(inode, new_size);
1149 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1150 LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1153 spin_unlock(&inode->i_lock);
1155 ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1163 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1164 const struct lu_buf *buf, loff_t *pos,
1165 struct thandle *handle, struct lustre_capa *capa,
1168 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1169 struct osd_thandle *oh;
1173 LASSERT(dt_object_exists(dt));
1175 if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1178 LASSERT(handle != NULL);
1179 LASSERT(inode != NULL);
1180 ll_vfs_dq_init(inode);
1182 /* XXX: don't check: one declared chunk can be used many times */
1183 /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
1185 oh = container_of(handle, struct osd_thandle, ot_super);
1186 LASSERT(oh->ot_handle->h_transaction != NULL);
1187 /* Write small symlink to inode body as we need to maintain correct
1188 * on-disk symlinks for ldiskfs.
1189 * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1190 * does not count it in.
1192 is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1193 if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1194 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1196 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1197 buf->lb_len, is_link, pos,
1200 result = buf->lb_len;
1204 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1205 __u64 start, __u64 end, struct thandle *th)
1207 struct osd_thandle *oh;
1208 struct inode *inode;
1213 oh = container_of(th, struct osd_thandle, ot_super);
1216 * we don't need to reserve credits for whole truncate
1217 * it's not possible as truncate may need to free too many
1218 * blocks and that won't fit a single transaction. instead
1219 * we reserve credits to change i_size and put inode onto
1220 * orphan list. if needed truncate will extend or restart
1223 osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
1224 osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
1226 inode = osd_dt_obj(dt)->oo_inode;
1229 rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1230 true, true, NULL, false);
1234 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1235 __u64 start, __u64 end, struct thandle *th,
1236 struct lustre_capa *capa)
1238 struct osd_thandle *oh;
1239 struct osd_object *obj = osd_dt_obj(dt);
1240 struct inode *inode = obj->oo_inode;
1246 LASSERT(end == OBD_OBJECT_EOF);
1247 LASSERT(dt_object_exists(dt));
1248 LASSERT(osd_invariant(obj));
1249 LASSERT(inode != NULL);
1250 ll_vfs_dq_init(inode);
1253 oh = container_of(th, struct osd_thandle, ot_super);
1254 LASSERT(oh->ot_handle->h_transaction != NULL);
1256 osd_trans_exec_op(env, th, OSD_OT_PUNCH);
1258 tid = oh->ot_handle->h_transaction->t_tid;
1260 rc = vmtruncate(inode, start);
1263 * For a partial-page truncate, flush the page to disk immediately to
1264 * avoid data corruption during direct disk write. b=17397
1266 if (rc == 0 && (start & ~CFS_PAGE_MASK) != 0)
1267 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1269 h = journal_current_handle();
1271 LASSERT(h == oh->ot_handle);
1273 if (tid != h->h_transaction->t_tid) {
1274 int credits = oh->ot_credits;
1276 * transaction has changed during truncate
1277 * we need to restart the handle with our credits
1279 if (h->h_buffer_credits < credits) {
1280 if (ldiskfs_journal_extend(h, credits))
1281 rc2 = ldiskfs_journal_restart(h, credits);
1285 RETURN(rc == 0 ? rc2 : rc);
1288 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1289 struct ll_user_fiemap *fm)
1291 struct inode *inode = osd_dt_obj(dt)->oo_inode;
1292 struct osd_thread_info *info = osd_oti_get(env);
1293 struct dentry *dentry = &info->oti_obj_dentry;
1294 struct file *file = &info->oti_file;
1295 mm_segment_t saved_fs;
1299 dentry->d_inode = inode;
1300 dentry->d_sb = inode->i_sb;
1301 file->f_dentry = dentry;
1302 file->f_mapping = inode->i_mapping;
1303 file->f_op = inode->i_fop;
1305 saved_fs = get_fs();
1307 /* ldiskfs_ioctl does not have a inode argument */
1308 if (inode->i_fop->unlocked_ioctl)
1309 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1318 * in some cases we may need declare methods for objects being created
1319 * e.g., when we create symlink
1321 const struct dt_body_operations osd_body_ops_new = {
1322 .dbo_declare_write = osd_declare_write,
1325 const struct dt_body_operations osd_body_ops = {
1326 .dbo_read = osd_read,
1327 .dbo_declare_write = osd_declare_write,
1328 .dbo_write = osd_write,
1329 .dbo_bufs_get = osd_bufs_get,
1330 .dbo_bufs_put = osd_bufs_put,
1331 .dbo_write_prep = osd_write_prep,
1332 .dbo_declare_write_commit = osd_declare_write_commit,
1333 .dbo_write_commit = osd_write_commit,
1334 .dbo_read_prep = osd_read_prep,
1335 .dbo_declare_punch = osd_declare_punch,
1336 .dbo_punch = osd_punch,
1337 .dbo_fiemap_get = osd_fiemap_get,