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, 2013, 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
45 # error This file is kernel only.
49 #include <lustre_lite.h>
51 #include "vvp_internal.h"
53 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
54 const struct cl_io_slice *slice);
57 * True, if \a io is a normal io, False for splice_{read,write}
59 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
61 struct vvp_io *vio = vvp_env_io(env);
63 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
65 return vio->cui_io_subtype == IO_NORMAL;
69 * For swapping layout. The file's layout may have changed.
70 * To avoid populating pages to a wrong stripe, we have to verify the
71 * correctness of layout. It works because swapping layout processes
72 * have to acquire group lock.
74 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
77 struct ll_inode_info *lli = ll_i2info(inode);
78 struct ccc_io *cio = ccc_env_io(env);
81 switch (io->ci_type) {
84 /* don't need lock here to check lli_layout_gen as we have held
85 * extent lock and GROUP lock has to hold to swap layout */
86 if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
87 io->ci_need_restart = 1;
88 /* this will return application a short read/write */
93 /* fault is okay because we've already had a page. */
101 /*****************************************************************************
107 static int vvp_io_write_iter_init(const struct lu_env *env,
108 const struct cl_io_slice *ios)
110 struct ccc_io *cio = cl2ccc_io(env, ios);
112 cl_page_list_init(&cio->u.write.cui_queue);
113 cio->u.write.cui_written = 0;
114 cio->u.write.cui_from = 0;
115 cio->u.write.cui_to = PAGE_SIZE;
120 static void vvp_io_write_iter_fini(const struct lu_env *env,
121 const struct cl_io_slice *ios)
123 struct ccc_io *cio = cl2ccc_io(env, ios);
125 LASSERT(cio->u.write.cui_queue.pl_nr == 0);
128 static int vvp_io_fault_iter_init(const struct lu_env *env,
129 const struct cl_io_slice *ios)
131 struct vvp_io *vio = cl2vvp_io(env, ios);
132 struct inode *inode = ccc_object_inode(ios->cis_obj);
135 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
136 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
140 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
142 struct cl_io *io = ios->cis_io;
143 struct cl_object *obj = io->ci_obj;
144 struct ccc_io *cio = cl2ccc_io(env, ios);
145 struct inode *inode = ccc_object_inode(obj);
147 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
149 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
150 "restore needed %d\n",
151 PFID(lu_object_fid(&obj->co_lu)),
152 io->ci_ignore_layout, io->ci_verify_layout,
153 cio->cui_layout_gen, io->ci_restore_needed);
155 if (io->ci_restore_needed == 1) {
158 /* file was detected release, we need to restore it
159 * before finishing the io
161 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
162 /* if restore registration failed, no restart,
163 * we will return -ENODATA */
164 /* The layout will change after restore, so we need to
165 * block on layout lock hold by the MDT
166 * as MDT will not send new layout in lvb (see LU-3124)
167 * we have to explicitly fetch it, all this will be done
168 * by ll_layout_refresh()
171 io->ci_restore_needed = 0;
172 io->ci_need_restart = 1;
173 io->ci_verify_layout = 1;
175 io->ci_restore_needed = 1;
176 io->ci_need_restart = 0;
177 io->ci_verify_layout = 0;
182 if (!io->ci_ignore_layout && io->ci_verify_layout) {
185 /* check layout version */
186 ll_layout_refresh(inode, &gen);
187 io->ci_need_restart = cio->cui_layout_gen != gen;
188 if (io->ci_need_restart) {
190 DFID" layout changed from %d to %d.\n",
191 PFID(lu_object_fid(&obj->co_lu)),
192 cio->cui_layout_gen, gen);
193 /* today successful restore is the only possible
195 /* restore was done, clear restoring state */
196 ll_i2info(ccc_object_inode(obj))->lli_flags &=
197 ~LLIF_FILE_RESTORING;
202 static void vvp_io_fault_fini(const struct lu_env *env,
203 const struct cl_io_slice *ios)
205 struct cl_io *io = ios->cis_io;
206 struct cl_page *page = io->u.ci_fault.ft_page;
208 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
211 lu_ref_del(&page->cp_reference, "fault", io);
212 cl_page_put(env, page);
213 io->u.ci_fault.ft_page = NULL;
215 vvp_io_fini(env, ios);
218 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
221 * we only want to hold PW locks if the mmap() can generate
222 * writes back to the file and that only happens in shared
225 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
230 static int vvp_mmap_locks(const struct lu_env *env,
231 struct ccc_io *vio, struct cl_io *io)
233 struct ccc_thread_info *cti = ccc_env_info(env);
234 struct mm_struct *mm = current->mm;
235 struct vm_area_struct *vma;
236 struct cl_lock_descr *descr = &cti->cti_descr;
237 ldlm_policy_data_t policy;
244 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
246 if (!cl_is_normalio(env, io))
249 if (vio->cui_iov == NULL) /* nfs or loop back device write */
252 /* No MM (e.g. NFS)? No vmas too. */
256 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
257 const struct iovec *iv = &vio->cui_iov[seg];
259 addr = (unsigned long)iv->iov_base;
264 count += addr & (~CFS_PAGE_MASK);
265 addr &= CFS_PAGE_MASK;
267 down_read(&mm->mmap_sem);
268 while((vma = our_vma(mm, addr, count)) != NULL) {
269 struct inode *inode = vma->vm_file->f_dentry->d_inode;
270 int flags = CEF_MUST;
272 if (ll_file_nolock(vma->vm_file)) {
274 * For no lock case, a lockless lock will be
281 * XXX: Required lock mode can be weakened: CIT_WRITE
282 * io only ever reads user level buffer, and CIT_READ
285 policy_from_vma(&policy, vma, addr, count);
286 descr->cld_mode = vvp_mode_from_vma(vma);
287 descr->cld_obj = ll_i2info(inode)->lli_clob;
288 descr->cld_start = cl_index(descr->cld_obj,
289 policy.l_extent.start);
290 descr->cld_end = cl_index(descr->cld_obj,
291 policy.l_extent.end);
292 descr->cld_enq_flags = flags;
293 result = cl_io_lock_alloc_add(env, io, descr);
295 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
296 descr->cld_mode, descr->cld_start,
300 up_read(&mm->mmap_sem);
304 if (vma->vm_end - addr >= count)
307 count -= vma->vm_end - addr;
310 up_read(&mm->mmap_sem);
315 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
316 enum cl_lock_mode mode, loff_t start, loff_t end)
318 struct ccc_io *cio = ccc_env_io(env);
322 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
325 ccc_io_update_iov(env, cio, io);
327 if (io->u.ci_rw.crw_nonblock)
328 ast_flags |= CEF_NONBLOCK;
329 result = vvp_mmap_locks(env, cio, io);
331 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
335 static int vvp_io_read_lock(const struct lu_env *env,
336 const struct cl_io_slice *ios)
338 struct cl_io *io = ios->cis_io;
339 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
343 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
344 rd->crw_pos + rd->crw_count - 1);
348 static int vvp_io_fault_lock(const struct lu_env *env,
349 const struct cl_io_slice *ios)
351 struct cl_io *io = ios->cis_io;
352 struct vvp_io *vio = cl2vvp_io(env, ios);
354 * XXX LDLM_FL_CBPENDING
356 return ccc_io_one_lock_index
357 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
358 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
361 static int vvp_io_write_lock(const struct lu_env *env,
362 const struct cl_io_slice *ios)
364 struct cl_io *io = ios->cis_io;
368 if (io->u.ci_wr.wr_append) {
370 end = OBD_OBJECT_EOF;
372 start = io->u.ci_wr.wr.crw_pos;
373 end = start + io->u.ci_wr.wr.crw_count - 1;
375 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
378 static int vvp_io_setattr_iter_init(const struct lu_env *env,
379 const struct cl_io_slice *ios)
385 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
387 * Handles "lockless io" mode when extent locking is done by server.
389 static int vvp_io_setattr_lock(const struct lu_env *env,
390 const struct cl_io_slice *ios)
392 struct ccc_io *cio = ccc_env_io(env);
393 struct cl_io *io = ios->cis_io;
397 if (cl_io_is_trunc(io)) {
398 new_size = io->u.ci_setattr.sa_attr.lvb_size;
400 enqflags = CEF_DISCARD_DATA;
402 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
403 io->u.ci_setattr.sa_attr.lvb_ctime) ||
404 (io->u.ci_setattr.sa_attr.lvb_atime >=
405 io->u.ci_setattr.sa_attr.lvb_ctime))
409 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
410 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
411 new_size, OBD_OBJECT_EOF);
414 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
419 * Only ll_inode_size_lock is taken at this level.
421 ll_inode_size_lock(inode);
422 result = inode_newsize_ok(inode, size);
424 ll_inode_size_unlock(inode);
427 i_size_write(inode, size);
429 ll_truncate_pagecache(inode, size);
430 ll_inode_size_unlock(inode);
434 static int vvp_io_setattr_trunc(const struct lu_env *env,
435 const struct cl_io_slice *ios,
436 struct inode *inode, loff_t size)
438 inode_dio_wait(inode);
442 static int vvp_io_setattr_time(const struct lu_env *env,
443 const struct cl_io_slice *ios)
445 struct cl_io *io = ios->cis_io;
446 struct cl_object *obj = io->ci_obj;
447 struct cl_attr *attr = ccc_env_thread_attr(env);
449 unsigned valid = CAT_CTIME;
451 cl_object_attr_lock(obj);
452 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
453 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
454 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
457 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
458 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
461 result = cl_object_attr_set(env, obj, attr, valid);
462 cl_object_attr_unlock(obj);
467 static int vvp_io_setattr_start(const struct lu_env *env,
468 const struct cl_io_slice *ios)
470 struct cl_io *io = ios->cis_io;
471 struct inode *inode = ccc_object_inode(io->ci_obj);
474 mutex_lock(&inode->i_mutex);
475 if (cl_io_is_trunc(io))
476 result = vvp_io_setattr_trunc(env, ios, inode,
477 io->u.ci_setattr.sa_attr.lvb_size);
479 result = vvp_io_setattr_time(env, ios);
483 static void vvp_io_setattr_end(const struct lu_env *env,
484 const struct cl_io_slice *ios)
486 struct cl_io *io = ios->cis_io;
487 struct inode *inode = ccc_object_inode(io->ci_obj);
489 if (cl_io_is_trunc(io)) {
490 /* Truncate in memory pages - they must be clean pages
491 * because osc has already notified to destroy osc_extents. */
492 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
493 inode_dio_write_done(inode);
495 mutex_unlock(&inode->i_mutex);
498 static void vvp_io_setattr_fini(const struct lu_env *env,
499 const struct cl_io_slice *ios)
501 vvp_io_fini(env, ios);
504 static int vvp_io_read_start(const struct lu_env *env,
505 const struct cl_io_slice *ios)
507 struct vvp_io *vio = cl2vvp_io(env, ios);
508 struct ccc_io *cio = cl2ccc_io(env, ios);
509 struct cl_io *io = ios->cis_io;
510 struct cl_object *obj = io->ci_obj;
511 struct inode *inode = ccc_object_inode(obj);
512 struct ll_ra_read *bead = &vio->cui_bead;
513 struct file *file = cio->cui_fd->fd_file;
516 loff_t pos = io->u.ci_rd.rd.crw_pos;
517 long cnt = io->u.ci_rd.rd.crw_count;
518 long tot = cio->cui_tot_count;
521 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
523 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
525 if (!can_populate_pages(env, io, inode))
528 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
531 else if (exceed != 0)
534 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
535 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
536 inode->i_ino, cnt, pos, i_size_read(inode));
538 /* turn off the kernel's read-ahead */
539 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
541 /* initialize read-ahead window once per syscall */
542 if (!vio->cui_ra_window_set) {
543 vio->cui_ra_window_set = 1;
544 bead->lrr_start = cl_index(obj, pos);
545 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
546 ll_ra_read_in(file, bead);
551 switch (vio->cui_io_subtype) {
553 LASSERT(cio->cui_iocb->ki_pos == pos);
554 result = generic_file_aio_read(cio->cui_iocb,
555 cio->cui_iov, cio->cui_nrsegs,
556 cio->cui_iocb->ki_pos);
559 result = generic_file_splice_read(file, &pos,
560 vio->u.splice.cui_pipe, cnt,
561 vio->u.splice.cui_flags);
562 /* LU-1109: do splice read stripe by stripe otherwise if it
563 * may make nfsd stuck if this read occupied all internal pipe
568 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
576 io->ci_nob += result;
577 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
585 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
587 struct vvp_io *vio = cl2vvp_io(env, ios);
588 struct ccc_io *cio = cl2ccc_io(env, ios);
590 if (vio->cui_ra_window_set)
591 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
593 vvp_io_fini(env, ios);
596 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
597 struct cl_page_list *plist, int from, int to)
599 struct cl_2queue *queue = &io->ci_queue;
600 struct cl_page *page;
601 unsigned int bytes = 0;
605 if (plist->pl_nr == 0)
608 if (from > 0 || to != PAGE_SIZE) {
609 page = cl_page_list_first(plist);
610 if (plist->pl_nr == 1) {
611 cl_page_clip(env, page, from, to);
612 } else if (from > 0) {
613 cl_page_clip(env, page, from, PAGE_SIZE);
615 page = cl_page_list_last(plist);
616 cl_page_clip(env, page, 0, to);
620 cl_2queue_init(queue);
621 cl_page_list_splice(plist, &queue->c2_qin);
622 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
624 /* plist is not sorted any more */
625 cl_page_list_splice(&queue->c2_qin, plist);
626 cl_page_list_splice(&queue->c2_qout, plist);
627 cl_2queue_fini(env, queue);
630 /* calculate bytes */
631 bytes = plist->pl_nr << PAGE_SHIFT;
632 bytes -= from + PAGE_SIZE - to;
634 while (plist->pl_nr > 0) {
635 page = cl_page_list_first(plist);
636 cl_page_list_del(env, plist, page);
638 cl_page_clip(env, page, 0, PAGE_SIZE);
640 SetPageUptodate(cl_page_vmpage(page));
641 cl_page_disown(env, io, page);
643 /* held in ll_cl_init() */
644 lu_ref_del(&page->cp_reference, "cl_io", io);
645 cl_page_put(env, page);
649 RETURN(bytes > 0 ? bytes : rc);
652 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
653 struct cl_page *page)
656 struct page *vmpage = page->cp_vmpage;
657 struct cl_object *clob = cl_io_top(io)->ci_obj;
659 SetPageUptodate(vmpage);
660 set_page_dirty(vmpage);
662 cp = cl2ccc_page(cl_object_page_slice(clob, page));
663 vvp_write_pending(cl2ccc(clob), cp);
665 cl_page_disown(env, io, page);
667 /* held in ll_cl_init() */
668 lu_ref_del(&page->cp_reference, "cl_io", io);
669 cl_page_put(env, page);
672 /* make sure the page list is contiguous */
673 static bool page_list_sanity_check(struct cl_object *obj,
674 struct cl_page_list *plist)
676 struct cl_page *page;
677 pgoff_t index = CL_PAGE_EOF;
679 cl_page_list_for_each(page, plist) {
680 struct ccc_page *cp = cl_object_page_slice(obj, page);
682 if (index == CL_PAGE_EOF) {
683 index = ccc_index(cp);
688 if (index == ccc_index(cp))
696 /* Return how many bytes have queued or written */
697 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
699 struct cl_object *obj = io->ci_obj;
700 struct inode *inode = ccc_object_inode(obj);
701 struct ccc_io *cio = ccc_env_io(env);
702 struct cl_page_list *queue = &cio->u.write.cui_queue;
703 struct cl_page *page;
706 unsigned int npages = cio->u.write.cui_queue.pl_nr;
712 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
713 npages, cio->u.write.cui_from, cio->u.write.cui_to);
715 LASSERT(page_list_sanity_check(obj, queue));
717 /* submit IO with async write */
718 rc = cl_io_commit_async(env, io, queue,
719 cio->u.write.cui_from, cio->u.write.cui_to,
720 write_commit_callback);
721 npages -= queue->pl_nr; /* already committed pages */
723 /* calculate how many bytes were written */
724 bytes = npages << PAGE_SHIFT;
727 bytes -= cio->u.write.cui_from;
728 if (queue->pl_nr == 0) /* last page */
729 bytes -= PAGE_SIZE - cio->u.write.cui_to;
730 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
732 cio->u.write.cui_written += bytes;
734 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
735 npages, bytes, cio->u.write.cui_written);
737 /* the first page must have been written. */
738 cio->u.write.cui_from = 0;
740 LASSERT(page_list_sanity_check(obj, queue));
741 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
743 /* out of quota, try sync write */
744 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
745 rc = vvp_io_commit_sync(env, io, queue,
746 cio->u.write.cui_from,
747 cio->u.write.cui_to);
749 cio->u.write.cui_written += rc;
754 /* update inode size */
755 ll_merge_lvb(env, inode);
757 /* Now the pages in queue were failed to commit, discard them
758 * unless they were dirtied before. */
759 while (queue->pl_nr > 0) {
760 page = cl_page_list_first(queue);
761 cl_page_list_del(env, queue, page);
763 if (!PageDirty(cl_page_vmpage(page)))
764 cl_page_discard(env, io, page);
766 cl_page_disown(env, io, page);
768 /* held in ll_cl_init() */
769 lu_ref_del(&page->cp_reference, "cl_io", io);
770 cl_page_put(env, page);
772 cl_page_list_fini(env, queue);
777 static int vvp_io_write_start(const struct lu_env *env,
778 const struct cl_io_slice *ios)
780 struct ccc_io *cio = cl2ccc_io(env, ios);
781 struct cl_io *io = ios->cis_io;
782 struct cl_object *obj = io->ci_obj;
783 struct inode *inode = ccc_object_inode(obj);
785 loff_t pos = io->u.ci_wr.wr.crw_pos;
786 size_t cnt = io->u.ci_wr.wr.crw_count;
790 if (!can_populate_pages(env, io, inode))
793 if (cl_io_is_append(io)) {
795 * PARALLEL IO This has to be changed for parallel IO doing
796 * out-of-order writes.
798 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
799 cio->cui_iocb->ki_pos = pos;
801 LASSERT(cio->cui_iocb->ki_pos == pos);
804 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
806 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
809 result = generic_file_aio_write(cio->cui_iocb,
810 cio->cui_iov, cio->cui_nrsegs,
811 cio->cui_iocb->ki_pos);
813 result = vvp_io_write_commit(env, io);
814 if (cio->u.write.cui_written > 0) {
815 result = cio->u.write.cui_written;
816 io->ci_nob += result;
818 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
823 struct ll_inode_info *lli = ll_i2info(inode);
825 spin_lock(&lli->lli_lock);
826 lli->lli_flags |= LLIF_DATA_MODIFIED;
827 spin_unlock(&lli->lli_lock);
831 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
832 cio->cui_fd, pos, result, WRITE);
839 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
841 struct vm_fault *vmf = cfio->fault.ft_vmf;
843 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
844 cfio->fault.ft_flags_valid = 1;
847 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
848 vmf->virtual_address);
849 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
850 lock_page(vmf->page);
851 cfio->fault.ft_flags |= VM_FAULT_LOCKED;
854 cfio->ft_vmpage = vmf->page;
858 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
859 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
863 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
864 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
868 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
871 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
875 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
876 struct cl_page *page)
879 struct cl_object *clob = cl_io_top(io)->ci_obj;
881 set_page_dirty(page->cp_vmpage);
883 cp = cl2ccc_page(cl_object_page_slice(clob, page));
884 vvp_write_pending(cl2ccc(clob), cp);
887 static int vvp_io_fault_start(const struct lu_env *env,
888 const struct cl_io_slice *ios)
890 struct vvp_io *vio = cl2vvp_io(env, ios);
891 struct cl_io *io = ios->cis_io;
892 struct cl_object *obj = io->ci_obj;
893 struct inode *inode = ccc_object_inode(obj);
894 struct cl_fault_io *fio = &io->u.ci_fault;
895 struct vvp_fault_io *cfio = &vio->u.fault;
898 struct page *vmpage = NULL;
899 struct cl_page *page;
904 if (fio->ft_executable &&
905 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
907 " changed while waiting for the page fault lock\n",
908 PFID(lu_object_fid(&obj->co_lu)));
910 /* offset of the last byte on the page */
911 offset = cl_offset(obj, fio->ft_index + 1) - 1;
912 LASSERT(cl_index(obj, offset) == fio->ft_index);
913 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
917 /* must return locked page */
918 if (fio->ft_mkwrite) {
919 LASSERT(cfio->ft_vmpage != NULL);
920 lock_page(cfio->ft_vmpage);
922 result = vvp_io_kernel_fault(cfio);
927 vmpage = cfio->ft_vmpage;
928 LASSERT(PageLocked(vmpage));
930 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
931 ll_invalidate_page(vmpage);
933 size = i_size_read(inode);
934 /* Though we have already held a cl_lock upon this page, but
935 * it still can be truncated locally. */
936 if (unlikely((vmpage->mapping != inode->i_mapping) ||
937 (page_offset(vmpage) > size))) {
938 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
940 /* return +1 to stop cl_io_loop() and ll_fault() will catch
942 GOTO(out, result = +1);
945 last_index = cl_index(obj, size - 1);
947 if (fio->ft_mkwrite ) {
949 * Capture the size while holding the lli_trunc_sem from above
950 * we want to make sure that we complete the mkwrite action
951 * while holding this lock. We need to make sure that we are
952 * not past the end of the file.
954 if (last_index < fio->ft_index) {
956 "llite: mkwrite and truncate race happened: "
958 vmpage->mapping,fio->ft_index,last_index);
960 * We need to return if we are
961 * passed the end of the file. This will propagate
962 * up the call stack to ll_page_mkwrite where
963 * we will return VM_FAULT_NOPAGE. Any non-negative
964 * value returned here will be silently
965 * converted to 0. If the vmpage->mapping is null
966 * the error code would be converted back to ENODATA
967 * in ll_page_mkwrite0. Thus we return -ENODATA
968 * to handle both cases
970 GOTO(out, result = -ENODATA);
974 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
976 GOTO(out, result = PTR_ERR(page));
978 /* if page is going to be written, we should add this page into cache
980 if (fio->ft_mkwrite) {
981 wait_on_page_writeback(vmpage);
982 if (!PageDirty(vmpage)) {
983 struct cl_page_list *plist = &io->ci_queue.c2_qin;
984 struct ccc_page *cp = cl_object_page_slice(obj, page);
987 /* vvp_page_assume() calls wait_on_page_writeback(). */
988 cl_page_assume(env, io, page);
990 cl_page_list_init(plist);
991 cl_page_list_add(plist, page);
994 if (last_index == ccc_index(cp))
995 to = size & ~CFS_PAGE_MASK;
997 /* Do not set Dirty bit here so that in case IO is
998 * started before the page is really made dirty, we
999 * still have chance to detect it. */
1000 result = cl_io_commit_async(env, io, plist, 0, to,
1001 mkwrite_commit_callback);
1002 LASSERT(cl_page_is_owned(page, io));
1003 cl_page_list_fini(env, plist);
1007 cl_page_discard(env, io, page);
1008 cl_page_disown(env, io, page);
1010 cl_page_put(env, page);
1012 /* we're in big trouble, what can we do now? */
1013 if (result == -EDQUOT)
1017 cl_page_disown(env, io, page);
1022 * The ft_index is only used in the case of
1023 * a mkwrite action. We need to check
1024 * our assertions are correct, since
1025 * we should have caught this above
1027 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1028 if (fio->ft_index == last_index)
1030 * Last page is mapped partially.
1032 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1034 fio->ft_nob = cl_page_size(obj);
1036 lu_ref_add(&page->cp_reference, "fault", io);
1037 fio->ft_page = page;
1041 /* return unlocked vmpage to avoid deadlocking */
1043 unlock_page(vmpage);
1044 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1048 static int vvp_io_fsync_start(const struct lu_env *env,
1049 const struct cl_io_slice *ios)
1051 /* we should mark TOWRITE bit to each dirty page in radix tree to
1052 * verify pages have been written, but this is difficult because of
1057 static int vvp_io_read_page(const struct lu_env *env,
1058 const struct cl_io_slice *ios,
1059 const struct cl_page_slice *slice)
1061 struct cl_io *io = ios->cis_io;
1062 struct ccc_page *cp = cl2ccc_page(slice);
1063 struct cl_page *page = slice->cpl_page;
1064 struct inode *inode = ccc_object_inode(slice->cpl_obj);
1065 struct ll_sb_info *sbi = ll_i2sbi(inode);
1066 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1067 struct ll_readahead_state *ras = &fd->fd_ras;
1068 struct cl_2queue *queue = &io->ci_queue;
1072 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1073 sbi->ll_ra_info.ra_max_pages > 0)
1074 ras_update(sbi, inode, ras, ccc_index(cp),
1075 cp->cpg_defer_uptodate);
1077 if (cp->cpg_defer_uptodate) {
1078 cp->cpg_ra_used = 1;
1079 cl_page_export(env, page, 1);
1083 * Add page into the queue even when it is marked uptodate above.
1084 * this will unlock it automatically as part of cl_page_list_disown().
1086 cl_2queue_add(queue, page);
1087 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1088 sbi->ll_ra_info.ra_max_pages > 0)
1089 ll_readahead(env, io, &queue->c2_qin, ras,
1090 cp->cpg_defer_uptodate);
1095 static const struct cl_io_operations vvp_io_ops = {
1098 .cio_fini = vvp_io_read_fini,
1099 .cio_lock = vvp_io_read_lock,
1100 .cio_start = vvp_io_read_start,
1101 .cio_advance = ccc_io_advance
1104 .cio_fini = vvp_io_fini,
1105 .cio_iter_init = vvp_io_write_iter_init,
1106 .cio_iter_fini = vvp_io_write_iter_fini,
1107 .cio_lock = vvp_io_write_lock,
1108 .cio_start = vvp_io_write_start,
1109 .cio_advance = ccc_io_advance
1112 .cio_fini = vvp_io_setattr_fini,
1113 .cio_iter_init = vvp_io_setattr_iter_init,
1114 .cio_lock = vvp_io_setattr_lock,
1115 .cio_start = vvp_io_setattr_start,
1116 .cio_end = vvp_io_setattr_end
1119 .cio_fini = vvp_io_fault_fini,
1120 .cio_iter_init = vvp_io_fault_iter_init,
1121 .cio_lock = vvp_io_fault_lock,
1122 .cio_start = vvp_io_fault_start,
1123 .cio_end = ccc_io_end
1126 .cio_start = vvp_io_fsync_start,
1127 .cio_fini = vvp_io_fini
1130 .cio_fini = vvp_io_fini
1133 .cio_read_page = vvp_io_read_page,
1136 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1139 struct vvp_io *vio = vvp_env_io(env);
1140 struct ccc_io *cio = ccc_env_io(env);
1141 struct inode *inode = ccc_object_inode(obj);
1144 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1147 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1148 "restore needed %d\n",
1149 PFID(lu_object_fid(&obj->co_lu)),
1150 io->ci_ignore_layout, io->ci_verify_layout,
1151 cio->cui_layout_gen, io->ci_restore_needed);
1153 CL_IO_SLICE_CLEAN(cio, cui_cl);
1154 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1155 vio->cui_ra_window_set = 0;
1157 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1159 struct ll_inode_info *lli = ll_i2info(inode);
1161 count = io->u.ci_rw.crw_count;
1162 /* "If nbyte is 0, read() will return 0 and have no other
1163 * results." -- Single Unix Spec */
1167 cio->cui_tot_count = count;
1168 cio->cui_tot_nrsegs = 0;
1171 /* for read/write, we store the jobid in the inode, and
1172 * it'll be fetched by osc when building RPC.
1174 * it's not accurate if the file is shared by different
1177 lustre_get_jobid(lli->lli_jobid);
1178 } else if (io->ci_type == CIT_SETATTR) {
1179 if (!cl_io_is_trunc(io))
1180 io->ci_lockreq = CILR_MANDATORY;
1183 /* ignore layout change for generic CIT_MISC but not for glimpse.
1184 * io context for glimpse must set ci_verify_layout to true,
1185 * see cl_glimpse_size0() for details. */
1186 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1187 io->ci_ignore_layout = 1;
1189 /* Enqueue layout lock and get layout version. We need to do this
1190 * even for operations requiring to open file, such as read and write,
1191 * because it might not grant layout lock in IT_OPEN. */
1192 if (result == 0 && !io->ci_ignore_layout) {
1193 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1194 if (result == -ENOENT)
1195 /* If the inode on MDS has been removed, but the objects
1196 * on OSTs haven't been destroyed (async unlink), layout
1197 * fetch will return -ENOENT, we'd ingore this error
1198 * and continue with dirty flush. LU-3230. */
1201 CERROR("%s: refresh file layout " DFID " error %d.\n",
1202 ll_get_fsname(inode->i_sb, NULL, 0),
1203 PFID(lu_object_fid(&obj->co_lu)), result);
1209 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1210 const struct cl_io_slice *slice)
1212 /* Caling just for assertion */
1213 cl2ccc_io(env, slice);
1214 return vvp_env_io(env);