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, 2017, 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"
44 #include <libcfs/linux/linux-misc.h>
46 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
47 const struct cl_io_slice *slice)
51 vio = container_of(slice, struct vvp_io, vui_cl);
52 LASSERT(vio == vvp_env_io(env));
58 * True, if \a io is a normal io, False for splice_{read,write}
60 static 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->vui_io_subtype == IO_NORMAL;
70 * For swapping layout. The file's layout may have changed.
71 * To avoid populating pages to a wrong stripe, we have to verify the
72 * correctness of layout. It works because swapping layout processes
73 * have to acquire group lock.
75 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
78 struct ll_inode_info *lli = ll_i2info(inode);
79 struct vvp_io *vio = vvp_env_io(env);
82 switch (io->ci_type) {
85 /* don't need lock here to check lli_layout_gen as we have held
86 * extent lock and GROUP lock has to hold to swap layout */
87 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
88 OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
89 io->ci_need_restart = 1;
90 /* this will cause a short read/write */
95 /* fault is okay because we've already had a page. */
103 static void vvp_object_size_lock(struct cl_object *obj)
105 struct inode *inode = vvp_object_inode(obj);
107 ll_inode_size_lock(inode);
108 cl_object_attr_lock(obj);
111 static void vvp_object_size_unlock(struct cl_object *obj)
113 struct inode *inode = vvp_object_inode(obj);
115 cl_object_attr_unlock(obj);
116 ll_inode_size_unlock(inode);
120 * Helper function that if necessary adjusts file size (inode->i_size), when
121 * position at the offset \a pos is accessed. File size can be arbitrary stale
122 * on a Lustre client, but client at least knows KMS. If accessed area is
123 * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
125 * Locking: i_size_lock is used to serialize changes to inode size and to
126 * protect consistency between inode size and cl_object
127 * attributes. cl_object_size_lock() protects consistency between cl_attr's of
128 * top-object and sub-objects.
130 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
131 struct cl_io *io, loff_t start, size_t count,
134 struct cl_attr *attr = vvp_env_thread_attr(env);
135 struct inode *inode = vvp_object_inode(obj);
136 loff_t pos = start + count - 1;
141 * Consistency guarantees: following possibilities exist for the
142 * relation between region being accessed and real file size at this
145 * (A): the region is completely inside of the file;
147 * (B-x): x bytes of region are inside of the file, the rest is
150 * (C): the region is completely outside of the file.
152 * This classification is stable under DLM lock already acquired by
153 * the caller, because to change the class, other client has to take
154 * DLM lock conflicting with our lock. Also, any updates to ->i_size
155 * by other threads on this client are serialized by
156 * ll_inode_size_lock(). This guarantees that short reads are handled
157 * correctly in the face of concurrent writes and truncates.
159 vvp_object_size_lock(obj);
160 result = cl_object_attr_get(env, obj, attr);
165 * A glimpse is necessary to determine whether we
166 * return a short read (B) or some zeroes at the end
169 vvp_object_size_unlock(obj);
170 result = cl_glimpse_lock(env, io, inode, obj, 0);
171 if (result == 0 && exceed != NULL) {
172 /* If objective page index exceed end-of-file
173 * page index, return directly. Do not expect
174 * kernel will check such case correctly.
175 * linux-2.6.18-128.1.1 miss to do that.
177 loff_t size = i_size_read(inode);
178 unsigned long cur_index = start >>
181 if ((size == 0 && cur_index != 0) ||
182 (((size - 1) >> PAGE_SHIFT) <
190 * region is within kms and, hence, within real file
191 * size (A). We need to increase i_size to cover the
192 * read region so that generic_file_read() will do its
193 * job, but that doesn't mean the kms size is
194 * _correct_, it is only the _minimum_ size. If
195 * someone does a stat they will get the correct size
196 * which will always be >= the kms value here.
199 if (i_size_read(inode) < kms) {
200 i_size_write(inode, kms);
202 DFID" updating i_size %llu\n",
203 PFID(lu_object_fid(&obj->co_lu)),
204 (__u64)i_size_read(inode));
209 vvp_object_size_unlock(obj);
214 /*****************************************************************************
220 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
221 __u32 enqflags, enum cl_lock_mode mode,
222 pgoff_t start, pgoff_t end)
224 struct vvp_io *vio = vvp_env_io(env);
225 struct cl_lock_descr *descr = &vio->vui_link.cill_descr;
226 struct cl_object *obj = io->ci_obj;
228 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
231 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
233 memset(&vio->vui_link, 0, sizeof vio->vui_link);
235 if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
236 descr->cld_mode = CLM_GROUP;
237 descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid;
238 enqflags |= CEF_LOCK_MATCH;
240 descr->cld_mode = mode;
243 descr->cld_obj = obj;
244 descr->cld_start = start;
245 descr->cld_end = end;
246 descr->cld_enq_flags = enqflags;
248 cl_io_lock_add(env, io, &vio->vui_link);
253 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
254 __u32 enqflags, enum cl_lock_mode mode,
255 loff_t start, loff_t end)
257 struct cl_object *obj = io->ci_obj;
259 return vvp_io_one_lock_index(env, io, enqflags, mode,
260 cl_index(obj, start), cl_index(obj, end));
263 static int vvp_io_write_iter_init(const struct lu_env *env,
264 const struct cl_io_slice *ios)
266 struct vvp_io *vio = cl2vvp_io(env, ios);
268 cl_page_list_init(&vio->u.write.vui_queue);
269 vio->u.write.vui_written = 0;
270 vio->u.write.vui_from = 0;
271 vio->u.write.vui_to = PAGE_SIZE;
276 static void vvp_io_write_iter_fini(const struct lu_env *env,
277 const struct cl_io_slice *ios)
279 struct vvp_io *vio = cl2vvp_io(env, ios);
281 LASSERT(vio->u.write.vui_queue.pl_nr == 0);
284 static int vvp_io_fault_iter_init(const struct lu_env *env,
285 const struct cl_io_slice *ios)
287 struct vvp_io *vio = cl2vvp_io(env, ios);
288 struct inode *inode = vvp_object_inode(ios->cis_obj);
290 LASSERT(inode == file_inode(vio->vui_fd->fd_file));
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);
305 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
307 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
308 "need write layout %d, restore needed %d\n",
309 PFID(lu_object_fid(&obj->co_lu)),
310 io->ci_ignore_layout, io->ci_verify_layout,
311 vio->vui_layout_gen, io->ci_need_write_intent,
312 io->ci_restore_needed);
314 if (io->ci_restore_needed) {
315 /* file was detected release, we need to restore it
316 * before finishing the io
318 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
319 /* if restore registration failed, no restart,
320 * we will return -ENODATA */
321 /* The layout will change after restore, so we need to
322 * block on layout lock held by the MDT
323 * as MDT will not send new layout in lvb (see LU-3124)
324 * we have to explicitly fetch it, all this will be done
325 * by ll_layout_refresh().
326 * Even if ll_layout_restore() returns zero, it doesn't mean
327 * that restore has been successful. Therefore it sets
328 * ci_verify_layout so that it will check layout at the end
332 io->ci_restore_needed = 1;
333 io->ci_need_restart = 0;
334 io->ci_verify_layout = 0;
339 io->ci_restore_needed = 0;
341 /* Even if ll_layout_restore() returns zero, it doesn't mean
342 * that restore has been successful. Therefore it should verify
343 * if there was layout change and restart I/O correspondingly.
345 ll_layout_refresh(inode, &gen);
346 io->ci_need_restart = vio->vui_layout_gen != gen;
347 if (io->ci_need_restart) {
349 DFID" layout changed from %d to %d.\n",
350 PFID(lu_object_fid(&obj->co_lu)),
351 vio->vui_layout_gen, gen);
352 /* today successful restore is the only possible
354 /* restore was done, clear restoring state */
355 ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
356 LLIF_FILE_RESTORING);
362 * dynamic layout change needed, send layout intent
365 if (io->ci_need_write_intent) {
366 enum layout_intent_opc opc = LAYOUT_INTENT_WRITE;
368 io->ci_need_write_intent = 0;
370 LASSERT(io->ci_type == CIT_WRITE ||
371 cl_io_is_trunc(io) || cl_io_is_mkwrite(io));
373 CDEBUG(D_VFSTRACE, DFID" write layout, type %u "DEXT"\n",
374 PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
375 PEXT(&io->ci_write_intent));
377 if (cl_io_is_trunc(io))
378 opc = LAYOUT_INTENT_TRUNC;
380 rc = ll_layout_write_intent(inode, opc, &io->ci_write_intent);
383 io->ci_need_restart = 1;
387 if (!io->ci_need_restart &&
388 !io->ci_ignore_layout && io->ci_verify_layout) {
389 /* check layout version */
390 ll_layout_refresh(inode, &gen);
391 io->ci_need_restart = vio->vui_layout_gen != gen;
392 if (io->ci_need_restart) {
394 DFID" layout changed from %d to %d.\n",
395 PFID(lu_object_fid(&obj->co_lu)),
396 vio->vui_layout_gen, gen);
404 static void vvp_io_fault_fini(const struct lu_env *env,
405 const struct cl_io_slice *ios)
407 struct cl_io *io = ios->cis_io;
408 struct cl_page *page = io->u.ci_fault.ft_page;
410 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
413 lu_ref_del(&page->cp_reference, "fault", io);
414 cl_page_put(env, page);
415 io->u.ci_fault.ft_page = NULL;
417 vvp_io_fini(env, ios);
420 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
423 * we only want to hold PW locks if the mmap() can generate
424 * writes back to the file and that only happens in shared
427 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
432 static int vvp_mmap_locks(const struct lu_env *env,
433 struct vvp_io *vio, struct cl_io *io)
435 struct vvp_thread_info *vti = vvp_env_info(env);
436 struct mm_struct *mm = current->mm;
437 struct vm_area_struct *vma;
438 struct cl_lock_descr *descr = &vti->vti_descr;
439 union ldlm_policy_data policy;
447 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
449 if (!cl_is_normalio(env, io))
452 /* nfs or loop back device write */
453 if (vio->vui_iter == NULL)
456 /* No MM (e.g. NFS)? No vmas too. */
460 if (!iter_is_iovec(vio->vui_iter) && !iov_iter_is_kvec(vio->vui_iter))
463 for (i = *vio->vui_iter;
465 iov_iter_advance(&i, iov.iov_len)) {
466 iov = iov_iter_iovec(&i);
467 addr = (unsigned long)iov.iov_base;
473 count += addr & ~PAGE_MASK;
476 down_read(&mm->mmap_sem);
477 while((vma = our_vma(mm, addr, count)) != NULL) {
478 struct dentry *de = file_dentry(vma->vm_file);
479 struct inode *inode = de->d_inode;
480 int flags = CEF_MUST;
482 if (ll_file_nolock(vma->vm_file)) {
484 * For no lock case is not allowed for mmap
491 * XXX: Required lock mode can be weakened: CIT_WRITE
492 * io only ever reads user level buffer, and CIT_READ
495 policy_from_vma(&policy, vma, addr, count);
496 descr->cld_mode = vvp_mode_from_vma(vma);
497 descr->cld_obj = ll_i2info(inode)->lli_clob;
498 descr->cld_start = cl_index(descr->cld_obj,
499 policy.l_extent.start);
500 descr->cld_end = cl_index(descr->cld_obj,
501 policy.l_extent.end);
502 descr->cld_enq_flags = flags;
503 result = cl_io_lock_alloc_add(env, io, descr);
505 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
506 descr->cld_mode, descr->cld_start,
512 if (vma->vm_end - addr >= count)
515 count -= vma->vm_end - addr;
518 up_read(&mm->mmap_sem);
525 static void vvp_io_advance(const struct lu_env *env,
526 const struct cl_io_slice *ios,
529 struct vvp_io *vio = cl2vvp_io(env, ios);
530 struct cl_io *io = ios->cis_io;
531 struct cl_object *obj = ios->cis_io->ci_obj;
533 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
535 if (!cl_is_normalio(env, io))
538 vio->vui_tot_count -= nob;
539 iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
542 static void vvp_io_update_iov(const struct lu_env *env,
543 struct vvp_io *vio, struct cl_io *io)
545 size_t size = io->u.ci_rw.crw_count;
547 if (!cl_is_normalio(env, io) || vio->vui_iter == NULL)
550 iov_iter_truncate(vio->vui_iter, size);
553 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
554 enum cl_lock_mode mode, loff_t start, loff_t end)
556 struct vvp_io *vio = vvp_env_io(env);
560 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
563 vvp_io_update_iov(env, vio, io);
565 if (io->u.ci_rw.crw_nonblock)
566 ast_flags |= CEF_NONBLOCK;
567 if (io->ci_lock_no_expand)
568 ast_flags |= CEF_LOCK_NO_EXPAND;
570 result = vvp_mmap_locks(env, vio, io);
572 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
577 static int vvp_io_read_lock(const struct lu_env *env,
578 const struct cl_io_slice *ios)
580 struct cl_io *io = ios->cis_io;
581 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
585 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
586 rd->crw_pos + rd->crw_count - 1);
590 static int vvp_io_fault_lock(const struct lu_env *env,
591 const struct cl_io_slice *ios)
593 struct cl_io *io = ios->cis_io;
594 struct vvp_io *vio = cl2vvp_io(env, ios);
596 * XXX LDLM_FL_CBPENDING
598 return vvp_io_one_lock_index(env,
600 vvp_mode_from_vma(vio->u.fault.ft_vma),
601 io->u.ci_fault.ft_index,
602 io->u.ci_fault.ft_index);
605 static int vvp_io_write_lock(const struct lu_env *env,
606 const struct cl_io_slice *ios)
608 struct cl_io *io = ios->cis_io;
612 if (io->u.ci_wr.wr_append) {
614 end = OBD_OBJECT_EOF;
616 start = io->u.ci_wr.wr.crw_pos;
617 end = start + io->u.ci_wr.wr.crw_count - 1;
620 RETURN(vvp_io_rw_lock(env, io, CLM_WRITE, start, end));
623 static int vvp_io_setattr_iter_init(const struct lu_env *env,
624 const struct cl_io_slice *ios)
631 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
633 * Handles "lockless io" mode when extent locking is done by server.
635 static int vvp_io_setattr_lock(const struct lu_env *env,
636 const struct cl_io_slice *ios)
638 struct cl_io *io = ios->cis_io;
642 if (cl_io_is_trunc(io)) {
643 new_size = io->u.ci_setattr.sa_attr.lvb_size;
645 enqflags = CEF_DISCARD_DATA;
647 unsigned int valid = io->u.ci_setattr.sa_avalid;
649 if (!(valid & TIMES_SET_FLAGS))
652 if ((!(valid & ATTR_MTIME) ||
653 io->u.ci_setattr.sa_attr.lvb_mtime >=
654 io->u.ci_setattr.sa_attr.lvb_ctime) &&
655 (!(valid & ATTR_ATIME) ||
656 io->u.ci_setattr.sa_attr.lvb_atime >=
657 io->u.ci_setattr.sa_attr.lvb_ctime))
663 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
664 new_size, OBD_OBJECT_EOF);
667 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
672 * Only ll_inode_size_lock is taken at this level.
674 ll_inode_size_lock(inode);
675 result = inode_newsize_ok(inode, size);
677 ll_inode_size_unlock(inode);
680 i_size_write(inode, size);
682 ll_truncate_pagecache(inode, size);
683 ll_inode_size_unlock(inode);
687 static int vvp_io_setattr_time(const struct lu_env *env,
688 const struct cl_io_slice *ios)
690 struct cl_io *io = ios->cis_io;
691 struct cl_object *obj = io->ci_obj;
692 struct cl_attr *attr = vvp_env_thread_attr(env);
694 unsigned valid = CAT_CTIME;
696 cl_object_attr_lock(obj);
697 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
698 if (io->u.ci_setattr.sa_avalid & ATTR_ATIME_SET) {
699 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
702 if (io->u.ci_setattr.sa_avalid & ATTR_MTIME_SET) {
703 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
706 result = cl_object_attr_update(env, obj, attr, valid);
707 cl_object_attr_unlock(obj);
712 static int vvp_io_setattr_start(const struct lu_env *env,
713 const struct cl_io_slice *ios)
715 struct cl_io *io = ios->cis_io;
716 struct inode *inode = vvp_object_inode(io->ci_obj);
717 struct ll_inode_info *lli = ll_i2info(inode);
719 if (cl_io_is_trunc(io)) {
720 down_write(&lli->lli_trunc_sem);
722 inode_dio_wait(inode);
727 if (io->u.ci_setattr.sa_avalid & TIMES_SET_FLAGS)
728 return vvp_io_setattr_time(env, ios);
733 static void vvp_io_setattr_end(const struct lu_env *env,
734 const struct cl_io_slice *ios)
736 struct cl_io *io = ios->cis_io;
737 struct inode *inode = vvp_object_inode(io->ci_obj);
738 struct ll_inode_info *lli = ll_i2info(inode);
740 if (cl_io_is_trunc(io)) {
741 /* Truncate in memory pages - they must be clean pages
742 * because osc has already notified to destroy osc_extents. */
743 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
744 inode_dio_write_done(inode);
746 up_write(&lli->lli_trunc_sem);
752 static void vvp_io_setattr_fini(const struct lu_env *env,
753 const struct cl_io_slice *ios)
755 bool restore_needed = ios->cis_io->ci_restore_needed;
756 struct inode *inode = vvp_object_inode(ios->cis_obj);
758 vvp_io_fini(env, ios);
760 if (restore_needed && !ios->cis_io->ci_restore_needed) {
761 /* restore finished, set data modified flag for HSM */
762 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
766 static int vvp_io_read_start(const struct lu_env *env,
767 const struct cl_io_slice *ios)
769 struct vvp_io *vio = cl2vvp_io(env, ios);
770 struct cl_io *io = ios->cis_io;
771 struct cl_object *obj = io->ci_obj;
772 struct inode *inode = vvp_object_inode(obj);
773 struct ll_inode_info *lli = ll_i2info(inode);
774 struct file *file = vio->vui_fd->fd_file;
775 loff_t pos = io->u.ci_rd.rd.crw_pos;
776 long cnt = io->u.ci_rd.rd.crw_count;
777 long tot = vio->vui_tot_count;
782 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
784 CDEBUG(D_VFSTRACE, "%s: read [%llu, %llu)\n",
785 file_dentry(file)->d_name.name,
788 if (vio->vui_io_subtype == IO_NORMAL)
789 down_read(&lli->lli_trunc_sem);
791 if (io->ci_async_readahead) {
796 if (!can_populate_pages(env, io, inode))
799 /* Unless this is reading a sparse file, otherwise the lock has already
800 * been acquired so vvp_prep_size() is an empty op. */
801 result = vvp_prep_size(env, obj, io, pos, cnt, &exceed);
804 else if (exceed != 0)
807 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
808 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
809 inode->i_ino, cnt, pos, i_size_read(inode));
811 /* turn off the kernel's read-ahead */
812 vio->vui_fd->fd_file->f_ra.ra_pages = 0;
814 /* initialize read-ahead window once per syscall */
815 if (!vio->vui_ra_valid) {
816 vio->vui_ra_valid = true;
817 vio->vui_ra_start = cl_index(obj, pos);
818 vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1);
819 /* If both start and end are unaligned, we read one more page
820 * than the index math suggests. */
821 if (pos % PAGE_SIZE != 0 && (pos + tot) % PAGE_SIZE != 0)
824 CDEBUG(D_READA, "tot %ld, ra_start %lu, ra_count %lu\n", tot,
825 vio->vui_ra_start, vio->vui_ra_count);
830 switch (vio->vui_io_subtype) {
832 LASSERT(vio->vui_iocb->ki_pos == pos);
833 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
836 result = generic_file_splice_read(file, &pos,
837 vio->u.splice.vui_pipe, cnt,
838 vio->u.splice.vui_flags);
839 /* LU-1109: do splice read stripe by stripe otherwise if it
840 * may make nfsd stuck if this read occupied all internal pipe
845 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
854 io->ci_nob += result;
861 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
862 struct cl_page_list *plist, int from, int to)
864 struct cl_2queue *queue = &io->ci_queue;
865 struct cl_page *page;
866 unsigned int bytes = 0;
870 if (plist->pl_nr == 0)
873 if (from > 0 || to != PAGE_SIZE) {
874 page = cl_page_list_first(plist);
875 if (plist->pl_nr == 1) {
876 cl_page_clip(env, page, from, to);
879 cl_page_clip(env, page, from, PAGE_SIZE);
880 if (to != PAGE_SIZE) {
881 page = cl_page_list_last(plist);
882 cl_page_clip(env, page, 0, to);
887 cl_2queue_init(queue);
888 cl_page_list_splice(plist, &queue->c2_qin);
889 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
891 /* plist is not sorted any more */
892 cl_page_list_splice(&queue->c2_qin, plist);
893 cl_page_list_splice(&queue->c2_qout, plist);
894 cl_2queue_fini(env, queue);
897 /* calculate bytes */
898 bytes = plist->pl_nr << PAGE_SHIFT;
899 bytes -= from + PAGE_SIZE - to;
901 while (plist->pl_nr > 0) {
902 page = cl_page_list_first(plist);
903 cl_page_list_del(env, plist, page);
905 cl_page_clip(env, page, 0, PAGE_SIZE);
907 SetPageUptodate(cl_page_vmpage(page));
908 cl_page_disown(env, io, page);
910 /* held in ll_cl_init() */
911 lu_ref_del(&page->cp_reference, "cl_io", io);
912 cl_page_put(env, page);
916 RETURN(bytes > 0 ? bytes : rc);
919 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
920 struct cl_page *page)
922 struct page *vmpage = page->cp_vmpage;
924 SetPageUptodate(vmpage);
925 set_page_dirty(vmpage);
927 cl_page_disown(env, io, page);
929 /* held in ll_cl_init() */
930 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
931 cl_page_put(env, page);
934 /* make sure the page list is contiguous */
935 static bool page_list_sanity_check(struct cl_object *obj,
936 struct cl_page_list *plist)
938 struct cl_page *page;
939 pgoff_t index = CL_PAGE_EOF;
941 cl_page_list_for_each(page, plist) {
942 struct vvp_page *vpg = cl_object_page_slice(obj, page);
944 if (index == CL_PAGE_EOF) {
945 index = vvp_index(vpg);
950 if (index == vvp_index(vpg))
958 /* Return how many bytes have queued or written */
959 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
961 struct cl_object *obj = io->ci_obj;
962 struct inode *inode = vvp_object_inode(obj);
963 struct vvp_io *vio = vvp_env_io(env);
964 struct cl_page_list *queue = &vio->u.write.vui_queue;
965 struct cl_page *page;
968 unsigned int npages = vio->u.write.vui_queue.pl_nr;
974 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
975 npages, vio->u.write.vui_from, vio->u.write.vui_to);
977 LASSERT(page_list_sanity_check(obj, queue));
979 /* submit IO with async write */
980 rc = cl_io_commit_async(env, io, queue,
981 vio->u.write.vui_from, vio->u.write.vui_to,
982 write_commit_callback);
983 npages -= queue->pl_nr; /* already committed pages */
985 /* calculate how many bytes were written */
986 bytes = npages << PAGE_SHIFT;
989 bytes -= vio->u.write.vui_from;
990 if (queue->pl_nr == 0) /* last page */
991 bytes -= PAGE_SIZE - vio->u.write.vui_to;
992 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
994 vio->u.write.vui_written += bytes;
996 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
997 npages, bytes, vio->u.write.vui_written);
999 /* the first page must have been written. */
1000 vio->u.write.vui_from = 0;
1002 LASSERT(page_list_sanity_check(obj, queue));
1003 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
1005 /* out of quota, try sync write */
1006 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
1007 rc = vvp_io_commit_sync(env, io, queue,
1008 vio->u.write.vui_from,
1009 vio->u.write.vui_to);
1011 vio->u.write.vui_written += rc;
1016 /* update inode size */
1017 ll_merge_attr(env, inode);
1019 /* Now the pages in queue were failed to commit, discard them
1020 * unless they were dirtied before. */
1021 while (queue->pl_nr > 0) {
1022 page = cl_page_list_first(queue);
1023 cl_page_list_del(env, queue, page);
1025 if (!PageDirty(cl_page_vmpage(page)))
1026 cl_page_discard(env, io, page);
1028 cl_page_disown(env, io, page);
1030 /* held in ll_cl_init() */
1031 lu_ref_del(&page->cp_reference, "cl_io", io);
1032 cl_page_put(env, page);
1034 cl_page_list_fini(env, queue);
1039 static int vvp_io_write_start(const struct lu_env *env,
1040 const struct cl_io_slice *ios)
1042 struct vvp_io *vio = cl2vvp_io(env, ios);
1043 struct cl_io *io = ios->cis_io;
1044 struct cl_object *obj = io->ci_obj;
1045 struct inode *inode = vvp_object_inode(obj);
1046 struct ll_inode_info *lli = ll_i2info(inode);
1047 struct file *file = vio->vui_fd->fd_file;
1049 loff_t pos = io->u.ci_wr.wr.crw_pos;
1050 size_t cnt = io->u.ci_wr.wr.crw_count;
1051 bool lock_inode = !IS_NOSEC(inode);
1055 if (vio->vui_io_subtype == IO_NORMAL)
1056 down_read(&lli->lli_trunc_sem);
1058 if (!can_populate_pages(env, io, inode))
1061 if (cl_io_is_append(io)) {
1063 * PARALLEL IO This has to be changed for parallel IO doing
1064 * out-of-order writes.
1066 ll_merge_attr(env, inode);
1067 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1068 vio->vui_iocb->ki_pos = pos;
1070 LASSERTF(vio->vui_iocb->ki_pos == pos,
1071 "ki_pos %lld [%lld, %lld)\n",
1072 vio->vui_iocb->ki_pos,
1076 CDEBUG(D_VFSTRACE, "%s: write [%llu, %llu)\n",
1077 file_dentry(file)->d_name.name,
1080 /* The maximum Lustre file size is variable, based on the OST maximum
1081 * object size and number of stripes. This needs another check in
1082 * addition to the VFS checks earlier. */
1083 if (pos + cnt > ll_file_maxbytes(inode)) {
1085 "%s: file %s ("DFID") offset %llu > maxbytes %llu\n",
1086 ll_i2sbi(inode)->ll_fsname,
1087 file_dentry(file)->d_name.name,
1088 PFID(ll_inode2fid(inode)), pos + cnt,
1089 ll_file_maxbytes(inode));
1093 /* Tests to verify we take the i_mutex correctly */
1094 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_SEC) && !lock_inode)
1097 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_NOSEC) && lock_inode)
1100 if (vio->vui_iter == NULL) {
1101 /* from a temp io in ll_cl_init(). */
1105 * When using the locked AIO function (generic_file_aio_write())
1106 * testing has shown the inode mutex to be a limiting factor
1107 * with multi-threaded single shared file performance. To get
1108 * around this, we now use the lockless version. To maintain
1109 * consistency, proper locking to protect against writes,
1110 * trucates, etc. is handled in the higher layers of lustre.
1112 lock_inode = !IS_NOSEC(inode);
1114 if (unlikely(lock_inode))
1116 result = __generic_file_write_iter(vio->vui_iocb,
1118 if (unlikely(lock_inode))
1119 inode_unlock(inode);
1121 if (result > 0 || result == -EIOCBQUEUED)
1122 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1123 result = generic_write_sync(vio->vui_iocb, result);
1128 err = generic_write_sync(vio->vui_iocb->ki_filp, pos,
1130 if (err < 0 && result > 0)
1137 result = vvp_io_write_commit(env, io);
1138 if (vio->u.write.vui_written > 0) {
1139 result = vio->u.write.vui_written;
1140 CDEBUG(D_VFSTRACE, "%s: write nob %zd, result: %zd\n",
1141 file_dentry(file)->d_name.name,
1142 io->ci_nob, result);
1143 io->ci_nob += result;
1147 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1150 io->ci_continue = 0;
1157 static void vvp_io_rw_end(const struct lu_env *env,
1158 const struct cl_io_slice *ios)
1160 struct vvp_io *vio = cl2vvp_io(env, ios);
1161 struct inode *inode = vvp_object_inode(ios->cis_obj);
1162 struct ll_inode_info *lli = ll_i2info(inode);
1164 if (vio->vui_io_subtype == IO_NORMAL)
1165 up_read(&lli->lli_trunc_sem);
1168 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1170 struct vm_fault *vmf = cfio->ft_vmf;
1172 cfio->ft_flags = ll_filemap_fault(cfio->ft_vma, vmf);
1173 cfio->ft_flags_valid = 1;
1176 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1177 get_vmf_address(vmf));
1178 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1179 lock_page(vmf->page);
1180 cfio->ft_flags |= VM_FAULT_LOCKED;
1183 cfio->ft_vmpage = vmf->page;
1188 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1189 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", get_vmf_address(vmf));
1193 if (cfio->ft_flags & VM_FAULT_OOM) {
1194 CDEBUG(D_PAGE, "got addr %p - OOM\n", get_vmf_address(vmf));
1198 if (cfio->ft_flags & VM_FAULT_RETRY)
1201 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1206 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1207 struct cl_page *page)
1209 set_page_dirty(page->cp_vmpage);
1212 static int vvp_io_fault_start(const struct lu_env *env,
1213 const struct cl_io_slice *ios)
1215 struct vvp_io *vio = cl2vvp_io(env, ios);
1216 struct cl_io *io = ios->cis_io;
1217 struct cl_object *obj = io->ci_obj;
1218 struct inode *inode = vvp_object_inode(obj);
1219 struct ll_inode_info *lli = ll_i2info(inode);
1220 struct cl_fault_io *fio = &io->u.ci_fault;
1221 struct vvp_fault_io *cfio = &vio->u.fault;
1224 struct page *vmpage = NULL;
1225 struct cl_page *page;
1230 down_read(&lli->lli_trunc_sem);
1232 /* offset of the last byte on the page */
1233 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1234 LASSERT(cl_index(obj, offset) == fio->ft_index);
1235 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1239 /* must return locked page */
1240 if (fio->ft_mkwrite) {
1241 LASSERT(cfio->ft_vmpage != NULL);
1242 lock_page(cfio->ft_vmpage);
1244 result = vvp_io_kernel_fault(cfio);
1249 vmpage = cfio->ft_vmpage;
1250 LASSERT(PageLocked(vmpage));
1252 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1253 generic_error_remove_page(vmpage->mapping, vmpage);
1255 size = i_size_read(inode);
1256 /* Though we have already held a cl_lock upon this page, but
1257 * it still can be truncated locally. */
1258 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1259 (page_offset(vmpage) > size))) {
1260 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1262 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1264 GOTO(out, result = +1);
1267 last_index = cl_index(obj, size - 1);
1269 if (fio->ft_mkwrite ) {
1271 * Capture the size while holding the lli_trunc_sem from above
1272 * we want to make sure that we complete the mkwrite action
1273 * while holding this lock. We need to make sure that we are
1274 * not past the end of the file.
1276 if (last_index < fio->ft_index) {
1278 "llite: mkwrite and truncate race happened: "
1279 "%p: 0x%lx 0x%lx\n",
1280 vmpage->mapping,fio->ft_index,last_index);
1282 * We need to return if we are
1283 * passed the end of the file. This will propagate
1284 * up the call stack to ll_page_mkwrite where
1285 * we will return VM_FAULT_NOPAGE. Any non-negative
1286 * value returned here will be silently
1287 * converted to 0. If the vmpage->mapping is null
1288 * the error code would be converted back to ENODATA
1289 * in ll_page_mkwrite0. Thus we return -ENODATA
1290 * to handle both cases
1292 GOTO(out, result = -ENODATA);
1296 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1298 GOTO(out, result = PTR_ERR(page));
1300 /* if page is going to be written, we should add this page into cache
1302 if (fio->ft_mkwrite) {
1303 wait_on_page_writeback(vmpage);
1304 if (!PageDirty(vmpage)) {
1305 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1306 struct vvp_page *vpg = cl_object_page_slice(obj, page);
1309 /* vvp_page_assume() calls wait_on_page_writeback(). */
1310 cl_page_assume(env, io, page);
1312 cl_page_list_init(plist);
1313 cl_page_list_add(plist, page);
1316 if (last_index == vvp_index(vpg))
1317 to = size & ~PAGE_MASK;
1319 /* Do not set Dirty bit here so that in case IO is
1320 * started before the page is really made dirty, we
1321 * still have chance to detect it. */
1322 result = cl_io_commit_async(env, io, plist, 0, to,
1323 mkwrite_commit_callback);
1324 LASSERT(cl_page_is_owned(page, io));
1325 cl_page_list_fini(env, plist);
1329 cl_page_discard(env, io, page);
1330 cl_page_disown(env, io, page);
1332 cl_page_put(env, page);
1334 /* we're in big trouble, what can we do now? */
1335 if (result == -EDQUOT)
1339 cl_page_disown(env, io, page);
1344 * The ft_index is only used in the case of
1345 * a mkwrite action. We need to check
1346 * our assertions are correct, since
1347 * we should have caught this above
1349 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1350 if (fio->ft_index == last_index)
1352 * Last page is mapped partially.
1354 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1356 fio->ft_nob = cl_page_size(obj);
1358 lu_ref_add(&page->cp_reference, "fault", io);
1359 fio->ft_page = page;
1363 /* return unlocked vmpage to avoid deadlocking */
1365 unlock_page(vmpage);
1367 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1372 static void vvp_io_fault_end(const struct lu_env *env,
1373 const struct cl_io_slice *ios)
1375 struct inode *inode = vvp_object_inode(ios->cis_obj);
1376 struct ll_inode_info *lli = ll_i2info(inode);
1378 CLOBINVRNT(env, ios->cis_io->ci_obj,
1379 vvp_object_invariant(ios->cis_io->ci_obj));
1380 up_read(&lli->lli_trunc_sem);
1383 static int vvp_io_fsync_start(const struct lu_env *env,
1384 const struct cl_io_slice *ios)
1386 /* we should mark TOWRITE bit to each dirty page in radix tree to
1387 * verify pages have been written, but this is difficult because of
1392 static int vvp_io_read_ahead(const struct lu_env *env,
1393 const struct cl_io_slice *ios,
1394 pgoff_t start, struct cl_read_ahead *ra)
1399 if (ios->cis_io->ci_type == CIT_READ ||
1400 ios->cis_io->ci_type == CIT_FAULT) {
1401 struct vvp_io *vio = cl2vvp_io(env, ios);
1403 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1404 ra->cra_end = CL_PAGE_EOF;
1405 result = +1; /* no need to call down */
1412 static const struct cl_io_operations vvp_io_ops = {
1415 .cio_fini = vvp_io_fini,
1416 .cio_lock = vvp_io_read_lock,
1417 .cio_start = vvp_io_read_start,
1418 .cio_end = vvp_io_rw_end,
1419 .cio_advance = vvp_io_advance,
1422 .cio_fini = vvp_io_fini,
1423 .cio_iter_init = vvp_io_write_iter_init,
1424 .cio_iter_fini = vvp_io_write_iter_fini,
1425 .cio_lock = vvp_io_write_lock,
1426 .cio_start = vvp_io_write_start,
1427 .cio_end = vvp_io_rw_end,
1428 .cio_advance = vvp_io_advance,
1431 .cio_fini = vvp_io_setattr_fini,
1432 .cio_iter_init = vvp_io_setattr_iter_init,
1433 .cio_lock = vvp_io_setattr_lock,
1434 .cio_start = vvp_io_setattr_start,
1435 .cio_end = vvp_io_setattr_end
1438 .cio_fini = vvp_io_fault_fini,
1439 .cio_iter_init = vvp_io_fault_iter_init,
1440 .cio_lock = vvp_io_fault_lock,
1441 .cio_start = vvp_io_fault_start,
1442 .cio_end = vvp_io_fault_end,
1445 .cio_start = vvp_io_fsync_start,
1446 .cio_fini = vvp_io_fini
1449 .cio_fini = vvp_io_fini
1452 .cio_fini = vvp_io_fini
1455 .cio_fini = vvp_io_fini
1458 .cio_read_ahead = vvp_io_read_ahead
1461 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1464 struct vvp_io *vio = vvp_env_io(env);
1465 struct inode *inode = vvp_object_inode(obj);
1468 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1471 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1472 "restore needed %d\n",
1473 PFID(lu_object_fid(&obj->co_lu)),
1474 io->ci_ignore_layout, io->ci_verify_layout,
1475 vio->vui_layout_gen, io->ci_restore_needed);
1477 CL_IO_SLICE_CLEAN(vio, vui_cl);
1478 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1479 vio->vui_ra_valid = false;
1481 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1483 struct ll_inode_info *lli = ll_i2info(inode);
1485 count = io->u.ci_rw.crw_count;
1486 /* "If nbyte is 0, read() will return 0 and have no other
1487 * results." -- Single Unix Spec */
1491 vio->vui_tot_count = count;
1493 /* for read/write, we store the jobid in the inode, and
1494 * it'll be fetched by osc when building RPC.
1496 * it's not accurate if the file is shared by different
1499 lustre_get_jobid(lli->lli_jobid, sizeof(lli->lli_jobid));
1500 } else if (io->ci_type == CIT_SETATTR) {
1501 if (!cl_io_is_trunc(io))
1502 io->ci_lockreq = CILR_MANDATORY;
1505 /* Enqueue layout lock and get layout version. We need to do this
1506 * even for operations requiring to open file, such as read and write,
1507 * because it might not grant layout lock in IT_OPEN. */
1508 if (result == 0 && !io->ci_ignore_layout) {
1509 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1510 if (result == -ENOENT)
1511 /* If the inode on MDS has been removed, but the objects
1512 * on OSTs haven't been destroyed (async unlink), layout
1513 * fetch will return -ENOENT, we'd ingore this error
1514 * and continue with dirty flush. LU-3230. */
1517 CERROR("%s: refresh file layout " DFID " error %d.\n",
1518 ll_i2sbi(inode)->ll_fsname,
1519 PFID(lu_object_fid(&obj->co_lu)), result);
1522 io->ci_result = result < 0 ? result : 0;