1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Implementation of cl_io for VVP layer.
40 * Author: Nikita Danilov <nikita.danilov@sun.com>
43 #define DEBUG_SUBSYSTEM S_LLITE
46 # error This file is kernel only.
50 #include <lustre_lite.h>
52 #include "vvp_internal.h"
54 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
55 const struct cl_io_slice *slice);
58 * True, if \a io is a normal io, False for sendfile() / splice_{read|write}
60 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
62 struct vvp_io *vio = vvp_env_io(env);
64 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
66 return vio->cui_io_subtype == IO_NORMAL;
69 /*****************************************************************************
75 static int vvp_io_fault_iter_init(const struct lu_env *env,
76 const struct cl_io_slice *ios)
78 struct vvp_io *vio = cl2vvp_io(env, ios);
79 struct inode *inode = ccc_object_inode(ios->cis_obj);
82 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
83 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
87 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
89 struct cl_io *io = ios->cis_io;
90 struct cl_object *obj = io->ci_obj;
92 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
93 if (io->ci_type == CIT_READ) {
94 struct vvp_io *vio = cl2vvp_io(env, ios);
95 struct ccc_io *cio = cl2ccc_io(env, ios);
97 if (vio->cui_ra_window_set)
98 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
103 static void vvp_io_fault_fini(const struct lu_env *env,
104 const struct cl_io_slice *ios)
106 struct cl_io *io = ios->cis_io;
107 struct cl_page *page = io->u.ci_fault.ft_page;
109 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
112 lu_ref_del(&page->cp_reference, "fault", io);
113 cl_page_put(env, page);
114 io->u.ci_fault.ft_page = NULL;
116 vvp_io_fini(env, ios);
119 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
122 * we only want to hold PW locks if the mmap() can generate
123 * writes back to the file and that only happens in shared
126 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
131 static int vvp_mmap_locks(const struct lu_env *env,
132 struct ccc_io *vio, struct cl_io *io)
134 struct ccc_thread_info *cti = ccc_env_info(env);
135 struct vm_area_struct *vma;
136 struct cl_lock_descr *descr = &cti->cti_descr;
137 ldlm_policy_data_t policy;
144 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
146 if (!cl_is_normalio(env, io))
149 if (vio->cui_iov == NULL) /* nfs or loop back device write */
152 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
153 const struct iovec *iv = &vio->cui_iov[seg];
155 addr = (unsigned long)iv->iov_base;
160 count += addr & (~CFS_PAGE_MASK);
161 addr &= CFS_PAGE_MASK;
162 while((vma = our_vma(addr, count)) != NULL) {
163 struct inode *inode = vma->vm_file->f_dentry->d_inode;
164 int flags = CEF_MUST;
166 if (ll_file_nolock(vma->vm_file)) {
168 * For no lock case, a lockless lock will be
175 * XXX: Required lock mode can be weakened: CIT_WRITE
176 * io only ever reads user level buffer, and CIT_READ
179 policy_from_vma(&policy, vma, addr, count);
180 descr->cld_mode = vvp_mode_from_vma(vma);
181 descr->cld_obj = ll_i2info(inode)->lli_clob;
182 descr->cld_start = cl_index(descr->cld_obj,
183 policy.l_extent.start);
184 descr->cld_end = cl_index(descr->cld_obj,
185 policy.l_extent.end);
186 descr->cld_enq_flags = flags;
187 result = cl_io_lock_alloc_add(env, io, descr);
189 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
190 descr->cld_mode, descr->cld_start,
196 if (vma->vm_end - addr >= count)
199 count -= vma->vm_end - addr;
206 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
207 enum cl_lock_mode mode, loff_t start, loff_t end)
209 struct ccc_io *cio = ccc_env_io(env);
213 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
216 ccc_io_update_iov(env, cio, io);
218 if (io->u.ci_rw.crw_nonblock)
219 ast_flags |= CEF_NONBLOCK;
220 result = vvp_mmap_locks(env, cio, io);
222 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
226 static int vvp_io_read_lock(const struct lu_env *env,
227 const struct cl_io_slice *ios)
229 struct cl_io *io = ios->cis_io;
230 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
234 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
235 if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
236 result = vvp_io_rw_lock(env, io, CLM_READ,
237 io->u.ci_rd.rd.crw_pos,
238 io->u.ci_rd.rd.crw_pos +
239 io->u.ci_rd.rd.crw_count - 1);
245 static int vvp_io_fault_lock(const struct lu_env *env,
246 const struct cl_io_slice *ios)
248 struct cl_io *io = ios->cis_io;
249 struct vvp_io *vio = cl2vvp_io(env, ios);
251 * XXX LDLM_FL_CBPENDING
253 return ccc_io_one_lock_index
254 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
255 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
258 static int vvp_io_write_lock(const struct lu_env *env,
259 const struct cl_io_slice *ios)
261 struct cl_io *io = ios->cis_io;
265 if (io->u.ci_wr.wr_append) {
267 end = OBD_OBJECT_EOF;
269 start = io->u.ci_wr.wr.crw_pos;
270 end = start + io->u.ci_wr.wr.crw_count - 1;
272 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
275 static int vvp_io_setattr_iter_init(const struct lu_env *env,
276 const struct cl_io_slice *ios)
278 struct ccc_io *cio = ccc_env_io(env);
279 struct inode *inode = ccc_object_inode(ios->cis_obj);
282 * We really need to get our PW lock before we change inode->i_size.
283 * If we don't we can race with other i_size updaters on our node,
284 * like ll_file_read. We can also race with i_size propogation to
285 * other nodes through dirtying and writeback of final cached pages.
286 * This last one is especially bad for racing o_append users on other
289 UNLOCK_INODE_MUTEX(inode);
290 if (cl_io_is_trunc(ios->cis_io))
291 UP_WRITE_I_ALLOC_SEM(inode);
292 cio->u.setattr.cui_locks_released = 1;
297 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
299 * Handles "lockless io" mode when extent locking is done by server.
301 static int vvp_io_setattr_lock(const struct lu_env *env,
302 const struct cl_io_slice *ios)
304 struct ccc_io *cio = ccc_env_io(env);
305 struct cl_io *io = ios->cis_io;
309 if (cl_io_is_trunc(io)) {
310 new_size = io->u.ci_setattr.sa_attr.lvb_size;
312 enqflags = CEF_DISCARD_DATA;
314 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
315 io->u.ci_setattr.sa_attr.lvb_ctime) ||
316 (io->u.ci_setattr.sa_attr.lvb_atime >=
317 io->u.ci_setattr.sa_attr.lvb_ctime))
321 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
322 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
323 new_size, OBD_OBJECT_EOF);
326 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
330 * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
331 * is grabbed by ll_truncate() only over call to obd_adjust_kms().
333 ll_inode_size_lock(inode, 0);
334 result = vmtruncate(inode, size);
335 ll_inode_size_unlock(inode, 0);
340 static int vvp_io_setattr_trunc(const struct lu_env *env,
341 const struct cl_io_slice *ios,
342 struct inode *inode, loff_t size)
344 struct vvp_io *vio = cl2vvp_io(env, ios);
345 struct cl_io *io = ios->cis_io;
346 struct cl_object *obj = ios->cis_obj;
347 pgoff_t start = cl_index(obj, size);
350 DOWN_WRITE_I_ALLOC_SEM(inode);
352 result = vvp_do_vmtruncate(inode, size);
355 * If a page is partially truncated, keep it owned across truncate to
358 * XXX this properly belongs to osc, because races in question are OST
361 if (cl_offset(obj, start) != size) {
362 struct cl_object_header *hdr;
364 hdr = cl_object_header(obj);
365 cfs_spin_lock(&hdr->coh_page_guard);
366 vio->cui_partpage = cl_page_lookup(hdr, start);
367 cfs_spin_unlock(&hdr->coh_page_guard);
369 if (vio->cui_partpage != NULL)
371 * Wait for the transfer completion for a partially
372 * truncated page to avoid dead-locking an OST with
373 * the concurrent page-wise overlapping WRITE and
374 * PUNCH requests. BUG:17397.
376 * Partial page is disowned in vvp_io_trunc_end().
378 cl_page_own(env, io, vio->cui_partpage);
380 vio->cui_partpage = NULL;
384 static int vvp_io_setattr_time(const struct lu_env *env,
385 const struct cl_io_slice *ios)
387 struct cl_io *io = ios->cis_io;
388 struct cl_object *obj = io->ci_obj;
389 struct cl_attr *attr = ccc_env_thread_attr(env);
391 unsigned valid = CAT_CTIME;
393 cl_object_attr_lock(obj);
394 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
395 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
396 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
399 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
400 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
403 result = cl_object_attr_set(env, obj, attr, valid);
404 cl_object_attr_unlock(obj);
409 static int vvp_io_setattr_start(const struct lu_env *env,
410 const struct cl_io_slice *ios)
412 struct ccc_io *cio = cl2ccc_io(env, ios);
413 struct cl_io *io = ios->cis_io;
414 struct inode *inode = ccc_object_inode(io->ci_obj);
416 LASSERT(cio->u.setattr.cui_locks_released);
418 LOCK_INODE_MUTEX(inode);
419 cio->u.setattr.cui_locks_released = 0;
421 if (cl_io_is_trunc(io))
422 return vvp_io_setattr_trunc(env, ios, inode,
423 io->u.ci_setattr.sa_attr.lvb_size);
425 return vvp_io_setattr_time(env, ios);
428 static void vvp_io_setattr_end(const struct lu_env *env,
429 const struct cl_io_slice *ios)
431 struct vvp_io *vio = cl2vvp_io(env, ios);
432 struct cl_io *io = ios->cis_io;
433 struct inode *inode = ccc_object_inode(io->ci_obj);
435 if (!cl_io_is_trunc(io))
437 if (vio->cui_partpage != NULL) {
438 cl_page_disown(env, ios->cis_io, vio->cui_partpage);
439 cl_page_put(env, vio->cui_partpage);
440 vio->cui_partpage = NULL;
444 * Do vmtruncate again, to remove possible stale pages populated by
445 * competing read threads. bz20645.
447 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
450 static void vvp_io_setattr_fini(const struct lu_env *env,
451 const struct cl_io_slice *ios)
453 struct ccc_io *cio = ccc_env_io(env);
454 struct cl_io *io = ios->cis_io;
455 struct inode *inode = ccc_object_inode(ios->cis_io->ci_obj);
457 if (cio->u.setattr.cui_locks_released) {
458 LOCK_INODE_MUTEX(inode);
459 if (cl_io_is_trunc(io))
460 DOWN_WRITE_I_ALLOC_SEM(inode);
461 cio->u.setattr.cui_locks_released = 0;
463 vvp_io_fini(env, ios);
466 #ifdef HAVE_FILE_READV
467 static ssize_t lustre_generic_file_read(struct file *file,
468 struct ccc_io *vio, loff_t *ppos)
470 return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
473 static ssize_t lustre_generic_file_write(struct file *file,
474 struct ccc_io *vio, loff_t *ppos)
476 return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
479 static ssize_t lustre_generic_file_read(struct file *file,
480 struct ccc_io *vio, loff_t *ppos)
482 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
483 vio->cui_nrsegs, *ppos);
486 static ssize_t lustre_generic_file_write(struct file *file,
487 struct ccc_io *vio, loff_t *ppos)
489 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
490 vio->cui_nrsegs, *ppos);
494 static int vvp_io_read_start(const struct lu_env *env,
495 const struct cl_io_slice *ios)
497 struct vvp_io *vio = cl2vvp_io(env, ios);
498 struct ccc_io *cio = cl2ccc_io(env, ios);
499 struct cl_io *io = ios->cis_io;
500 struct cl_object *obj = io->ci_obj;
501 struct inode *inode = ccc_object_inode(obj);
502 struct ll_ra_read *bead = &vio->cui_bead;
503 struct file *file = cio->cui_fd->fd_file;
506 loff_t pos = io->u.ci_rd.rd.crw_pos;
507 long cnt = io->u.ci_rd.rd.crw_count;
508 long tot = cio->cui_tot_count;
511 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
513 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
515 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
518 else if (exceed != 0)
521 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
522 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
523 inode->i_ino, cnt, pos, i_size_read(inode));
525 /* turn off the kernel's read-ahead */
526 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
528 /* initialize read-ahead window once per syscall */
529 if (!vio->cui_ra_window_set) {
530 vio->cui_ra_window_set = 1;
531 bead->lrr_start = cl_index(obj, pos);
533 * XXX: explicit CFS_PAGE_SIZE
535 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
536 ll_ra_read_in(file, bead);
541 switch (vio->cui_io_subtype) {
543 result = lustre_generic_file_read(file, cio, &pos);
545 #ifdef HAVE_KERNEL_SENDFILE
547 result = generic_file_sendfile(file, &pos, cnt,
548 vio->u.sendfile.cui_actor,
549 vio->u.sendfile.cui_target);
552 #ifdef HAVE_KERNEL_SPLICE_READ
554 result = generic_file_splice_read(file, &pos,
555 vio->u.splice.cui_pipe, cnt,
556 vio->u.splice.cui_flags);
557 /* LU-1109: do splice read stripe by stripe otherwise if it
558 * may make nfsd stuck if this read occupied all internal pipe
564 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
572 io->ci_nob += result;
573 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
574 cio->cui_fd, pos, result, 0);
580 static int vvp_io_write_start(const struct lu_env *env,
581 const struct cl_io_slice *ios)
583 struct ccc_io *cio = cl2ccc_io(env, ios);
584 struct cl_io *io = ios->cis_io;
585 struct cl_object *obj = io->ci_obj;
586 struct inode *inode = ccc_object_inode(obj);
587 struct file *file = cio->cui_fd->fd_file;
589 loff_t pos = io->u.ci_wr.wr.crw_pos;
590 size_t cnt = io->u.ci_wr.wr.crw_count;
594 if (cl_io_is_append(io)) {
596 * PARALLEL IO This has to be changed for parallel IO doing
597 * out-of-order writes.
599 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
600 #ifndef HAVE_FILE_WRITEV
601 cio->cui_iocb->ki_pos = pos;
605 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
607 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
610 result = lustre_generic_file_write(file, cio, &pos);
615 io->ci_nob += result;
616 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
617 cio->cui_fd, pos, result, 0);
623 #ifndef HAVE_VM_OP_FAULT
624 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
628 vmpage = filemap_nopage(cfio->ft_vma, cfio->nopage.ft_address,
629 cfio->nopage.ft_type);
631 if (vmpage == NOPAGE_SIGBUS) {
632 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
633 cfio->nopage.ft_address,(long)cfio->nopage.ft_type);
635 } else if (vmpage == NOPAGE_OOM) {
636 CDEBUG(D_PAGE, "got addr %lu type %lx - OOM\n",
637 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
641 LL_CDEBUG_PAGE(D_PAGE, vmpage, "got addr %lu type %lx\n",
642 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
644 cfio->ft_vmpage = vmpage;
650 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
652 struct vm_fault *vmf = cfio->fault.ft_vmf;
654 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
657 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
658 vmf->virtual_address);
659 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
660 lock_page(vmf->page);
661 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
664 cfio->ft_vmpage = vmf->page;
668 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
669 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
673 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
674 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
678 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
681 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
687 static int vvp_io_fault_start(const struct lu_env *env,
688 const struct cl_io_slice *ios)
690 struct vvp_io *vio = cl2vvp_io(env, ios);
691 struct cl_io *io = ios->cis_io;
692 struct cl_object *obj = io->ci_obj;
693 struct inode *inode = ccc_object_inode(obj);
694 struct cl_fault_io *fio = &io->u.ci_fault;
695 struct vvp_fault_io *cfio = &vio->u.fault;
698 cfs_page_t *vmpage = NULL;
699 struct cl_page *page;
701 pgoff_t last; /* last page in a file data region */
703 if (fio->ft_executable &&
704 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
706 " changed while waiting for the page fault lock\n",
707 PFID(lu_object_fid(&obj->co_lu)));
709 /* offset of the last byte on the page */
710 offset = cl_offset(obj, fio->ft_index + 1) - 1;
711 LASSERT(cl_index(obj, offset) == fio->ft_index);
712 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
716 /* must return locked page */
717 if (fio->ft_mkwrite) {
718 LASSERT(cfio->ft_vmpage != NULL);
719 lock_page(cfio->ft_vmpage);
721 result = vvp_io_kernel_fault(cfio);
726 vmpage = cfio->ft_vmpage;
727 LASSERT(PageLocked(vmpage));
729 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
730 ll_invalidate_page(vmpage);
732 /* Though we have already held a cl_lock upon this page, but
733 * it still can be truncated locally. */
734 if (unlikely(vmpage->mapping == NULL)) {
735 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
737 /* return +1 to stop cl_io_loop() and ll_fault() will catch
739 GOTO(out, result = +1);
742 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
744 GOTO(out, result = PTR_ERR(page));
746 /* if page is going to be written, we should add this page into cache
748 if (fio->ft_mkwrite) {
749 wait_on_page_writeback(vmpage);
750 if (set_page_dirty(vmpage)) {
753 /* vvp_page_assume() calls wait_on_page_writeback(). */
754 cl_page_assume(env, io, page);
756 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
757 vvp_write_pending(cl2ccc(obj), cp);
759 /* Do not set Dirty bit here so that in case IO is
760 * started before the page is really made dirty, we
761 * still have chance to detect it. */
762 result = cl_page_cache_add(env, io, page, CRT_WRITE);
764 cl_page_unassume(env, io, page);
765 cl_page_put(env, page);
767 /* we're in big trouble, what can we do now? */
768 if (result == -EDQUOT)
775 size = i_size_read(inode);
776 last = cl_index(obj, size - 1);
777 LASSERT(fio->ft_index <= last);
778 if (fio->ft_index == last)
780 * Last page is mapped partially.
782 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
784 fio->ft_nob = cl_page_size(obj);
786 lu_ref_add(&page->cp_reference, "fault", io);
791 /* return unlocked vmpage to avoid deadlocking */
793 #ifdef HAVE_VM_OP_FAULT
794 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
799 static int vvp_io_read_page(const struct lu_env *env,
800 const struct cl_io_slice *ios,
801 const struct cl_page_slice *slice)
803 struct cl_io *io = ios->cis_io;
804 struct cl_object *obj = slice->cpl_obj;
805 struct ccc_page *cp = cl2ccc_page(slice);
806 struct cl_page *page = slice->cpl_page;
807 struct inode *inode = ccc_object_inode(obj);
808 struct ll_sb_info *sbi = ll_i2sbi(inode);
809 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
810 struct ll_readahead_state *ras = &fd->fd_ras;
811 cfs_page_t *vmpage = cp->cpg_page;
812 struct cl_2queue *queue = &io->ci_queue;
815 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
816 LASSERT(slice->cpl_obj == obj);
820 if (sbi->ll_ra_info.ra_max_pages_per_file &&
821 sbi->ll_ra_info.ra_max_pages)
822 ras_update(sbi, inode, ras, page->cp_index,
823 cp->cpg_defer_uptodate);
825 /* Sanity check whether the page is protected by a lock. */
826 rc = cl_page_is_under_lock(env, io, page);
828 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
829 rc == -ENODATA ? "without a lock" :
835 if (cp->cpg_defer_uptodate) {
837 cl_page_export(env, page, 1);
840 * Add page into the queue even when it is marked uptodate above.
841 * this will unlock it automatically as part of cl_page_list_disown().
843 cl_2queue_add(queue, page);
844 if (sbi->ll_ra_info.ra_max_pages_per_file &&
845 sbi->ll_ra_info.ra_max_pages)
846 ll_readahead(env, io, ras,
847 vmpage->mapping, &queue->c2_qin, fd->fd_flags);
852 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
853 struct cl_page *page, struct ccc_page *cp,
854 enum cl_req_type crt)
856 struct cl_2queue *queue;
859 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
861 queue = &io->ci_queue;
862 cl_2queue_init_page(queue, page);
864 result = cl_io_submit_sync(env, io, crt, queue, CRP_NORMAL, 0);
865 LASSERT(cl_page_is_owned(page, io));
869 * in CRT_WRITE case page is left locked even in case of
872 cl_page_list_disown(env, io, &queue->c2_qin);
873 cl_2queue_fini(env, queue);
879 * Prepare partially written-to page for a write.
881 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
882 struct cl_object *obj, struct cl_page *pg,
884 unsigned from, unsigned to)
886 struct cl_attr *attr = ccc_env_thread_attr(env);
887 loff_t offset = cl_offset(obj, pg->cp_index);
890 cl_object_attr_lock(obj);
891 result = cl_object_attr_get(env, obj, attr);
892 cl_object_attr_unlock(obj);
895 * If are writing to a new page, no need to read old data.
896 * The extent locking will have updated the KMS, and for our
897 * purposes here we can treat it like i_size.
899 if (attr->cat_kms <= offset) {
900 char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
902 memset(kaddr, 0, cl_page_size(obj));
903 kunmap_atomic(kaddr, KM_USER0);
904 } else if (cp->cpg_defer_uptodate)
907 result = vvp_page_sync_io(env, io, pg, cp, CRT_READ);
909 * In older implementations, obdo_refresh_inode is called here
910 * to update the inode because the write might modify the
911 * object info at OST. However, this has been proven useless,
912 * since LVB functions will be called when user space program
913 * tries to retrieve inode attribute. Also, see bug 15909 for
917 cl_page_export(env, pg, 1);
922 static int vvp_io_prepare_write(const struct lu_env *env,
923 const struct cl_io_slice *ios,
924 const struct cl_page_slice *slice,
925 unsigned from, unsigned to)
927 struct cl_object *obj = slice->cpl_obj;
928 struct ccc_page *cp = cl2ccc_page(slice);
929 struct cl_page *pg = slice->cpl_page;
930 cfs_page_t *vmpage = cp->cpg_page;
936 LINVRNT(cl_page_is_vmlocked(env, pg));
937 LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
941 CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
942 if (!PageUptodate(vmpage)) {
944 * We're completely overwriting an existing page, so _don't_
945 * set it up to date until commit_write
947 if (from == 0 && to == CFS_PAGE_SIZE) {
948 CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
949 POISON_PAGE(page, 0x11);
951 result = vvp_io_prepare_partial(env, ios->cis_io, obj,
954 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
958 static int vvp_io_commit_write(const struct lu_env *env,
959 const struct cl_io_slice *ios,
960 const struct cl_page_slice *slice,
961 unsigned from, unsigned to)
963 struct cl_object *obj = slice->cpl_obj;
964 struct cl_io *io = ios->cis_io;
965 struct ccc_page *cp = cl2ccc_page(slice);
966 struct cl_page *pg = slice->cpl_page;
967 struct inode *inode = ccc_object_inode(obj);
968 struct ll_sb_info *sbi = ll_i2sbi(inode);
969 cfs_page_t *vmpage = cp->cpg_page;
977 LINVRNT(cl_page_is_vmlocked(env, pg));
978 LASSERT(vmpage->mapping->host == inode);
980 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
981 CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
984 * queue a write for some time in the future the first time we
987 * This is different from what other file systems do: they usually
988 * just mark page (and some of its buffers) dirty and rely on
989 * balance_dirty_pages() to start a write-back. Lustre wants write-back
990 * to be started earlier for the following reasons:
992 * (1) with a large number of clients we need to limit the amount
993 * of cached data on the clients a lot;
995 * (2) large compute jobs generally want compute-only then io-only
996 * and the IO should complete as quickly as possible;
998 * (3) IO is batched up to the RPC size and is async until the
999 * client max cache is hit
1000 * (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
1003 if (!PageDirty(vmpage)) {
1004 tallyop = LPROC_LL_DIRTY_MISSES;
1005 vvp_write_pending(cl2ccc(obj), cp);
1006 set_page_dirty(vmpage);
1007 /* ll_set_page_dirty() does the same for now, but
1008 * it will not soon. */
1009 vvp_write_pending(cl2ccc(obj), cp);
1010 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
1011 if (result == -EDQUOT) {
1012 pgoff_t last_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
1013 bool need_clip = true;
1016 * Client ran out of disk space grant. Possible
1019 * (a) do a sync write, renewing grant;
1021 * (b) stop writing on this stripe, switch to the
1024 * (b) is a part of "parallel io" design that is the
1025 * ultimate goal. (a) is what "old" client did, and
1026 * what the new code continues to do for the time
1029 if (last_index > pg->cp_index) {
1032 } else if (last_index == pg->cp_index) {
1033 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
1038 cl_page_clip(env, pg, 0, to);
1039 result = vvp_page_sync_io(env, io, pg, cp, CRT_WRITE);
1041 CERROR("Write page %lu of inode %p failed %d\n",
1042 pg->cp_index, inode, result);
1045 tallyop = LPROC_LL_DIRTY_HITS;
1048 ll_stats_ops_tally(sbi, tallyop, 1);
1050 size = cl_offset(obj, pg->cp_index) + to;
1052 ll_inode_size_lock(inode, 0);
1054 if (size > i_size_read(inode)) {
1055 cl_isize_write_nolock(inode, size);
1056 CDEBUG(D_VFSTRACE, DFID" updating i_size %lu\n",
1057 PFID(lu_object_fid(&obj->co_lu)),
1058 (unsigned long)size);
1060 cl_page_export(env, pg, 1);
1062 if (size > i_size_read(inode))
1063 cl_page_discard(env, io, pg);
1065 ll_inode_size_unlock(inode, 0);
1069 static const struct cl_io_operations vvp_io_ops = {
1072 .cio_fini = vvp_io_fini,
1073 .cio_lock = vvp_io_read_lock,
1074 .cio_start = vvp_io_read_start,
1075 .cio_advance = ccc_io_advance
1078 .cio_fini = vvp_io_fini,
1079 .cio_lock = vvp_io_write_lock,
1080 .cio_start = vvp_io_write_start,
1081 .cio_advance = ccc_io_advance
1084 .cio_fini = vvp_io_setattr_fini,
1085 .cio_iter_init = vvp_io_setattr_iter_init,
1086 .cio_lock = vvp_io_setattr_lock,
1087 .cio_start = vvp_io_setattr_start,
1088 .cio_end = vvp_io_setattr_end
1091 .cio_fini = vvp_io_fault_fini,
1092 .cio_iter_init = vvp_io_fault_iter_init,
1093 .cio_lock = vvp_io_fault_lock,
1094 .cio_start = vvp_io_fault_start,
1095 .cio_end = ccc_io_end
1098 .cio_fini = vvp_io_fini
1101 .cio_read_page = vvp_io_read_page,
1102 .cio_prepare_write = vvp_io_prepare_write,
1103 .cio_commit_write = vvp_io_commit_write
1106 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1109 struct vvp_io *vio = vvp_env_io(env);
1110 struct ccc_io *cio = ccc_env_io(env);
1113 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1116 CL_IO_SLICE_CLEAN(cio, cui_cl);
1117 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1118 vio->cui_ra_window_set = 0;
1120 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1123 count = io->u.ci_rw.crw_count;
1124 /* "If nbyte is 0, read() will return 0 and have no other
1125 * results." -- Single Unix Spec */
1129 cio->cui_tot_count = count;
1130 cio->cui_tot_nrsegs = 0;
1132 } else if (io->ci_type == CIT_SETATTR) {
1133 if (!cl_io_is_trunc(io))
1134 io->ci_lockreq = CILR_MANDATORY;
1139 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1140 const struct cl_io_slice *slice)
1142 /* Caling just for assertion */
1143 cl2ccc_io(env, slice);
1144 return vvp_env_io(env);