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 ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
343 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
344 if (lli->lli_has_smd) /* lsm-less file doesn't need to lock */
345 result = vvp_io_rw_lock(env, io, CLM_READ,
346 io->u.ci_rd.rd.crw_pos,
347 io->u.ci_rd.rd.crw_pos +
348 io->u.ci_rd.rd.crw_count - 1);
354 static int vvp_io_fault_lock(const struct lu_env *env,
355 const struct cl_io_slice *ios)
357 struct cl_io *io = ios->cis_io;
358 struct vvp_io *vio = cl2vvp_io(env, ios);
360 * XXX LDLM_FL_CBPENDING
362 return ccc_io_one_lock_index
363 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
364 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
367 static int vvp_io_write_lock(const struct lu_env *env,
368 const struct cl_io_slice *ios)
370 struct cl_io *io = ios->cis_io;
374 if (io->u.ci_wr.wr_append) {
376 end = OBD_OBJECT_EOF;
378 start = io->u.ci_wr.wr.crw_pos;
379 end = start + io->u.ci_wr.wr.crw_count - 1;
381 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
384 static int vvp_io_setattr_iter_init(const struct lu_env *env,
385 const struct cl_io_slice *ios)
391 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
393 * Handles "lockless io" mode when extent locking is done by server.
395 static int vvp_io_setattr_lock(const struct lu_env *env,
396 const struct cl_io_slice *ios)
398 struct ccc_io *cio = ccc_env_io(env);
399 struct cl_io *io = ios->cis_io;
403 if (cl_io_is_trunc(io)) {
404 new_size = io->u.ci_setattr.sa_attr.lvb_size;
406 enqflags = CEF_DISCARD_DATA;
408 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
409 io->u.ci_setattr.sa_attr.lvb_ctime) ||
410 (io->u.ci_setattr.sa_attr.lvb_atime >=
411 io->u.ci_setattr.sa_attr.lvb_ctime))
415 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
416 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
417 new_size, OBD_OBJECT_EOF);
420 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
425 * Only ll_inode_size_lock is taken at this level.
427 ll_inode_size_lock(inode);
428 result = inode_newsize_ok(inode, size);
430 ll_inode_size_unlock(inode);
433 i_size_write(inode, size);
435 ll_truncate_pagecache(inode, 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 int vvp_io_read_start(const struct lu_env *env,
511 const struct cl_io_slice *ios)
513 struct vvp_io *vio = cl2vvp_io(env, ios);
514 struct ccc_io *cio = cl2ccc_io(env, ios);
515 struct cl_io *io = ios->cis_io;
516 struct cl_object *obj = io->ci_obj;
517 struct inode *inode = ccc_object_inode(obj);
518 struct ll_ra_read *bead = &vio->cui_bead;
519 struct file *file = cio->cui_fd->fd_file;
522 loff_t pos = io->u.ci_rd.rd.crw_pos;
523 long cnt = io->u.ci_rd.rd.crw_count;
524 long tot = cio->cui_tot_count;
527 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
529 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
531 if (!can_populate_pages(env, io, inode))
534 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
537 else if (exceed != 0)
540 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
541 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
542 inode->i_ino, cnt, pos, i_size_read(inode));
544 /* turn off the kernel's read-ahead */
545 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
547 /* initialize read-ahead window once per syscall */
548 if (!vio->cui_ra_window_set) {
549 vio->cui_ra_window_set = 1;
550 bead->lrr_start = cl_index(obj, pos);
551 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
552 ll_ra_read_in(file, bead);
557 switch (vio->cui_io_subtype) {
559 LASSERT(cio->cui_iocb->ki_pos == pos);
560 result = generic_file_aio_read(cio->cui_iocb,
561 cio->cui_iov, cio->cui_nrsegs,
562 cio->cui_iocb->ki_pos);
565 result = generic_file_splice_read(file, &pos,
566 vio->u.splice.cui_pipe, cnt,
567 vio->u.splice.cui_flags);
568 /* LU-1109: do splice read stripe by stripe otherwise if it
569 * may make nfsd stuck if this read occupied all internal pipe
574 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
582 io->ci_nob += result;
583 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
591 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
593 struct vvp_io *vio = cl2vvp_io(env, ios);
594 struct ccc_io *cio = cl2ccc_io(env, ios);
596 if (vio->cui_ra_window_set)
597 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
599 vvp_io_fini(env, ios);
602 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
603 struct cl_page_list *plist, int from, int to)
605 struct cl_2queue *queue = &io->ci_queue;
606 struct cl_page *page;
607 unsigned int bytes = 0;
611 if (plist->pl_nr == 0)
614 if (from > 0 || to != PAGE_SIZE) {
615 page = cl_page_list_first(plist);
616 if (plist->pl_nr == 1) {
617 cl_page_clip(env, page, from, to);
618 } else if (from > 0) {
619 cl_page_clip(env, page, from, PAGE_SIZE);
621 page = cl_page_list_last(plist);
622 cl_page_clip(env, page, 0, to);
626 cl_2queue_init(queue);
627 cl_page_list_splice(plist, &queue->c2_qin);
628 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
630 /* plist is not sorted any more */
631 cl_page_list_splice(&queue->c2_qin, plist);
632 cl_page_list_splice(&queue->c2_qout, plist);
633 cl_2queue_fini(env, queue);
636 /* calculate bytes */
637 bytes = plist->pl_nr << PAGE_SHIFT;
638 bytes -= from + PAGE_SIZE - to;
640 while (plist->pl_nr > 0) {
641 page = cl_page_list_first(plist);
642 cl_page_list_del(env, plist, page);
644 cl_page_clip(env, page, 0, PAGE_SIZE);
646 SetPageUptodate(cl_page_vmpage(page));
647 cl_page_disown(env, io, page);
649 /* held in ll_cl_init() */
650 lu_ref_del(&page->cp_reference, "cl_io", io);
651 cl_page_put(env, page);
655 RETURN(bytes > 0 ? bytes : rc);
658 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
659 struct cl_page *page)
662 struct page *vmpage = page->cp_vmpage;
663 struct cl_object *clob = cl_io_top(io)->ci_obj;
665 SetPageUptodate(vmpage);
666 set_page_dirty(vmpage);
668 cp = cl2ccc_page(cl_object_page_slice(clob, page));
669 vvp_write_pending(cl2ccc(clob), cp);
671 cl_page_disown(env, io, page);
673 /* held in ll_cl_init() */
674 lu_ref_del(&page->cp_reference, "cl_io", io);
675 cl_page_put(env, page);
678 /* make sure the page list is contiguous */
679 static bool page_list_sanity_check(struct cl_object *obj,
680 struct cl_page_list *plist)
682 struct cl_page *page;
683 pgoff_t index = CL_PAGE_EOF;
685 cl_page_list_for_each(page, plist) {
686 struct ccc_page *cp = cl_object_page_slice(obj, page);
688 if (index == CL_PAGE_EOF) {
689 index = ccc_index(cp);
694 if (index == ccc_index(cp))
702 /* Return how many bytes have queued or written */
703 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
705 struct cl_object *obj = io->ci_obj;
706 struct inode *inode = ccc_object_inode(obj);
707 struct ccc_io *cio = ccc_env_io(env);
708 struct cl_page_list *queue = &cio->u.write.cui_queue;
709 struct cl_page *page;
712 unsigned int npages = cio->u.write.cui_queue.pl_nr;
718 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
719 npages, cio->u.write.cui_from, cio->u.write.cui_to);
721 LASSERT(page_list_sanity_check(obj, queue));
723 /* submit IO with async write */
724 rc = cl_io_commit_async(env, io, queue,
725 cio->u.write.cui_from, cio->u.write.cui_to,
726 write_commit_callback);
727 npages -= queue->pl_nr; /* already committed pages */
729 /* calculate how many bytes were written */
730 bytes = npages << PAGE_SHIFT;
733 bytes -= cio->u.write.cui_from;
734 if (queue->pl_nr == 0) /* last page */
735 bytes -= PAGE_SIZE - cio->u.write.cui_to;
736 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
738 cio->u.write.cui_written += bytes;
740 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
741 npages, bytes, cio->u.write.cui_written);
743 /* the first page must have been written. */
744 cio->u.write.cui_from = 0;
746 LASSERT(page_list_sanity_check(obj, queue));
747 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
749 /* out of quota, try sync write */
750 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
751 rc = vvp_io_commit_sync(env, io, queue,
752 cio->u.write.cui_from,
753 cio->u.write.cui_to);
755 cio->u.write.cui_written += rc;
760 /* update inode size */
761 ll_merge_lvb(env, inode);
763 /* Now the pages in queue were failed to commit, discard them
764 * unless they were dirtied before. */
765 while (queue->pl_nr > 0) {
766 page = cl_page_list_first(queue);
767 cl_page_list_del(env, queue, page);
769 if (!PageDirty(cl_page_vmpage(page)))
770 cl_page_discard(env, io, page);
772 cl_page_disown(env, io, page);
774 /* held in ll_cl_init() */
775 lu_ref_del(&page->cp_reference, "cl_io", io);
776 cl_page_put(env, page);
778 cl_page_list_fini(env, queue);
783 static int vvp_io_write_start(const struct lu_env *env,
784 const struct cl_io_slice *ios)
786 struct ccc_io *cio = cl2ccc_io(env, ios);
787 struct cl_io *io = ios->cis_io;
788 struct cl_object *obj = io->ci_obj;
789 struct inode *inode = ccc_object_inode(obj);
791 loff_t pos = io->u.ci_wr.wr.crw_pos;
792 size_t cnt = io->u.ci_wr.wr.crw_count;
796 if (!can_populate_pages(env, io, inode))
799 if (cl_io_is_append(io)) {
801 * PARALLEL IO This has to be changed for parallel IO doing
802 * out-of-order writes.
804 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
805 cio->cui_iocb->ki_pos = pos;
807 LASSERT(cio->cui_iocb->ki_pos == pos);
810 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
812 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
815 result = generic_file_aio_write(cio->cui_iocb,
816 cio->cui_iov, cio->cui_nrsegs,
817 cio->cui_iocb->ki_pos);
819 result = vvp_io_write_commit(env, io);
820 if (cio->u.write.cui_written > 0) {
821 result = cio->u.write.cui_written;
822 io->ci_nob += result;
824 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
829 struct ll_inode_info *lli = ll_i2info(inode);
831 spin_lock(&lli->lli_lock);
832 lli->lli_flags |= LLIF_DATA_MODIFIED;
833 spin_unlock(&lli->lli_lock);
837 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
838 cio->cui_fd, pos, result, WRITE);
845 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
847 struct vm_fault *vmf = cfio->fault.ft_vmf;
849 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
852 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
853 vmf->virtual_address);
854 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
855 lock_page(vmf->page);
856 cfio->fault.ft_flags |= VM_FAULT_LOCKED;
859 cfio->ft_vmpage = vmf->page;
863 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
864 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
868 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
869 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
873 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
876 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
880 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
881 struct cl_page *page)
884 struct cl_object *clob = cl_io_top(io)->ci_obj;
886 set_page_dirty(page->cp_vmpage);
888 cp = cl2ccc_page(cl_object_page_slice(clob, page));
889 vvp_write_pending(cl2ccc(clob), cp);
892 static int vvp_io_fault_start(const struct lu_env *env,
893 const struct cl_io_slice *ios)
895 struct vvp_io *vio = cl2vvp_io(env, ios);
896 struct cl_io *io = ios->cis_io;
897 struct cl_object *obj = io->ci_obj;
898 struct inode *inode = ccc_object_inode(obj);
899 struct cl_fault_io *fio = &io->u.ci_fault;
900 struct vvp_fault_io *cfio = &vio->u.fault;
903 struct page *vmpage = NULL;
904 struct cl_page *page;
909 if (fio->ft_executable &&
910 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
912 " changed while waiting for the page fault lock\n",
913 PFID(lu_object_fid(&obj->co_lu)));
915 /* offset of the last byte on the page */
916 offset = cl_offset(obj, fio->ft_index + 1) - 1;
917 LASSERT(cl_index(obj, offset) == fio->ft_index);
918 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
922 /* must return locked page */
923 if (fio->ft_mkwrite) {
924 LASSERT(cfio->ft_vmpage != NULL);
925 lock_page(cfio->ft_vmpage);
927 result = vvp_io_kernel_fault(cfio);
932 vmpage = cfio->ft_vmpage;
933 LASSERT(PageLocked(vmpage));
935 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
936 ll_invalidate_page(vmpage);
938 size = i_size_read(inode);
939 /* Though we have already held a cl_lock upon this page, but
940 * it still can be truncated locally. */
941 if (unlikely((vmpage->mapping != inode->i_mapping) ||
942 (page_offset(vmpage) > size))) {
943 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
945 /* return +1 to stop cl_io_loop() and ll_fault() will catch
947 GOTO(out, result = +1);
950 last_index = cl_index(obj, size - 1);
952 if (fio->ft_mkwrite ) {
954 * Capture the size while holding the lli_trunc_sem from above
955 * we want to make sure that we complete the mkwrite action
956 * while holding this lock. We need to make sure that we are
957 * not past the end of the file.
959 if (last_index < fio->ft_index) {
961 "llite: mkwrite and truncate race happened: "
963 vmpage->mapping,fio->ft_index,last_index);
965 * We need to return if we are
966 * passed the end of the file. This will propagate
967 * up the call stack to ll_page_mkwrite where
968 * we will return VM_FAULT_NOPAGE. Any non-negative
969 * value returned here will be silently
970 * converted to 0. If the vmpage->mapping is null
971 * the error code would be converted back to ENODATA
972 * in ll_page_mkwrite0. Thus we return -ENODATA
973 * to handle both cases
975 GOTO(out, result = -ENODATA);
979 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
981 GOTO(out, result = PTR_ERR(page));
983 /* if page is going to be written, we should add this page into cache
985 if (fio->ft_mkwrite) {
986 wait_on_page_writeback(vmpage);
987 if (!PageDirty(vmpage)) {
988 struct cl_page_list *plist = &io->ci_queue.c2_qin;
989 struct ccc_page *cp = cl_object_page_slice(obj, page);
992 /* vvp_page_assume() calls wait_on_page_writeback(). */
993 cl_page_assume(env, io, page);
995 cl_page_list_init(plist);
996 cl_page_list_add(plist, page);
999 if (last_index == ccc_index(cp))
1000 to = size & ~CFS_PAGE_MASK;
1002 /* Do not set Dirty bit here so that in case IO is
1003 * started before the page is really made dirty, we
1004 * still have chance to detect it. */
1005 result = cl_io_commit_async(env, io, plist, 0, to,
1006 mkwrite_commit_callback);
1007 LASSERT(cl_page_is_owned(page, io));
1008 cl_page_list_fini(env, plist);
1012 cl_page_discard(env, io, page);
1013 cl_page_disown(env, io, page);
1015 cl_page_put(env, page);
1017 /* we're in big trouble, what can we do now? */
1018 if (result == -EDQUOT)
1022 cl_page_disown(env, io, page);
1027 * The ft_index is only used in the case of
1028 * a mkwrite action. We need to check
1029 * our assertions are correct, since
1030 * we should have caught this above
1032 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1033 if (fio->ft_index == last_index)
1035 * Last page is mapped partially.
1037 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1039 fio->ft_nob = cl_page_size(obj);
1041 lu_ref_add(&page->cp_reference, "fault", io);
1042 fio->ft_page = page;
1046 /* return unlocked vmpage to avoid deadlocking */
1048 unlock_page(vmpage);
1049 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1053 static int vvp_io_fsync_start(const struct lu_env *env,
1054 const struct cl_io_slice *ios)
1056 /* we should mark TOWRITE bit to each dirty page in radix tree to
1057 * verify pages have been written, but this is difficult because of
1062 static int vvp_io_read_page(const struct lu_env *env,
1063 const struct cl_io_slice *ios,
1064 const struct cl_page_slice *slice)
1066 struct cl_io *io = ios->cis_io;
1067 struct ccc_page *cp = cl2ccc_page(slice);
1068 struct cl_page *page = slice->cpl_page;
1069 struct inode *inode = ccc_object_inode(slice->cpl_obj);
1070 struct ll_sb_info *sbi = ll_i2sbi(inode);
1071 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
1072 struct ll_readahead_state *ras = &fd->fd_ras;
1073 struct cl_2queue *queue = &io->ci_queue;
1077 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1078 sbi->ll_ra_info.ra_max_pages > 0)
1079 ras_update(sbi, inode, ras, ccc_index(cp),
1080 cp->cpg_defer_uptodate);
1082 if (cp->cpg_defer_uptodate) {
1083 cp->cpg_ra_used = 1;
1084 cl_page_export(env, page, 1);
1088 * Add page into the queue even when it is marked uptodate above.
1089 * this will unlock it automatically as part of cl_page_list_disown().
1091 cl_2queue_add(queue, page);
1092 if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1093 sbi->ll_ra_info.ra_max_pages > 0)
1094 ll_readahead(env, io, &queue->c2_qin, ras,
1095 cp->cpg_defer_uptodate);
1100 static const struct cl_io_operations vvp_io_ops = {
1103 .cio_fini = vvp_io_read_fini,
1104 .cio_lock = vvp_io_read_lock,
1105 .cio_start = vvp_io_read_start,
1106 .cio_advance = ccc_io_advance
1109 .cio_fini = vvp_io_fini,
1110 .cio_iter_init = vvp_io_write_iter_init,
1111 .cio_iter_fini = vvp_io_write_iter_fini,
1112 .cio_lock = vvp_io_write_lock,
1113 .cio_start = vvp_io_write_start,
1114 .cio_advance = ccc_io_advance
1117 .cio_fini = vvp_io_setattr_fini,
1118 .cio_iter_init = vvp_io_setattr_iter_init,
1119 .cio_lock = vvp_io_setattr_lock,
1120 .cio_start = vvp_io_setattr_start,
1121 .cio_end = vvp_io_setattr_end
1124 .cio_fini = vvp_io_fault_fini,
1125 .cio_iter_init = vvp_io_fault_iter_init,
1126 .cio_lock = vvp_io_fault_lock,
1127 .cio_start = vvp_io_fault_start,
1128 .cio_end = ccc_io_end
1131 .cio_start = vvp_io_fsync_start,
1132 .cio_fini = vvp_io_fini
1135 .cio_fini = vvp_io_fini
1138 .cio_read_page = vvp_io_read_page,
1141 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1144 struct vvp_io *vio = vvp_env_io(env);
1145 struct ccc_io *cio = ccc_env_io(env);
1146 struct inode *inode = ccc_object_inode(obj);
1149 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1152 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1153 "restore needed %d\n",
1154 PFID(lu_object_fid(&obj->co_lu)),
1155 io->ci_ignore_layout, io->ci_verify_layout,
1156 cio->cui_layout_gen, io->ci_restore_needed);
1158 CL_IO_SLICE_CLEAN(cio, cui_cl);
1159 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1160 vio->cui_ra_window_set = 0;
1162 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1164 struct ll_inode_info *lli = ll_i2info(inode);
1166 count = io->u.ci_rw.crw_count;
1167 /* "If nbyte is 0, read() will return 0 and have no other
1168 * results." -- Single Unix Spec */
1172 cio->cui_tot_count = count;
1173 cio->cui_tot_nrsegs = 0;
1176 /* for read/write, we store the jobid in the inode, and
1177 * it'll be fetched by osc when building RPC.
1179 * it's not accurate if the file is shared by different
1182 lustre_get_jobid(lli->lli_jobid);
1183 } else if (io->ci_type == CIT_SETATTR) {
1184 if (!cl_io_is_trunc(io))
1185 io->ci_lockreq = CILR_MANDATORY;
1188 /* ignore layout change for generic CIT_MISC but not for glimpse.
1189 * io context for glimpse must set ci_verify_layout to true,
1190 * see cl_glimpse_size0() for details. */
1191 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1192 io->ci_ignore_layout = 1;
1194 /* Enqueue layout lock and get layout version. We need to do this
1195 * even for operations requiring to open file, such as read and write,
1196 * because it might not grant layout lock in IT_OPEN. */
1197 if (result == 0 && !io->ci_ignore_layout) {
1198 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1199 if (result == -ENOENT)
1200 /* If the inode on MDS has been removed, but the objects
1201 * on OSTs haven't been destroyed (async unlink), layout
1202 * fetch will return -ENOENT, we'd ingore this error
1203 * and continue with dirty flush. LU-3230. */
1206 CERROR("%s: refresh file layout " DFID " error %d.\n",
1207 ll_get_fsname(inode->i_sb, NULL, 0),
1208 PFID(lu_object_fid(&obj->co_lu)), result);
1214 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1215 const struct cl_io_slice *slice)
1217 /* Caling just for assertion */
1218 cl2ccc_io(env, slice);
1219 return vvp_env_io(env);