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) {
345 LASSERT(io->ci_type == CIT_WRITE || cl_io_is_trunc(io));
347 io->ci_need_write_intent = 0;
349 if (io->ci_type == CIT_WRITE) {
350 start = io->u.ci_rw.crw_pos;
351 end = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
353 end = io->u.ci_setattr.sa_attr.lvb_size;
356 CDEBUG(D_VFSTRACE, DFID" type %d [%llx, %llx)\n",
357 PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
359 rc = ll_layout_write_intent(inode, start, end);
362 io->ci_need_restart = 1;
365 if (!io->ci_ignore_layout && io->ci_verify_layout) {
368 /* check layout version */
369 ll_layout_refresh(inode, &gen);
370 io->ci_need_restart = vio->vui_layout_gen != gen;
371 if (io->ci_need_restart) {
373 DFID" layout changed from %d to %d.\n",
374 PFID(lu_object_fid(&obj->co_lu)),
375 vio->vui_layout_gen, gen);
376 /* today successful restore is the only possible
378 /* restore was done, clear restoring state */
379 ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
380 LLIF_FILE_RESTORING);
385 static void vvp_io_fault_fini(const struct lu_env *env,
386 const struct cl_io_slice *ios)
388 struct cl_io *io = ios->cis_io;
389 struct cl_page *page = io->u.ci_fault.ft_page;
391 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
394 lu_ref_del(&page->cp_reference, "fault", io);
395 cl_page_put(env, page);
396 io->u.ci_fault.ft_page = NULL;
398 vvp_io_fini(env, ios);
401 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
404 * we only want to hold PW locks if the mmap() can generate
405 * writes back to the file and that only happens in shared
408 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
413 static int vvp_mmap_locks(const struct lu_env *env,
414 struct vvp_io *vio, struct cl_io *io)
416 struct vvp_thread_info *vti = vvp_env_info(env);
417 struct mm_struct *mm = current->mm;
418 struct vm_area_struct *vma;
419 struct cl_lock_descr *descr = &vti->vti_descr;
420 union ldlm_policy_data policy;
426 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
428 if (!cl_is_normalio(env, io))
431 /* nfs or loop back device write */
432 if (vio->vui_iter == NULL)
435 /* No MM (e.g. NFS)? No vmas too. */
439 iov_for_each(iov, i, *(vio->vui_iter)) {
440 unsigned long addr = (unsigned long)iov.iov_base;
441 size_t count = iov.iov_len;
446 count += addr & ~PAGE_MASK;
449 down_read(&mm->mmap_sem);
450 while((vma = our_vma(mm, addr, count)) != NULL) {
451 struct dentry *de = file_dentry(vma->vm_file);
452 struct inode *inode = de->d_inode;
453 int flags = CEF_MUST;
455 if (ll_file_nolock(vma->vm_file)) {
457 * For no lock case is not allowed for mmap
464 * XXX: Required lock mode can be weakened: CIT_WRITE
465 * io only ever reads user level buffer, and CIT_READ
468 policy_from_vma(&policy, vma, addr, count);
469 descr->cld_mode = vvp_mode_from_vma(vma);
470 descr->cld_obj = ll_i2info(inode)->lli_clob;
471 descr->cld_start = cl_index(descr->cld_obj,
472 policy.l_extent.start);
473 descr->cld_end = cl_index(descr->cld_obj,
474 policy.l_extent.end);
475 descr->cld_enq_flags = flags;
476 result = cl_io_lock_alloc_add(env, io, descr);
478 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
479 descr->cld_mode, descr->cld_start,
485 if (vma->vm_end - addr >= count)
488 count -= vma->vm_end - addr;
491 up_read(&mm->mmap_sem);
498 static void vvp_io_advance(const struct lu_env *env,
499 const struct cl_io_slice *ios,
502 struct vvp_io *vio = cl2vvp_io(env, ios);
503 struct cl_io *io = ios->cis_io;
504 struct cl_object *obj = ios->cis_io->ci_obj;
506 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
508 if (!cl_is_normalio(env, io))
511 vio->vui_tot_count -= nob;
512 iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
515 static void vvp_io_update_iov(const struct lu_env *env,
516 struct vvp_io *vio, struct cl_io *io)
518 size_t size = io->u.ci_rw.crw_count;
520 if (!cl_is_normalio(env, io) || vio->vui_iter == NULL)
523 iov_iter_truncate(vio->vui_iter, size);
526 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
527 enum cl_lock_mode mode, loff_t start, loff_t end)
529 struct vvp_io *vio = vvp_env_io(env);
533 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
536 vvp_io_update_iov(env, vio, io);
538 if (io->u.ci_rw.crw_nonblock)
539 ast_flags |= CEF_NONBLOCK;
541 result = vvp_mmap_locks(env, vio, io);
543 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
548 static int vvp_io_read_lock(const struct lu_env *env,
549 const struct cl_io_slice *ios)
551 struct cl_io *io = ios->cis_io;
552 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
556 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
557 rd->crw_pos + rd->crw_count - 1);
561 static int vvp_io_fault_lock(const struct lu_env *env,
562 const struct cl_io_slice *ios)
564 struct cl_io *io = ios->cis_io;
565 struct vvp_io *vio = cl2vvp_io(env, ios);
567 * XXX LDLM_FL_CBPENDING
569 return vvp_io_one_lock_index(env,
571 vvp_mode_from_vma(vio->u.fault.ft_vma),
572 io->u.ci_fault.ft_index,
573 io->u.ci_fault.ft_index);
576 static int vvp_io_write_lock(const struct lu_env *env,
577 const struct cl_io_slice *ios)
579 struct cl_io *io = ios->cis_io;
583 if (io->u.ci_wr.wr_append) {
585 end = OBD_OBJECT_EOF;
587 start = io->u.ci_wr.wr.crw_pos;
588 end = start + io->u.ci_wr.wr.crw_count - 1;
590 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
593 static int vvp_io_setattr_iter_init(const struct lu_env *env,
594 const struct cl_io_slice *ios)
600 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
602 * Handles "lockless io" mode when extent locking is done by server.
604 static int vvp_io_setattr_lock(const struct lu_env *env,
605 const struct cl_io_slice *ios)
607 struct cl_io *io = ios->cis_io;
611 if (cl_io_is_trunc(io)) {
612 new_size = io->u.ci_setattr.sa_attr.lvb_size;
614 enqflags = CEF_DISCARD_DATA;
616 unsigned int valid = io->u.ci_setattr.sa_valid;
618 if (!(valid & TIMES_SET_FLAGS))
621 if ((!(valid & ATTR_MTIME) ||
622 io->u.ci_setattr.sa_attr.lvb_mtime >=
623 io->u.ci_setattr.sa_attr.lvb_ctime) &&
624 (!(valid & ATTR_ATIME) ||
625 io->u.ci_setattr.sa_attr.lvb_atime >=
626 io->u.ci_setattr.sa_attr.lvb_ctime))
632 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
633 new_size, OBD_OBJECT_EOF);
636 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
641 * Only ll_inode_size_lock is taken at this level.
643 ll_inode_size_lock(inode);
644 result = inode_newsize_ok(inode, size);
646 ll_inode_size_unlock(inode);
649 i_size_write(inode, size);
651 ll_truncate_pagecache(inode, size);
652 ll_inode_size_unlock(inode);
656 static int vvp_io_setattr_time(const struct lu_env *env,
657 const struct cl_io_slice *ios)
659 struct cl_io *io = ios->cis_io;
660 struct cl_object *obj = io->ci_obj;
661 struct cl_attr *attr = vvp_env_thread_attr(env);
663 unsigned valid = CAT_CTIME;
665 cl_object_attr_lock(obj);
666 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
667 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
668 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
671 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
672 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
675 result = cl_object_attr_update(env, obj, attr, valid);
676 cl_object_attr_unlock(obj);
681 static int vvp_io_setattr_start(const struct lu_env *env,
682 const struct cl_io_slice *ios)
684 struct cl_io *io = ios->cis_io;
685 struct inode *inode = vvp_object_inode(io->ci_obj);
686 struct ll_inode_info *lli = ll_i2info(inode);
688 if (cl_io_is_trunc(io)) {
689 down_write(&lli->lli_trunc_sem);
691 inode_dio_wait(inode);
696 if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
697 return vvp_io_setattr_time(env, ios);
702 static void vvp_io_setattr_end(const struct lu_env *env,
703 const struct cl_io_slice *ios)
705 struct cl_io *io = ios->cis_io;
706 struct inode *inode = vvp_object_inode(io->ci_obj);
707 struct ll_inode_info *lli = ll_i2info(inode);
709 if (cl_io_is_trunc(io)) {
710 /* Truncate in memory pages - they must be clean pages
711 * because osc has already notified to destroy osc_extents. */
712 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
713 inode_dio_write_done(inode);
715 up_write(&lli->lli_trunc_sem);
721 static void vvp_io_setattr_fini(const struct lu_env *env,
722 const struct cl_io_slice *ios)
724 bool restore_needed = ios->cis_io->ci_restore_needed;
725 struct inode *inode = vvp_object_inode(ios->cis_obj);
727 vvp_io_fini(env, ios);
729 if (restore_needed && !ios->cis_io->ci_restore_needed) {
730 /* restore finished, set data modified flag for HSM */
731 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
735 static int vvp_io_read_start(const struct lu_env *env,
736 const struct cl_io_slice *ios)
738 struct vvp_io *vio = cl2vvp_io(env, ios);
739 struct cl_io *io = ios->cis_io;
740 struct cl_object *obj = io->ci_obj;
741 struct inode *inode = vvp_object_inode(obj);
742 struct ll_inode_info *lli = ll_i2info(inode);
743 struct file *file = vio->vui_fd->fd_file;
746 loff_t pos = io->u.ci_rd.rd.crw_pos;
747 long cnt = io->u.ci_rd.rd.crw_count;
748 long tot = vio->vui_tot_count;
751 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
753 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
755 if (vio->vui_io_subtype == IO_NORMAL)
756 down_read(&lli->lli_trunc_sem);
758 if (!can_populate_pages(env, io, inode))
761 result = vvp_prep_size(env, obj, io, pos, tot, &exceed);
764 else if (exceed != 0)
767 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
768 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
769 inode->i_ino, cnt, pos, i_size_read(inode));
771 /* turn off the kernel's read-ahead */
772 vio->vui_fd->fd_file->f_ra.ra_pages = 0;
774 /* initialize read-ahead window once per syscall */
775 if (!vio->vui_ra_valid) {
776 vio->vui_ra_valid = true;
777 vio->vui_ra_start = cl_index(obj, pos);
778 vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1);
784 switch (vio->vui_io_subtype) {
786 LASSERT(vio->vui_iocb->ki_pos == pos);
787 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
790 result = generic_file_splice_read(file, &pos,
791 vio->u.splice.vui_pipe, cnt,
792 vio->u.splice.vui_flags);
793 /* LU-1109: do splice read stripe by stripe otherwise if it
794 * may make nfsd stuck if this read occupied all internal pipe
799 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
807 io->ci_nob += result;
808 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, vio->vui_fd,
816 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
817 struct cl_page_list *plist, int from, int to)
819 struct cl_2queue *queue = &io->ci_queue;
820 struct cl_page *page;
821 unsigned int bytes = 0;
825 if (plist->pl_nr == 0)
828 if (from > 0 || to != PAGE_SIZE) {
829 page = cl_page_list_first(plist);
830 if (plist->pl_nr == 1) {
831 cl_page_clip(env, page, from, to);
834 cl_page_clip(env, page, from, PAGE_SIZE);
835 if (to != PAGE_SIZE) {
836 page = cl_page_list_last(plist);
837 cl_page_clip(env, page, 0, to);
842 cl_2queue_init(queue);
843 cl_page_list_splice(plist, &queue->c2_qin);
844 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
846 /* plist is not sorted any more */
847 cl_page_list_splice(&queue->c2_qin, plist);
848 cl_page_list_splice(&queue->c2_qout, plist);
849 cl_2queue_fini(env, queue);
852 /* calculate bytes */
853 bytes = plist->pl_nr << PAGE_SHIFT;
854 bytes -= from + PAGE_SIZE - to;
856 while (plist->pl_nr > 0) {
857 page = cl_page_list_first(plist);
858 cl_page_list_del(env, plist, page);
860 cl_page_clip(env, page, 0, PAGE_SIZE);
862 SetPageUptodate(cl_page_vmpage(page));
863 cl_page_disown(env, io, page);
865 /* held in ll_cl_init() */
866 lu_ref_del(&page->cp_reference, "cl_io", io);
867 cl_page_put(env, page);
871 RETURN(bytes > 0 ? bytes : rc);
874 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
875 struct cl_page *page)
877 struct page *vmpage = page->cp_vmpage;
879 SetPageUptodate(vmpage);
880 set_page_dirty(vmpage);
882 cl_page_disown(env, io, page);
884 /* held in ll_cl_init() */
885 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
886 cl_page_put(env, page);
889 /* make sure the page list is contiguous */
890 static bool page_list_sanity_check(struct cl_object *obj,
891 struct cl_page_list *plist)
893 struct cl_page *page;
894 pgoff_t index = CL_PAGE_EOF;
896 cl_page_list_for_each(page, plist) {
897 struct vvp_page *vpg = cl_object_page_slice(obj, page);
899 if (index == CL_PAGE_EOF) {
900 index = vvp_index(vpg);
905 if (index == vvp_index(vpg))
913 /* Return how many bytes have queued or written */
914 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
916 struct cl_object *obj = io->ci_obj;
917 struct inode *inode = vvp_object_inode(obj);
918 struct vvp_io *vio = vvp_env_io(env);
919 struct cl_page_list *queue = &vio->u.write.vui_queue;
920 struct cl_page *page;
923 unsigned int npages = vio->u.write.vui_queue.pl_nr;
929 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
930 npages, vio->u.write.vui_from, vio->u.write.vui_to);
932 LASSERT(page_list_sanity_check(obj, queue));
934 /* submit IO with async write */
935 rc = cl_io_commit_async(env, io, queue,
936 vio->u.write.vui_from, vio->u.write.vui_to,
937 write_commit_callback);
938 npages -= queue->pl_nr; /* already committed pages */
940 /* calculate how many bytes were written */
941 bytes = npages << PAGE_SHIFT;
944 bytes -= vio->u.write.vui_from;
945 if (queue->pl_nr == 0) /* last page */
946 bytes -= PAGE_SIZE - vio->u.write.vui_to;
947 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
949 vio->u.write.vui_written += bytes;
951 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
952 npages, bytes, vio->u.write.vui_written);
954 /* the first page must have been written. */
955 vio->u.write.vui_from = 0;
957 LASSERT(page_list_sanity_check(obj, queue));
958 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
960 /* out of quota, try sync write */
961 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
962 rc = vvp_io_commit_sync(env, io, queue,
963 vio->u.write.vui_from,
964 vio->u.write.vui_to);
966 vio->u.write.vui_written += rc;
971 /* update inode size */
972 ll_merge_attr(env, inode);
974 /* Now the pages in queue were failed to commit, discard them
975 * unless they were dirtied before. */
976 while (queue->pl_nr > 0) {
977 page = cl_page_list_first(queue);
978 cl_page_list_del(env, queue, page);
980 if (!PageDirty(cl_page_vmpage(page)))
981 cl_page_discard(env, io, page);
983 cl_page_disown(env, io, page);
985 /* held in ll_cl_init() */
986 lu_ref_del(&page->cp_reference, "cl_io", io);
987 cl_page_put(env, page);
989 cl_page_list_fini(env, queue);
994 static int vvp_io_write_start(const struct lu_env *env,
995 const struct cl_io_slice *ios)
997 struct vvp_io *vio = cl2vvp_io(env, ios);
998 struct cl_io *io = ios->cis_io;
999 struct cl_object *obj = io->ci_obj;
1000 struct inode *inode = vvp_object_inode(obj);
1001 struct ll_inode_info *lli = ll_i2info(inode);
1003 loff_t pos = io->u.ci_wr.wr.crw_pos;
1004 size_t cnt = io->u.ci_wr.wr.crw_count;
1008 if (vio->vui_io_subtype == IO_NORMAL)
1009 down_read(&lli->lli_trunc_sem);
1011 if (!can_populate_pages(env, io, inode))
1014 if (cl_io_is_append(io)) {
1016 * PARALLEL IO This has to be changed for parallel IO doing
1017 * out-of-order writes.
1019 ll_merge_attr(env, inode);
1020 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1021 vio->vui_iocb->ki_pos = pos;
1023 LASSERT(vio->vui_iocb->ki_pos == pos);
1026 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
1028 /* The maximum Lustre file size is variable, based on the OST maximum
1029 * object size and number of stripes. This needs another check in
1030 * addition to the VFS checks earlier. */
1031 if (pos + cnt > ll_file_maxbytes(inode)) {
1033 "%s: file "DFID" offset %llu > maxbytes %llu\n",
1034 ll_get_fsname(inode->i_sb, NULL, 0),
1035 PFID(ll_inode2fid(inode)), pos + cnt,
1036 ll_file_maxbytes(inode));
1040 if (vio->vui_iter == NULL) {
1041 /* from a temp io in ll_cl_init(). */
1045 * When using the locked AIO function (generic_file_aio_write())
1046 * testing has shown the inode mutex to be a limiting factor
1047 * with multi-threaded single shared file performance. To get
1048 * around this, we now use the lockless version. To maintain
1049 * consistency, proper locking to protect against writes,
1050 * trucates, etc. is handled in the higher layers of lustre.
1052 bool lock_node = !IS_NOSEC(inode);
1056 result = __generic_file_write_iter(vio->vui_iocb,
1059 inode_unlock(inode);
1061 if (result > 0 || result == -EIOCBQUEUED) {
1064 err = generic_write_sync(vio->vui_iocb->ki_filp,
1066 if (err < 0 && result > 0)
1072 result = vvp_io_write_commit(env, io);
1073 if (vio->u.write.vui_written > 0) {
1074 result = vio->u.write.vui_written;
1075 io->ci_nob += result;
1077 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
1078 io->ci_nob, result);
1082 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1085 io->ci_continue = 0;
1086 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
1087 vio->vui_fd, pos, result, WRITE);
1094 static void vvp_io_rw_end(const struct lu_env *env,
1095 const struct cl_io_slice *ios)
1097 struct vvp_io *vio = cl2vvp_io(env, ios);
1098 struct inode *inode = vvp_object_inode(ios->cis_obj);
1099 struct ll_inode_info *lli = ll_i2info(inode);
1101 if (vio->vui_io_subtype == IO_NORMAL)
1102 up_read(&lli->lli_trunc_sem);
1105 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1107 struct vm_fault *vmf = cfio->ft_vmf;
1109 cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf);
1110 cfio->ft_flags_valid = 1;
1113 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1114 vmf->virtual_address);
1115 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1116 lock_page(vmf->page);
1117 cfio->ft_flags |= VM_FAULT_LOCKED;
1120 cfio->ft_vmpage = vmf->page;
1125 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1126 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
1130 if (cfio->ft_flags & VM_FAULT_OOM) {
1131 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
1135 if (cfio->ft_flags & VM_FAULT_RETRY)
1138 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1143 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1144 struct cl_page *page)
1146 set_page_dirty(page->cp_vmpage);
1149 static int vvp_io_fault_start(const struct lu_env *env,
1150 const struct cl_io_slice *ios)
1152 struct vvp_io *vio = cl2vvp_io(env, ios);
1153 struct cl_io *io = ios->cis_io;
1154 struct cl_object *obj = io->ci_obj;
1155 struct inode *inode = vvp_object_inode(obj);
1156 struct ll_inode_info *lli = ll_i2info(inode);
1157 struct cl_fault_io *fio = &io->u.ci_fault;
1158 struct vvp_fault_io *cfio = &vio->u.fault;
1161 struct page *vmpage = NULL;
1162 struct cl_page *page;
1167 down_read(&lli->lli_trunc_sem);
1169 /* offset of the last byte on the page */
1170 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1171 LASSERT(cl_index(obj, offset) == fio->ft_index);
1172 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1176 /* must return locked page */
1177 if (fio->ft_mkwrite) {
1178 LASSERT(cfio->ft_vmpage != NULL);
1179 lock_page(cfio->ft_vmpage);
1181 result = vvp_io_kernel_fault(cfio);
1186 vmpage = cfio->ft_vmpage;
1187 LASSERT(PageLocked(vmpage));
1189 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1190 ll_invalidate_page(vmpage);
1192 size = i_size_read(inode);
1193 /* Though we have already held a cl_lock upon this page, but
1194 * it still can be truncated locally. */
1195 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1196 (page_offset(vmpage) > size))) {
1197 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1199 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1201 GOTO(out, result = +1);
1204 last_index = cl_index(obj, size - 1);
1206 if (fio->ft_mkwrite ) {
1208 * Capture the size while holding the lli_trunc_sem from above
1209 * we want to make sure that we complete the mkwrite action
1210 * while holding this lock. We need to make sure that we are
1211 * not past the end of the file.
1213 if (last_index < fio->ft_index) {
1215 "llite: mkwrite and truncate race happened: "
1216 "%p: 0x%lx 0x%lx\n",
1217 vmpage->mapping,fio->ft_index,last_index);
1219 * We need to return if we are
1220 * passed the end of the file. This will propagate
1221 * up the call stack to ll_page_mkwrite where
1222 * we will return VM_FAULT_NOPAGE. Any non-negative
1223 * value returned here will be silently
1224 * converted to 0. If the vmpage->mapping is null
1225 * the error code would be converted back to ENODATA
1226 * in ll_page_mkwrite0. Thus we return -ENODATA
1227 * to handle both cases
1229 GOTO(out, result = -ENODATA);
1233 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1235 GOTO(out, result = PTR_ERR(page));
1237 /* if page is going to be written, we should add this page into cache
1239 if (fio->ft_mkwrite) {
1240 wait_on_page_writeback(vmpage);
1241 if (!PageDirty(vmpage)) {
1242 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1243 struct vvp_page *vpg = cl_object_page_slice(obj, page);
1246 /* vvp_page_assume() calls wait_on_page_writeback(). */
1247 cl_page_assume(env, io, page);
1249 cl_page_list_init(plist);
1250 cl_page_list_add(plist, page);
1253 if (last_index == vvp_index(vpg))
1254 to = size & ~PAGE_MASK;
1256 /* Do not set Dirty bit here so that in case IO is
1257 * started before the page is really made dirty, we
1258 * still have chance to detect it. */
1259 result = cl_io_commit_async(env, io, plist, 0, to,
1260 mkwrite_commit_callback);
1261 LASSERT(cl_page_is_owned(page, io));
1262 cl_page_list_fini(env, plist);
1266 cl_page_discard(env, io, page);
1267 cl_page_disown(env, io, page);
1269 cl_page_put(env, page);
1271 /* we're in big trouble, what can we do now? */
1272 if (result == -EDQUOT)
1276 cl_page_disown(env, io, page);
1281 * The ft_index is only used in the case of
1282 * a mkwrite action. We need to check
1283 * our assertions are correct, since
1284 * we should have caught this above
1286 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1287 if (fio->ft_index == last_index)
1289 * Last page is mapped partially.
1291 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1293 fio->ft_nob = cl_page_size(obj);
1295 lu_ref_add(&page->cp_reference, "fault", io);
1296 fio->ft_page = page;
1300 /* return unlocked vmpage to avoid deadlocking */
1302 unlock_page(vmpage);
1304 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1309 static void vvp_io_fault_end(const struct lu_env *env,
1310 const struct cl_io_slice *ios)
1312 struct inode *inode = vvp_object_inode(ios->cis_obj);
1313 struct ll_inode_info *lli = ll_i2info(inode);
1315 CLOBINVRNT(env, ios->cis_io->ci_obj,
1316 vvp_object_invariant(ios->cis_io->ci_obj));
1317 up_read(&lli->lli_trunc_sem);
1320 static int vvp_io_fsync_start(const struct lu_env *env,
1321 const struct cl_io_slice *ios)
1323 /* we should mark TOWRITE bit to each dirty page in radix tree to
1324 * verify pages have been written, but this is difficult because of
1329 static int vvp_io_read_ahead(const struct lu_env *env,
1330 const struct cl_io_slice *ios,
1331 pgoff_t start, struct cl_read_ahead *ra)
1336 if (ios->cis_io->ci_type == CIT_READ ||
1337 ios->cis_io->ci_type == CIT_FAULT) {
1338 struct vvp_io *vio = cl2vvp_io(env, ios);
1340 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1341 ra->cra_end = CL_PAGE_EOF;
1342 result = +1; /* no need to call down */
1349 static const struct cl_io_operations vvp_io_ops = {
1352 .cio_fini = vvp_io_fini,
1353 .cio_lock = vvp_io_read_lock,
1354 .cio_start = vvp_io_read_start,
1355 .cio_end = vvp_io_rw_end,
1356 .cio_advance = vvp_io_advance,
1359 .cio_fini = vvp_io_fini,
1360 .cio_iter_init = vvp_io_write_iter_init,
1361 .cio_iter_fini = vvp_io_write_iter_fini,
1362 .cio_lock = vvp_io_write_lock,
1363 .cio_start = vvp_io_write_start,
1364 .cio_end = vvp_io_rw_end,
1365 .cio_advance = vvp_io_advance,
1368 .cio_fini = vvp_io_setattr_fini,
1369 .cio_iter_init = vvp_io_setattr_iter_init,
1370 .cio_lock = vvp_io_setattr_lock,
1371 .cio_start = vvp_io_setattr_start,
1372 .cio_end = vvp_io_setattr_end
1375 .cio_fini = vvp_io_fault_fini,
1376 .cio_iter_init = vvp_io_fault_iter_init,
1377 .cio_lock = vvp_io_fault_lock,
1378 .cio_start = vvp_io_fault_start,
1379 .cio_end = vvp_io_fault_end,
1382 .cio_start = vvp_io_fsync_start,
1383 .cio_fini = vvp_io_fini
1386 .cio_fini = vvp_io_fini
1389 .cio_fini = vvp_io_fini
1392 .cio_read_ahead = vvp_io_read_ahead
1395 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1398 struct vvp_io *vio = vvp_env_io(env);
1399 struct inode *inode = vvp_object_inode(obj);
1402 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1405 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1406 "restore needed %d\n",
1407 PFID(lu_object_fid(&obj->co_lu)),
1408 io->ci_ignore_layout, io->ci_verify_layout,
1409 vio->vui_layout_gen, io->ci_restore_needed);
1411 CL_IO_SLICE_CLEAN(vio, vui_cl);
1412 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1413 vio->vui_ra_valid = false;
1415 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1417 struct ll_inode_info *lli = ll_i2info(inode);
1419 count = io->u.ci_rw.crw_count;
1420 /* "If nbyte is 0, read() will return 0 and have no other
1421 * results." -- Single Unix Spec */
1425 vio->vui_tot_count = count;
1427 /* for read/write, we store the jobid in the inode, and
1428 * it'll be fetched by osc when building RPC.
1430 * it's not accurate if the file is shared by different
1433 lustre_get_jobid(lli->lli_jobid);
1434 } else if (io->ci_type == CIT_SETATTR) {
1435 if (!cl_io_is_trunc(io))
1436 io->ci_lockreq = CILR_MANDATORY;
1439 /* Enqueue layout lock and get layout version. We need to do this
1440 * even for operations requiring to open file, such as read and write,
1441 * because it might not grant layout lock in IT_OPEN. */
1442 if (result == 0 && !io->ci_ignore_layout) {
1443 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1444 if (result == -ENOENT)
1445 /* If the inode on MDS has been removed, but the objects
1446 * on OSTs haven't been destroyed (async unlink), layout
1447 * fetch will return -ENOENT, we'd ingore this error
1448 * and continue with dirty flush. LU-3230. */
1451 CERROR("%s: refresh file layout " DFID " error %d.\n",
1452 ll_get_fsname(inode->i_sb, NULL, 0),
1453 PFID(lu_object_fid(&obj->co_lu)), result);