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 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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);
55 /*****************************************************************************
61 static int vvp_io_fault_iter_init(const struct lu_env *env,
62 const struct cl_io_slice *ios)
64 struct vvp_io *vio = cl2vvp_io(env, ios);
65 struct inode *inode = ccc_object_inode(ios->cis_obj);
68 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
69 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
73 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
75 struct cl_io *io = ios->cis_io;
76 struct cl_object *obj = io->ci_obj;
78 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
79 if (io->ci_type == CIT_WRITE)
80 up(&ll_i2info(ccc_object_inode(obj))->lli_write_sem);
82 struct vvp_io *vio = cl2vvp_io(env, ios);
83 struct ccc_io *cio = cl2ccc_io(env, ios);
85 if (vio->cui_ra_window_set)
86 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
91 static void vvp_io_fault_fini(const struct lu_env *env,
92 const struct cl_io_slice *ios)
94 struct cl_io *io = ios->cis_io;
95 struct cl_page *page = io->u.ci_fault.ft_page;
97 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
100 lu_ref_del(&page->cp_reference, "fault", io);
101 cl_page_put(env, page);
102 io->u.ci_fault.ft_page = NULL;
104 vvp_io_fini(env, ios);
107 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
110 * we only want to hold PW locks if the mmap() can generate
111 * writes back to the file and that only happens in shared
114 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
119 static int vvp_mmap_locks(const struct lu_env *env,
120 struct ccc_io *vio, struct cl_io *io)
122 struct ccc_thread_info *cti = ccc_env_info(env);
123 struct vm_area_struct *vma;
124 struct cl_lock_descr *descr = &cti->cti_descr;
125 ldlm_policy_data_t policy;
133 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
135 if (cl_io_is_sendfile(io))
138 for (seg = 0; seg < vio->cui_nrsegs; seg++) {
139 const struct iovec *iv = &vio->cui_iov[seg];
141 addr = (unsigned long)iv->iov_base;
146 count += addr & (~CFS_PAGE_MASK);
147 addr &= CFS_PAGE_MASK;
148 while((vma = our_vma(addr, count)) != NULL) {
149 LASSERT(vma->vm_file);
151 inode = vma->vm_file->f_dentry->d_inode;
153 * XXX: Required lock mode can be weakened: CIT_WRITE
154 * io only ever reads user level buffer, and CIT_READ
157 policy_from_vma(&policy, vma, addr, count);
158 descr->cld_mode = vvp_mode_from_vma(vma);
159 descr->cld_obj = ll_i2info(inode)->lli_clob;
160 descr->cld_start = cl_index(descr->cld_obj,
161 policy.l_extent.start);
162 descr->cld_end = cl_index(descr->cld_obj,
163 policy.l_extent.end);
164 result = cl_io_lock_alloc_add(env, io, descr);
167 if (vma->vm_end - addr >= count)
169 count -= vma->vm_end - addr;
176 static void vvp_io_update_iov(const struct lu_env *env,
177 struct ccc_io *vio, struct cl_io *io)
180 size_t size = io->u.ci_rw.crw_count;
182 vio->cui_iov_olen = 0;
183 if (cl_io_is_sendfile(io) || size == vio->cui_tot_count)
186 if (vio->cui_tot_nrsegs == 0)
187 vio->cui_tot_nrsegs = vio->cui_nrsegs;
189 for (i = 0; i < vio->cui_tot_nrsegs; i++) {
190 struct iovec *iv = &vio->cui_iov[i];
192 if (iv->iov_len < size)
195 if (iv->iov_len > size) {
196 vio->cui_iov_olen = iv->iov_len;
203 vio->cui_nrsegs = i + 1;
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);
214 LASSERT(vvp_env_io(env)->cui_oneshot == 0);
217 vvp_io_update_iov(env, cio, io);
219 if (io->u.ci_rw.crw_nonblock)
220 ast_flags |= CEF_NONBLOCK;
221 result = vvp_mmap_locks(env, cio, io);
223 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
227 static int vvp_io_read_lock(const struct lu_env *env,
228 const struct cl_io_slice *ios)
230 struct cl_io *io = ios->cis_io;
231 struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
235 /* XXX: Layer violation, we shouldn't see lsm at llite level. */
236 if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
237 result = vvp_io_rw_lock(env, io, CLM_READ,
238 io->u.ci_rd.rd.crw_pos,
239 io->u.ci_rd.rd.crw_pos +
240 io->u.ci_rd.rd.crw_count - 1);
246 static int vvp_io_fault_lock(const struct lu_env *env,
247 const struct cl_io_slice *ios)
249 struct cl_io *io = ios->cis_io;
250 struct vvp_io *vio = cl2vvp_io(env, ios);
252 * XXX LDLM_FL_CBPENDING
254 return ccc_io_one_lock_index
255 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
256 io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
259 static int vvp_io_write_lock(const struct lu_env *env,
260 const struct cl_io_slice *ios)
262 struct cl_io *io = ios->cis_io;
267 if (cl2vvp_io(env, ios)->cui_oneshot == 0) {
268 if (io->u.ci_wr.wr_append) {
270 end = OBD_OBJECT_EOF;
272 start = io->u.ci_wr.wr.crw_pos;
273 end = start + io->u.ci_wr.wr.crw_count - 1;
275 result = vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
281 static int vvp_io_trunc_iter_init(const struct lu_env *env,
282 const struct cl_io_slice *ios)
284 struct ccc_io *vio = cl2ccc_io(env, ios);
285 struct inode *inode = ccc_object_inode(ios->cis_obj);
288 * We really need to get our PW lock before we change inode->i_size.
289 * If we don't we can race with other i_size updaters on our node,
290 * like ll_file_read. We can also race with i_size propogation to
291 * other nodes through dirtying and writeback of final cached pages.
292 * This last one is especially bad for racing o_append users on other
296 UNLOCK_INODE_MUTEX(inode);
297 UP_WRITE_I_ALLOC_SEM(inode);
298 vio->u.trunc.cui_locks_released = 1;
303 * Implementation of cl_io_operations::vio_lock() method for CIT_TRUNC io.
305 * Handles "lockless io" mode when extent locking is done by server.
307 static int vvp_io_trunc_lock(const struct lu_env *env,
308 const struct cl_io_slice *ios)
310 struct ccc_io *vio = cl2ccc_io(env, ios);
311 struct cl_io *io = ios->cis_io;
312 size_t new_size = io->u.ci_truncate.tr_size;
313 __u32 enqflags = new_size == 0 ? CEF_DISCARD_DATA : 0;
316 vio->u.trunc.cui_local_lock = TRUNC_EXTENT;
317 result = ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
318 new_size, OBD_OBJECT_EOF);
322 static int vvp_io_trunc_start(const struct lu_env *env,
323 const struct cl_io_slice *ios)
325 struct ccc_io *cio = cl2ccc_io(env, ios);
326 struct vvp_io *vio = cl2vvp_io(env, ios);
327 struct cl_io *io = ios->cis_io;
328 struct inode *inode = ccc_object_inode(io->ci_obj);
329 struct cl_object *obj = ios->cis_obj;
330 size_t size = io->u.ci_truncate.tr_size;
331 pgoff_t start = cl_index(obj, size);
334 LASSERT(cio->u.trunc.cui_locks_released);
335 LASSERT(vio->cui_oneshot == 0);
337 LOCK_INODE_MUTEX(inode);
338 DOWN_WRITE_I_ALLOC_SEM(inode);
339 cio->u.trunc.cui_locks_released = 0;
342 * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
343 * is grabbed by ll_truncate() only over call to obd_adjust_kms(). If
344 * vmtruncate returns 0, then ll_truncate dropped ll_inode_size_lock()
346 ll_inode_size_lock(inode, 0);
347 result = vmtruncate(inode, size);
349 ll_inode_size_unlock(inode, 0);
351 * If a page is partially truncated, keep it owned across truncate to
354 * XXX this properly belongs to osc, because races in question are OST
357 if (cl_offset(obj, start) != size) {
358 struct cl_object_header *hdr;
360 hdr = cl_object_header(obj);
361 spin_lock(&hdr->coh_page_guard);
362 vio->cui_partpage = cl_page_lookup(hdr, start);
363 spin_unlock(&hdr->coh_page_guard);
365 if (vio->cui_partpage != NULL)
367 * Wait for the transfer completion for a partially
368 * truncated page to avoid dead-locking an OST with
369 * the concurrent page-wise overlapping WRITE and
372 * Partial page is disowned in vvp_io_trunc_end().
374 cl_page_own(env, io, vio->cui_partpage);
376 vio->cui_partpage = NULL;
380 static void vvp_io_trunc_end(const struct lu_env *env,
381 const struct cl_io_slice *ios)
383 struct vvp_io *vio = cl2vvp_io(env, ios);
385 if (vio->cui_partpage != NULL) {
386 cl_page_disown(env, ios->cis_io, vio->cui_partpage);
387 cl_page_put(env, vio->cui_partpage);
388 vio->cui_partpage = NULL;
392 static void vvp_io_trunc_fini(const struct lu_env *env,
393 const struct cl_io_slice *ios)
395 struct ccc_io *cio = ccc_env_io(env);
396 struct inode *inode = ccc_object_inode(ios->cis_io->ci_obj);
398 if (cio->u.trunc.cui_locks_released) {
399 LOCK_INODE_MUTEX(inode);
400 DOWN_WRITE_I_ALLOC_SEM(inode);
401 cio->u.trunc.cui_locks_released = 0;
403 vvp_io_fini(env, ios);
406 #ifdef HAVE_FILE_READV
407 static ssize_t lustre_generic_file_read(struct file *file,
408 struct ccc_io *vio, loff_t *ppos)
410 return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
413 static ssize_t lustre_generic_file_write(struct file *file,
414 struct ccc_io *vio, loff_t *ppos)
416 return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
419 static ssize_t lustre_generic_file_read(struct file *file,
420 struct ccc_io *vio, loff_t *ppos)
422 return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
423 vio->cui_nrsegs, *ppos);
426 static ssize_t lustre_generic_file_write(struct file *file,
427 struct ccc_io *vio, loff_t *ppos)
429 return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
430 vio->cui_nrsegs, *ppos);
434 static int vvp_io_read_start(const struct lu_env *env,
435 const struct cl_io_slice *ios)
437 struct vvp_io *vio = cl2vvp_io(env, ios);
438 struct ccc_io *cio = cl2ccc_io(env, ios);
439 struct cl_io *io = ios->cis_io;
440 struct cl_object *obj = io->ci_obj;
441 struct inode *inode = ccc_object_inode(obj);
442 struct ll_ra_read *bead = &vio->cui_bead;
443 struct file *file = cio->cui_fd->fd_file;
446 loff_t pos = io->u.ci_rd.rd.crw_pos;
447 size_t cnt = io->u.ci_rd.rd.crw_count;
448 size_t tot = cio->cui_tot_count;
450 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
451 LASSERT(vio->cui_oneshot == 0);
453 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
455 result = ccc_prep_size(env, obj, io, pos + tot - 1, 1);
459 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
460 "Read ino %lu, "LPSZ" bytes, offset %lld, size %llu\n",
461 inode->i_ino, cnt, pos, i_size_read(inode));
463 /* turn off the kernel's read-ahead */
464 cio->cui_fd->fd_file->f_ra.ra_pages = 0;
466 /* initialize read-ahead window once per syscall */
467 if (!vio->cui_ra_window_set) {
468 vio->cui_ra_window_set = 1;
469 bead->lrr_start = cl_index(obj, pos);
471 * XXX: explicit CFS_PAGE_SIZE
473 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
474 ll_ra_read_in(file, bead);
479 if (cl_io_is_sendfile(io)) {
480 result = generic_file_sendfile(file, &pos, cnt,
481 vio->u.read.cui_actor, vio->u.read.cui_target);
483 result = lustre_generic_file_read(file, cio, &pos);
489 io->ci_nob += result;
490 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
491 cio->cui_fd, pos, result, 0);
497 static int vvp_io_write_start(const struct lu_env *env,
498 const struct cl_io_slice *ios)
500 struct ccc_io *cio = cl2ccc_io(env, ios);
501 struct cl_io *io = ios->cis_io;
502 struct cl_object *obj = io->ci_obj;
503 struct inode *inode = ccc_object_inode(obj);
504 struct file *file = cio->cui_fd->fd_file;
506 loff_t pos = io->u.ci_wr.wr.crw_pos;
507 size_t cnt = io->u.ci_wr.wr.crw_count;
511 if (cl_io_is_append(io))
513 * PARALLEL IO This has to be changed for parallel IO doing
514 * out-of-order writes.
516 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
518 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + cnt);
520 if (cl2vvp_io(env, ios)->cui_oneshot > 0)
523 result = lustre_generic_file_write(file, cio, &pos);
528 io->ci_nob += result;
529 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
530 cio->cui_fd, pos, result, 0);
536 static int vvp_io_fault_start(const struct lu_env *env,
537 const struct cl_io_slice *ios)
539 struct vvp_io *vio = cl2vvp_io(env, ios);
540 struct cl_io *io = ios->cis_io;
541 struct cl_object *obj = io->ci_obj;
542 struct inode *inode = ccc_object_inode(obj);
543 struct cl_fault_io *fio = &io->u.ci_fault;
544 struct vvp_fault_io *cfio = &vio->u.fault;
549 LASSERT(vio->cui_oneshot == 0);
551 if (fio->ft_executable &&
552 LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
554 " changed while waiting for the page fault lock\n",
555 PFID(lu_object_fid(&obj->co_lu)));
557 /* offset of the last byte on the page */
558 offset = cl_offset(obj, fio->ft_index + 1) - 1;
559 LASSERT(cl_index(obj, offset) == fio->ft_index);
560 result = ccc_prep_size(env, obj, io, offset, 0);
564 vmpage = filemap_nopage(cfio->ft_vma, cfio->ft_address, cfio->ft_type);
565 if (vmpage != NOPAGE_SIGBUS && vmpage != NOPAGE_OOM)
566 LL_CDEBUG_PAGE(D_PAGE, vmpage,
567 "got addr %lu type %lx\n",
568 cfio->ft_address, (long)cfio->ft_type);
570 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
571 cfio->ft_address, (long)cfio->ft_type);
573 if (vmpage == NOPAGE_SIGBUS)
575 else if (vmpage == NOPAGE_OOM)
578 struct cl_page *page;
580 pgoff_t last; /* last page in a file data region */
582 /* Temporarily lock vmpage to keep cl_page_find() happy. */
584 page = cl_page_find(env, obj, fio->ft_index, vmpage,
588 size = i_size_read(inode);
589 last = cl_index(obj, size - 1);
590 if (fio->ft_index == last)
592 * Last page is mapped partially.
594 fio->ft_nob = size - cl_offset(obj,
597 fio->ft_nob = cl_page_size(obj);
598 lu_ref_add(&page->cp_reference, "fault", io);
601 * Certain 2.6 kernels return not-NULL from
602 * filemap_nopage() when page is beyond the file size,
603 * on the grounds that "An external ptracer can access
604 * pages that normally aren't accessible.." Don't
605 * propagate such page fault to the lower layers to
606 * avoid side-effects like KMS updates.
608 if (fio->ft_index > last)
611 result = PTR_ERR(page);
616 static void vvp_io_advance(const struct lu_env *env,
617 const struct cl_io_slice *ios, size_t nob)
619 struct ccc_io *vio = cl2ccc_io(env, ios);
620 struct cl_io *io = ios->cis_io;
621 struct cl_object *obj = ios->cis_io->ci_obj;
623 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
625 if (!cl_io_is_sendfile(io) && io->ci_continue) {
627 LASSERT(vio->cui_tot_nrsegs >= vio->cui_nrsegs);
628 LASSERT(vio->cui_tot_count >= nob);
630 vio->cui_iov += vio->cui_nrsegs;
631 vio->cui_tot_nrsegs -= vio->cui_nrsegs;
632 vio->cui_tot_count -= nob;
634 if (vio->cui_iov_olen) {
638 vio->cui_tot_nrsegs++;
639 iv = &vio->cui_iov[0];
640 iv->iov_base += iv->iov_len;
641 LASSERT(vio->cui_iov_olen > iv->iov_len);
642 iv->iov_len = vio->cui_iov_olen - iv->iov_len;
647 static int vvp_io_read_page(const struct lu_env *env,
648 const struct cl_io_slice *ios,
649 const struct cl_page_slice *slice)
651 struct cl_io *io = ios->cis_io;
652 struct cl_object *obj = slice->cpl_obj;
653 struct ccc_page *cp = cl2ccc_page(slice);
654 struct cl_page *page = slice->cpl_page;
655 struct inode *inode = ccc_object_inode(obj);
656 struct ll_sb_info *sbi = ll_i2sbi(inode);
657 struct ll_file_data *fd = cl2ccc_io(env, ios)->cui_fd;
658 struct ll_readahead_state *ras = &fd->fd_ras;
659 cfs_page_t *vmpage = cp->cpg_page;
660 struct cl_2queue *queue = &io->ci_queue;
662 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
663 LASSERT(cl2vvp_io(env, ios)->cui_oneshot == 0);
664 LASSERT(slice->cpl_obj == obj);
668 if (sbi->ll_ra_info.ra_max_pages)
669 ras_update(sbi, inode, ras, page->cp_index,
670 cp->cpg_defer_uptodate);
672 /* Sanity check whether the page is protected by a lock. */
673 if (likely(!(fd->fd_flags & LL_FILE_IGNORE_LOCK))) {
676 rc = cl_page_is_under_lock(env, io, page);
678 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %i\n",
679 rc == -ENODATA ? "without a lock" :
686 if (cp->cpg_defer_uptodate) {
688 cl_page_export(env, page);
691 * Add page into the queue even when it is marked uptodate above.
692 * this will unlock it automatically as part of cl_page_list_disown().
694 cl_2queue_add(queue, page);
695 if (sbi->ll_ra_info.ra_max_pages)
696 ll_readahead(env, io, ras,
697 vmpage->mapping, &queue->c2_qin, fd->fd_flags);
702 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
703 struct cl_page *page, struct ccc_page *cp,
704 int from, int to, enum cl_req_type crt)
706 struct cl_2queue *queue;
707 struct ccc_object *cobo = cl2ccc(page->cp_obj);
708 struct cl_sync_io *anchor = &ccc_env_info(env)->cti_sync_io;
710 int writing = io->ci_type == CIT_WRITE;
713 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
715 queue = &io->ci_queue;
717 cl_2queue_init_page(queue, page);
720 /* Do not pass llap here as it is sync write. */
721 vvp_write_pending(cobo, cp);
723 cl_sync_io_init(anchor, 1);
724 cp->cpg_sync_io = anchor;
725 cl_page_clip(env, page, from, to);
726 result = cl_io_submit_rw(env, io, crt, queue);
728 result = cl_sync_io_wait(env, io, &queue->c2_qout, anchor);
730 cp->cpg_sync_io = NULL;
731 LASSERT(cl_page_is_owned(page, io));
732 cl_page_clip(env, page, 0, CFS_PAGE_SIZE);
736 * in CRT_WRITE case page is left locked even in case of
739 cl_page_list_disown(env, io, &queue->c2_qin);
740 cl_2queue_fini(env, queue);
746 * Prepare partially written-to page for a write.
748 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
749 struct cl_object *obj, struct cl_page *pg,
751 unsigned from, unsigned to)
753 struct cl_attr *attr = &ccc_env_info(env)->cti_attr;
754 loff_t offset = cl_offset(obj, pg->cp_index);
757 cl_object_attr_lock(obj);
758 result = cl_object_attr_get(env, obj, attr);
759 cl_object_attr_unlock(obj);
762 * If are writing to a new page, no need to read old data.
763 * The extent locking will have updated the KMS, and for our
764 * purposes here we can treat it like i_size.
766 if (attr->cat_kms <= offset) {
767 char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
769 memset(kaddr, 0, cl_page_size(obj));
770 kunmap_atomic(kaddr, KM_USER0);
771 } else if (cp->cpg_defer_uptodate)
774 result = vvp_page_sync_io(env, io, pg, cp,
775 0, CFS_PAGE_SIZE, CRT_READ);
777 * In older implementations, obdo_refresh_inode is called here
778 * to update the inode because the write might modify the
779 * object info at OST. However, this has been proven useless,
780 * since LVB functions will be called when user space program
781 * tries to retrieve inode attribute. Also, see bug 15909 for
785 cl_page_export(env, pg);
790 static int vvp_io_prepare_write(const struct lu_env *env,
791 const struct cl_io_slice *ios,
792 const struct cl_page_slice *slice,
793 unsigned from, unsigned to)
795 struct cl_object *obj = slice->cpl_obj;
796 struct ccc_page *cp = cl2ccc_page(slice);
797 struct cl_page *pg = slice->cpl_page;
798 cfs_page_t *vmpage = cp->cpg_page;
804 LINVRNT(cl_page_is_vmlocked(env, pg));
805 LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
809 CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
810 if (!PageUptodate(vmpage)) {
812 * We're completely overwriting an existing page, so _don't_
813 * set it up to date until commit_write
815 if (from == 0 && to == CFS_PAGE_SIZE) {
816 CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
817 POISON_PAGE(page, 0x11);
819 result = vvp_io_prepare_partial(env, ios->cis_io, obj,
822 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
826 static int vvp_io_commit_write(const struct lu_env *env,
827 const struct cl_io_slice *ios,
828 const struct cl_page_slice *slice,
829 unsigned from, unsigned to)
831 struct cl_object *obj = slice->cpl_obj;
832 struct cl_io *io = ios->cis_io;
833 struct ccc_page *cp = cl2ccc_page(slice);
834 struct cl_page *pg = slice->cpl_page;
835 struct inode *inode = ccc_object_inode(obj);
836 struct ll_sb_info *sbi = ll_i2sbi(inode);
837 cfs_page_t *vmpage = cp->cpg_page;
845 LINVRNT(cl_page_is_vmlocked(env, pg));
846 LASSERT(vmpage->mapping->host == inode);
848 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
849 CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
852 * queue a write for some time in the future the first time we
855 * This is different from what other file systems do: they usually
856 * just mark page (and some of its buffers) dirty and rely on
857 * balance_dirty_pages() to start a write-back. Lustre wants write-back
858 * to be started earlier for the following reasons:
860 * (1) with a large number of clients we need to limit the amount
861 * of cached data on the clients a lot;
863 * (2) large compute jobs generally want compute-only then io-only
864 * and the IO should complete as quickly as possible;
866 * (3) IO is batched up to the RPC size and is async until the
867 * client max cache is hit
868 * (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
871 if (!PageDirty(vmpage)) {
872 tallyop = LPROC_LL_DIRTY_MISSES;
873 vvp_write_pending(cl2ccc(obj), cp);
874 set_page_dirty(vmpage);
875 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
876 if (result == -EDQUOT)
878 * Client ran out of disk space grant. Possible
881 * (a) do a sync write, renewing grant;
883 * (b) stop writing on this stripe, switch to the
886 * (b) is a part of "parallel io" design that is the
887 * ultimate goal. (a) is what "old" client did, and
888 * what the new code continues to do for the time
891 result = vvp_page_sync_io(env, io, pg, cp,
892 from, to, CRT_WRITE);
894 tallyop = LPROC_LL_DIRTY_HITS;
897 ll_stats_ops_tally(sbi, tallyop, 1);
899 size = cl_offset(obj, pg->cp_index) + to;
902 if (size > i_size_read(inode))
903 i_size_write(inode, size);
904 cl_page_export(env, pg);
905 } else if (size > i_size_read(inode))
906 cl_page_discard(env, io, pg);
910 static const struct cl_io_operations vvp_io_ops = {
913 .cio_fini = vvp_io_fini,
914 .cio_lock = vvp_io_read_lock,
915 .cio_start = vvp_io_read_start,
916 .cio_advance = vvp_io_advance
919 .cio_fini = vvp_io_fini,
920 .cio_lock = vvp_io_write_lock,
921 .cio_start = vvp_io_write_start,
922 .cio_advance = vvp_io_advance
925 .cio_fini = vvp_io_trunc_fini,
926 .cio_iter_init = vvp_io_trunc_iter_init,
927 .cio_lock = vvp_io_trunc_lock,
928 .cio_start = vvp_io_trunc_start,
929 .cio_end = vvp_io_trunc_end
932 .cio_fini = vvp_io_fault_fini,
933 .cio_iter_init = vvp_io_fault_iter_init,
934 .cio_lock = vvp_io_fault_lock,
935 .cio_start = vvp_io_fault_start,
936 .cio_end = ccc_io_end
939 .cio_fini = vvp_io_fini
942 .cio_read_page = vvp_io_read_page,
943 .cio_prepare_write = vvp_io_prepare_write,
944 .cio_commit_write = vvp_io_commit_write
947 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
950 struct vvp_io *vio = vvp_env_io(env);
951 struct ccc_io *cio = ccc_env_io(env);
952 struct inode *inode = ccc_object_inode(obj);
953 struct ll_sb_info *sbi = ll_i2sbi(inode);
956 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
959 CL_IO_SLICE_CLEAN(cio, cui_cl);
960 cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
961 vio->cui_oneshot = 0;
962 vio->cui_ra_window_set = 0;
964 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
968 count = io->u.ci_rw.crw_count;
969 op = io->ci_type == CIT_READ ?
970 LPROC_LL_READ_BYTES : LPROC_LL_WRITE_BYTES;
971 if (io->ci_type == CIT_WRITE)
972 down(&ll_i2info(inode)->lli_write_sem);
973 /* "If nbyte is 0, read() will return 0 and have no other
974 * results." -- Single Unix Spec */
978 cio->cui_tot_count = count;
979 cio->cui_tot_nrsegs = 0;
980 ll_stats_ops_tally(sbi, op, count);
982 } else if (io->ci_type == CIT_TRUNC) {
983 /* lockless truncate? */
984 ll_stats_ops_tally(sbi, LPROC_LL_TRUNC, 1);
989 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
990 const struct cl_io_slice *slice)
992 /* Caling just for assertion */
993 cl2ccc_io(env, slice);
994 return vvp_env_io(env);