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>
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
45 # error This file is kernel only.
49 #include <lustre_lite.h>
51 #include "vvp_internal.h"
53 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
54 const struct cl_io_slice *slice);
57 * True, if \a io is a normal io, False for sendfile() / splice_{read|write}
59 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
61 struct vvp_io *vio = vvp_env_io(env);
63 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
65 return vio->cui_io_subtype == IO_NORMAL;
68 /*****************************************************************************
74 static int vvp_io_fault_iter_init(const struct lu_env *env,
75 const struct cl_io_slice *ios)
77 struct vvp_io *vio = cl2vvp_io(env, ios);
78 struct inode *inode = ccc_object_inode(ios->cis_obj);
81 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
82 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
86 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
88 struct cl_io *io = ios->cis_io;
89 struct cl_object *obj = io->ci_obj;
91 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
92 if (io->ci_type == CIT_READ) {
93 struct vvp_io *vio = cl2vvp_io(env, ios);
94 struct ccc_io *cio = cl2ccc_io(env, ios);
96 if (vio->cui_ra_window_set)
97 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
102 static void vvp_io_fault_fini(const struct lu_env *env,
103 const struct cl_io_slice *ios)
105 struct cl_io *io = ios->cis_io;
106 struct cl_page *page = io->u.ci_fault.ft_page;
108 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
111 lu_ref_del(&page->cp_reference, "fault", io);
112 cl_page_put(env, page);
113 io->u.ci_fault.ft_page = NULL;
115 vvp_io_fini(env, ios);
118 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
121 * we only want to hold PW locks if the mmap() can generate
122 * writes back to the file and that only happens in shared
125 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
130 static int vvp_mmap_locks(const struct lu_env *env,
131 struct ccc_io *vio, struct cl_io *io)
133 struct ccc_thread_info *cti = ccc_env_info(env);
134 struct mm_struct *mm = current->mm;
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 /* No MM (e.g. NFS)? No vmas too. */
156 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
157 const struct iovec *iv = &vio->cui_iov[seg];
159 addr = (unsigned long)iv->iov_base;
164 count += addr & (~CFS_PAGE_MASK);
165 addr &= CFS_PAGE_MASK;
167 down_read(&mm->mmap_sem);
168 while((vma = our_vma(mm, addr, count)) != NULL) {
169 struct inode *inode = vma->vm_file->f_dentry->d_inode;
170 int flags = CEF_MUST;
172 if (ll_file_nolock(vma->vm_file)) {
174 * For no lock case, a lockless lock will be
181 * XXX: Required lock mode can be weakened: CIT_WRITE
182 * io only ever reads user level buffer, and CIT_READ
185 policy_from_vma(&policy, vma, addr, count);
186 descr->cld_mode = vvp_mode_from_vma(vma);
187 descr->cld_obj = ll_i2info(inode)->lli_clob;
188 descr->cld_start = cl_index(descr->cld_obj,
189 policy.l_extent.start);
190 descr->cld_end = cl_index(descr->cld_obj,
191 policy.l_extent.end);
192 descr->cld_enq_flags = flags;
193 result = cl_io_lock_alloc_add(env, io, descr);
195 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
196 descr->cld_mode, descr->cld_start,
202 if (vma->vm_end - addr >= count)
205 count -= vma->vm_end - addr;
208 up_read(&mm->mmap_sem);
213 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
214 enum cl_lock_mode mode, loff_t start, loff_t end)
216 struct ccc_io *cio = ccc_env_io(env);
220 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
223 ccc_io_update_iov(env, cio, io);
225 if (io->u.ci_rw.crw_nonblock)
226 ast_flags |= CEF_NONBLOCK;
227 result = vvp_mmap_locks(env, cio, io);
229 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
233 static int vvp_io_read_lock(const struct lu_env *env,
234 const struct cl_io_slice *ios)
236 struct cl_io *io = ios->cis_io;
237 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
241 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
242 if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
243 result = vvp_io_rw_lock(env, io, CLM_READ,
244 io->u.ci_rd.rd.crw_pos,
245 io->u.ci_rd.rd.crw_pos +
246 io->u.ci_rd.rd.crw_count - 1);
252 static int vvp_io_fault_lock(const struct lu_env *env,
253 const struct cl_io_slice *ios)
255 struct cl_io *io = ios->cis_io;
256 struct vvp_io *vio = cl2vvp_io(env, ios);
258 * XXX LDLM_FL_CBPENDING
260 return ccc_io_one_lock_index
261 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
262 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
265 static int vvp_io_write_lock(const struct lu_env *env,
266 const struct cl_io_slice *ios)
268 struct cl_io *io = ios->cis_io;
272 if (io->u.ci_wr.wr_append) {
274 end = OBD_OBJECT_EOF;
276 start = io->u.ci_wr.wr.crw_pos;
277 end = start + io->u.ci_wr.wr.crw_count - 1;
279 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
282 static int vvp_io_setattr_iter_init(const struct lu_env *env,
283 const struct cl_io_slice *ios)
285 struct ccc_io *cio = ccc_env_io(env);
286 struct inode *inode = ccc_object_inode(ios->cis_obj);
289 * We really need to get our PW lock before we change inode->i_size.
290 * If we don't we can race with other i_size updaters on our node,
291 * like ll_file_read. We can also race with i_size propogation to
292 * other nodes through dirtying and writeback of final cached pages.
293 * This last one is especially bad for racing o_append users on other
296 UNLOCK_INODE_MUTEX(inode);
297 if (cl_io_is_trunc(ios->cis_io))
298 UP_WRITE_I_ALLOC_SEM(inode);
299 cio->u.setattr.cui_locks_released = 1;
304 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
306 * Handles "lockless io" mode when extent locking is done by server.
308 static int vvp_io_setattr_lock(const struct lu_env *env,
309 const struct cl_io_slice *ios)
311 struct ccc_io *cio = ccc_env_io(env);
312 struct cl_io *io = ios->cis_io;
316 if (cl_io_is_trunc(io)) {
317 new_size = io->u.ci_setattr.sa_attr.lvb_size;
319 enqflags = CEF_DISCARD_DATA;
321 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
322 io->u.ci_setattr.sa_attr.lvb_ctime) ||
323 (io->u.ci_setattr.sa_attr.lvb_atime >=
324 io->u.ci_setattr.sa_attr.lvb_ctime))
328 cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
329 return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
330 new_size, OBD_OBJECT_EOF);
333 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
337 * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
338 * is grabbed by ll_truncate() only over call to obd_adjust_kms().
340 ll_inode_size_lock(inode, 0);
341 result = vmtruncate(inode, size);
342 ll_inode_size_unlock(inode, 0);
347 static int vvp_io_setattr_trunc(const struct lu_env *env,
348 const struct cl_io_slice *ios,
349 struct inode *inode, loff_t size)
351 DOWN_WRITE_I_ALLOC_SEM(inode);
355 static int vvp_io_setattr_time(const struct lu_env *env,
356 const struct cl_io_slice *ios)
358 struct cl_io *io = ios->cis_io;
359 struct cl_object *obj = io->ci_obj;
360 struct cl_attr *attr = ccc_env_thread_attr(env);
362 unsigned valid = CAT_CTIME;
364 cl_object_attr_lock(obj);
365 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
366 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
367 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
370 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
371 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
374 result = cl_object_attr_set(env, obj, attr, valid);
375 cl_object_attr_unlock(obj);
380 static int vvp_io_setattr_start(const struct lu_env *env,
381 const struct cl_io_slice *ios)
383 struct ccc_io *cio = cl2ccc_io(env, ios);
384 struct cl_io *io = ios->cis_io;
385 struct inode *inode = ccc_object_inode(io->ci_obj);
387 LASSERT(cio->u.setattr.cui_locks_released);
389 LOCK_INODE_MUTEX(inode);
390 cio->u.setattr.cui_locks_released = 0;
392 if (cl_io_is_trunc(io))
393 return vvp_io_setattr_trunc(env, ios, inode,
394 io->u.ci_setattr.sa_attr.lvb_size);
396 return vvp_io_setattr_time(env, ios);
399 static void vvp_io_setattr_end(const struct lu_env *env,
400 const struct cl_io_slice *ios)
402 struct cl_io *io = ios->cis_io;
403 struct inode *inode = ccc_object_inode(io->ci_obj);
405 if (!cl_io_is_trunc(io))
408 /* Truncate in memory pages - they must be clean pages because osc
409 * has already notified to destroy osc_extents. */
410 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
413 static void vvp_io_setattr_fini(const struct lu_env *env,
414 const struct cl_io_slice *ios)
416 struct ccc_io *cio = ccc_env_io(env);
417 struct cl_io *io = ios->cis_io;
418 struct inode *inode = ccc_object_inode(ios->cis_io->ci_obj);
420 if (cio->u.setattr.cui_locks_released) {
421 LOCK_INODE_MUTEX(inode);
422 if (cl_io_is_trunc(io))
423 DOWN_WRITE_I_ALLOC_SEM(inode);
424 cio->u.setattr.cui_locks_released = 0;
426 vvp_io_fini(env, ios);
429 #ifdef HAVE_FILE_READV
430 static ssize_t lustre_generic_file_read(struct file *file,
431 struct ccc_io *vio, loff_t *ppos)
433 return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
436 static ssize_t lustre_generic_file_write(struct file *file,
437 struct ccc_io *vio, loff_t *ppos)
439 return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
442 static ssize_t lustre_generic_file_read(struct file *file,
443 struct ccc_io *vio, loff_t *ppos)
445 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
446 vio->cui_nrsegs, *ppos);
449 static ssize_t lustre_generic_file_write(struct file *file,
450 struct ccc_io *vio, loff_t *ppos)
452 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
453 vio->cui_nrsegs, *ppos);
457 static int vvp_io_read_start(const struct lu_env *env,
458 const struct cl_io_slice *ios)
460 struct vvp_io *vio = cl2vvp_io(env, ios);
461 struct ccc_io *cio = cl2ccc_io(env, ios);
462 struct cl_io *io = ios->cis_io;
463 struct cl_object *obj = io->ci_obj;
464 struct inode *inode = ccc_object_inode(obj);
465 struct ll_ra_read *bead = &vio->cui_bead;
466 struct file *file = cio->cui_fd->fd_file;
469 loff_t pos = io->u.ci_rd.rd.crw_pos;
470 long cnt = io->u.ci_rd.rd.crw_count;
471 long tot = cio->cui_tot_count;
474 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
476 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
478 result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
481 else if (exceed != 0)
484 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
485 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
486 inode->i_ino, cnt, pos, i_size_read(inode));
488 /* turn off the kernel's read-ahead */
489 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
491 /* initialize read-ahead window once per syscall */
492 if (!vio->cui_ra_window_set) {
493 vio->cui_ra_window_set = 1;
494 bead->lrr_start = cl_index(obj, pos);
496 * XXX: explicit CFS_PAGE_SIZE
498 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
499 ll_ra_read_in(file, bead);
504 switch (vio->cui_io_subtype) {
506 result = lustre_generic_file_read(file, cio, &pos);
508 #ifdef HAVE_KERNEL_SENDFILE
510 result = generic_file_sendfile(file, &pos, cnt,
511 vio->u.sendfile.cui_actor,
512 vio->u.sendfile.cui_target);
515 #ifdef HAVE_KERNEL_SPLICE_READ
517 result = generic_file_splice_read(file, &pos,
518 vio->u.splice.cui_pipe, cnt,
519 vio->u.splice.cui_flags);
520 /* LU-1109: do splice read stripe by stripe otherwise if it
521 * may make nfsd stuck if this read occupied all internal pipe
527 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
535 io->ci_nob += result;
536 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
537 cio->cui_fd, pos, result, 0);
543 static int vvp_io_write_start(const struct lu_env *env,
544 const struct cl_io_slice *ios)
546 struct ccc_io *cio = cl2ccc_io(env, ios);
547 struct cl_io *io = ios->cis_io;
548 struct cl_object *obj = io->ci_obj;
549 struct inode *inode = ccc_object_inode(obj);
550 struct file *file = cio->cui_fd->fd_file;
552 loff_t pos = io->u.ci_wr.wr.crw_pos;
553 size_t cnt = io->u.ci_wr.wr.crw_count;
557 if (cl_io_is_append(io)) {
559 * PARALLEL IO This has to be changed for parallel IO doing
560 * out-of-order writes.
562 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
563 #ifndef HAVE_FILE_WRITEV
564 cio->cui_iocb->ki_pos = pos;
568 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
570 if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
573 result = lustre_generic_file_write(file, cio, &pos);
578 io->ci_nob += result;
579 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
580 cio->cui_fd, pos, result, 0);
586 #ifndef HAVE_VM_OP_FAULT
587 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
591 vmpage = filemap_nopage(cfio->ft_vma, cfio->nopage.ft_address,
592 cfio->nopage.ft_type);
594 if (vmpage == NOPAGE_SIGBUS) {
595 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
596 cfio->nopage.ft_address,(long)cfio->nopage.ft_type);
598 } else if (vmpage == NOPAGE_OOM) {
599 CDEBUG(D_PAGE, "got addr %lu type %lx - OOM\n",
600 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
604 LL_CDEBUG_PAGE(D_PAGE, vmpage, "got addr %lu type %lx\n",
605 cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
607 cfio->ft_vmpage = vmpage;
613 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
615 struct vm_fault *vmf = cfio->fault.ft_vmf;
617 cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
620 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
621 vmf->virtual_address);
622 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
623 lock_page(vmf->page);
624 cfio->fault.ft_flags &= VM_FAULT_LOCKED;
627 cfio->ft_vmpage = vmf->page;
631 if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
632 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
636 if (cfio->fault.ft_flags & VM_FAULT_OOM) {
637 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
641 if (cfio->fault.ft_flags & VM_FAULT_RETRY)
644 CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
650 static int vvp_io_fault_start(const struct lu_env *env,
651 const struct cl_io_slice *ios)
653 struct vvp_io *vio = cl2vvp_io(env, ios);
654 struct cl_io *io = ios->cis_io;
655 struct cl_object *obj = io->ci_obj;
656 struct inode *inode = ccc_object_inode(obj);
657 struct cl_fault_io *fio = &io->u.ci_fault;
658 struct vvp_fault_io *cfio = &vio->u.fault;
661 cfs_page_t *vmpage = NULL;
662 struct cl_page *page;
664 pgoff_t last; /* last page in a file data region */
666 if (fio->ft_executable &&
667 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
669 " changed while waiting for the page fault lock\n",
670 PFID(lu_object_fid(&obj->co_lu)));
672 /* offset of the last byte on the page */
673 offset = cl_offset(obj, fio->ft_index + 1) - 1;
674 LASSERT(cl_index(obj, offset) == fio->ft_index);
675 result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
679 /* must return locked page */
680 if (fio->ft_mkwrite) {
681 /* we grab alloc_sem to exclude truncate case.
682 * Otherwise, we could add dirty pages into osc cache
683 * while truncate is on-going. */
684 DOWN_READ_I_ALLOC_SEM(inode);
686 LASSERT(cfio->ft_vmpage != NULL);
687 lock_page(cfio->ft_vmpage);
689 result = vvp_io_kernel_fault(cfio);
694 vmpage = cfio->ft_vmpage;
695 LASSERT(PageLocked(vmpage));
697 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
698 ll_invalidate_page(vmpage);
700 /* Though we have already held a cl_lock upon this page, but
701 * it still can be truncated locally. */
702 if (unlikely(vmpage->mapping == NULL)) {
703 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
705 /* return +1 to stop cl_io_loop() and ll_fault() will catch
707 GOTO(out, result = +1);
710 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
712 GOTO(out, result = PTR_ERR(page));
714 /* if page is going to be written, we should add this page into cache
716 if (fio->ft_mkwrite) {
717 wait_on_page_writeback(vmpage);
718 if (set_page_dirty(vmpage)) {
721 /* vvp_page_assume() calls wait_on_page_writeback(). */
722 cl_page_assume(env, io, page);
724 cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
725 vvp_write_pending(cl2ccc(obj), cp);
727 /* Do not set Dirty bit here so that in case IO is
728 * started before the page is really made dirty, we
729 * still have chance to detect it. */
730 result = cl_page_cache_add(env, io, page, CRT_WRITE);
731 LASSERT(cl_page_is_owned(page, io));
735 cl_page_unmap(env, io, page);
736 cl_page_discard(env, io, page);
737 cl_page_disown(env, io, page);
739 cl_page_put(env, page);
741 /* we're in big trouble, what can we do now? */
742 if (result == -EDQUOT)
746 cl_page_disown(env, io, page);
750 size = i_size_read(inode);
751 last = cl_index(obj, size - 1);
752 LASSERT(fio->ft_index <= last);
753 if (fio->ft_index == last)
755 * Last page is mapped partially.
757 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
759 fio->ft_nob = cl_page_size(obj);
761 lu_ref_add(&page->cp_reference, "fault", io);
766 /* return unlocked vmpage to avoid deadlocking */
770 UP_READ_I_ALLOC_SEM(inode);
771 #ifdef HAVE_VM_OP_FAULT
772 cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
777 static int vvp_io_fsync_start(const struct lu_env *env,
778 const struct cl_io_slice *ios)
780 /* we should mark TOWRITE bit to each dirty page in radix tree to
781 * verify pages have been written, but this is difficult because of
786 static int vvp_io_read_page(const struct lu_env *env,
787 const struct cl_io_slice *ios,
788 const struct cl_page_slice *slice)
790 struct cl_io *io = ios->cis_io;
791 struct cl_object *obj = slice->cpl_obj;
792 struct ccc_page *cp = cl2ccc_page(slice);
793 struct cl_page *page = slice->cpl_page;
794 struct inode *inode = ccc_object_inode(obj);
795 struct ll_sb_info *sbi = ll_i2sbi(inode);
796 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
797 struct ll_readahead_state *ras = &fd->fd_ras;
798 cfs_page_t *vmpage = cp->cpg_page;
799 struct cl_2queue *queue = &io->ci_queue;
802 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
803 LASSERT(slice->cpl_obj == obj);
807 if (sbi->ll_ra_info.ra_max_pages_per_file &&
808 sbi->ll_ra_info.ra_max_pages)
809 ras_update(sbi, inode, ras, page->cp_index,
810 cp->cpg_defer_uptodate);
812 /* Sanity check whether the page is protected by a lock. */
813 rc = cl_page_is_under_lock(env, io, page);
815 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
816 rc == -ENODATA ? "without a lock" :
822 if (cp->cpg_defer_uptodate) {
824 cl_page_export(env, page, 1);
827 * Add page into the queue even when it is marked uptodate above.
828 * this will unlock it automatically as part of cl_page_list_disown().
830 cl_2queue_add(queue, page);
831 if (sbi->ll_ra_info.ra_max_pages_per_file &&
832 sbi->ll_ra_info.ra_max_pages)
833 ll_readahead(env, io, ras,
834 vmpage->mapping, &queue->c2_qin, fd->fd_flags);
839 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
840 struct cl_page *page, struct ccc_page *cp,
841 enum cl_req_type crt)
843 struct cl_2queue *queue;
846 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
848 queue = &io->ci_queue;
849 cl_2queue_init_page(queue, page);
851 result = cl_io_submit_sync(env, io, crt, queue, 0);
852 LASSERT(cl_page_is_owned(page, io));
856 * in CRT_WRITE case page is left locked even in case of
859 cl_page_list_disown(env, io, &queue->c2_qin);
860 cl_2queue_fini(env, queue);
866 * Prepare partially written-to page for a write.
868 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
869 struct cl_object *obj, struct cl_page *pg,
871 unsigned from, unsigned to)
873 struct cl_attr *attr = ccc_env_thread_attr(env);
874 loff_t offset = cl_offset(obj, pg->cp_index);
877 cl_object_attr_lock(obj);
878 result = cl_object_attr_get(env, obj, attr);
879 cl_object_attr_unlock(obj);
882 * If are writing to a new page, no need to read old data.
883 * The extent locking will have updated the KMS, and for our
884 * purposes here we can treat it like i_size.
886 if (attr->cat_kms <= offset) {
887 char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
889 memset(kaddr, 0, cl_page_size(obj));
890 kunmap_atomic(kaddr, KM_USER0);
891 } else if (cp->cpg_defer_uptodate)
894 result = vvp_page_sync_io(env, io, pg, cp, CRT_READ);
896 * In older implementations, obdo_refresh_inode is called here
897 * to update the inode because the write might modify the
898 * object info at OST. However, this has been proven useless,
899 * since LVB functions will be called when user space program
900 * tries to retrieve inode attribute. Also, see bug 15909 for
904 cl_page_export(env, pg, 1);
909 static int vvp_io_prepare_write(const struct lu_env *env,
910 const struct cl_io_slice *ios,
911 const struct cl_page_slice *slice,
912 unsigned from, unsigned to)
914 struct cl_object *obj = slice->cpl_obj;
915 struct ccc_page *cp = cl2ccc_page(slice);
916 struct cl_page *pg = slice->cpl_page;
917 cfs_page_t *vmpage = cp->cpg_page;
923 LINVRNT(cl_page_is_vmlocked(env, pg));
924 LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
928 CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
929 if (!PageUptodate(vmpage)) {
931 * We're completely overwriting an existing page, so _don't_
932 * set it up to date until commit_write
934 if (from == 0 && to == CFS_PAGE_SIZE) {
935 CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
936 POISON_PAGE(page, 0x11);
938 result = vvp_io_prepare_partial(env, ios->cis_io, obj,
941 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
945 static int vvp_io_commit_write(const struct lu_env *env,
946 const struct cl_io_slice *ios,
947 const struct cl_page_slice *slice,
948 unsigned from, unsigned to)
950 struct cl_object *obj = slice->cpl_obj;
951 struct cl_io *io = ios->cis_io;
952 struct ccc_page *cp = cl2ccc_page(slice);
953 struct cl_page *pg = slice->cpl_page;
954 struct inode *inode = ccc_object_inode(obj);
955 struct ll_sb_info *sbi = ll_i2sbi(inode);
956 cfs_page_t *vmpage = cp->cpg_page;
964 LINVRNT(cl_page_is_vmlocked(env, pg));
965 LASSERT(vmpage->mapping->host == inode);
967 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
968 CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
971 * queue a write for some time in the future the first time we
974 * This is different from what other file systems do: they usually
975 * just mark page (and some of its buffers) dirty and rely on
976 * balance_dirty_pages() to start a write-back. Lustre wants write-back
977 * to be started earlier for the following reasons:
979 * (1) with a large number of clients we need to limit the amount
980 * of cached data on the clients a lot;
982 * (2) large compute jobs generally want compute-only then io-only
983 * and the IO should complete as quickly as possible;
985 * (3) IO is batched up to the RPC size and is async until the
986 * client max cache is hit
987 * (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
990 if (!PageDirty(vmpage)) {
991 tallyop = LPROC_LL_DIRTY_MISSES;
992 vvp_write_pending(cl2ccc(obj), cp);
993 set_page_dirty(vmpage);
994 /* ll_set_page_dirty() does the same for now, but
995 * it will not soon. */
996 vvp_write_pending(cl2ccc(obj), cp);
997 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
998 if (result == -EDQUOT) {
999 pgoff_t last_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
1000 bool need_clip = true;
1003 * Client ran out of disk space grant. Possible
1006 * (a) do a sync write, renewing grant;
1008 * (b) stop writing on this stripe, switch to the
1011 * (b) is a part of "parallel io" design that is the
1012 * ultimate goal. (a) is what "old" client did, and
1013 * what the new code continues to do for the time
1016 if (last_index > pg->cp_index) {
1019 } else if (last_index == pg->cp_index) {
1020 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
1025 cl_page_clip(env, pg, 0, to);
1026 clear_page_dirty_for_io(vmpage);
1027 result = vvp_page_sync_io(env, io, pg, cp, CRT_WRITE);
1029 CERROR("Write page %lu of inode %p failed %d\n",
1030 pg->cp_index, inode, result);
1033 tallyop = LPROC_LL_DIRTY_HITS;
1036 ll_stats_ops_tally(sbi, tallyop, 1);
1038 size = cl_offset(obj, pg->cp_index) + to;
1040 ll_inode_size_lock(inode, 0);
1042 if (size > i_size_read(inode)) {
1043 cl_isize_write_nolock(inode, size);
1044 CDEBUG(D_VFSTRACE, DFID" updating i_size %lu\n",
1045 PFID(lu_object_fid(&obj->co_lu)),
1046 (unsigned long)size);
1048 cl_page_export(env, pg, 1);
1050 if (size > i_size_read(inode))
1051 cl_page_discard(env, io, pg);
1053 ll_inode_size_unlock(inode, 0);
1057 static const struct cl_io_operations vvp_io_ops = {
1060 .cio_fini = vvp_io_fini,
1061 .cio_lock = vvp_io_read_lock,
1062 .cio_start = vvp_io_read_start,
1063 .cio_advance = ccc_io_advance
1066 .cio_fini = vvp_io_fini,
1067 .cio_lock = vvp_io_write_lock,
1068 .cio_start = vvp_io_write_start,
1069 .cio_advance = ccc_io_advance
1072 .cio_fini = vvp_io_setattr_fini,
1073 .cio_iter_init = vvp_io_setattr_iter_init,
1074 .cio_lock = vvp_io_setattr_lock,
1075 .cio_start = vvp_io_setattr_start,
1076 .cio_end = vvp_io_setattr_end
1079 .cio_fini = vvp_io_fault_fini,
1080 .cio_iter_init = vvp_io_fault_iter_init,
1081 .cio_lock = vvp_io_fault_lock,
1082 .cio_start = vvp_io_fault_start,
1083 .cio_end = ccc_io_end
1086 .cio_start = vvp_io_fsync_start,
1087 .cio_fini = vvp_io_fini
1090 .cio_fini = vvp_io_fini
1093 .cio_read_page = vvp_io_read_page,
1094 .cio_prepare_write = vvp_io_prepare_write,
1095 .cio_commit_write = vvp_io_commit_write
1098 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1101 struct vvp_io *vio = vvp_env_io(env);
1102 struct ccc_io *cio = ccc_env_io(env);
1105 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1108 CL_IO_SLICE_CLEAN(cio, cui_cl);
1109 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1110 vio->cui_ra_window_set = 0;
1112 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1114 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(obj));
1116 count = io->u.ci_rw.crw_count;
1117 /* "If nbyte is 0, read() will return 0 and have no other
1118 * results." -- Single Unix Spec */
1122 cio->cui_tot_count = count;
1123 cio->cui_tot_nrsegs = 0;
1125 /* for read/write, we store the jobid in the inode, and
1126 * it'll be fetched by osc when building RPC.
1128 * it's not accurate if the file is shared by different
1131 lustre_get_jobid(lli->lli_jobid);
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);