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 sendfile() / 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 (lli->lli_layout_gen != 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 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,
302 if (vma->vm_end - addr >= count)
305 count -= vma->vm_end - addr;
308 up_read(&mm->mmap_sem);
313 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
314 enum cl_lock_mode mode, loff_t start, loff_t end)
316 struct ccc_io *cio = ccc_env_io(env);
320 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
323 ccc_io_update_iov(env, cio, io);
325 if (io->u.ci_rw.crw_nonblock)
326 ast_flags |= CEF_NONBLOCK;
327 result = vvp_mmap_locks(env, cio, io);
329 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
333 static int vvp_io_read_lock(const struct lu_env *env,
334 const struct cl_io_slice *ios)
336 struct cl_io *io = ios->cis_io;
337 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
341 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
342 if (lli->lli_has_smd) /* lsm-less file doesn't need to lock */
343 result = vvp_io_rw_lock(env, io, CLM_READ,
344 io->u.ci_rd.rd.crw_pos,
345 io->u.ci_rd.rd.crw_pos +
346 io->u.ci_rd.rd.crw_count - 1);
352 static int vvp_io_fault_lock(const struct lu_env *env,
353 const struct cl_io_slice *ios)
355 struct cl_io *io = ios->cis_io;
356 struct vvp_io *vio = cl2vvp_io(env, ios);
358 * XXX LDLM_FL_CBPENDING
360 return ccc_io_one_lock_index
361 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
362 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
365 static int vvp_io_write_lock(const struct lu_env *env,
366 const struct cl_io_slice *ios)
368 struct cl_io *io = ios->cis_io;
372 if (io->u.ci_wr.wr_append) {
374 end = OBD_OBJECT_EOF;
376 start = io->u.ci_wr.wr.crw_pos;
377 end = start + io->u.ci_wr.wr.crw_count - 1;
379 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
382 static int vvp_io_setattr_iter_init(const struct lu_env *env,
383 const struct cl_io_slice *ios)
389 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
391 * Handles "lockless io" mode when extent locking is done by server.
393 static int vvp_io_setattr_lock(const struct lu_env *env,
394 const struct cl_io_slice *ios)
396 struct ccc_io *cio = ccc_env_io(env);
397 struct cl_io *io = ios->cis_io;
401 if (cl_io_is_trunc(io)) {
402 new_size = io->u.ci_setattr.sa_attr.lvb_size;
404 enqflags = CEF_DISCARD_DATA;
406 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
407 io->u.ci_setattr.sa_attr.lvb_ctime) ||
408 (io->u.ci_setattr.sa_attr.lvb_atime >=
409 io->u.ci_setattr.sa_attr.lvb_ctime))
413 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
414 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
415 new_size, OBD_OBJECT_EOF);
418 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
423 * Only ll_inode_size_lock is taken at this level.
425 ll_inode_size_lock(inode);
426 result = inode_newsize_ok(inode, size);
428 ll_inode_size_unlock(inode);
431 i_size_write(inode, size);
433 ll_truncate_pagecache(inode, size);
434 ll_inode_size_unlock(inode);
438 static int vvp_io_setattr_trunc(const struct lu_env *env,
439 const struct cl_io_slice *ios,
440 struct inode *inode, loff_t size)
442 inode_dio_wait(inode);
446 static int vvp_io_setattr_time(const struct lu_env *env,
447 const struct cl_io_slice *ios)
449 struct cl_io *io = ios->cis_io;
450 struct cl_object *obj = io->ci_obj;
451 struct cl_attr *attr = ccc_env_thread_attr(env);
453 unsigned valid = CAT_CTIME;
455 cl_object_attr_lock(obj);
456 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
457 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
458 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
461 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
462 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
465 result = cl_object_attr_set(env, obj, attr, valid);
466 cl_object_attr_unlock(obj);
471 static int vvp_io_setattr_start(const struct lu_env *env,
472 const struct cl_io_slice *ios)
474 struct cl_io *io = ios->cis_io;
475 struct inode *inode = ccc_object_inode(io->ci_obj);
478 mutex_lock(&inode->i_mutex);
479 if (cl_io_is_trunc(io))
480 result = vvp_io_setattr_trunc(env, ios, inode,
481 io->u.ci_setattr.sa_attr.lvb_size);
483 result = vvp_io_setattr_time(env, ios);
487 static void vvp_io_setattr_end(const struct lu_env *env,
488 const struct cl_io_slice *ios)
490 struct cl_io *io = ios->cis_io;
491 struct inode *inode = ccc_object_inode(io->ci_obj);
493 if (cl_io_is_trunc(io)) {
494 /* Truncate in memory pages - they must be clean pages
495 * because osc has already notified to destroy osc_extents. */
496 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
497 inode_dio_write_done(inode);
499 mutex_unlock(&inode->i_mutex);
502 static void vvp_io_setattr_fini(const struct lu_env *env,
503 const struct cl_io_slice *ios)
505 vvp_io_fini(env, ios);
508 static ssize_t lustre_generic_file_read(struct file *file,
509 struct ccc_io *vio, loff_t *ppos)
511 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
512 vio->cui_nrsegs, *ppos);
515 static ssize_t lustre_generic_file_write(struct file *file,
516 struct ccc_io *vio, loff_t *ppos)
518 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
519 vio->cui_nrsegs, *ppos);
522 static int vvp_io_read_start(const struct lu_env *env,
523 const struct cl_io_slice *ios)
525 struct vvp_io *vio = cl2vvp_io(env, ios);
526 struct ccc_io *cio = cl2ccc_io(env, ios);
527 struct cl_io *io = ios->cis_io;
528 struct cl_object *obj = io->ci_obj;
529 struct inode *inode = ccc_object_inode(obj);
530 struct ll_ra_read *bead = &vio->cui_bead;
531 struct file *file = cio->cui_fd->fd_file;
534 loff_t pos = io->u.ci_rd.rd.crw_pos;
535 long cnt = io->u.ci_rd.rd.crw_count;
536 long tot = cio->cui_tot_count;
539 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
541 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
543 if (!can_populate_pages(env, io, inode))
546 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
549 else if (exceed != 0)
552 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
553 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
554 inode->i_ino, cnt, pos, i_size_read(inode));
556 /* turn off the kernel's read-ahead */
557 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
559 /* initialize read-ahead window once per syscall */
560 if (!vio->cui_ra_window_set) {
561 vio->cui_ra_window_set = 1;
562 bead->lrr_start = cl_index(obj, pos);
563 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
564 ll_ra_read_in(file, bead);
569 switch (vio->cui_io_subtype) {
571 result = lustre_generic_file_read(file, cio, &pos);
574 result = generic_file_splice_read(file, &pos,
575 vio->u.splice.cui_pipe, cnt,
576 vio->u.splice.cui_flags);
577 /* LU-1109: do splice read stripe by stripe otherwise if it
578 * may make nfsd stuck if this read occupied all internal pipe
583 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
591 io->ci_nob += result;
592 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
600 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
602 struct vvp_io *vio = cl2vvp_io(env, ios);
603 struct ccc_io *cio = cl2ccc_io(env, ios);
605 if (vio->cui_ra_window_set)
606 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
608 vvp_io_fini(env, ios);
611 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
612 struct cl_page_list *plist, int from, int to)
614 struct cl_2queue *queue = &io->ci_queue;
615 struct cl_page *page;
616 unsigned int bytes = 0;
620 if (plist->pl_nr == 0)
623 if (from > 0 || to != PAGE_SIZE) {
624 page = cl_page_list_first(plist);
625 if (plist->pl_nr == 1) {
626 cl_page_clip(env, page, from, to);
627 } else if (from > 0) {
628 cl_page_clip(env, page, from, PAGE_SIZE);
630 page = cl_page_list_last(plist);
631 cl_page_clip(env, page, 0, to);
635 cl_2queue_init(queue);
636 cl_page_list_splice(plist, &queue->c2_qin);
637 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
639 /* plist is not sorted any more */
640 cl_page_list_splice(&queue->c2_qin, plist);
641 cl_page_list_splice(&queue->c2_qout, plist);
642 cl_2queue_fini(env, queue);
645 /* calculate bytes */
646 bytes = plist->pl_nr << PAGE_SHIFT;
647 bytes -= from + PAGE_SIZE - to;
649 while (plist->pl_nr > 0) {
650 page = cl_page_list_first(plist);
651 cl_page_list_del(env, plist, page);
653 cl_page_clip(env, page, 0, PAGE_SIZE);
655 SetPageUptodate(cl_page_vmpage(page));
656 cl_page_disown(env, io, page);
658 /* held in ll_cl_init() */
659 lu_ref_del(&page->cp_reference, "cl_io", io);
660 cl_page_put(env, page);
664 RETURN(bytes > 0 ? bytes : rc);
667 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
668 struct cl_page *page)
671 struct page *vmpage = page->cp_vmpage;
672 struct cl_object *clob = cl_io_top(io)->ci_obj;
674 SetPageUptodate(vmpage);
675 set_page_dirty(vmpage);
677 cp = cl2ccc_page(cl_object_page_slice(clob, page));
678 vvp_write_pending(cl2ccc(clob), cp);
680 cl_page_disown(env, io, page);
682 /* held in ll_cl_init() */
683 lu_ref_del(&page->cp_reference, "cl_io", io);
684 cl_page_put(env, page);
687 /* make sure the page list is contiguous */
688 static bool page_list_sanity_check(struct cl_object *obj,
689 struct cl_page_list *plist)
691 struct cl_page *page;
692 pgoff_t index = CL_PAGE_EOF;
694 cl_page_list_for_each(page, plist) {
695 struct ccc_page *cp = cl_object_page_slice(obj, page);
697 if (index == CL_PAGE_EOF) {
698 index = ccc_index(cp);
703 if (index == ccc_index(cp))
711 /* Return how many bytes have queued or written */
712 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
714 struct cl_object *obj = io->ci_obj;
715 struct inode *inode = ccc_object_inode(obj);
716 struct ccc_io *cio = ccc_env_io(env);
717 struct cl_page_list *queue = &cio->u.write.cui_queue;
718 struct cl_page *page;
721 unsigned int npages = cio->u.write.cui_queue.pl_nr;
727 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
728 npages, cio->u.write.cui_from, cio->u.write.cui_to);
730 LASSERT(page_list_sanity_check(obj, queue));
732 /* submit IO with async write */
733 rc = cl_io_commit_async(env, io, queue,
734 cio->u.write.cui_from, cio->u.write.cui_to,
735 write_commit_callback);
736 npages -= queue->pl_nr; /* already committed pages */
738 /* calculate how many bytes were written */
739 bytes = npages << PAGE_SHIFT;
742 bytes -= cio->u.write.cui_from;
743 if (queue->pl_nr == 0) /* last page */
744 bytes -= PAGE_SIZE - cio->u.write.cui_to;
745 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
747 cio->u.write.cui_written += bytes;
749 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
750 npages, bytes, cio->u.write.cui_written);
752 /* the first page must have been written. */
753 cio->u.write.cui_from = 0;
755 LASSERT(page_list_sanity_check(obj, queue));
756 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
758 /* out of quota, try sync write */
759 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
760 rc = vvp_io_commit_sync(env, io, queue,
761 cio->u.write.cui_from,
762 cio->u.write.cui_to);
764 cio->u.write.cui_written += rc;
769 /* update inode size */
770 ll_merge_lvb(env, inode);
772 /* Now the pages in queue were failed to commit, discard them
773 * unless they were dirtied before. */
774 while (queue->pl_nr > 0) {
775 page = cl_page_list_first(queue);
776 cl_page_list_del(env, queue, page);
778 if (!PageDirty(cl_page_vmpage(page)))
779 cl_page_discard(env, io, page);
781 cl_page_disown(env, io, page);
783 /* held in ll_cl_init() */
784 lu_ref_del(&page->cp_reference, "cl_io", io);
785 cl_page_put(env, page);
787 cl_page_list_fini(env, queue);
792 static int vvp_io_write_start(const struct lu_env *env,
793 const struct cl_io_slice *ios)
795 struct ccc_io *cio = cl2ccc_io(env, ios);
796 struct cl_io *io = ios->cis_io;
797 struct cl_object *obj = io->ci_obj;
798 struct inode *inode = ccc_object_inode(obj);
799 struct file *file = cio->cui_fd->fd_file;
801 loff_t pos = io->u.ci_wr.wr.crw_pos;
802 size_t cnt = io->u.ci_wr.wr.crw_count;
806 if (!can_populate_pages(env, io, inode))
809 if (cl_io_is_append(io)) {
811 * PARALLEL IO This has to be changed for parallel IO doing
812 * out-of-order writes.
814 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
815 cio->cui_iocb->ki_pos = pos;
818 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
820 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
823 result = lustre_generic_file_write(file, cio, &pos);
826 result = vvp_io_write_commit(env, io);
827 if (cio->u.write.cui_written > 0) {
828 result = cio->u.write.cui_written;
829 io->ci_nob += result;
831 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
836 struct ll_inode_info *lli = ll_i2info(inode);
838 spin_lock(&lli->lli_lock);
839 lli->lli_flags |= LLIF_DATA_MODIFIED;
840 spin_unlock(&lli->lli_lock);
844 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
845 cio->cui_fd, pos, result, WRITE);
852 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
854 struct vm_fault *vmf = cfio->fault.ft_vmf;
856 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
859 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
860 vmf->virtual_address);
861 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
862 lock_page(vmf->page);
863 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
866 cfio->ft_vmpage = vmf->page;
870 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
871 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
875 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
876 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
880 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
883 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
887 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
888 struct cl_page *page)
891 struct cl_object *clob = cl_io_top(io)->ci_obj;
893 set_page_dirty(page->cp_vmpage);
895 cp = cl2ccc_page(cl_object_page_slice(clob, page));
896 vvp_write_pending(cl2ccc(clob), cp);
899 static int vvp_io_fault_start(const struct lu_env *env,
900 const struct cl_io_slice *ios)
902 struct vvp_io *vio = cl2vvp_io(env, ios);
903 struct cl_io *io = ios->cis_io;
904 struct cl_object *obj = io->ci_obj;
905 struct inode *inode = ccc_object_inode(obj);
906 struct cl_fault_io *fio = &io->u.ci_fault;
907 struct vvp_fault_io *cfio = &vio->u.fault;
910 struct page *vmpage = NULL;
911 struct cl_page *page;
916 if (fio->ft_executable &&
917 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
919 " changed while waiting for the page fault lock\n",
920 PFID(lu_object_fid(&obj->co_lu)));
922 /* offset of the last byte on the page */
923 offset = cl_offset(obj, fio->ft_index + 1) - 1;
924 LASSERT(cl_index(obj, offset) == fio->ft_index);
925 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
929 /* must return locked page */
930 if (fio->ft_mkwrite) {
931 LASSERT(cfio->ft_vmpage != NULL);
932 lock_page(cfio->ft_vmpage);
934 result = vvp_io_kernel_fault(cfio);
939 vmpage = cfio->ft_vmpage;
940 LASSERT(PageLocked(vmpage));
942 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
943 ll_invalidate_page(vmpage);
945 size = i_size_read(inode);
946 /* Though we have already held a cl_lock upon this page, but
947 * it still can be truncated locally. */
948 if (unlikely((vmpage->mapping != inode->i_mapping) ||
949 (page_offset(vmpage) > size))) {
950 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
952 /* return +1 to stop cl_io_loop() and ll_fault() will catch
954 GOTO(out, result = +1);
957 last_index = cl_index(obj, size - 1);
959 if (fio->ft_mkwrite ) {
961 * Capture the size while holding the lli_trunc_sem from above
962 * we want to make sure that we complete the mkwrite action
963 * while holding this lock. We need to make sure that we are
964 * not past the end of the file.
966 if (last_index < fio->ft_index) {
968 "llite: mkwrite and truncate race happened: "
970 vmpage->mapping,fio->ft_index,last_index);
972 * We need to return if we are
973 * passed the end of the file. This will propagate
974 * up the call stack to ll_page_mkwrite where
975 * we will return VM_FAULT_NOPAGE. Any non-negative
976 * value returned here will be silently
977 * converted to 0. If the vmpage->mapping is null
978 * the error code would be converted back to ENODATA
979 * in ll_page_mkwrite0. Thus we return -ENODATA
980 * to handle both cases
982 GOTO(out, result = -ENODATA);
986 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
988 GOTO(out, result = PTR_ERR(page));
990 /* if page is going to be written, we should add this page into cache
992 if (fio->ft_mkwrite) {
993 wait_on_page_writeback(vmpage);
994 if (!PageDirty(vmpage)) {
995 struct cl_page_list *plist = &io->ci_queue.c2_qin;
996 struct ccc_page *cp = cl_object_page_slice(obj, page);
999 /* vvp_page_assume() calls wait_on_page_writeback(). */
1000 cl_page_assume(env, io, page);
1002 cl_page_list_init(plist);
1003 cl_page_list_add(plist, page);
1006 if (last_index == ccc_index(cp))
1007 to = size & ~CFS_PAGE_MASK;
1009 /* Do not set Dirty bit here so that in case IO is
1010 * started before the page is really made dirty, we
1011 * still have chance to detect it. */
1012 result = cl_io_commit_async(env, io, plist, 0, to,
1013 mkwrite_commit_callback);
1014 LASSERT(cl_page_is_owned(page, io));
1015 cl_page_list_fini(env, plist);
1019 cl_page_discard(env, io, page);
1020 cl_page_disown(env, io, page);
1022 cl_page_put(env, page);
1024 /* we're in big trouble, what can we do now? */
1025 if (result == -EDQUOT)
1029 cl_page_disown(env, io, page);
1034 * The ft_index is only used in the case of
1035 * a mkwrite action. We need to check
1036 * our assertions are correct, since
1037 * we should have caught this above
1039 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1040 if (fio->ft_index == last_index)
1042 * Last page is mapped partially.
1044 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1046 fio->ft_nob = cl_page_size(obj);
1048 lu_ref_add(&page->cp_reference, "fault", io);
1049 fio->ft_page = page;
1053 /* return unlocked vmpage to avoid deadlocking */
1055 unlock_page(vmpage);
1056 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1060 static int vvp_io_fsync_start(const struct lu_env *env,
1061 const struct cl_io_slice *ios)
1063 /* we should mark TOWRITE bit to each dirty page in radix tree to
1064 * verify pages have been written, but this is difficult because of
1069 static int vvp_io_read_page(const struct lu_env *env,
1070 const struct cl_io_slice *ios,
1071 const struct cl_page_slice *slice)
1073 struct cl_io *io = ios->cis_io;
1074 struct ccc_page *cp = cl2ccc_page(slice);
1075 struct cl_page *page = slice->cpl_page;
1076 struct inode *inode = ccc_object_inode(slice->cpl_obj);
1077 struct ll_sb_info *sbi = ll_i2sbi(inode);
1078 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1079 struct ll_readahead_state *ras = &fd->fd_ras;
1080 struct cl_2queue *queue = &io->ci_queue;
1084 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1085 sbi->ll_ra_info.ra_max_pages > 0)
1086 ras_update(sbi, inode, ras, ccc_index(cp),
1087 cp->cpg_defer_uptodate);
1089 if (cp->cpg_defer_uptodate) {
1090 cp->cpg_ra_used = 1;
1091 cl_page_export(env, page, 1);
1095 * Add page into the queue even when it is marked uptodate above.
1096 * this will unlock it automatically as part of cl_page_list_disown().
1098 cl_2queue_add(queue, page);
1099 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1100 sbi->ll_ra_info.ra_max_pages > 0)
1101 ll_readahead(env, io, &queue->c2_qin, ras,
1102 cp->cpg_defer_uptodate);
1107 static const struct cl_io_operations vvp_io_ops = {
1110 .cio_fini = vvp_io_read_fini,
1111 .cio_lock = vvp_io_read_lock,
1112 .cio_start = vvp_io_read_start,
1113 .cio_advance = ccc_io_advance
1116 .cio_fini = vvp_io_fini,
1117 .cio_iter_init = vvp_io_write_iter_init,
1118 .cio_iter_fini = vvp_io_write_iter_fini,
1119 .cio_lock = vvp_io_write_lock,
1120 .cio_start = vvp_io_write_start,
1121 .cio_advance = ccc_io_advance
1124 .cio_fini = vvp_io_setattr_fini,
1125 .cio_iter_init = vvp_io_setattr_iter_init,
1126 .cio_lock = vvp_io_setattr_lock,
1127 .cio_start = vvp_io_setattr_start,
1128 .cio_end = vvp_io_setattr_end
1131 .cio_fini = vvp_io_fault_fini,
1132 .cio_iter_init = vvp_io_fault_iter_init,
1133 .cio_lock = vvp_io_fault_lock,
1134 .cio_start = vvp_io_fault_start,
1135 .cio_end = ccc_io_end
1138 .cio_start = vvp_io_fsync_start,
1139 .cio_fini = vvp_io_fini
1142 .cio_fini = vvp_io_fini
1145 .cio_read_page = vvp_io_read_page,
1148 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1151 struct vvp_io *vio = vvp_env_io(env);
1152 struct ccc_io *cio = ccc_env_io(env);
1153 struct inode *inode = ccc_object_inode(obj);
1156 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1159 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1160 "restore needed %d\n",
1161 PFID(lu_object_fid(&obj->co_lu)),
1162 io->ci_ignore_layout, io->ci_verify_layout,
1163 cio->cui_layout_gen, io->ci_restore_needed);
1165 CL_IO_SLICE_CLEAN(cio, cui_cl);
1166 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1167 vio->cui_ra_window_set = 0;
1169 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1171 struct ll_inode_info *lli = ll_i2info(inode);
1173 count = io->u.ci_rw.crw_count;
1174 /* "If nbyte is 0, read() will return 0 and have no other
1175 * results." -- Single Unix Spec */
1179 cio->cui_tot_count = count;
1180 cio->cui_tot_nrsegs = 0;
1183 /* for read/write, we store the jobid in the inode, and
1184 * it'll be fetched by osc when building RPC.
1186 * it's not accurate if the file is shared by different
1189 lustre_get_jobid(lli->lli_jobid);
1190 } else if (io->ci_type == CIT_SETATTR) {
1191 if (!cl_io_is_trunc(io))
1192 io->ci_lockreq = CILR_MANDATORY;
1195 /* ignore layout change for generic CIT_MISC but not for glimpse.
1196 * io context for glimpse must set ci_verify_layout to true,
1197 * see cl_glimpse_size0() for details. */
1198 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1199 io->ci_ignore_layout = 1;
1201 /* Enqueue layout lock and get layout version. We need to do this
1202 * even for operations requiring to open file, such as read and write,
1203 * because it might not grant layout lock in IT_OPEN. */
1204 if (result == 0 && !io->ci_ignore_layout) {
1205 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1206 if (result == -ENOENT)
1207 /* If the inode on MDS has been removed, but the objects
1208 * on OSTs haven't been destroyed (async unlink), layout
1209 * fetch will return -ENOENT, we'd ingore this error
1210 * and continue with dirty flush. LU-3230. */
1213 CERROR("%s: refresh file layout " DFID " error %d.\n",
1214 ll_get_fsname(inode->i_sb, NULL, 0),
1215 PFID(lu_object_fid(&obj->co_lu)), result);
1221 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1222 const struct cl_io_slice *slice)
1224 /* Caling just for assertion */
1225 cl2ccc_io(env, slice);
1226 return vvp_env_io(env);