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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_io for VVP layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
46 #include "llite_internal.h"
47 #include "vvp_internal.h"
49 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
50 const struct cl_io_slice *slice);
53 * True, if \a io is a normal io, False for splice_{read,write}
55 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
57 struct vvp_io *vio = vvp_env_io(env);
59 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
61 return vio->cui_io_subtype == IO_NORMAL;
65 * For swapping layout. The file's layout may have changed.
66 * To avoid populating pages to a wrong stripe, we have to verify the
67 * correctness of layout. It works because swapping layout processes
68 * have to acquire group lock.
70 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
73 struct ll_inode_info *lli = ll_i2info(inode);
74 struct ccc_io *cio = ccc_env_io(env);
77 switch (io->ci_type) {
80 /* don't need lock here to check lli_layout_gen as we have held
81 * extent lock and GROUP lock has to hold to swap layout */
82 if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
83 io->ci_need_restart = 1;
84 /* this will return application a short read/write */
89 /* fault is okay because we've already had a page. */
97 /*****************************************************************************
103 static int vvp_io_write_iter_init(const struct lu_env *env,
104 const struct cl_io_slice *ios)
106 struct ccc_io *cio = cl2ccc_io(env, ios);
108 cl_page_list_init(&cio->u.write.cui_queue);
109 cio->u.write.cui_written = 0;
110 cio->u.write.cui_from = 0;
111 cio->u.write.cui_to = PAGE_SIZE;
116 static void vvp_io_write_iter_fini(const struct lu_env *env,
117 const struct cl_io_slice *ios)
119 struct ccc_io *cio = cl2ccc_io(env, ios);
121 LASSERT(cio->u.write.cui_queue.pl_nr == 0);
124 static int vvp_io_fault_iter_init(const struct lu_env *env,
125 const struct cl_io_slice *ios)
127 struct vvp_io *vio = cl2vvp_io(env, ios);
128 struct inode *inode = vvp_object_inode(ios->cis_obj);
131 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
132 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
136 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
138 struct cl_io *io = ios->cis_io;
139 struct cl_object *obj = io->ci_obj;
140 struct ccc_io *cio = cl2ccc_io(env, ios);
141 struct inode *inode = vvp_object_inode(obj);
143 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
145 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
146 "restore needed %d\n",
147 PFID(lu_object_fid(&obj->co_lu)),
148 io->ci_ignore_layout, io->ci_verify_layout,
149 cio->cui_layout_gen, io->ci_restore_needed);
151 if (io->ci_restore_needed == 1) {
154 /* file was detected release, we need to restore it
155 * before finishing the io
157 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
158 /* if restore registration failed, no restart,
159 * we will return -ENODATA */
160 /* The layout will change after restore, so we need to
161 * block on layout lock hold by the MDT
162 * as MDT will not send new layout in lvb (see LU-3124)
163 * we have to explicitly fetch it, all this will be done
164 * by ll_layout_refresh()
167 io->ci_restore_needed = 0;
168 io->ci_need_restart = 1;
169 io->ci_verify_layout = 1;
171 io->ci_restore_needed = 1;
172 io->ci_need_restart = 0;
173 io->ci_verify_layout = 0;
178 if (!io->ci_ignore_layout && io->ci_verify_layout) {
181 /* check layout version */
182 ll_layout_refresh(inode, &gen);
183 io->ci_need_restart = cio->cui_layout_gen != gen;
184 if (io->ci_need_restart) {
186 DFID" layout changed from %d to %d.\n",
187 PFID(lu_object_fid(&obj->co_lu)),
188 cio->cui_layout_gen, gen);
189 /* today successful restore is the only possible
191 /* restore was done, clear restoring state */
192 ll_i2info(vvp_object_inode(obj))->lli_flags &=
193 ~LLIF_FILE_RESTORING;
198 static void vvp_io_fault_fini(const struct lu_env *env,
199 const struct cl_io_slice *ios)
201 struct cl_io *io = ios->cis_io;
202 struct cl_page *page = io->u.ci_fault.ft_page;
204 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
207 lu_ref_del(&page->cp_reference, "fault", io);
208 cl_page_put(env, page);
209 io->u.ci_fault.ft_page = NULL;
211 vvp_io_fini(env, ios);
214 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
217 * we only want to hold PW locks if the mmap() can generate
218 * writes back to the file and that only happens in shared
221 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
226 static int vvp_mmap_locks(const struct lu_env *env,
227 struct ccc_io *vio, struct cl_io *io)
229 struct ccc_thread_info *cti = ccc_env_info(env);
230 struct mm_struct *mm = current->mm;
231 struct vm_area_struct *vma;
232 struct cl_lock_descr *descr = &cti->cti_descr;
233 ldlm_policy_data_t policy;
240 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
242 if (!cl_is_normalio(env, io))
245 if (vio->cui_iov == NULL) /* nfs or loop back device write */
248 /* No MM (e.g. NFS)? No vmas too. */
252 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
253 const struct iovec *iv = &vio->cui_iov[seg];
255 addr = (unsigned long)iv->iov_base;
260 count += addr & (~CFS_PAGE_MASK);
261 addr &= CFS_PAGE_MASK;
263 down_read(&mm->mmap_sem);
264 while((vma = our_vma(mm, addr, count)) != NULL) {
265 struct inode *inode = vma->vm_file->f_dentry->d_inode;
266 int flags = CEF_MUST;
268 if (ll_file_nolock(vma->vm_file)) {
270 * For no lock case is not allowed for mmap
277 * XXX: Required lock mode can be weakened: CIT_WRITE
278 * io only ever reads user level buffer, and CIT_READ
281 policy_from_vma(&policy, vma, addr, count);
282 descr->cld_mode = vvp_mode_from_vma(vma);
283 descr->cld_obj = ll_i2info(inode)->lli_clob;
284 descr->cld_start = cl_index(descr->cld_obj,
285 policy.l_extent.start);
286 descr->cld_end = cl_index(descr->cld_obj,
287 policy.l_extent.end);
288 descr->cld_enq_flags = flags;
289 result = cl_io_lock_alloc_add(env, io, descr);
291 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
292 descr->cld_mode, descr->cld_start,
298 if (vma->vm_end - addr >= count)
301 count -= vma->vm_end - addr;
304 up_read(&mm->mmap_sem);
311 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
312 enum cl_lock_mode mode, loff_t start, loff_t end)
314 struct ccc_io *cio = ccc_env_io(env);
318 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
321 ccc_io_update_iov(env, cio, io);
323 if (io->u.ci_rw.crw_nonblock)
324 ast_flags |= CEF_NONBLOCK;
325 result = vvp_mmap_locks(env, cio, io);
327 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
331 static int vvp_io_read_lock(const struct lu_env *env,
332 const struct cl_io_slice *ios)
334 struct cl_io *io = ios->cis_io;
335 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
339 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
340 rd->crw_pos + rd->crw_count - 1);
344 static int vvp_io_fault_lock(const struct lu_env *env,
345 const struct cl_io_slice *ios)
347 struct cl_io *io = ios->cis_io;
348 struct vvp_io *vio = cl2vvp_io(env, ios);
350 * XXX LDLM_FL_CBPENDING
352 return ccc_io_one_lock_index
353 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
354 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
357 static int vvp_io_write_lock(const struct lu_env *env,
358 const struct cl_io_slice *ios)
360 struct cl_io *io = ios->cis_io;
364 if (io->u.ci_wr.wr_append) {
366 end = OBD_OBJECT_EOF;
368 start = io->u.ci_wr.wr.crw_pos;
369 end = start + io->u.ci_wr.wr.crw_count - 1;
371 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
374 static int vvp_io_setattr_iter_init(const struct lu_env *env,
375 const struct cl_io_slice *ios)
381 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
383 * Handles "lockless io" mode when extent locking is done by server.
385 static int vvp_io_setattr_lock(const struct lu_env *env,
386 const struct cl_io_slice *ios)
388 struct ccc_io *cio = ccc_env_io(env);
389 struct cl_io *io = ios->cis_io;
393 if (cl_io_is_trunc(io)) {
394 new_size = io->u.ci_setattr.sa_attr.lvb_size;
396 enqflags = CEF_DISCARD_DATA;
398 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
399 io->u.ci_setattr.sa_attr.lvb_ctime) ||
400 (io->u.ci_setattr.sa_attr.lvb_atime >=
401 io->u.ci_setattr.sa_attr.lvb_ctime))
405 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
406 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
407 new_size, OBD_OBJECT_EOF);
410 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
415 * Only ll_inode_size_lock is taken at this level.
417 ll_inode_size_lock(inode);
418 result = inode_newsize_ok(inode, size);
420 ll_inode_size_unlock(inode);
423 i_size_write(inode, size);
425 ll_truncate_pagecache(inode, size);
426 ll_inode_size_unlock(inode);
430 static int vvp_io_setattr_trunc(const struct lu_env *env,
431 const struct cl_io_slice *ios,
432 struct inode *inode, loff_t size)
434 inode_dio_wait(inode);
438 static int vvp_io_setattr_time(const struct lu_env *env,
439 const struct cl_io_slice *ios)
441 struct cl_io *io = ios->cis_io;
442 struct cl_object *obj = io->ci_obj;
443 struct cl_attr *attr = ccc_env_thread_attr(env);
445 unsigned valid = CAT_CTIME;
447 cl_object_attr_lock(obj);
448 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
449 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
450 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
453 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
454 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
457 result = cl_object_attr_set(env, obj, attr, valid);
458 cl_object_attr_unlock(obj);
463 static int vvp_io_setattr_start(const struct lu_env *env,
464 const struct cl_io_slice *ios)
466 struct cl_io *io = ios->cis_io;
467 struct inode *inode = vvp_object_inode(io->ci_obj);
470 mutex_lock(&inode->i_mutex);
471 if (cl_io_is_trunc(io))
472 result = vvp_io_setattr_trunc(env, ios, inode,
473 io->u.ci_setattr.sa_attr.lvb_size);
475 result = vvp_io_setattr_time(env, ios);
479 static void vvp_io_setattr_end(const struct lu_env *env,
480 const struct cl_io_slice *ios)
482 struct cl_io *io = ios->cis_io;
483 struct inode *inode = vvp_object_inode(io->ci_obj);
485 if (cl_io_is_trunc(io)) {
486 /* Truncate in memory pages - they must be clean pages
487 * because osc has already notified to destroy osc_extents. */
488 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
489 inode_dio_write_done(inode);
491 mutex_unlock(&inode->i_mutex);
494 static void vvp_io_setattr_fini(const struct lu_env *env,
495 const struct cl_io_slice *ios)
497 vvp_io_fini(env, ios);
500 static int vvp_io_read_start(const struct lu_env *env,
501 const struct cl_io_slice *ios)
503 struct vvp_io *vio = cl2vvp_io(env, ios);
504 struct ccc_io *cio = cl2ccc_io(env, ios);
505 struct cl_io *io = ios->cis_io;
506 struct cl_object *obj = io->ci_obj;
507 struct inode *inode = vvp_object_inode(obj);
508 struct ll_ra_read *bead = &vio->cui_bead;
509 struct file *file = cio->cui_fd->fd_file;
512 loff_t pos = io->u.ci_rd.rd.crw_pos;
513 long cnt = io->u.ci_rd.rd.crw_count;
514 long tot = cio->cui_tot_count;
517 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
519 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
521 if (!can_populate_pages(env, io, inode))
524 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
527 else if (exceed != 0)
530 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
531 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
532 inode->i_ino, cnt, pos, i_size_read(inode));
534 /* turn off the kernel's read-ahead */
535 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
537 /* initialize read-ahead window once per syscall */
538 if (!vio->cui_ra_window_set) {
539 vio->cui_ra_window_set = 1;
540 bead->lrr_start = cl_index(obj, pos);
541 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
542 ll_ra_read_in(file, bead);
547 switch (vio->cui_io_subtype) {
549 LASSERT(cio->cui_iocb->ki_pos == pos);
550 result = generic_file_aio_read(cio->cui_iocb,
551 cio->cui_iov, cio->cui_nrsegs,
552 cio->cui_iocb->ki_pos);
555 result = generic_file_splice_read(file, &pos,
556 vio->u.splice.cui_pipe, cnt,
557 vio->u.splice.cui_flags);
558 /* LU-1109: do splice read stripe by stripe otherwise if it
559 * may make nfsd stuck if this read occupied all internal pipe
564 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
572 io->ci_nob += result;
573 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
581 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
583 struct vvp_io *vio = cl2vvp_io(env, ios);
584 struct ccc_io *cio = cl2ccc_io(env, ios);
586 if (vio->cui_ra_window_set)
587 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
589 vvp_io_fini(env, ios);
592 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
593 struct cl_page_list *plist, int from, int to)
595 struct cl_2queue *queue = &io->ci_queue;
596 struct cl_page *page;
597 unsigned int bytes = 0;
601 if (plist->pl_nr == 0)
604 if (from > 0 || to != PAGE_SIZE) {
605 page = cl_page_list_first(plist);
606 if (plist->pl_nr == 1) {
607 cl_page_clip(env, page, from, to);
610 cl_page_clip(env, page, from, PAGE_SIZE);
611 if (to != PAGE_SIZE) {
612 page = cl_page_list_last(plist);
613 cl_page_clip(env, page, 0, to);
618 cl_2queue_init(queue);
619 cl_page_list_splice(plist, &queue->c2_qin);
620 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
622 /* plist is not sorted any more */
623 cl_page_list_splice(&queue->c2_qin, plist);
624 cl_page_list_splice(&queue->c2_qout, plist);
625 cl_2queue_fini(env, queue);
628 /* calculate bytes */
629 bytes = plist->pl_nr << PAGE_SHIFT;
630 bytes -= from + PAGE_SIZE - to;
632 while (plist->pl_nr > 0) {
633 page = cl_page_list_first(plist);
634 cl_page_list_del(env, plist, page);
636 cl_page_clip(env, page, 0, PAGE_SIZE);
638 SetPageUptodate(cl_page_vmpage(page));
639 cl_page_disown(env, io, page);
641 /* held in ll_cl_init() */
642 lu_ref_del(&page->cp_reference, "cl_io", io);
643 cl_page_put(env, page);
647 RETURN(bytes > 0 ? bytes : rc);
650 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
651 struct cl_page *page)
654 struct page *vmpage = page->cp_vmpage;
655 struct cl_object *clob = cl_io_top(io)->ci_obj;
657 SetPageUptodate(vmpage);
658 set_page_dirty(vmpage);
660 cp = cl2ccc_page(cl_object_page_slice(clob, page));
661 vvp_write_pending(cl2vvp(clob), cp);
663 cl_page_disown(env, io, page);
665 /* held in ll_cl_init() */
666 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
667 cl_page_put(env, page);
670 /* make sure the page list is contiguous */
671 static bool page_list_sanity_check(struct cl_object *obj,
672 struct cl_page_list *plist)
674 struct cl_page *page;
675 pgoff_t index = CL_PAGE_EOF;
677 cl_page_list_for_each(page, plist) {
678 struct ccc_page *cp = cl_object_page_slice(obj, page);
680 if (index == CL_PAGE_EOF) {
681 index = ccc_index(cp);
686 if (index == ccc_index(cp))
694 /* Return how many bytes have queued or written */
695 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
697 struct cl_object *obj = io->ci_obj;
698 struct inode *inode = vvp_object_inode(obj);
699 struct ccc_io *cio = ccc_env_io(env);
700 struct cl_page_list *queue = &cio->u.write.cui_queue;
701 struct cl_page *page;
704 unsigned int npages = cio->u.write.cui_queue.pl_nr;
710 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
711 npages, cio->u.write.cui_from, cio->u.write.cui_to);
713 LASSERT(page_list_sanity_check(obj, queue));
715 /* submit IO with async write */
716 rc = cl_io_commit_async(env, io, queue,
717 cio->u.write.cui_from, cio->u.write.cui_to,
718 write_commit_callback);
719 npages -= queue->pl_nr; /* already committed pages */
721 /* calculate how many bytes were written */
722 bytes = npages << PAGE_SHIFT;
725 bytes -= cio->u.write.cui_from;
726 if (queue->pl_nr == 0) /* last page */
727 bytes -= PAGE_SIZE - cio->u.write.cui_to;
728 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
730 cio->u.write.cui_written += bytes;
732 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
733 npages, bytes, cio->u.write.cui_written);
735 /* the first page must have been written. */
736 cio->u.write.cui_from = 0;
738 LASSERT(page_list_sanity_check(obj, queue));
739 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
741 /* out of quota, try sync write */
742 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
743 rc = vvp_io_commit_sync(env, io, queue,
744 cio->u.write.cui_from,
745 cio->u.write.cui_to);
747 cio->u.write.cui_written += rc;
752 /* update inode size */
753 ll_merge_attr(env, inode);
755 /* Now the pages in queue were failed to commit, discard them
756 * unless they were dirtied before. */
757 while (queue->pl_nr > 0) {
758 page = cl_page_list_first(queue);
759 cl_page_list_del(env, queue, page);
761 if (!PageDirty(cl_page_vmpage(page)))
762 cl_page_discard(env, io, page);
764 cl_page_disown(env, io, page);
766 /* held in ll_cl_init() */
767 lu_ref_del(&page->cp_reference, "cl_io", io);
768 cl_page_put(env, page);
770 cl_page_list_fini(env, queue);
775 static int vvp_io_write_start(const struct lu_env *env,
776 const struct cl_io_slice *ios)
778 struct ccc_io *cio = cl2ccc_io(env, ios);
779 struct cl_io *io = ios->cis_io;
780 struct cl_object *obj = io->ci_obj;
781 struct inode *inode = vvp_object_inode(obj);
783 loff_t pos = io->u.ci_wr.wr.crw_pos;
784 size_t cnt = io->u.ci_wr.wr.crw_count;
788 if (!can_populate_pages(env, io, inode))
791 if (cl_io_is_append(io)) {
793 * PARALLEL IO This has to be changed for parallel IO doing
794 * out-of-order writes.
796 ll_merge_attr(env, inode);
797 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
798 cio->cui_iocb->ki_pos = pos;
800 LASSERT(cio->cui_iocb->ki_pos == pos);
803 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
805 if (cio->cui_iov == NULL) {
806 /* from a temp io in ll_cl_init(). */
810 * When using the locked AIO function (generic_file_aio_write())
811 * testing has shown the inode mutex to be a limiting factor
812 * with multi-threaded single shared file performance. To get
813 * around this, we now use the lockless version. To maintain
814 * consistency, proper locking to protect against writes,
815 * trucates, etc. is handled in the higher layers of lustre.
817 result = __generic_file_aio_write(cio->cui_iocb,
818 cio->cui_iov, cio->cui_nrsegs,
819 &cio->cui_iocb->ki_pos);
820 if (result > 0 || result == -EIOCBQUEUED) {
823 err = generic_write_sync(cio->cui_iocb->ki_filp,
825 if (err < 0 && result > 0)
831 result = vvp_io_write_commit(env, io);
832 if (cio->u.write.cui_written > 0) {
833 result = cio->u.write.cui_written;
834 io->ci_nob += result;
836 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
841 struct ll_inode_info *lli = ll_i2info(inode);
843 spin_lock(&lli->lli_lock);
844 lli->lli_flags |= LLIF_DATA_MODIFIED;
845 spin_unlock(&lli->lli_lock);
849 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
850 cio->cui_fd, pos, result, WRITE);
857 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
859 struct vm_fault *vmf = cfio->fault.ft_vmf;
861 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
862 cfio->fault.ft_flags_valid = 1;
865 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
866 vmf->virtual_address);
867 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
868 lock_page(vmf->page);
869 cfio->fault.ft_flags |= VM_FAULT_LOCKED;
872 cfio->ft_vmpage = vmf->page;
876 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
877 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
881 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
882 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
886 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
889 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
893 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
894 struct cl_page *page)
897 struct cl_object *clob = cl_io_top(io)->ci_obj;
899 set_page_dirty(page->cp_vmpage);
901 cp = cl2ccc_page(cl_object_page_slice(clob, page));
902 vvp_write_pending(cl2vvp(clob), cp);
905 static int vvp_io_fault_start(const struct lu_env *env,
906 const struct cl_io_slice *ios)
908 struct vvp_io *vio = cl2vvp_io(env, ios);
909 struct cl_io *io = ios->cis_io;
910 struct cl_object *obj = io->ci_obj;
911 struct inode *inode = vvp_object_inode(obj);
912 struct cl_fault_io *fio = &io->u.ci_fault;
913 struct vvp_fault_io *cfio = &vio->u.fault;
916 struct page *vmpage = NULL;
917 struct cl_page *page;
922 if (fio->ft_executable &&
923 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
925 " changed while waiting for the page fault lock\n",
926 PFID(lu_object_fid(&obj->co_lu)));
928 /* offset of the last byte on the page */
929 offset = cl_offset(obj, fio->ft_index + 1) - 1;
930 LASSERT(cl_index(obj, offset) == fio->ft_index);
931 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
935 /* must return locked page */
936 if (fio->ft_mkwrite) {
937 LASSERT(cfio->ft_vmpage != NULL);
938 lock_page(cfio->ft_vmpage);
940 result = vvp_io_kernel_fault(cfio);
945 vmpage = cfio->ft_vmpage;
946 LASSERT(PageLocked(vmpage));
948 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
949 ll_invalidate_page(vmpage);
951 size = i_size_read(inode);
952 /* Though we have already held a cl_lock upon this page, but
953 * it still can be truncated locally. */
954 if (unlikely((vmpage->mapping != inode->i_mapping) ||
955 (page_offset(vmpage) > size))) {
956 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
958 /* return +1 to stop cl_io_loop() and ll_fault() will catch
960 GOTO(out, result = +1);
963 last_index = cl_index(obj, size - 1);
965 if (fio->ft_mkwrite ) {
967 * Capture the size while holding the lli_trunc_sem from above
968 * we want to make sure that we complete the mkwrite action
969 * while holding this lock. We need to make sure that we are
970 * not past the end of the file.
972 if (last_index < fio->ft_index) {
974 "llite: mkwrite and truncate race happened: "
976 vmpage->mapping,fio->ft_index,last_index);
978 * We need to return if we are
979 * passed the end of the file. This will propagate
980 * up the call stack to ll_page_mkwrite where
981 * we will return VM_FAULT_NOPAGE. Any non-negative
982 * value returned here will be silently
983 * converted to 0. If the vmpage->mapping is null
984 * the error code would be converted back to ENODATA
985 * in ll_page_mkwrite0. Thus we return -ENODATA
986 * to handle both cases
988 GOTO(out, result = -ENODATA);
992 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
994 GOTO(out, result = PTR_ERR(page));
996 /* if page is going to be written, we should add this page into cache
998 if (fio->ft_mkwrite) {
999 wait_on_page_writeback(vmpage);
1000 if (!PageDirty(vmpage)) {
1001 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1002 struct ccc_page *cp = cl_object_page_slice(obj, page);
1005 /* vvp_page_assume() calls wait_on_page_writeback(). */
1006 cl_page_assume(env, io, page);
1008 cl_page_list_init(plist);
1009 cl_page_list_add(plist, page);
1012 if (last_index == ccc_index(cp))
1013 to = size & ~CFS_PAGE_MASK;
1015 /* Do not set Dirty bit here so that in case IO is
1016 * started before the page is really made dirty, we
1017 * still have chance to detect it. */
1018 result = cl_io_commit_async(env, io, plist, 0, to,
1019 mkwrite_commit_callback);
1020 LASSERT(cl_page_is_owned(page, io));
1021 cl_page_list_fini(env, plist);
1025 cl_page_discard(env, io, page);
1026 cl_page_disown(env, io, page);
1028 cl_page_put(env, page);
1030 /* we're in big trouble, what can we do now? */
1031 if (result == -EDQUOT)
1035 cl_page_disown(env, io, page);
1040 * The ft_index is only used in the case of
1041 * a mkwrite action. We need to check
1042 * our assertions are correct, since
1043 * we should have caught this above
1045 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1046 if (fio->ft_index == last_index)
1048 * Last page is mapped partially.
1050 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1052 fio->ft_nob = cl_page_size(obj);
1054 lu_ref_add(&page->cp_reference, "fault", io);
1055 fio->ft_page = page;
1059 /* return unlocked vmpage to avoid deadlocking */
1061 unlock_page(vmpage);
1062 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1066 static int vvp_io_fsync_start(const struct lu_env *env,
1067 const struct cl_io_slice *ios)
1069 /* we should mark TOWRITE bit to each dirty page in radix tree to
1070 * verify pages have been written, but this is difficult because of
1075 static int vvp_io_read_page(const struct lu_env *env,
1076 const struct cl_io_slice *ios,
1077 const struct cl_page_slice *slice)
1079 struct cl_io *io = ios->cis_io;
1080 struct ccc_page *cp = cl2ccc_page(slice);
1081 struct cl_page *page = slice->cpl_page;
1082 struct inode *inode = vvp_object_inode(slice->cpl_obj);
1083 struct ll_sb_info *sbi = ll_i2sbi(inode);
1084 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1085 struct ll_readahead_state *ras = &fd->fd_ras;
1086 struct cl_2queue *queue = &io->ci_queue;
1090 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1091 sbi->ll_ra_info.ra_max_pages > 0)
1092 ras_update(sbi, inode, ras, ccc_index(cp),
1093 cp->cpg_defer_uptodate);
1095 if (cp->cpg_defer_uptodate) {
1096 cp->cpg_ra_used = 1;
1097 cl_page_export(env, page, 1);
1101 * Add page into the queue even when it is marked uptodate above.
1102 * this will unlock it automatically as part of cl_page_list_disown().
1104 cl_2queue_add(queue, page);
1105 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1106 sbi->ll_ra_info.ra_max_pages > 0)
1107 ll_readahead(env, io, &queue->c2_qin, ras,
1108 cp->cpg_defer_uptodate);
1113 static const struct cl_io_operations vvp_io_ops = {
1116 .cio_fini = vvp_io_read_fini,
1117 .cio_lock = vvp_io_read_lock,
1118 .cio_start = vvp_io_read_start,
1119 .cio_advance = ccc_io_advance
1122 .cio_fini = vvp_io_fini,
1123 .cio_iter_init = vvp_io_write_iter_init,
1124 .cio_iter_fini = vvp_io_write_iter_fini,
1125 .cio_lock = vvp_io_write_lock,
1126 .cio_start = vvp_io_write_start,
1127 .cio_advance = ccc_io_advance
1130 .cio_fini = vvp_io_setattr_fini,
1131 .cio_iter_init = vvp_io_setattr_iter_init,
1132 .cio_lock = vvp_io_setattr_lock,
1133 .cio_start = vvp_io_setattr_start,
1134 .cio_end = vvp_io_setattr_end
1137 .cio_fini = vvp_io_fault_fini,
1138 .cio_iter_init = vvp_io_fault_iter_init,
1139 .cio_lock = vvp_io_fault_lock,
1140 .cio_start = vvp_io_fault_start,
1141 .cio_end = ccc_io_end
1144 .cio_start = vvp_io_fsync_start,
1145 .cio_fini = vvp_io_fini
1148 .cio_fini = vvp_io_fini
1151 .cio_read_page = vvp_io_read_page,
1154 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1157 struct vvp_io *vio = vvp_env_io(env);
1158 struct ccc_io *cio = ccc_env_io(env);
1159 struct inode *inode = vvp_object_inode(obj);
1162 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1165 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1166 "restore needed %d\n",
1167 PFID(lu_object_fid(&obj->co_lu)),
1168 io->ci_ignore_layout, io->ci_verify_layout,
1169 cio->cui_layout_gen, io->ci_restore_needed);
1171 CL_IO_SLICE_CLEAN(cio, cui_cl);
1172 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1173 vio->cui_ra_window_set = 0;
1175 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1177 struct ll_inode_info *lli = ll_i2info(inode);
1179 count = io->u.ci_rw.crw_count;
1180 /* "If nbyte is 0, read() will return 0 and have no other
1181 * results." -- Single Unix Spec */
1185 cio->cui_tot_count = count;
1186 cio->cui_tot_nrsegs = 0;
1189 /* for read/write, we store the jobid in the inode, and
1190 * it'll be fetched by osc when building RPC.
1192 * it's not accurate if the file is shared by different
1195 lustre_get_jobid(lli->lli_jobid);
1196 } else if (io->ci_type == CIT_SETATTR) {
1197 if (!cl_io_is_trunc(io))
1198 io->ci_lockreq = CILR_MANDATORY;
1201 /* ignore layout change for generic CIT_MISC but not for glimpse.
1202 * io context for glimpse must set ci_verify_layout to true,
1203 * see cl_glimpse_size0() for details. */
1204 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1205 io->ci_ignore_layout = 1;
1207 /* Enqueue layout lock and get layout version. We need to do this
1208 * even for operations requiring to open file, such as read and write,
1209 * because it might not grant layout lock in IT_OPEN. */
1210 if (result == 0 && !io->ci_ignore_layout) {
1211 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1212 if (result == -ENOENT)
1213 /* If the inode on MDS has been removed, but the objects
1214 * on OSTs haven't been destroyed (async unlink), layout
1215 * fetch will return -ENOENT, we'd ingore this error
1216 * and continue with dirty flush. LU-3230. */
1219 CERROR("%s: refresh file layout " DFID " error %d.\n",
1220 ll_get_fsname(inode->i_sb, NULL, 0),
1221 PFID(lu_object_fid(&obj->co_lu)), result);
1227 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1228 const struct cl_io_slice *slice)
1230 /* Caling just for assertion */
1231 cl2ccc_io(env, slice);
1232 return vvp_env_io(env);