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,
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 int vvp_io_read_start(const struct lu_env *env,
509 const struct cl_io_slice *ios)
511 struct vvp_io *vio = cl2vvp_io(env, ios);
512 struct ccc_io *cio = cl2ccc_io(env, ios);
513 struct cl_io *io = ios->cis_io;
514 struct cl_object *obj = io->ci_obj;
515 struct inode *inode = ccc_object_inode(obj);
516 struct ll_ra_read *bead = &vio->cui_bead;
517 struct file *file = cio->cui_fd->fd_file;
520 loff_t pos = io->u.ci_rd.rd.crw_pos;
521 long cnt = io->u.ci_rd.rd.crw_count;
522 long tot = cio->cui_tot_count;
525 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
527 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
529 if (!can_populate_pages(env, io, inode))
532 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
535 else if (exceed != 0)
538 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
539 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
540 inode->i_ino, cnt, pos, i_size_read(inode));
542 /* turn off the kernel's read-ahead */
543 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
545 /* initialize read-ahead window once per syscall */
546 if (!vio->cui_ra_window_set) {
547 vio->cui_ra_window_set = 1;
548 bead->lrr_start = cl_index(obj, pos);
549 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
550 ll_ra_read_in(file, bead);
555 switch (vio->cui_io_subtype) {
557 LASSERT(cio->cui_iocb->ki_pos == pos);
558 result = generic_file_aio_read(cio->cui_iocb,
559 cio->cui_iov, cio->cui_nrsegs,
560 cio->cui_iocb->ki_pos);
563 result = generic_file_splice_read(file, &pos,
564 vio->u.splice.cui_pipe, cnt,
565 vio->u.splice.cui_flags);
566 /* LU-1109: do splice read stripe by stripe otherwise if it
567 * may make nfsd stuck if this read occupied all internal pipe
572 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
580 io->ci_nob += result;
581 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
589 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
591 struct vvp_io *vio = cl2vvp_io(env, ios);
592 struct ccc_io *cio = cl2ccc_io(env, ios);
594 if (vio->cui_ra_window_set)
595 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
597 vvp_io_fini(env, ios);
600 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
601 struct cl_page_list *plist, int from, int to)
603 struct cl_2queue *queue = &io->ci_queue;
604 struct cl_page *page;
605 unsigned int bytes = 0;
609 if (plist->pl_nr == 0)
612 if (from > 0 || to != PAGE_SIZE) {
613 page = cl_page_list_first(plist);
614 if (plist->pl_nr == 1) {
615 cl_page_clip(env, page, from, to);
616 } else if (from > 0) {
617 cl_page_clip(env, page, from, PAGE_SIZE);
619 page = cl_page_list_last(plist);
620 cl_page_clip(env, page, 0, to);
624 cl_2queue_init(queue);
625 cl_page_list_splice(plist, &queue->c2_qin);
626 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
628 /* plist is not sorted any more */
629 cl_page_list_splice(&queue->c2_qin, plist);
630 cl_page_list_splice(&queue->c2_qout, plist);
631 cl_2queue_fini(env, queue);
634 /* calculate bytes */
635 bytes = plist->pl_nr << PAGE_SHIFT;
636 bytes -= from + PAGE_SIZE - to;
638 while (plist->pl_nr > 0) {
639 page = cl_page_list_first(plist);
640 cl_page_list_del(env, plist, page);
642 cl_page_clip(env, page, 0, PAGE_SIZE);
644 SetPageUptodate(cl_page_vmpage(page));
645 cl_page_disown(env, io, page);
647 /* held in ll_cl_init() */
648 lu_ref_del(&page->cp_reference, "cl_io", io);
649 cl_page_put(env, page);
653 RETURN(bytes > 0 ? bytes : rc);
656 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
657 struct cl_page *page)
660 struct page *vmpage = page->cp_vmpage;
661 struct cl_object *clob = cl_io_top(io)->ci_obj;
663 SetPageUptodate(vmpage);
664 set_page_dirty(vmpage);
666 cp = cl2ccc_page(cl_object_page_slice(clob, page));
667 vvp_write_pending(cl2ccc(clob), cp);
669 cl_page_disown(env, io, page);
671 /* held in ll_cl_init() */
672 lu_ref_del(&page->cp_reference, "cl_io", io);
673 cl_page_put(env, page);
676 /* make sure the page list is contiguous */
677 static bool page_list_sanity_check(struct cl_object *obj,
678 struct cl_page_list *plist)
680 struct cl_page *page;
681 pgoff_t index = CL_PAGE_EOF;
683 cl_page_list_for_each(page, plist) {
684 struct ccc_page *cp = cl_object_page_slice(obj, page);
686 if (index == CL_PAGE_EOF) {
687 index = ccc_index(cp);
692 if (index == ccc_index(cp))
700 /* Return how many bytes have queued or written */
701 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
703 struct cl_object *obj = io->ci_obj;
704 struct inode *inode = ccc_object_inode(obj);
705 struct ccc_io *cio = ccc_env_io(env);
706 struct cl_page_list *queue = &cio->u.write.cui_queue;
707 struct cl_page *page;
710 unsigned int npages = cio->u.write.cui_queue.pl_nr;
716 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
717 npages, cio->u.write.cui_from, cio->u.write.cui_to);
719 LASSERT(page_list_sanity_check(obj, queue));
721 /* submit IO with async write */
722 rc = cl_io_commit_async(env, io, queue,
723 cio->u.write.cui_from, cio->u.write.cui_to,
724 write_commit_callback);
725 npages -= queue->pl_nr; /* already committed pages */
727 /* calculate how many bytes were written */
728 bytes = npages << PAGE_SHIFT;
731 bytes -= cio->u.write.cui_from;
732 if (queue->pl_nr == 0) /* last page */
733 bytes -= PAGE_SIZE - cio->u.write.cui_to;
734 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
736 cio->u.write.cui_written += bytes;
738 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
739 npages, bytes, cio->u.write.cui_written);
741 /* the first page must have been written. */
742 cio->u.write.cui_from = 0;
744 LASSERT(page_list_sanity_check(obj, queue));
745 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
747 /* out of quota, try sync write */
748 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
749 rc = vvp_io_commit_sync(env, io, queue,
750 cio->u.write.cui_from,
751 cio->u.write.cui_to);
753 cio->u.write.cui_written += rc;
758 /* update inode size */
759 ll_merge_lvb(env, inode);
761 /* Now the pages in queue were failed to commit, discard them
762 * unless they were dirtied before. */
763 while (queue->pl_nr > 0) {
764 page = cl_page_list_first(queue);
765 cl_page_list_del(env, queue, page);
767 if (!PageDirty(cl_page_vmpage(page)))
768 cl_page_discard(env, io, page);
770 cl_page_disown(env, io, page);
772 /* held in ll_cl_init() */
773 lu_ref_del(&page->cp_reference, "cl_io", io);
774 cl_page_put(env, page);
776 cl_page_list_fini(env, queue);
781 static int vvp_io_write_start(const struct lu_env *env,
782 const struct cl_io_slice *ios)
784 struct ccc_io *cio = cl2ccc_io(env, ios);
785 struct cl_io *io = ios->cis_io;
786 struct cl_object *obj = io->ci_obj;
787 struct inode *inode = ccc_object_inode(obj);
789 loff_t pos = io->u.ci_wr.wr.crw_pos;
790 size_t cnt = io->u.ci_wr.wr.crw_count;
794 if (!can_populate_pages(env, io, inode))
797 if (cl_io_is_append(io)) {
799 * PARALLEL IO This has to be changed for parallel IO doing
800 * out-of-order writes.
802 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
803 cio->cui_iocb->ki_pos = pos;
805 LASSERT(cio->cui_iocb->ki_pos == pos);
808 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
810 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
813 result = generic_file_aio_write(cio->cui_iocb,
814 cio->cui_iov, cio->cui_nrsegs,
815 cio->cui_iocb->ki_pos);
817 result = vvp_io_write_commit(env, io);
818 if (cio->u.write.cui_written > 0) {
819 result = cio->u.write.cui_written;
820 io->ci_nob += result;
822 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
827 struct ll_inode_info *lli = ll_i2info(inode);
829 spin_lock(&lli->lli_lock);
830 lli->lli_flags |= LLIF_DATA_MODIFIED;
831 spin_unlock(&lli->lli_lock);
835 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
836 cio->cui_fd, pos, result, WRITE);
843 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
845 struct vm_fault *vmf = cfio->fault.ft_vmf;
847 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
850 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
851 vmf->virtual_address);
852 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
853 lock_page(vmf->page);
854 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
857 cfio->ft_vmpage = vmf->page;
861 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
862 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
866 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
867 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
871 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
874 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
878 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
879 struct cl_page *page)
882 struct cl_object *clob = cl_io_top(io)->ci_obj;
884 set_page_dirty(page->cp_vmpage);
886 cp = cl2ccc_page(cl_object_page_slice(clob, page));
887 vvp_write_pending(cl2ccc(clob), cp);
890 static int vvp_io_fault_start(const struct lu_env *env,
891 const struct cl_io_slice *ios)
893 struct vvp_io *vio = cl2vvp_io(env, ios);
894 struct cl_io *io = ios->cis_io;
895 struct cl_object *obj = io->ci_obj;
896 struct inode *inode = ccc_object_inode(obj);
897 struct cl_fault_io *fio = &io->u.ci_fault;
898 struct vvp_fault_io *cfio = &vio->u.fault;
901 struct page *vmpage = NULL;
902 struct cl_page *page;
907 if (fio->ft_executable &&
908 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
910 " changed while waiting for the page fault lock\n",
911 PFID(lu_object_fid(&obj->co_lu)));
913 /* offset of the last byte on the page */
914 offset = cl_offset(obj, fio->ft_index + 1) - 1;
915 LASSERT(cl_index(obj, offset) == fio->ft_index);
916 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
920 /* must return locked page */
921 if (fio->ft_mkwrite) {
922 LASSERT(cfio->ft_vmpage != NULL);
923 lock_page(cfio->ft_vmpage);
925 result = vvp_io_kernel_fault(cfio);
930 vmpage = cfio->ft_vmpage;
931 LASSERT(PageLocked(vmpage));
933 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
934 ll_invalidate_page(vmpage);
936 size = i_size_read(inode);
937 /* Though we have already held a cl_lock upon this page, but
938 * it still can be truncated locally. */
939 if (unlikely((vmpage->mapping != inode->i_mapping) ||
940 (page_offset(vmpage) > size))) {
941 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
943 /* return +1 to stop cl_io_loop() and ll_fault() will catch
945 GOTO(out, result = +1);
948 last_index = cl_index(obj, size - 1);
950 if (fio->ft_mkwrite ) {
952 * Capture the size while holding the lli_trunc_sem from above
953 * we want to make sure that we complete the mkwrite action
954 * while holding this lock. We need to make sure that we are
955 * not past the end of the file.
957 if (last_index < fio->ft_index) {
959 "llite: mkwrite and truncate race happened: "
961 vmpage->mapping,fio->ft_index,last_index);
963 * We need to return if we are
964 * passed the end of the file. This will propagate
965 * up the call stack to ll_page_mkwrite where
966 * we will return VM_FAULT_NOPAGE. Any non-negative
967 * value returned here will be silently
968 * converted to 0. If the vmpage->mapping is null
969 * the error code would be converted back to ENODATA
970 * in ll_page_mkwrite0. Thus we return -ENODATA
971 * to handle both cases
973 GOTO(out, result = -ENODATA);
977 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
979 GOTO(out, result = PTR_ERR(page));
981 /* if page is going to be written, we should add this page into cache
983 if (fio->ft_mkwrite) {
984 wait_on_page_writeback(vmpage);
985 if (!PageDirty(vmpage)) {
986 struct cl_page_list *plist = &io->ci_queue.c2_qin;
987 struct ccc_page *cp = cl_object_page_slice(obj, page);
990 /* vvp_page_assume() calls wait_on_page_writeback(). */
991 cl_page_assume(env, io, page);
993 cl_page_list_init(plist);
994 cl_page_list_add(plist, page);
997 if (last_index == ccc_index(cp))
998 to = size & ~CFS_PAGE_MASK;
1000 /* Do not set Dirty bit here so that in case IO is
1001 * started before the page is really made dirty, we
1002 * still have chance to detect it. */
1003 result = cl_io_commit_async(env, io, plist, 0, to,
1004 mkwrite_commit_callback);
1005 LASSERT(cl_page_is_owned(page, io));
1006 cl_page_list_fini(env, plist);
1010 cl_page_discard(env, io, page);
1011 cl_page_disown(env, io, page);
1013 cl_page_put(env, page);
1015 /* we're in big trouble, what can we do now? */
1016 if (result == -EDQUOT)
1020 cl_page_disown(env, io, page);
1025 * The ft_index is only used in the case of
1026 * a mkwrite action. We need to check
1027 * our assertions are correct, since
1028 * we should have caught this above
1030 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1031 if (fio->ft_index == last_index)
1033 * Last page is mapped partially.
1035 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1037 fio->ft_nob = cl_page_size(obj);
1039 lu_ref_add(&page->cp_reference, "fault", io);
1040 fio->ft_page = page;
1044 /* return unlocked vmpage to avoid deadlocking */
1046 unlock_page(vmpage);
1047 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1051 static int vvp_io_fsync_start(const struct lu_env *env,
1052 const struct cl_io_slice *ios)
1054 /* we should mark TOWRITE bit to each dirty page in radix tree to
1055 * verify pages have been written, but this is difficult because of
1060 static int vvp_io_read_page(const struct lu_env *env,
1061 const struct cl_io_slice *ios,
1062 const struct cl_page_slice *slice)
1064 struct cl_io *io = ios->cis_io;
1065 struct ccc_page *cp = cl2ccc_page(slice);
1066 struct cl_page *page = slice->cpl_page;
1067 struct inode *inode = ccc_object_inode(slice->cpl_obj);
1068 struct ll_sb_info *sbi = ll_i2sbi(inode);
1069 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1070 struct ll_readahead_state *ras = &fd->fd_ras;
1071 struct cl_2queue *queue = &io->ci_queue;
1075 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1076 sbi->ll_ra_info.ra_max_pages > 0)
1077 ras_update(sbi, inode, ras, ccc_index(cp),
1078 cp->cpg_defer_uptodate);
1080 if (cp->cpg_defer_uptodate) {
1081 cp->cpg_ra_used = 1;
1082 cl_page_export(env, page, 1);
1086 * Add page into the queue even when it is marked uptodate above.
1087 * this will unlock it automatically as part of cl_page_list_disown().
1089 cl_2queue_add(queue, page);
1090 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1091 sbi->ll_ra_info.ra_max_pages > 0)
1092 ll_readahead(env, io, &queue->c2_qin, ras,
1093 cp->cpg_defer_uptodate);
1098 static const struct cl_io_operations vvp_io_ops = {
1101 .cio_fini = vvp_io_read_fini,
1102 .cio_lock = vvp_io_read_lock,
1103 .cio_start = vvp_io_read_start,
1104 .cio_advance = ccc_io_advance
1107 .cio_fini = vvp_io_fini,
1108 .cio_iter_init = vvp_io_write_iter_init,
1109 .cio_iter_fini = vvp_io_write_iter_fini,
1110 .cio_lock = vvp_io_write_lock,
1111 .cio_start = vvp_io_write_start,
1112 .cio_advance = ccc_io_advance
1115 .cio_fini = vvp_io_setattr_fini,
1116 .cio_iter_init = vvp_io_setattr_iter_init,
1117 .cio_lock = vvp_io_setattr_lock,
1118 .cio_start = vvp_io_setattr_start,
1119 .cio_end = vvp_io_setattr_end
1122 .cio_fini = vvp_io_fault_fini,
1123 .cio_iter_init = vvp_io_fault_iter_init,
1124 .cio_lock = vvp_io_fault_lock,
1125 .cio_start = vvp_io_fault_start,
1126 .cio_end = ccc_io_end
1129 .cio_start = vvp_io_fsync_start,
1130 .cio_fini = vvp_io_fini
1133 .cio_fini = vvp_io_fini
1136 .cio_read_page = vvp_io_read_page,
1139 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1142 struct vvp_io *vio = vvp_env_io(env);
1143 struct ccc_io *cio = ccc_env_io(env);
1144 struct inode *inode = ccc_object_inode(obj);
1147 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1150 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1151 "restore needed %d\n",
1152 PFID(lu_object_fid(&obj->co_lu)),
1153 io->ci_ignore_layout, io->ci_verify_layout,
1154 cio->cui_layout_gen, io->ci_restore_needed);
1156 CL_IO_SLICE_CLEAN(cio, cui_cl);
1157 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1158 vio->cui_ra_window_set = 0;
1160 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1162 struct ll_inode_info *lli = ll_i2info(inode);
1164 count = io->u.ci_rw.crw_count;
1165 /* "If nbyte is 0, read() will return 0 and have no other
1166 * results." -- Single Unix Spec */
1170 cio->cui_tot_count = count;
1171 cio->cui_tot_nrsegs = 0;
1174 /* for read/write, we store the jobid in the inode, and
1175 * it'll be fetched by osc when building RPC.
1177 * it's not accurate if the file is shared by different
1180 lustre_get_jobid(lli->lli_jobid);
1181 } else if (io->ci_type == CIT_SETATTR) {
1182 if (!cl_io_is_trunc(io))
1183 io->ci_lockreq = CILR_MANDATORY;
1186 /* ignore layout change for generic CIT_MISC but not for glimpse.
1187 * io context for glimpse must set ci_verify_layout to true,
1188 * see cl_glimpse_size0() for details. */
1189 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1190 io->ci_ignore_layout = 1;
1192 /* Enqueue layout lock and get layout version. We need to do this
1193 * even for operations requiring to open file, such as read and write,
1194 * because it might not grant layout lock in IT_OPEN. */
1195 if (result == 0 && !io->ci_ignore_layout) {
1196 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1197 if (result == -ENOENT)
1198 /* If the inode on MDS has been removed, but the objects
1199 * on OSTs haven't been destroyed (async unlink), layout
1200 * fetch will return -ENOENT, we'd ingore this error
1201 * and continue with dirty flush. LU-3230. */
1204 CERROR("%s: refresh file layout " DFID " error %d.\n",
1205 ll_get_fsname(inode->i_sb, NULL, 0),
1206 PFID(lu_object_fid(&obj->co_lu)), result);
1212 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1213 const struct cl_io_slice *slice)
1215 /* Caling just for assertion */
1216 cl2ccc_io(env, slice);
1217 return vvp_env_io(env);