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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_io for VVP layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
42 #include "llite_internal.h"
43 #include "vvp_internal.h"
45 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
46 const struct cl_io_slice *slice)
50 vio = container_of(slice, struct vvp_io, vui_cl);
51 LASSERT(vio == vvp_env_io(env));
57 * True, if \a io is a normal io, False for splice_{read,write}
59 static 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->vui_io_subtype == IO_NORMAL;
69 * For swapping layout. The file's layout may have changed.
70 * To avoid populating pages to a wrong stripe, we have to verify the
71 * correctness of layout. It works because swapping layout processes
72 * have to acquire group lock.
74 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
77 struct ll_inode_info *lli = ll_i2info(inode);
78 struct vvp_io *vio = vvp_env_io(env);
81 switch (io->ci_type) {
84 /* don't need lock here to check lli_layout_gen as we have held
85 * extent lock and GROUP lock has to hold to swap layout */
86 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
87 OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
88 io->ci_need_restart = 1;
89 /* this will cause a short read/write */
94 /* fault is okay because we've already had a page. */
102 static void vvp_object_size_lock(struct cl_object *obj)
104 struct inode *inode = vvp_object_inode(obj);
106 ll_inode_size_lock(inode);
107 cl_object_attr_lock(obj);
110 static void vvp_object_size_unlock(struct cl_object *obj)
112 struct inode *inode = vvp_object_inode(obj);
114 cl_object_attr_unlock(obj);
115 ll_inode_size_unlock(inode);
119 * Helper function that if necessary adjusts file size (inode->i_size), when
120 * position at the offset \a pos is accessed. File size can be arbitrary stale
121 * on a Lustre client, but client at least knows KMS. If accessed area is
122 * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
124 * Locking: i_size_lock is used to serialize changes to inode size and to
125 * protect consistency between inode size and cl_object
126 * attributes. cl_object_size_lock() protects consistency between cl_attr's of
127 * top-object and sub-objects.
129 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
130 struct cl_io *io, loff_t start, size_t count,
133 struct cl_attr *attr = vvp_env_thread_attr(env);
134 struct inode *inode = vvp_object_inode(obj);
135 loff_t pos = start + count - 1;
140 * Consistency guarantees: following possibilities exist for the
141 * relation between region being accessed and real file size at this
144 * (A): the region is completely inside of the file;
146 * (B-x): x bytes of region are inside of the file, the rest is
149 * (C): the region is completely outside of the file.
151 * This classification is stable under DLM lock already acquired by
152 * the caller, because to change the class, other client has to take
153 * DLM lock conflicting with our lock. Also, any updates to ->i_size
154 * by other threads on this client are serialized by
155 * ll_inode_size_lock(). This guarantees that short reads are handled
156 * correctly in the face of concurrent writes and truncates.
158 vvp_object_size_lock(obj);
159 result = cl_object_attr_get(env, obj, attr);
164 * A glimpse is necessary to determine whether we
165 * return a short read (B) or some zeroes at the end
168 vvp_object_size_unlock(obj);
169 result = cl_glimpse_lock(env, io, inode, obj, 0);
170 if (result == 0 && exceed != NULL) {
171 /* If objective page index exceed end-of-file
172 * page index, return directly. Do not expect
173 * kernel will check such case correctly.
174 * linux-2.6.18-128.1.1 miss to do that.
176 loff_t size = i_size_read(inode);
177 unsigned long cur_index = start >>
180 if ((size == 0 && cur_index != 0) ||
181 (((size - 1) >> PAGE_SHIFT) <
189 * region is within kms and, hence, within real file
190 * size (A). We need to increase i_size to cover the
191 * read region so that generic_file_read() will do its
192 * job, but that doesn't mean the kms size is
193 * _correct_, it is only the _minimum_ size. If
194 * someone does a stat they will get the correct size
195 * which will always be >= the kms value here.
198 if (i_size_read(inode) < kms) {
199 i_size_write(inode, kms);
201 DFID" updating i_size %llu\n",
202 PFID(lu_object_fid(&obj->co_lu)),
203 (__u64)i_size_read(inode));
208 vvp_object_size_unlock(obj);
213 /*****************************************************************************
219 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
220 __u32 enqflags, enum cl_lock_mode mode,
221 pgoff_t start, pgoff_t end)
223 struct vvp_io *vio = vvp_env_io(env);
224 struct cl_lock_descr *descr = &vio->vui_link.cill_descr;
225 struct cl_object *obj = io->ci_obj;
227 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
230 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
232 memset(&vio->vui_link, 0, sizeof vio->vui_link);
234 if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
235 descr->cld_mode = CLM_GROUP;
236 descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid;
237 enqflags |= CEF_LOCK_MATCH;
239 descr->cld_mode = mode;
242 descr->cld_obj = obj;
243 descr->cld_start = start;
244 descr->cld_end = end;
245 descr->cld_enq_flags = enqflags;
247 cl_io_lock_add(env, io, &vio->vui_link);
252 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
253 __u32 enqflags, enum cl_lock_mode mode,
254 loff_t start, loff_t end)
256 struct cl_object *obj = io->ci_obj;
258 return vvp_io_one_lock_index(env, io, enqflags, mode,
259 cl_index(obj, start), cl_index(obj, end));
262 static int vvp_io_write_iter_init(const struct lu_env *env,
263 const struct cl_io_slice *ios)
265 struct vvp_io *vio = cl2vvp_io(env, ios);
267 cl_page_list_init(&vio->u.write.vui_queue);
268 vio->u.write.vui_written = 0;
269 vio->u.write.vui_from = 0;
270 vio->u.write.vui_to = PAGE_SIZE;
275 static void vvp_io_write_iter_fini(const struct lu_env *env,
276 const struct cl_io_slice *ios)
278 struct vvp_io *vio = cl2vvp_io(env, ios);
280 LASSERT(vio->u.write.vui_queue.pl_nr == 0);
283 static int vvp_io_fault_iter_init(const struct lu_env *env,
284 const struct cl_io_slice *ios)
286 struct vvp_io *vio = cl2vvp_io(env, ios);
287 struct inode *inode = vvp_object_inode(ios->cis_obj);
289 LASSERT(inode == file_inode(vio->vui_fd->fd_file));
290 vio->u.fault.ft_mtime = inode->i_mtime.tv_sec;
295 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
297 struct cl_io *io = ios->cis_io;
298 struct cl_object *obj = io->ci_obj;
299 struct vvp_io *vio = cl2vvp_io(env, ios);
300 struct inode *inode = vvp_object_inode(obj);
303 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
305 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
306 "need write layout %d, restore needed %d\n",
307 PFID(lu_object_fid(&obj->co_lu)),
308 io->ci_ignore_layout, io->ci_verify_layout,
309 vio->vui_layout_gen, io->ci_need_write_intent,
310 io->ci_restore_needed);
312 if (io->ci_restore_needed) {
313 /* file was detected release, we need to restore it
314 * before finishing the io
316 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
317 /* if restore registration failed, no restart,
318 * we will return -ENODATA */
319 /* The layout will change after restore, so we need to
320 * block on layout lock held by the MDT
321 * as MDT will not send new layout in lvb (see LU-3124)
322 * we have to explicitly fetch it, all this will be done
323 * by ll_layout_refresh()
326 io->ci_restore_needed = 0;
327 io->ci_need_restart = 1;
328 io->ci_verify_layout = 1;
330 io->ci_restore_needed = 1;
331 io->ci_need_restart = 0;
332 io->ci_verify_layout = 0;
338 * dynamic layout change needed, send layout intent
341 if (io->ci_need_write_intent) {
343 loff_t end = OBD_OBJECT_EOF;
345 io->ci_need_write_intent = 0;
347 LASSERT(io->ci_type == CIT_WRITE ||
348 cl_io_is_trunc(io) || cl_io_is_mkwrite(io));
350 if (io->ci_type == CIT_WRITE) {
351 if (!cl_io_is_append(io)) {
352 start = io->u.ci_rw.rw_range.cir_pos;
353 end = start + io->u.ci_rw.rw_range.cir_count;
355 } else if (cl_io_is_trunc(io)) {
356 end = io->u.ci_setattr.sa_attr.lvb_size;
357 } else { /* mkwrite */
358 pgoff_t index = io->u.ci_fault.ft_index;
360 start = cl_offset(io->ci_obj, index);
361 end = cl_offset(io->ci_obj, index + 1);
364 CDEBUG(D_VFSTRACE, DFID" write layout, type %u [%llu, %llu)\n",
365 PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
367 rc = ll_layout_write_intent(inode, start, end);
370 io->ci_need_restart = 1;
373 if (!io->ci_ignore_layout && io->ci_verify_layout) {
376 /* check layout version */
377 ll_layout_refresh(inode, &gen);
378 io->ci_need_restart = vio->vui_layout_gen != gen;
379 if (io->ci_need_restart) {
381 DFID" layout changed from %d to %d.\n",
382 PFID(lu_object_fid(&obj->co_lu)),
383 vio->vui_layout_gen, gen);
384 /* today successful restore is the only possible
386 /* restore was done, clear restoring state */
387 ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
388 LLIF_FILE_RESTORING);
393 static void vvp_io_fault_fini(const struct lu_env *env,
394 const struct cl_io_slice *ios)
396 struct cl_io *io = ios->cis_io;
397 struct cl_page *page = io->u.ci_fault.ft_page;
399 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
402 lu_ref_del(&page->cp_reference, "fault", io);
403 cl_page_put(env, page);
404 io->u.ci_fault.ft_page = NULL;
406 vvp_io_fini(env, ios);
409 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
412 * we only want to hold PW locks if the mmap() can generate
413 * writes back to the file and that only happens in shared
416 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
421 static int vvp_mmap_locks(const struct lu_env *env, struct cl_io *io)
423 struct vvp_thread_info *vti = vvp_env_info(env);
424 struct mm_struct *mm = current->mm;
425 struct vm_area_struct *vma;
426 struct cl_lock_descr *descr = &vti->vti_descr;
427 union ldlm_policy_data policy;
433 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
435 if (!cl_is_normalio(env, io))
438 /* No MM (e.g. NFS)? No vmas too. */
442 iov_for_each(iov, i, io->u.ci_rw.rw_iter) {
443 unsigned long addr = (unsigned long)iov.iov_base;
444 size_t count = iov.iov_len;
449 count += addr & ~PAGE_MASK;
452 down_read(&mm->mmap_sem);
453 while((vma = our_vma(mm, addr, count)) != NULL) {
454 struct dentry *de = file_dentry(vma->vm_file);
455 struct inode *inode = de->d_inode;
456 int flags = CEF_MUST;
458 if (ll_file_nolock(vma->vm_file)) {
460 * For no lock case is not allowed for mmap
467 * XXX: Required lock mode can be weakened: CIT_WRITE
468 * io only ever reads user level buffer, and CIT_READ
471 policy_from_vma(&policy, vma, addr, count);
472 descr->cld_mode = vvp_mode_from_vma(vma);
473 descr->cld_obj = ll_i2info(inode)->lli_clob;
474 descr->cld_start = cl_index(descr->cld_obj,
475 policy.l_extent.start);
476 descr->cld_end = cl_index(descr->cld_obj,
477 policy.l_extent.end);
478 descr->cld_enq_flags = flags;
479 result = cl_io_lock_alloc_add(env, io, descr);
481 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
482 descr->cld_mode, descr->cld_start,
488 if (vma->vm_end - addr >= count)
491 count -= vma->vm_end - addr;
494 up_read(&mm->mmap_sem);
501 static void vvp_io_advance(const struct lu_env *env,
502 const struct cl_io_slice *ios,
505 struct vvp_io *vio = cl2vvp_io(env, ios);
506 struct cl_io *io = ios->cis_io;
507 struct cl_object *obj = ios->cis_io->ci_obj;
509 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
511 if (!cl_is_normalio(env, io))
514 vio->vui_tot_count -= nob;
516 iov_iter_advance(&io->u.ci_rw.rw_iter, nob);
517 io->u.ci_rw.rw_iocb.ki_pos = io->u.ci_rw.rw_range.cir_pos;
518 #ifdef HAVE_KIOCB_KI_LEFT
519 io->u.ci_rw.rw_iocb.ki_left = vio->vui_tot_count;
520 #elif defined(HAVE_KI_NBYTES)
521 io->u.ci_rw.rw_iocb.ki_nbytes = vio->vui_tot_count;
524 /* It was truncated to stripe size in vvp_io_rw_lock() */
525 iov_iter_reexpand(&io->u.ci_rw.rw_iter, vio->vui_tot_count);
529 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
530 enum cl_lock_mode mode, loff_t start, loff_t end)
535 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
538 if (cl_is_normalio(env, io))
539 iov_iter_truncate(&io->u.ci_rw.rw_iter,
540 io->u.ci_rw.rw_range.cir_count);
542 if (io->u.ci_rw.rw_nonblock)
543 ast_flags |= CEF_NONBLOCK;
545 result = vvp_mmap_locks(env, io);
547 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
552 static int vvp_io_read_lock(const struct lu_env *env,
553 const struct cl_io_slice *ios)
555 struct cl_io *io = ios->cis_io;
556 struct cl_io_range *range = &io->u.ci_rw.rw_range;
560 rc = vvp_io_rw_lock(env, io, CLM_READ, range->cir_pos,
561 range->cir_pos + range->cir_count - 1);
565 static int vvp_io_fault_lock(const struct lu_env *env,
566 const struct cl_io_slice *ios)
568 struct cl_io *io = ios->cis_io;
569 struct vvp_io *vio = cl2vvp_io(env, ios);
571 * XXX LDLM_FL_CBPENDING
573 return vvp_io_one_lock_index(env,
575 vvp_mode_from_vma(vio->u.fault.ft_vma),
576 io->u.ci_fault.ft_index,
577 io->u.ci_fault.ft_index);
580 static int vvp_io_write_lock(const struct lu_env *env,
581 const struct cl_io_slice *ios)
583 struct cl_io *io = ios->cis_io;
589 if (io->u.ci_rw.rw_append) {
591 end = OBD_OBJECT_EOF;
593 start = io->u.ci_rw.rw_range.cir_pos;
594 end = start + io->u.ci_rw.rw_range.cir_count - 1;
596 rc = vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
600 static int vvp_io_setattr_iter_init(const struct lu_env *env,
601 const struct cl_io_slice *ios)
607 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
609 * Handles "lockless io" mode when extent locking is done by server.
611 static int vvp_io_setattr_lock(const struct lu_env *env,
612 const struct cl_io_slice *ios)
614 struct cl_io *io = ios->cis_io;
618 if (cl_io_is_trunc(io)) {
619 new_size = io->u.ci_setattr.sa_attr.lvb_size;
621 enqflags = CEF_DISCARD_DATA;
623 unsigned int valid = io->u.ci_setattr.sa_valid;
625 if (!(valid & TIMES_SET_FLAGS))
628 if ((!(valid & ATTR_MTIME) ||
629 io->u.ci_setattr.sa_attr.lvb_mtime >=
630 io->u.ci_setattr.sa_attr.lvb_ctime) &&
631 (!(valid & ATTR_ATIME) ||
632 io->u.ci_setattr.sa_attr.lvb_atime >=
633 io->u.ci_setattr.sa_attr.lvb_ctime))
639 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
640 new_size, OBD_OBJECT_EOF);
643 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
648 * Only ll_inode_size_lock is taken at this level.
650 ll_inode_size_lock(inode);
651 result = inode_newsize_ok(inode, size);
653 ll_inode_size_unlock(inode);
656 i_size_write(inode, size);
658 ll_truncate_pagecache(inode, size);
659 ll_inode_size_unlock(inode);
663 static int vvp_io_setattr_time(const struct lu_env *env,
664 const struct cl_io_slice *ios)
666 struct cl_io *io = ios->cis_io;
667 struct cl_object *obj = io->ci_obj;
668 struct cl_attr *attr = vvp_env_thread_attr(env);
670 unsigned valid = CAT_CTIME;
672 cl_object_attr_lock(obj);
673 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
674 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
675 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
678 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
679 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
682 result = cl_object_attr_update(env, obj, attr, valid);
683 cl_object_attr_unlock(obj);
688 static int vvp_io_setattr_start(const struct lu_env *env,
689 const struct cl_io_slice *ios)
691 struct cl_io *io = ios->cis_io;
692 struct inode *inode = vvp_object_inode(io->ci_obj);
693 struct ll_inode_info *lli = ll_i2info(inode);
695 if (cl_io_is_trunc(io)) {
696 down_write(&lli->lli_trunc_sem);
698 inode_dio_wait(inode);
703 if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
704 return vvp_io_setattr_time(env, ios);
709 static void vvp_io_setattr_end(const struct lu_env *env,
710 const struct cl_io_slice *ios)
712 struct cl_io *io = ios->cis_io;
713 struct inode *inode = vvp_object_inode(io->ci_obj);
714 struct ll_inode_info *lli = ll_i2info(inode);
716 if (cl_io_is_trunc(io)) {
717 /* Truncate in memory pages - they must be clean pages
718 * because osc has already notified to destroy osc_extents. */
719 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
720 inode_dio_write_done(inode);
722 up_write(&lli->lli_trunc_sem);
728 static void vvp_io_setattr_fini(const struct lu_env *env,
729 const struct cl_io_slice *ios)
731 bool restore_needed = ios->cis_io->ci_restore_needed;
732 struct inode *inode = vvp_object_inode(ios->cis_obj);
734 vvp_io_fini(env, ios);
736 if (restore_needed && !ios->cis_io->ci_restore_needed) {
737 /* restore finished, set data modified flag for HSM */
738 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
742 static int vvp_io_read_start(const struct lu_env *env,
743 const struct cl_io_slice *ios)
745 struct vvp_io *vio = cl2vvp_io(env, ios);
746 struct cl_io *io = ios->cis_io;
747 struct cl_object *obj = io->ci_obj;
748 struct inode *inode = vvp_object_inode(obj);
749 struct ll_inode_info *lli = ll_i2info(inode);
750 struct file *file = vio->vui_fd->fd_file;
751 struct cl_io_range *range = &io->u.ci_rw.rw_range;
752 loff_t pos = range->cir_pos; /* for generic_file_splice_read() only */
753 size_t tot = vio->vui_tot_count;
757 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
759 CDEBUG(D_VFSTRACE, "%s: read [%llu, %llu)\n",
760 file_dentry(file)->d_name.name,
761 range->cir_pos, range->cir_pos + range->cir_count);
763 if (vio->vui_io_subtype == IO_NORMAL)
764 down_read(&lli->lli_trunc_sem);
766 if (!can_populate_pages(env, io, inode))
769 result = vvp_prep_size(env, obj, io, range->cir_pos, tot, &exceed);
772 else if (exceed != 0)
775 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
776 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
777 inode->i_ino, range->cir_count, range->cir_pos,
780 /* turn off the kernel's read-ahead */
781 vio->vui_fd->fd_file->f_ra.ra_pages = 0;
783 /* initialize read-ahead window once per syscall */
784 if (!vio->vui_ra_valid) {
785 vio->vui_ra_valid = true;
786 vio->vui_ra_start = cl_index(obj, range->cir_pos);
787 vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1);
793 switch (vio->vui_io_subtype) {
795 LASSERTF(io->u.ci_rw.rw_iocb.ki_pos == range->cir_pos,
796 "ki_pos %lld [%lld, %lld)\n",
797 io->u.ci_rw.rw_iocb.ki_pos,
798 range->cir_pos, range->cir_pos + range->cir_count);
799 result = generic_file_read_iter(&io->u.ci_rw.rw_iocb,
800 &io->u.ci_rw.rw_iter);
803 result = generic_file_splice_read(file, &pos,
804 vio->u.splice.vui_pipe,
806 vio->u.splice.vui_flags);
807 /* LU-1109: do splice read stripe by stripe otherwise if it
808 * may make nfsd stuck if this read occupied all internal pipe
813 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
819 if (result < range->cir_count)
821 io->ci_nob += result;
822 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, vio->vui_fd,
823 range->cir_pos, result, READ);
830 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
831 struct cl_page_list *plist, int from, int to)
833 struct cl_2queue *queue = &io->ci_queue;
834 struct cl_page *page;
835 unsigned int bytes = 0;
839 if (plist->pl_nr == 0)
842 if (from > 0 || to != PAGE_SIZE) {
843 page = cl_page_list_first(plist);
844 if (plist->pl_nr == 1) {
845 cl_page_clip(env, page, from, to);
848 cl_page_clip(env, page, from, PAGE_SIZE);
849 if (to != PAGE_SIZE) {
850 page = cl_page_list_last(plist);
851 cl_page_clip(env, page, 0, to);
856 cl_2queue_init(queue);
857 cl_page_list_splice(plist, &queue->c2_qin);
858 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
860 /* plist is not sorted any more */
861 cl_page_list_splice(&queue->c2_qin, plist);
862 cl_page_list_splice(&queue->c2_qout, plist);
863 cl_2queue_fini(env, queue);
866 /* calculate bytes */
867 bytes = plist->pl_nr << PAGE_SHIFT;
868 bytes -= from + PAGE_SIZE - to;
870 while (plist->pl_nr > 0) {
871 page = cl_page_list_first(plist);
872 cl_page_list_del(env, plist, page);
874 cl_page_clip(env, page, 0, PAGE_SIZE);
876 SetPageUptodate(cl_page_vmpage(page));
877 cl_page_disown(env, io, page);
879 lu_ref_del(&page->cp_reference, "cl_io", io);
880 cl_page_put(env, page);
884 RETURN(bytes > 0 ? bytes : rc);
887 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
888 struct cl_page *page)
890 struct page *vmpage = page->cp_vmpage;
892 SetPageUptodate(vmpage);
893 set_page_dirty(vmpage);
895 cl_page_disown(env, io, page);
897 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
898 cl_page_put(env, page);
901 /* make sure the page list is contiguous */
902 static bool page_list_sanity_check(struct cl_object *obj,
903 struct cl_page_list *plist)
905 struct cl_page *page;
906 pgoff_t index = CL_PAGE_EOF;
908 cl_page_list_for_each(page, plist) {
909 struct vvp_page *vpg = cl_object_page_slice(obj, page);
911 if (index == CL_PAGE_EOF) {
912 index = vvp_index(vpg);
917 if (index == vvp_index(vpg))
925 /* Return how many bytes have queued or written */
926 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
928 struct cl_object *obj = io->ci_obj;
929 struct inode *inode = vvp_object_inode(obj);
930 struct vvp_io *vio = vvp_env_io(env);
931 struct cl_page_list *queue = &vio->u.write.vui_queue;
932 struct cl_page *page;
935 unsigned int npages = vio->u.write.vui_queue.pl_nr;
941 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
942 npages, vio->u.write.vui_from, vio->u.write.vui_to);
944 LASSERT(page_list_sanity_check(obj, queue));
946 /* submit IO with async write */
947 rc = cl_io_commit_async(env, io, queue,
948 vio->u.write.vui_from, vio->u.write.vui_to,
949 write_commit_callback);
950 npages -= queue->pl_nr; /* already committed pages */
952 /* calculate how many bytes were written */
953 bytes = npages << PAGE_SHIFT;
956 bytes -= vio->u.write.vui_from;
957 if (queue->pl_nr == 0) /* last page */
958 bytes -= PAGE_SIZE - vio->u.write.vui_to;
959 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
961 vio->u.write.vui_written += bytes;
963 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
964 npages, bytes, vio->u.write.vui_written);
966 /* the first page must have been written. */
967 vio->u.write.vui_from = 0;
969 LASSERT(page_list_sanity_check(obj, queue));
970 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
972 /* out of quota, try sync write */
973 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
974 rc = vvp_io_commit_sync(env, io, queue,
975 vio->u.write.vui_from,
976 vio->u.write.vui_to);
978 vio->u.write.vui_written += rc;
983 /* update inode size */
984 ll_merge_attr(env, inode);
986 /* Now the pages in queue were failed to commit, discard them
987 * unless they were dirtied before. */
988 while (queue->pl_nr > 0) {
989 page = cl_page_list_first(queue);
990 cl_page_list_del(env, queue, page);
992 if (!PageDirty(cl_page_vmpage(page)))
993 cl_page_discard(env, io, page);
995 cl_page_disown(env, io, page);
997 lu_ref_del(&page->cp_reference, "cl_io", io);
998 cl_page_put(env, page);
1000 cl_page_list_fini(env, queue);
1005 static int vvp_io_write_start(const struct lu_env *env,
1006 const struct cl_io_slice *ios)
1008 struct vvp_io *vio = cl2vvp_io(env, ios);
1009 struct cl_io *io = ios->cis_io;
1010 struct cl_object *obj = io->ci_obj;
1011 struct inode *inode = vvp_object_inode(obj);
1012 struct ll_inode_info *lli = ll_i2info(inode);
1013 struct file *file = vio->vui_fd->fd_file;
1014 struct cl_io_range *range = &io->u.ci_rw.rw_range;
1015 bool lock_inode = !lli->lli_inode_locked &&
1020 if (vio->vui_io_subtype == IO_NORMAL)
1021 down_read(&lli->lli_trunc_sem);
1023 if (!can_populate_pages(env, io, inode))
1026 if (cl_io_is_append(io)) {
1028 * PARALLEL IO This has to be changed for parallel IO doing
1029 * out-of-order writes.
1031 ll_merge_attr(env, inode);
1032 range->cir_pos = i_size_read(inode);
1033 io->u.ci_rw.rw_iocb.ki_pos = range->cir_pos;
1035 LASSERTF(io->u.ci_rw.rw_iocb.ki_pos == range->cir_pos,
1036 "ki_pos %lld [%lld, %lld)\n",
1037 io->u.ci_rw.rw_iocb.ki_pos,
1038 range->cir_pos, range->cir_pos + range->cir_count);
1041 CDEBUG(D_VFSTRACE, "%s: write [%llu, %llu)\n",
1042 file_dentry(file)->d_name.name,
1043 range->cir_pos, range->cir_pos + range->cir_count);
1045 /* The maximum Lustre file size is variable, based on the OST maximum
1046 * object size and number of stripes. This needs another check in
1047 * addition to the VFS checks earlier. */
1048 if (range->cir_pos + range->cir_count > ll_file_maxbytes(inode)) {
1050 "%s: file %s ("DFID") offset %llu > maxbytes %llu\n",
1051 ll_get_fsname(inode->i_sb, NULL, 0),
1052 file_dentry(file)->d_name.name,
1053 PFID(ll_inode2fid(inode)),
1054 range->cir_pos + range->cir_count,
1055 ll_file_maxbytes(inode));
1059 /* Tests to verify we take the i_mutex correctly */
1060 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_SEC) && !lock_inode)
1063 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_NOSEC) && lock_inode)
1067 * When using the locked AIO function (generic_file_aio_write())
1068 * testing has shown the inode mutex to be a limiting factor
1069 * with multi-threaded single shared file performance. To get
1070 * around this, we now use the lockless version. To maintain
1071 * consistency, proper locking to protect against writes,
1072 * trucates, etc. is handled in the higher layers of lustre.
1076 result = __generic_file_write_iter(&io->u.ci_rw.rw_iocb,
1077 &io->u.ci_rw.rw_iter);
1079 inode_unlock(inode);
1081 if (result > 0 || result == -EIOCBQUEUED)
1082 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1083 result = generic_write_sync(&io->u.ci_rw.rw_iocb, result);
1088 err = generic_write_sync(io->u.ci_rw.rw_iocb.ki_filp,
1089 range->cir_pos, result);
1090 if (err < 0 && result > 0)
1096 result = vvp_io_write_commit(env, io);
1097 if (vio->u.write.vui_written > 0) {
1098 result = vio->u.write.vui_written;
1099 CDEBUG(D_VFSTRACE, "%s: write nob %zd, result: %zd\n",
1100 file_dentry(file)->d_name.name,
1101 io->ci_nob, result);
1102 io->ci_nob += result;
1106 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1108 if (result < range->cir_count)
1109 io->ci_continue = 0;
1110 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
1111 vio->vui_fd, range->cir_pos, result, WRITE);
1118 static void vvp_io_rw_end(const struct lu_env *env,
1119 const struct cl_io_slice *ios)
1121 struct vvp_io *vio = cl2vvp_io(env, ios);
1122 struct inode *inode = vvp_object_inode(ios->cis_obj);
1123 struct ll_inode_info *lli = ll_i2info(inode);
1125 if (vio->vui_io_subtype == IO_NORMAL)
1126 up_read(&lli->lli_trunc_sem);
1129 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1131 struct vm_fault *vmf = cfio->ft_vmf;
1133 cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf);
1134 cfio->ft_flags_valid = 1;
1137 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1138 get_vmf_address(vmf));
1139 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1140 lock_page(vmf->page);
1141 cfio->ft_flags |= VM_FAULT_LOCKED;
1144 cfio->ft_vmpage = vmf->page;
1149 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1150 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", get_vmf_address(vmf));
1154 if (cfio->ft_flags & VM_FAULT_OOM) {
1155 CDEBUG(D_PAGE, "got addr %p - OOM\n", get_vmf_address(vmf));
1159 if (cfio->ft_flags & VM_FAULT_RETRY)
1162 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1167 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1168 struct cl_page *page)
1170 set_page_dirty(page->cp_vmpage);
1173 static int vvp_io_fault_start(const struct lu_env *env,
1174 const struct cl_io_slice *ios)
1176 struct vvp_io *vio = cl2vvp_io(env, ios);
1177 struct cl_io *io = ios->cis_io;
1178 struct cl_object *obj = io->ci_obj;
1179 struct inode *inode = vvp_object_inode(obj);
1180 struct ll_inode_info *lli = ll_i2info(inode);
1181 struct cl_fault_io *fio = &io->u.ci_fault;
1182 struct vvp_fault_io *cfio = &vio->u.fault;
1185 struct page *vmpage = NULL;
1186 struct cl_page *page;
1191 down_read(&lli->lli_trunc_sem);
1193 /* offset of the last byte on the page */
1194 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1195 LASSERT(cl_index(obj, offset) == fio->ft_index);
1196 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1200 /* must return locked page */
1201 if (fio->ft_mkwrite) {
1202 LASSERT(cfio->ft_vmpage != NULL);
1203 lock_page(cfio->ft_vmpage);
1205 result = vvp_io_kernel_fault(cfio);
1210 vmpage = cfio->ft_vmpage;
1211 LASSERT(PageLocked(vmpage));
1213 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1214 ll_invalidate_page(vmpage);
1216 size = i_size_read(inode);
1217 /* Though we have already held a cl_lock upon this page, but
1218 * it still can be truncated locally. */
1219 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1220 (page_offset(vmpage) > size))) {
1221 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1223 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1225 GOTO(out, result = +1);
1228 last_index = cl_index(obj, size - 1);
1230 if (fio->ft_mkwrite ) {
1232 * Capture the size while holding the lli_trunc_sem from above
1233 * we want to make sure that we complete the mkwrite action
1234 * while holding this lock. We need to make sure that we are
1235 * not past the end of the file.
1237 if (last_index < fio->ft_index) {
1239 "llite: mkwrite and truncate race happened: "
1240 "%p: 0x%lx 0x%lx\n",
1241 vmpage->mapping,fio->ft_index,last_index);
1243 * We need to return if we are
1244 * passed the end of the file. This will propagate
1245 * up the call stack to ll_page_mkwrite where
1246 * we will return VM_FAULT_NOPAGE. Any non-negative
1247 * value returned here will be silently
1248 * converted to 0. If the vmpage->mapping is null
1249 * the error code would be converted back to ENODATA
1250 * in ll_page_mkwrite0. Thus we return -ENODATA
1251 * to handle both cases
1253 GOTO(out, result = -ENODATA);
1257 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1259 GOTO(out, result = PTR_ERR(page));
1261 /* if page is going to be written, we should add this page into cache
1263 if (fio->ft_mkwrite) {
1264 wait_on_page_writeback(vmpage);
1265 if (!PageDirty(vmpage)) {
1266 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1267 struct vvp_page *vpg = cl_object_page_slice(obj, page);
1270 /* vvp_page_assume() calls wait_on_page_writeback(). */
1271 cl_page_assume(env, io, page);
1273 cl_page_list_init(plist);
1274 cl_page_list_add(plist, page);
1277 if (last_index == vvp_index(vpg))
1278 to = size & ~PAGE_MASK;
1280 /* Do not set Dirty bit here so that in case IO is
1281 * started before the page is really made dirty, we
1282 * still have chance to detect it. */
1283 result = cl_io_commit_async(env, io, plist, 0, to,
1284 mkwrite_commit_callback);
1285 LASSERT(cl_page_is_owned(page, io));
1286 cl_page_list_fini(env, plist);
1290 cl_page_discard(env, io, page);
1291 cl_page_disown(env, io, page);
1293 cl_page_put(env, page);
1295 /* we're in big trouble, what can we do now? */
1296 if (result == -EDQUOT)
1300 cl_page_disown(env, io, page);
1305 * The ft_index is only used in the case of
1306 * a mkwrite action. We need to check
1307 * our assertions are correct, since
1308 * we should have caught this above
1310 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1311 if (fio->ft_index == last_index)
1313 * Last page is mapped partially.
1315 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1317 fio->ft_nob = cl_page_size(obj);
1319 lu_ref_add(&page->cp_reference, "fault", io);
1320 fio->ft_page = page;
1324 /* return unlocked vmpage to avoid deadlocking */
1326 unlock_page(vmpage);
1328 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1333 static void vvp_io_fault_end(const struct lu_env *env,
1334 const struct cl_io_slice *ios)
1336 struct inode *inode = vvp_object_inode(ios->cis_obj);
1337 struct ll_inode_info *lli = ll_i2info(inode);
1339 CLOBINVRNT(env, ios->cis_io->ci_obj,
1340 vvp_object_invariant(ios->cis_io->ci_obj));
1341 up_read(&lli->lli_trunc_sem);
1344 static int vvp_io_fsync_start(const struct lu_env *env,
1345 const struct cl_io_slice *ios)
1347 /* we should mark TOWRITE bit to each dirty page in radix tree to
1348 * verify pages have been written, but this is difficult because of
1353 static int vvp_io_read_ahead(const struct lu_env *env,
1354 const struct cl_io_slice *ios,
1355 pgoff_t start, struct cl_read_ahead *ra)
1360 if (ios->cis_io->ci_type == CIT_READ ||
1361 ios->cis_io->ci_type == CIT_FAULT) {
1362 struct vvp_io *vio = cl2vvp_io(env, ios);
1364 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1365 ra->cra_end = CL_PAGE_EOF;
1366 result = +1; /* no need to call down */
1373 static const struct cl_io_operations vvp_io_ops = {
1376 .cio_fini = vvp_io_fini,
1377 .cio_lock = vvp_io_read_lock,
1378 .cio_start = vvp_io_read_start,
1379 .cio_end = vvp_io_rw_end,
1380 .cio_advance = vvp_io_advance,
1383 .cio_fini = vvp_io_fini,
1384 .cio_iter_init = vvp_io_write_iter_init,
1385 .cio_iter_fini = vvp_io_write_iter_fini,
1386 .cio_lock = vvp_io_write_lock,
1387 .cio_start = vvp_io_write_start,
1388 .cio_end = vvp_io_rw_end,
1389 .cio_advance = vvp_io_advance,
1392 .cio_fini = vvp_io_setattr_fini,
1393 .cio_iter_init = vvp_io_setattr_iter_init,
1394 .cio_lock = vvp_io_setattr_lock,
1395 .cio_start = vvp_io_setattr_start,
1396 .cio_end = vvp_io_setattr_end
1399 .cio_fini = vvp_io_fault_fini,
1400 .cio_iter_init = vvp_io_fault_iter_init,
1401 .cio_lock = vvp_io_fault_lock,
1402 .cio_start = vvp_io_fault_start,
1403 .cio_end = vvp_io_fault_end,
1406 .cio_start = vvp_io_fsync_start,
1407 .cio_fini = vvp_io_fini
1410 .cio_fini = vvp_io_fini
1413 .cio_fini = vvp_io_fini
1416 .cio_read_ahead = vvp_io_read_ahead
1419 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1422 struct vvp_io *vio = vvp_env_io(env);
1423 struct inode *inode = vvp_object_inode(obj);
1426 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1429 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1430 "restore needed %d\n",
1431 PFID(lu_object_fid(&obj->co_lu)),
1432 io->ci_ignore_layout, io->ci_verify_layout,
1433 vio->vui_layout_gen, io->ci_restore_needed);
1435 CL_IO_SLICE_CLEAN(vio, vui_cl);
1436 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1437 vio->vui_ra_valid = false;
1439 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1440 struct ll_inode_info *lli = ll_i2info(inode);
1442 vio->vui_tot_count = io->u.ci_rw.rw_range.cir_count;
1443 /* "If nbyte is 0, read() will return 0 and have no other
1444 * results." -- Single Unix Spec */
1445 if (vio->vui_tot_count == 0)
1448 /* for read/write, we store the jobid in the inode, and
1449 * it'll be fetched by osc when building RPC.
1451 * it's not accurate if the file is shared by different
1454 lustre_get_jobid(lli->lli_jobid);
1455 } else if (io->ci_type == CIT_SETATTR) {
1456 if (!cl_io_is_trunc(io))
1457 io->ci_lockreq = CILR_MANDATORY;
1460 /* Enqueue layout lock and get layout version. We need to do this
1461 * even for operations requiring to open file, such as read and write,
1462 * because it might not grant layout lock in IT_OPEN. */
1463 if (result == 0 && !io->ci_ignore_layout) {
1464 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1465 if (result == -ENOENT)
1466 /* If the inode on MDS has been removed, but the objects
1467 * on OSTs haven't been destroyed (async unlink), layout
1468 * fetch will return -ENOENT, we'd ingore this error
1469 * and continue with dirty flush. LU-3230. */
1472 CERROR("%s: refresh file layout " DFID " error %d.\n",
1473 ll_get_fsname(inode->i_sb, NULL, 0),
1474 PFID(lu_object_fid(&obj->co_lu)), result);