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, 2012, Whamcloud, Inc.
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>
41 #define DEBUG_SUBSYSTEM S_LLITE
44 # error This file is kernel only.
48 #include <lustre_lite.h>
50 #include "vvp_internal.h"
52 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
53 const struct cl_io_slice *slice);
56 * True, if \a io is a normal io, False for sendfile() / splice_{read|write}
58 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
60 struct vvp_io *vio = vvp_env_io(env);
62 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
64 return vio->cui_io_subtype == IO_NORMAL;
67 /*****************************************************************************
73 static int vvp_io_fault_iter_init(const struct lu_env *env,
74 const struct cl_io_slice *ios)
76 struct vvp_io *vio = cl2vvp_io(env, ios);
77 struct inode *inode = ccc_object_inode(ios->cis_obj);
80 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
81 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
85 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
87 struct cl_io *io = ios->cis_io;
88 struct cl_object *obj = io->ci_obj;
90 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
91 if (io->ci_type == CIT_READ) {
92 struct vvp_io *vio = cl2vvp_io(env, ios);
93 struct ccc_io *cio = cl2ccc_io(env, ios);
95 if (vio->cui_ra_window_set)
96 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
101 static void vvp_io_fault_fini(const struct lu_env *env,
102 const struct cl_io_slice *ios)
104 struct cl_io *io = ios->cis_io;
105 struct cl_page *page = io->u.ci_fault.ft_page;
107 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
110 lu_ref_del(&page->cp_reference, "fault", io);
111 cl_page_put(env, page);
112 io->u.ci_fault.ft_page = NULL;
114 vvp_io_fini(env, ios);
117 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
120 * we only want to hold PW locks if the mmap() can generate
121 * writes back to the file and that only happens in shared
124 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
129 static int vvp_mmap_locks(const struct lu_env *env,
130 struct ccc_io *vio, struct cl_io *io)
132 struct ccc_thread_info *cti = ccc_env_info(env);
133 struct vm_area_struct *vma;
134 struct cl_lock_descr *descr = &cti->cti_descr;
135 ldlm_policy_data_t policy;
142 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
144 if (!cl_is_normalio(env, io))
147 if (vio->cui_iov == NULL) /* nfs or loop back device write */
150 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
151 const struct iovec *iv = &vio->cui_iov[seg];
153 addr = (unsigned long)iv->iov_base;
158 count += addr & (~CFS_PAGE_MASK);
159 addr &= CFS_PAGE_MASK;
160 while((vma = our_vma(addr, count)) != NULL) {
161 struct inode *inode = vma->vm_file->f_dentry->d_inode;
162 int flags = CEF_MUST;
164 if (ll_file_nolock(vma->vm_file)) {
166 * For no lock case, a lockless lock will be
173 * XXX: Required lock mode can be weakened: CIT_WRITE
174 * io only ever reads user level buffer, and CIT_READ
177 policy_from_vma(&policy, vma, addr, count);
178 descr->cld_mode = vvp_mode_from_vma(vma);
179 descr->cld_obj = ll_i2info(inode)->lli_clob;
180 descr->cld_start = cl_index(descr->cld_obj,
181 policy.l_extent.start);
182 descr->cld_end = cl_index(descr->cld_obj,
183 policy.l_extent.end);
184 descr->cld_enq_flags = flags;
185 result = cl_io_lock_alloc_add(env, io, descr);
187 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
188 descr->cld_mode, descr->cld_start,
194 if (vma->vm_end - addr >= count)
197 count -= vma->vm_end - addr;
204 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
205 enum cl_lock_mode mode, loff_t start, loff_t end)
207 struct ccc_io *cio = ccc_env_io(env);
211 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
214 ccc_io_update_iov(env, cio, io);
216 if (io->u.ci_rw.crw_nonblock)
217 ast_flags |= CEF_NONBLOCK;
218 result = vvp_mmap_locks(env, cio, io);
220 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
224 static int vvp_io_read_lock(const struct lu_env *env,
225 const struct cl_io_slice *ios)
227 struct cl_io *io = ios->cis_io;
228 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
232 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
233 if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
234 result = vvp_io_rw_lock(env, io, CLM_READ,
235 io->u.ci_rd.rd.crw_pos,
236 io->u.ci_rd.rd.crw_pos +
237 io->u.ci_rd.rd.crw_count - 1);
243 static int vvp_io_fault_lock(const struct lu_env *env,
244 const struct cl_io_slice *ios)
246 struct cl_io *io = ios->cis_io;
247 struct vvp_io *vio = cl2vvp_io(env, ios);
249 * XXX LDLM_FL_CBPENDING
251 return ccc_io_one_lock_index
252 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
253 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
256 static int vvp_io_write_lock(const struct lu_env *env,
257 const struct cl_io_slice *ios)
259 struct cl_io *io = ios->cis_io;
263 if (io->u.ci_wr.wr_append) {
265 end = OBD_OBJECT_EOF;
267 start = io->u.ci_wr.wr.crw_pos;
268 end = start + io->u.ci_wr.wr.crw_count - 1;
270 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
273 static int vvp_io_setattr_iter_init(const struct lu_env *env,
274 const struct cl_io_slice *ios)
276 struct ccc_io *cio = ccc_env_io(env);
277 struct inode *inode = ccc_object_inode(ios->cis_obj);
280 * We really need to get our PW lock before we change inode->i_size.
281 * If we don't we can race with other i_size updaters on our node,
282 * like ll_file_read. We can also race with i_size propogation to
283 * other nodes through dirtying and writeback of final cached pages.
284 * This last one is especially bad for racing o_append users on other
287 UNLOCK_INODE_MUTEX(inode);
288 if (cl_io_is_trunc(ios->cis_io))
289 UP_WRITE_I_ALLOC_SEM(inode);
290 cio->u.setattr.cui_locks_released = 1;
295 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
297 * Handles "lockless io" mode when extent locking is done by server.
299 static int vvp_io_setattr_lock(const struct lu_env *env,
300 const struct cl_io_slice *ios)
302 struct ccc_io *cio = ccc_env_io(env);
303 struct cl_io *io = ios->cis_io;
307 if (cl_io_is_trunc(io)) {
308 new_size = io->u.ci_setattr.sa_attr.lvb_size;
310 enqflags = CEF_DISCARD_DATA;
312 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
313 io->u.ci_setattr.sa_attr.lvb_ctime) ||
314 (io->u.ci_setattr.sa_attr.lvb_atime >=
315 io->u.ci_setattr.sa_attr.lvb_ctime))
319 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
320 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
321 new_size, OBD_OBJECT_EOF);
324 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
328 * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
329 * is grabbed by ll_truncate() only over call to obd_adjust_kms().
331 ll_inode_size_lock(inode, 0);
332 result = vmtruncate(inode, size);
333 ll_inode_size_unlock(inode, 0);
338 static int vvp_io_setattr_trunc(const struct lu_env *env,
339 const struct cl_io_slice *ios,
340 struct inode *inode, loff_t size)
342 struct vvp_io *vio = cl2vvp_io(env, ios);
343 struct cl_io *io = ios->cis_io;
344 struct cl_object *obj = ios->cis_obj;
345 pgoff_t start = cl_index(obj, size);
348 DOWN_WRITE_I_ALLOC_SEM(inode);
350 result = vvp_do_vmtruncate(inode, size);
353 * If a page is partially truncated, keep it owned across truncate to
356 * XXX this properly belongs to osc, because races in question are OST
359 if (cl_offset(obj, start) != size) {
360 struct cl_object_header *hdr;
362 hdr = cl_object_header(obj);
363 cfs_spin_lock(&hdr->coh_page_guard);
364 vio->cui_partpage = cl_page_lookup(hdr, start);
365 cfs_spin_unlock(&hdr->coh_page_guard);
367 if (vio->cui_partpage != NULL)
369 * Wait for the transfer completion for a partially
370 * truncated page to avoid dead-locking an OST with
371 * the concurrent page-wise overlapping WRITE and
372 * PUNCH requests. BUG:17397.
374 * Partial page is disowned in vvp_io_trunc_end().
376 cl_page_own(env, io, vio->cui_partpage);
378 vio->cui_partpage = NULL;
382 static int vvp_io_setattr_time(const struct lu_env *env,
383 const struct cl_io_slice *ios)
385 struct cl_io *io = ios->cis_io;
386 struct cl_object *obj = io->ci_obj;
387 struct cl_attr *attr = ccc_env_thread_attr(env);
389 unsigned valid = CAT_CTIME;
391 cl_object_attr_lock(obj);
392 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
393 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
394 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
397 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
398 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
401 result = cl_object_attr_set(env, obj, attr, valid);
402 cl_object_attr_unlock(obj);
407 static int vvp_io_setattr_start(const struct lu_env *env,
408 const struct cl_io_slice *ios)
410 struct ccc_io *cio = cl2ccc_io(env, ios);
411 struct cl_io *io = ios->cis_io;
412 struct inode *inode = ccc_object_inode(io->ci_obj);
414 LASSERT(cio->u.setattr.cui_locks_released);
416 LOCK_INODE_MUTEX(inode);
417 cio->u.setattr.cui_locks_released = 0;
419 if (cl_io_is_trunc(io))
420 return vvp_io_setattr_trunc(env, ios, inode,
421 io->u.ci_setattr.sa_attr.lvb_size);
423 return vvp_io_setattr_time(env, ios);
426 static void vvp_io_setattr_end(const struct lu_env *env,
427 const struct cl_io_slice *ios)
429 struct vvp_io *vio = cl2vvp_io(env, ios);
430 struct cl_io *io = ios->cis_io;
431 struct inode *inode = ccc_object_inode(io->ci_obj);
433 if (!cl_io_is_trunc(io))
435 if (vio->cui_partpage != NULL) {
436 cl_page_disown(env, ios->cis_io, vio->cui_partpage);
437 cl_page_put(env, vio->cui_partpage);
438 vio->cui_partpage = NULL;
442 * Do vmtruncate again, to remove possible stale pages populated by
443 * competing read threads. bz20645.
445 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
448 static void vvp_io_setattr_fini(const struct lu_env *env,
449 const struct cl_io_slice *ios)
451 struct ccc_io *cio = ccc_env_io(env);
452 struct cl_io *io = ios->cis_io;
453 struct inode *inode = ccc_object_inode(ios->cis_io->ci_obj);
455 if (cio->u.setattr.cui_locks_released) {
456 LOCK_INODE_MUTEX(inode);
457 if (cl_io_is_trunc(io))
458 DOWN_WRITE_I_ALLOC_SEM(inode);
459 cio->u.setattr.cui_locks_released = 0;
461 vvp_io_fini(env, ios);
464 #ifdef HAVE_FILE_READV
465 static ssize_t lustre_generic_file_read(struct file *file,
466 struct ccc_io *vio, loff_t *ppos)
468 return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
471 static ssize_t lustre_generic_file_write(struct file *file,
472 struct ccc_io *vio, loff_t *ppos)
474 return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
477 static ssize_t lustre_generic_file_read(struct file *file,
478 struct ccc_io *vio, loff_t *ppos)
480 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
481 vio->cui_nrsegs, *ppos);
484 static ssize_t lustre_generic_file_write(struct file *file,
485 struct ccc_io *vio, loff_t *ppos)
487 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
488 vio->cui_nrsegs, *ppos);
492 static int vvp_io_read_start(const struct lu_env *env,
493 const struct cl_io_slice *ios)
495 struct vvp_io *vio = cl2vvp_io(env, ios);
496 struct ccc_io *cio = cl2ccc_io(env, ios);
497 struct cl_io *io = ios->cis_io;
498 struct cl_object *obj = io->ci_obj;
499 struct inode *inode = ccc_object_inode(obj);
500 struct ll_ra_read *bead = &vio->cui_bead;
501 struct file *file = cio->cui_fd->fd_file;
504 loff_t pos = io->u.ci_rd.rd.crw_pos;
505 long cnt = io->u.ci_rd.rd.crw_count;
506 long tot = cio->cui_tot_count;
509 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
511 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
513 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
516 else if (exceed != 0)
519 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
520 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
521 inode->i_ino, cnt, pos, i_size_read(inode));
523 /* turn off the kernel's read-ahead */
524 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
526 /* initialize read-ahead window once per syscall */
527 if (!vio->cui_ra_window_set) {
528 vio->cui_ra_window_set = 1;
529 bead->lrr_start = cl_index(obj, pos);
531 * XXX: explicit CFS_PAGE_SIZE
533 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
534 ll_ra_read_in(file, bead);
539 switch (vio->cui_io_subtype) {
541 result = lustre_generic_file_read(file, cio, &pos);
543 #ifdef HAVE_KERNEL_SENDFILE
545 result = generic_file_sendfile(file, &pos, cnt,
546 vio->u.sendfile.cui_actor,
547 vio->u.sendfile.cui_target);
550 #ifdef HAVE_KERNEL_SPLICE_READ
552 result = generic_file_splice_read(file, &pos,
553 vio->u.splice.cui_pipe, cnt,
554 vio->u.splice.cui_flags);
555 /* LU-1109: do splice read stripe by stripe otherwise if it
556 * may make nfsd stuck if this read occupied all internal pipe
562 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
570 io->ci_nob += result;
571 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
572 cio->cui_fd, pos, result, 0);
578 static int vvp_io_write_start(const struct lu_env *env,
579 const struct cl_io_slice *ios)
581 struct ccc_io *cio = cl2ccc_io(env, ios);
582 struct cl_io *io = ios->cis_io;
583 struct cl_object *obj = io->ci_obj;
584 struct inode *inode = ccc_object_inode(obj);
585 struct file *file = cio->cui_fd->fd_file;
587 loff_t pos = io->u.ci_wr.wr.crw_pos;
588 size_t cnt = io->u.ci_wr.wr.crw_count;
592 if (cl_io_is_append(io)) {
594 * PARALLEL IO This has to be changed for parallel IO doing
595 * out-of-order writes.
597 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
598 #ifndef HAVE_FILE_WRITEV
599 cio->cui_iocb->ki_pos = pos;
603 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
605 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
608 result = lustre_generic_file_write(file, cio, &pos);
613 io->ci_nob += result;
614 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
615 cio->cui_fd, pos, result, 0);
621 #ifndef HAVE_VM_OP_FAULT
622 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
626 vmpage = filemap_nopage(cfio->ft_vma, cfio->nopage.ft_address,
627 cfio->nopage.ft_type);
629 if (vmpage == NOPAGE_SIGBUS) {
630 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
631 cfio->nopage.ft_address,(long)cfio->nopage.ft_type);
633 } else if (vmpage == NOPAGE_OOM) {
634 CDEBUG(D_PAGE, "got addr %lu type %lx - OOM\n",
635 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
639 LL_CDEBUG_PAGE(D_PAGE, vmpage, "got addr %lu type %lx\n",
640 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
642 cfio->ft_vmpage = vmpage;
648 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
650 struct vm_fault *vmf = cfio->fault.ft_vmf;
652 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
655 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
656 vmf->virtual_address);
657 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
658 lock_page(vmf->page);
659 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
662 cfio->ft_vmpage = vmf->page;
666 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
667 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
671 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
672 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
676 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
679 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
685 static int vvp_io_fault_start(const struct lu_env *env,
686 const struct cl_io_slice *ios)
688 struct vvp_io *vio = cl2vvp_io(env, ios);
689 struct cl_io *io = ios->cis_io;
690 struct cl_object *obj = io->ci_obj;
691 struct inode *inode = ccc_object_inode(obj);
692 struct cl_fault_io *fio = &io->u.ci_fault;
693 struct vvp_fault_io *cfio = &vio->u.fault;
696 cfs_page_t *vmpage = NULL;
697 struct cl_page *page;
699 pgoff_t last; /* last page in a file data region */
701 if (fio->ft_executable &&
702 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
704 " changed while waiting for the page fault lock\n",
705 PFID(lu_object_fid(&obj->co_lu)));
707 /* offset of the last byte on the page */
708 offset = cl_offset(obj, fio->ft_index + 1) - 1;
709 LASSERT(cl_index(obj, offset) == fio->ft_index);
710 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
714 /* must return locked page */
715 if (fio->ft_mkwrite) {
716 LASSERT(cfio->ft_vmpage != NULL);
717 lock_page(cfio->ft_vmpage);
719 result = vvp_io_kernel_fault(cfio);
724 vmpage = cfio->ft_vmpage;
725 LASSERT(PageLocked(vmpage));
727 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
728 ll_invalidate_page(vmpage);
730 /* Though we have already held a cl_lock upon this page, but
731 * it still can be truncated locally. */
732 if (unlikely(vmpage->mapping == NULL)) {
733 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
735 /* return +1 to stop cl_io_loop() and ll_fault() will catch
737 GOTO(out, result = +1);
740 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
742 GOTO(out, result = PTR_ERR(page));
744 /* if page is going to be written, we should add this page into cache
746 if (fio->ft_mkwrite) {
747 wait_on_page_writeback(vmpage);
748 if (set_page_dirty(vmpage)) {
751 /* vvp_page_assume() calls wait_on_page_writeback(). */
752 cl_page_assume(env, io, page);
754 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
755 vvp_write_pending(cl2ccc(obj), cp);
757 /* Do not set Dirty bit here so that in case IO is
758 * started before the page is really made dirty, we
759 * still have chance to detect it. */
760 result = cl_page_cache_add(env, io, page, CRT_WRITE);
762 cl_page_unassume(env, io, page);
763 cl_page_put(env, page);
765 /* we're in big trouble, what can we do now? */
766 if (result == -EDQUOT)
773 size = i_size_read(inode);
774 last = cl_index(obj, size - 1);
775 LASSERT(fio->ft_index <= last);
776 if (fio->ft_index == last)
778 * Last page is mapped partially.
780 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
782 fio->ft_nob = cl_page_size(obj);
784 lu_ref_add(&page->cp_reference, "fault", io);
789 /* return unlocked vmpage to avoid deadlocking */
791 #ifdef HAVE_VM_OP_FAULT
792 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
797 static int vvp_io_read_page(const struct lu_env *env,
798 const struct cl_io_slice *ios,
799 const struct cl_page_slice *slice)
801 struct cl_io *io = ios->cis_io;
802 struct cl_object *obj = slice->cpl_obj;
803 struct ccc_page *cp = cl2ccc_page(slice);
804 struct cl_page *page = slice->cpl_page;
805 struct inode *inode = ccc_object_inode(obj);
806 struct ll_sb_info *sbi = ll_i2sbi(inode);
807 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
808 struct ll_readahead_state *ras = &fd->fd_ras;
809 cfs_page_t *vmpage = cp->cpg_page;
810 struct cl_2queue *queue = &io->ci_queue;
813 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
814 LASSERT(slice->cpl_obj == obj);
818 if (sbi->ll_ra_info.ra_max_pages_per_file &&
819 sbi->ll_ra_info.ra_max_pages)
820 ras_update(sbi, inode, ras, page->cp_index,
821 cp->cpg_defer_uptodate);
823 /* Sanity check whether the page is protected by a lock. */
824 rc = cl_page_is_under_lock(env, io, page);
826 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
827 rc == -ENODATA ? "without a lock" :
833 if (cp->cpg_defer_uptodate) {
835 cl_page_export(env, page, 1);
838 * Add page into the queue even when it is marked uptodate above.
839 * this will unlock it automatically as part of cl_page_list_disown().
841 cl_2queue_add(queue, page);
842 if (sbi->ll_ra_info.ra_max_pages_per_file &&
843 sbi->ll_ra_info.ra_max_pages)
844 ll_readahead(env, io, ras,
845 vmpage->mapping, &queue->c2_qin, fd->fd_flags);
850 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
851 struct cl_page *page, struct ccc_page *cp,
852 enum cl_req_type crt)
854 struct cl_2queue *queue;
857 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
859 queue = &io->ci_queue;
860 cl_2queue_init_page(queue, page);
862 result = cl_io_submit_sync(env, io, crt, queue, CRP_NORMAL, 0);
863 LASSERT(cl_page_is_owned(page, io));
867 * in CRT_WRITE case page is left locked even in case of
870 cl_page_list_disown(env, io, &queue->c2_qin);
871 cl_2queue_fini(env, queue);
877 * Prepare partially written-to page for a write.
879 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
880 struct cl_object *obj, struct cl_page *pg,
882 unsigned from, unsigned to)
884 struct cl_attr *attr = ccc_env_thread_attr(env);
885 loff_t offset = cl_offset(obj, pg->cp_index);
888 cl_object_attr_lock(obj);
889 result = cl_object_attr_get(env, obj, attr);
890 cl_object_attr_unlock(obj);
893 * If are writing to a new page, no need to read old data.
894 * The extent locking will have updated the KMS, and for our
895 * purposes here we can treat it like i_size.
897 if (attr->cat_kms <= offset) {
898 char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
900 memset(kaddr, 0, cl_page_size(obj));
901 kunmap_atomic(kaddr, KM_USER0);
902 } else if (cp->cpg_defer_uptodate)
905 result = vvp_page_sync_io(env, io, pg, cp, CRT_READ);
907 * In older implementations, obdo_refresh_inode is called here
908 * to update the inode because the write might modify the
909 * object info at OST. However, this has been proven useless,
910 * since LVB functions will be called when user space program
911 * tries to retrieve inode attribute. Also, see bug 15909 for
915 cl_page_export(env, pg, 1);
920 static int vvp_io_prepare_write(const struct lu_env *env,
921 const struct cl_io_slice *ios,
922 const struct cl_page_slice *slice,
923 unsigned from, unsigned to)
925 struct cl_object *obj = slice->cpl_obj;
926 struct ccc_page *cp = cl2ccc_page(slice);
927 struct cl_page *pg = slice->cpl_page;
928 cfs_page_t *vmpage = cp->cpg_page;
934 LINVRNT(cl_page_is_vmlocked(env, pg));
935 LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
939 CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
940 if (!PageUptodate(vmpage)) {
942 * We're completely overwriting an existing page, so _don't_
943 * set it up to date until commit_write
945 if (from == 0 && to == CFS_PAGE_SIZE) {
946 CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
947 POISON_PAGE(page, 0x11);
949 result = vvp_io_prepare_partial(env, ios->cis_io, obj,
952 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
956 static int vvp_io_commit_write(const struct lu_env *env,
957 const struct cl_io_slice *ios,
958 const struct cl_page_slice *slice,
959 unsigned from, unsigned to)
961 struct cl_object *obj = slice->cpl_obj;
962 struct cl_io *io = ios->cis_io;
963 struct ccc_page *cp = cl2ccc_page(slice);
964 struct cl_page *pg = slice->cpl_page;
965 struct inode *inode = ccc_object_inode(obj);
966 struct ll_sb_info *sbi = ll_i2sbi(inode);
967 cfs_page_t *vmpage = cp->cpg_page;
975 LINVRNT(cl_page_is_vmlocked(env, pg));
976 LASSERT(vmpage->mapping->host == inode);
978 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
979 CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
982 * queue a write for some time in the future the first time we
985 * This is different from what other file systems do: they usually
986 * just mark page (and some of its buffers) dirty and rely on
987 * balance_dirty_pages() to start a write-back. Lustre wants write-back
988 * to be started earlier for the following reasons:
990 * (1) with a large number of clients we need to limit the amount
991 * of cached data on the clients a lot;
993 * (2) large compute jobs generally want compute-only then io-only
994 * and the IO should complete as quickly as possible;
996 * (3) IO is batched up to the RPC size and is async until the
997 * client max cache is hit
998 * (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
1001 if (!PageDirty(vmpage)) {
1002 tallyop = LPROC_LL_DIRTY_MISSES;
1003 vvp_write_pending(cl2ccc(obj), cp);
1004 set_page_dirty(vmpage);
1005 /* ll_set_page_dirty() does the same for now, but
1006 * it will not soon. */
1007 vvp_write_pending(cl2ccc(obj), cp);
1008 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
1009 if (result == -EDQUOT) {
1010 pgoff_t last_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
1011 bool need_clip = true;
1014 * Client ran out of disk space grant. Possible
1017 * (a) do a sync write, renewing grant;
1019 * (b) stop writing on this stripe, switch to the
1022 * (b) is a part of "parallel io" design that is the
1023 * ultimate goal. (a) is what "old" client did, and
1024 * what the new code continues to do for the time
1027 if (last_index > pg->cp_index) {
1030 } else if (last_index == pg->cp_index) {
1031 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
1036 cl_page_clip(env, pg, 0, to);
1037 result = vvp_page_sync_io(env, io, pg, cp, CRT_WRITE);
1039 CERROR("Write page %lu of inode %p failed %d\n",
1040 pg->cp_index, inode, result);
1043 tallyop = LPROC_LL_DIRTY_HITS;
1046 ll_stats_ops_tally(sbi, tallyop, 1);
1048 size = cl_offset(obj, pg->cp_index) + to;
1050 ll_inode_size_lock(inode, 0);
1052 if (size > i_size_read(inode)) {
1053 cl_isize_write_nolock(inode, size);
1054 CDEBUG(D_VFSTRACE, DFID" updating i_size %lu\n",
1055 PFID(lu_object_fid(&obj->co_lu)),
1056 (unsigned long)size);
1058 cl_page_export(env, pg, 1);
1060 if (size > i_size_read(inode))
1061 cl_page_discard(env, io, pg);
1063 ll_inode_size_unlock(inode, 0);
1067 static const struct cl_io_operations vvp_io_ops = {
1070 .cio_fini = vvp_io_fini,
1071 .cio_lock = vvp_io_read_lock,
1072 .cio_start = vvp_io_read_start,
1073 .cio_advance = ccc_io_advance
1076 .cio_fini = vvp_io_fini,
1077 .cio_lock = vvp_io_write_lock,
1078 .cio_start = vvp_io_write_start,
1079 .cio_advance = ccc_io_advance
1082 .cio_fini = vvp_io_setattr_fini,
1083 .cio_iter_init = vvp_io_setattr_iter_init,
1084 .cio_lock = vvp_io_setattr_lock,
1085 .cio_start = vvp_io_setattr_start,
1086 .cio_end = vvp_io_setattr_end
1089 .cio_fini = vvp_io_fault_fini,
1090 .cio_iter_init = vvp_io_fault_iter_init,
1091 .cio_lock = vvp_io_fault_lock,
1092 .cio_start = vvp_io_fault_start,
1093 .cio_end = ccc_io_end
1096 .cio_fini = vvp_io_fini
1099 .cio_read_page = vvp_io_read_page,
1100 .cio_prepare_write = vvp_io_prepare_write,
1101 .cio_commit_write = vvp_io_commit_write
1104 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1107 struct vvp_io *vio = vvp_env_io(env);
1108 struct ccc_io *cio = ccc_env_io(env);
1111 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1114 CL_IO_SLICE_CLEAN(cio, cui_cl);
1115 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1116 vio->cui_ra_window_set = 0;
1118 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1121 count = io->u.ci_rw.crw_count;
1122 /* "If nbyte is 0, read() will return 0 and have no other
1123 * results." -- Single Unix Spec */
1127 cio->cui_tot_count = count;
1128 cio->cui_tot_nrsegs = 0;
1130 } else if (io->ci_type == CIT_SETATTR) {
1131 if (!cl_io_is_trunc(io))
1132 io->ci_lockreq = CILR_MANDATORY;
1137 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1138 const struct cl_io_slice *slice)
1140 /* Caling just for assertion */
1141 cl2ccc_io(env, slice);
1142 return vvp_env_io(env);