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)
424 * Only ll_inode_size_lock is taken at this level.
426 ll_inode_size_lock(inode);
427 result = inode_newsize_ok(inode, size);
429 ll_inode_size_unlock(inode);
432 oldsize = inode->i_size;
433 i_size_write(inode, size);
435 truncate_pagecache(inode, oldsize, size);
436 ll_inode_size_unlock(inode);
440 static int vvp_io_setattr_trunc(const struct lu_env *env,
441 const struct cl_io_slice *ios,
442 struct inode *inode, loff_t size)
444 inode_dio_wait(inode);
448 static int vvp_io_setattr_time(const struct lu_env *env,
449 const struct cl_io_slice *ios)
451 struct cl_io *io = ios->cis_io;
452 struct cl_object *obj = io->ci_obj;
453 struct cl_attr *attr = ccc_env_thread_attr(env);
455 unsigned valid = CAT_CTIME;
457 cl_object_attr_lock(obj);
458 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
459 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
460 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
463 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
464 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
467 result = cl_object_attr_set(env, obj, attr, valid);
468 cl_object_attr_unlock(obj);
473 static int vvp_io_setattr_start(const struct lu_env *env,
474 const struct cl_io_slice *ios)
476 struct cl_io *io = ios->cis_io;
477 struct inode *inode = ccc_object_inode(io->ci_obj);
480 mutex_lock(&inode->i_mutex);
481 if (cl_io_is_trunc(io))
482 result = vvp_io_setattr_trunc(env, ios, inode,
483 io->u.ci_setattr.sa_attr.lvb_size);
485 result = vvp_io_setattr_time(env, ios);
489 static void vvp_io_setattr_end(const struct lu_env *env,
490 const struct cl_io_slice *ios)
492 struct cl_io *io = ios->cis_io;
493 struct inode *inode = ccc_object_inode(io->ci_obj);
495 if (cl_io_is_trunc(io)) {
496 /* Truncate in memory pages - they must be clean pages
497 * because osc has already notified to destroy osc_extents. */
498 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
499 inode_dio_write_done(inode);
501 mutex_unlock(&inode->i_mutex);
504 static void vvp_io_setattr_fini(const struct lu_env *env,
505 const struct cl_io_slice *ios)
507 vvp_io_fini(env, ios);
510 static ssize_t lustre_generic_file_read(struct file *file,
511 struct ccc_io *vio, loff_t *ppos)
513 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
514 vio->cui_nrsegs, *ppos);
517 static ssize_t lustre_generic_file_write(struct file *file,
518 struct ccc_io *vio, loff_t *ppos)
520 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
521 vio->cui_nrsegs, *ppos);
524 static int vvp_io_read_start(const struct lu_env *env,
525 const struct cl_io_slice *ios)
527 struct vvp_io *vio = cl2vvp_io(env, ios);
528 struct ccc_io *cio = cl2ccc_io(env, ios);
529 struct cl_io *io = ios->cis_io;
530 struct cl_object *obj = io->ci_obj;
531 struct inode *inode = ccc_object_inode(obj);
532 struct ll_ra_read *bead = &vio->cui_bead;
533 struct file *file = cio->cui_fd->fd_file;
536 loff_t pos = io->u.ci_rd.rd.crw_pos;
537 long cnt = io->u.ci_rd.rd.crw_count;
538 long tot = cio->cui_tot_count;
541 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
543 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
545 if (!can_populate_pages(env, io, inode))
548 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
551 else if (exceed != 0)
554 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
555 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
556 inode->i_ino, cnt, pos, i_size_read(inode));
558 /* turn off the kernel's read-ahead */
559 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
561 /* initialize read-ahead window once per syscall */
562 if (!vio->cui_ra_window_set) {
563 vio->cui_ra_window_set = 1;
564 bead->lrr_start = cl_index(obj, pos);
565 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
566 ll_ra_read_in(file, bead);
571 switch (vio->cui_io_subtype) {
573 result = lustre_generic_file_read(file, cio, &pos);
576 result = generic_file_splice_read(file, &pos,
577 vio->u.splice.cui_pipe, cnt,
578 vio->u.splice.cui_flags);
579 /* LU-1109: do splice read stripe by stripe otherwise if it
580 * may make nfsd stuck if this read occupied all internal pipe
585 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
593 io->ci_nob += result;
594 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
602 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
604 struct vvp_io *vio = cl2vvp_io(env, ios);
605 struct ccc_io *cio = cl2ccc_io(env, ios);
607 if (vio->cui_ra_window_set)
608 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
610 vvp_io_fini(env, ios);
613 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
614 struct cl_page_list *plist, int from, int to)
616 struct cl_2queue *queue = &io->ci_queue;
617 struct cl_page *page;
618 unsigned int bytes = 0;
622 if (plist->pl_nr == 0)
625 if (from > 0 || to != PAGE_SIZE) {
626 page = cl_page_list_first(plist);
627 if (plist->pl_nr == 1) {
628 cl_page_clip(env, page, from, to);
629 } else if (from > 0) {
630 cl_page_clip(env, page, from, PAGE_SIZE);
632 page = cl_page_list_last(plist);
633 cl_page_clip(env, page, 0, to);
637 cl_2queue_init(queue);
638 cl_page_list_splice(plist, &queue->c2_qin);
639 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
641 /* plist is not sorted any more */
642 cl_page_list_splice(&queue->c2_qin, plist);
643 cl_page_list_splice(&queue->c2_qout, plist);
644 cl_2queue_fini(env, queue);
647 /* calculate bytes */
648 bytes = plist->pl_nr << PAGE_SHIFT;
649 bytes -= from + PAGE_SIZE - to;
651 while (plist->pl_nr > 0) {
652 page = cl_page_list_first(plist);
653 cl_page_list_del(env, plist, page);
655 cl_page_clip(env, page, 0, PAGE_SIZE);
657 SetPageUptodate(cl_page_vmpage(page));
658 cl_page_disown(env, io, page);
660 /* held in ll_cl_init() */
661 lu_ref_del(&page->cp_reference, "cl_io", io);
662 cl_page_put(env, page);
666 RETURN(bytes > 0 ? bytes : rc);
669 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
670 struct cl_page *page)
673 struct page *vmpage = page->cp_vmpage;
674 struct cl_object *clob = cl_io_top(io)->ci_obj;
676 SetPageUptodate(vmpage);
677 set_page_dirty(vmpage);
679 cp = cl2ccc_page(cl_object_page_slice(clob, page));
680 vvp_write_pending(cl2ccc(clob), cp);
682 cl_page_disown(env, io, page);
684 /* held in ll_cl_init() */
685 lu_ref_del(&page->cp_reference, "cl_io", io);
686 cl_page_put(env, page);
689 /* make sure the page list is contiguous */
690 static bool page_list_sanity_check(struct cl_object *obj,
691 struct cl_page_list *plist)
693 struct cl_page *page;
694 pgoff_t index = CL_PAGE_EOF;
696 cl_page_list_for_each(page, plist) {
697 struct ccc_page *cp = cl_object_page_slice(obj, page);
699 if (index == CL_PAGE_EOF) {
700 index = ccc_index(cp);
705 if (index == ccc_index(cp))
713 /* Return how many bytes have queued or written */
714 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
716 struct cl_object *obj = io->ci_obj;
717 struct inode *inode = ccc_object_inode(obj);
718 struct ccc_io *cio = ccc_env_io(env);
719 struct cl_page_list *queue = &cio->u.write.cui_queue;
720 struct cl_page *page;
723 unsigned int npages = cio->u.write.cui_queue.pl_nr;
729 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
730 npages, cio->u.write.cui_from, cio->u.write.cui_to);
732 LASSERT(page_list_sanity_check(obj, queue));
734 /* submit IO with async write */
735 rc = cl_io_commit_async(env, io, queue,
736 cio->u.write.cui_from, cio->u.write.cui_to,
737 write_commit_callback);
738 npages -= queue->pl_nr; /* already committed pages */
740 /* calculate how many bytes were written */
741 bytes = npages << PAGE_SHIFT;
744 bytes -= cio->u.write.cui_from;
745 if (queue->pl_nr == 0) /* last page */
746 bytes -= PAGE_SIZE - cio->u.write.cui_to;
747 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
749 cio->u.write.cui_written += bytes;
751 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
752 npages, bytes, cio->u.write.cui_written);
754 /* the first page must have been written. */
755 cio->u.write.cui_from = 0;
757 LASSERT(page_list_sanity_check(obj, queue));
758 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
760 /* out of quota, try sync write */
761 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
762 rc = vvp_io_commit_sync(env, io, queue,
763 cio->u.write.cui_from,
764 cio->u.write.cui_to);
766 cio->u.write.cui_written += rc;
771 /* update inode size */
772 ll_merge_lvb(env, inode);
774 /* Now the pages in queue were failed to commit, discard them
775 * unless they were dirtied before. */
776 while (queue->pl_nr > 0) {
777 page = cl_page_list_first(queue);
778 cl_page_list_del(env, queue, page);
780 if (!PageDirty(cl_page_vmpage(page)))
781 cl_page_discard(env, io, page);
783 cl_page_disown(env, io, page);
785 /* held in ll_cl_init() */
786 lu_ref_del(&page->cp_reference, "cl_io", io);
787 cl_page_put(env, page);
789 cl_page_list_fini(env, queue);
794 static int vvp_io_write_start(const struct lu_env *env,
795 const struct cl_io_slice *ios)
797 struct ccc_io *cio = cl2ccc_io(env, ios);
798 struct cl_io *io = ios->cis_io;
799 struct cl_object *obj = io->ci_obj;
800 struct inode *inode = ccc_object_inode(obj);
801 struct file *file = cio->cui_fd->fd_file;
803 loff_t pos = io->u.ci_wr.wr.crw_pos;
804 size_t cnt = io->u.ci_wr.wr.crw_count;
808 if (!can_populate_pages(env, io, inode))
811 if (cl_io_is_append(io)) {
813 * PARALLEL IO This has to be changed for parallel IO doing
814 * out-of-order writes.
816 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
817 cio->cui_iocb->ki_pos = pos;
820 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
822 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
825 result = lustre_generic_file_write(file, cio, &pos);
828 result = vvp_io_write_commit(env, io);
829 if (cio->u.write.cui_written > 0) {
830 result = cio->u.write.cui_written;
831 io->ci_nob += result;
833 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
838 struct ll_inode_info *lli = ll_i2info(inode);
840 spin_lock(&lli->lli_lock);
841 lli->lli_flags |= LLIF_DATA_MODIFIED;
842 spin_unlock(&lli->lli_lock);
846 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
847 cio->cui_fd, pos, result, WRITE);
854 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
856 struct vm_fault *vmf = cfio->fault.ft_vmf;
858 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
861 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
862 vmf->virtual_address);
863 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
864 lock_page(vmf->page);
865 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
868 cfio->ft_vmpage = vmf->page;
872 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
873 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
877 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
878 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
882 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
885 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
889 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
890 struct cl_page *page)
893 struct cl_object *clob = cl_io_top(io)->ci_obj;
895 set_page_dirty(page->cp_vmpage);
897 cp = cl2ccc_page(cl_object_page_slice(clob, page));
898 vvp_write_pending(cl2ccc(clob), cp);
901 static int vvp_io_fault_start(const struct lu_env *env,
902 const struct cl_io_slice *ios)
904 struct vvp_io *vio = cl2vvp_io(env, ios);
905 struct cl_io *io = ios->cis_io;
906 struct cl_object *obj = io->ci_obj;
907 struct inode *inode = ccc_object_inode(obj);
908 struct cl_fault_io *fio = &io->u.ci_fault;
909 struct vvp_fault_io *cfio = &vio->u.fault;
912 struct page *vmpage = NULL;
913 struct cl_page *page;
918 if (fio->ft_executable &&
919 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
921 " changed while waiting for the page fault lock\n",
922 PFID(lu_object_fid(&obj->co_lu)));
924 /* offset of the last byte on the page */
925 offset = cl_offset(obj, fio->ft_index + 1) - 1;
926 LASSERT(cl_index(obj, offset) == fio->ft_index);
927 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
931 /* must return locked page */
932 if (fio->ft_mkwrite) {
933 LASSERT(cfio->ft_vmpage != NULL);
934 lock_page(cfio->ft_vmpage);
936 result = vvp_io_kernel_fault(cfio);
941 vmpage = cfio->ft_vmpage;
942 LASSERT(PageLocked(vmpage));
944 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
945 ll_invalidate_page(vmpage);
947 size = i_size_read(inode);
948 /* Though we have already held a cl_lock upon this page, but
949 * it still can be truncated locally. */
950 if (unlikely((vmpage->mapping != inode->i_mapping) ||
951 (page_offset(vmpage) > size))) {
952 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
954 /* return +1 to stop cl_io_loop() and ll_fault() will catch
956 GOTO(out, result = +1);
959 last_index = cl_index(obj, size - 1);
961 if (fio->ft_mkwrite ) {
963 * Capture the size while holding the lli_trunc_sem from above
964 * we want to make sure that we complete the mkwrite action
965 * while holding this lock. We need to make sure that we are
966 * not past the end of the file.
968 if (last_index < fio->ft_index) {
970 "llite: mkwrite and truncate race happened: "
972 vmpage->mapping,fio->ft_index,last_index);
974 * We need to return if we are
975 * passed the end of the file. This will propagate
976 * up the call stack to ll_page_mkwrite where
977 * we will return VM_FAULT_NOPAGE. Any non-negative
978 * value returned here will be silently
979 * converted to 0. If the vmpage->mapping is null
980 * the error code would be converted back to ENODATA
981 * in ll_page_mkwrite0. Thus we return -ENODATA
982 * to handle both cases
984 GOTO(out, result = -ENODATA);
988 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
990 GOTO(out, result = PTR_ERR(page));
992 /* if page is going to be written, we should add this page into cache
994 if (fio->ft_mkwrite) {
995 wait_on_page_writeback(vmpage);
996 if (!PageDirty(vmpage)) {
997 struct cl_page_list *plist = &io->ci_queue.c2_qin;
998 struct ccc_page *cp = cl_object_page_slice(obj, page);
1001 /* vvp_page_assume() calls wait_on_page_writeback(). */
1002 cl_page_assume(env, io, page);
1004 cl_page_list_init(plist);
1005 cl_page_list_add(plist, page);
1008 if (last_index == ccc_index(cp))
1009 to = size & ~CFS_PAGE_MASK;
1011 /* Do not set Dirty bit here so that in case IO is
1012 * started before the page is really made dirty, we
1013 * still have chance to detect it. */
1014 result = cl_io_commit_async(env, io, plist, 0, to,
1015 mkwrite_commit_callback);
1016 LASSERT(cl_page_is_owned(page, io));
1017 cl_page_list_fini(env, plist);
1021 cl_page_discard(env, io, page);
1022 cl_page_disown(env, io, page);
1024 cl_page_put(env, page);
1026 /* we're in big trouble, what can we do now? */
1027 if (result == -EDQUOT)
1031 cl_page_disown(env, io, page);
1036 * The ft_index is only used in the case of
1037 * a mkwrite action. We need to check
1038 * our assertions are correct, since
1039 * we should have caught this above
1041 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1042 if (fio->ft_index == last_index)
1044 * Last page is mapped partially.
1046 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1048 fio->ft_nob = cl_page_size(obj);
1050 lu_ref_add(&page->cp_reference, "fault", io);
1051 fio->ft_page = page;
1055 /* return unlocked vmpage to avoid deadlocking */
1057 unlock_page(vmpage);
1058 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1062 static int vvp_io_fsync_start(const struct lu_env *env,
1063 const struct cl_io_slice *ios)
1065 /* we should mark TOWRITE bit to each dirty page in radix tree to
1066 * verify pages have been written, but this is difficult because of
1071 static int vvp_io_read_page(const struct lu_env *env,
1072 const struct cl_io_slice *ios,
1073 const struct cl_page_slice *slice)
1075 struct cl_io *io = ios->cis_io;
1076 struct cl_object *obj = slice->cpl_obj;
1077 struct ccc_page *cp = cl2ccc_page(slice);
1078 struct cl_page *page = slice->cpl_page;
1079 struct inode *inode = ccc_object_inode(obj);
1080 struct ll_sb_info *sbi = ll_i2sbi(inode);
1081 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1082 struct ll_readahead_state *ras = &fd->fd_ras;
1083 struct page *vmpage = cp->cpg_page;
1084 struct cl_2queue *queue = &io->ci_queue;
1087 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1088 LASSERT(slice->cpl_obj == obj);
1092 if (sbi->ll_ra_info.ra_max_pages_per_file &&
1093 sbi->ll_ra_info.ra_max_pages)
1094 ras_update(sbi, inode, ras, ccc_index(cp),
1095 cp->cpg_defer_uptodate);
1097 /* Sanity check whether the page is protected by a lock. */
1098 rc = cl_page_is_under_lock(env, io, page);
1100 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
1101 rc == -ENODATA ? "without a lock" :
1102 "match failed", rc);
1107 if (cp->cpg_defer_uptodate) {
1108 cp->cpg_ra_used = 1;
1109 cl_page_export(env, page, 1);
1112 * Add page into the queue even when it is marked uptodate above.
1113 * this will unlock it automatically as part of cl_page_list_disown().
1115 cl_2queue_add(queue, page);
1116 if (sbi->ll_ra_info.ra_max_pages_per_file &&
1117 sbi->ll_ra_info.ra_max_pages)
1118 ll_readahead(env, io, ras,
1119 vmpage->mapping, &queue->c2_qin, fd->fd_flags);
1124 static const struct cl_io_operations vvp_io_ops = {
1127 .cio_fini = vvp_io_read_fini,
1128 .cio_lock = vvp_io_read_lock,
1129 .cio_start = vvp_io_read_start,
1130 .cio_advance = ccc_io_advance
1133 .cio_fini = vvp_io_fini,
1134 .cio_iter_init = vvp_io_write_iter_init,
1135 .cio_iter_fini = vvp_io_write_iter_fini,
1136 .cio_lock = vvp_io_write_lock,
1137 .cio_start = vvp_io_write_start,
1138 .cio_advance = ccc_io_advance
1141 .cio_fini = vvp_io_setattr_fini,
1142 .cio_iter_init = vvp_io_setattr_iter_init,
1143 .cio_lock = vvp_io_setattr_lock,
1144 .cio_start = vvp_io_setattr_start,
1145 .cio_end = vvp_io_setattr_end
1148 .cio_fini = vvp_io_fault_fini,
1149 .cio_iter_init = vvp_io_fault_iter_init,
1150 .cio_lock = vvp_io_fault_lock,
1151 .cio_start = vvp_io_fault_start,
1152 .cio_end = ccc_io_end
1155 .cio_start = vvp_io_fsync_start,
1156 .cio_fini = vvp_io_fini
1159 .cio_fini = vvp_io_fini
1162 .cio_read_page = vvp_io_read_page,
1165 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1168 struct vvp_io *vio = vvp_env_io(env);
1169 struct ccc_io *cio = ccc_env_io(env);
1170 struct inode *inode = ccc_object_inode(obj);
1173 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1176 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1177 "restore needed %d\n",
1178 PFID(lu_object_fid(&obj->co_lu)),
1179 io->ci_ignore_layout, io->ci_verify_layout,
1180 cio->cui_layout_gen, io->ci_restore_needed);
1182 CL_IO_SLICE_CLEAN(cio, cui_cl);
1183 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1184 vio->cui_ra_window_set = 0;
1186 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1188 struct ll_inode_info *lli = ll_i2info(inode);
1190 count = io->u.ci_rw.crw_count;
1191 /* "If nbyte is 0, read() will return 0 and have no other
1192 * results." -- Single Unix Spec */
1196 cio->cui_tot_count = count;
1197 cio->cui_tot_nrsegs = 0;
1200 /* for read/write, we store the jobid in the inode, and
1201 * it'll be fetched by osc when building RPC.
1203 * it's not accurate if the file is shared by different
1206 lustre_get_jobid(lli->lli_jobid);
1207 } else if (io->ci_type == CIT_SETATTR) {
1208 if (!cl_io_is_trunc(io))
1209 io->ci_lockreq = CILR_MANDATORY;
1212 /* ignore layout change for generic CIT_MISC but not for glimpse.
1213 * io context for glimpse must set ci_verify_layout to true,
1214 * see cl_glimpse_size0() for details. */
1215 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1216 io->ci_ignore_layout = 1;
1218 /* Enqueue layout lock and get layout version. We need to do this
1219 * even for operations requiring to open file, such as read and write,
1220 * because it might not grant layout lock in IT_OPEN. */
1221 if (result == 0 && !io->ci_ignore_layout) {
1222 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1223 if (result == -ENOENT)
1224 /* If the inode on MDS has been removed, but the objects
1225 * on OSTs haven't been destroyed (async unlink), layout
1226 * fetch will return -ENOENT, we'd ingore this error
1227 * and continue with dirty flush. LU-3230. */
1230 CERROR("%s: refresh file layout " DFID " error %d.\n",
1231 ll_get_fsname(inode->i_sb, NULL, 0),
1232 PFID(lu_object_fid(&obj->co_lu)), result);
1238 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1239 const struct cl_io_slice *slice)
1241 /* Caling just for assertion */
1242 cl2ccc_io(env, slice);
1243 return vvp_env_io(env);