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/
31 * Implementation of cl_io for VVP layer.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
37 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <linux/pagevec.h>
41 #include <linux/memcontrol.h>
42 #include <linux/falloc.h>
44 #include "llite_internal.h"
45 #include "vvp_internal.h"
46 #include <libcfs/linux/linux-misc.h>
48 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
49 const struct cl_io_slice *slice)
53 vio = container_of(slice, struct vvp_io, vui_cl);
54 LASSERT(vio == vvp_env_io(env));
60 * For swapping layout. The file's layout may have changed.
61 * To avoid populating pages to a wrong stripe, we have to verify the
62 * correctness of layout. It works because swapping layout processes
63 * have to acquire group lock.
65 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
68 struct ll_inode_info *lli = ll_i2info(inode);
69 struct vvp_io *vio = vvp_env_io(env);
72 switch (io->ci_type) {
75 /* don't need lock here to check lli_layout_gen as we have held
76 * extent lock and GROUP lock has to hold to swap layout */
77 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
78 OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
79 io->ci_need_restart = 1;
80 /* this will cause a short read/write */
85 /* fault is okay because we've already had a page. */
93 static void vvp_object_size_lock(struct cl_object *obj)
95 struct inode *inode = vvp_object_inode(obj);
97 ll_inode_size_lock(inode);
98 cl_object_attr_lock(obj);
101 static void vvp_object_size_unlock(struct cl_object *obj)
103 struct inode *inode = vvp_object_inode(obj);
105 cl_object_attr_unlock(obj);
106 ll_inode_size_unlock(inode);
110 * Helper function that if necessary adjusts file size (inode->i_size), when
111 * position at the offset \a pos is accessed. File size can be arbitrary stale
112 * on a Lustre client, but client at least knows KMS. If accessed area is
113 * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
115 * Locking: i_size_lock is used to serialize changes to inode size and to
116 * protect consistency between inode size and cl_object
117 * attributes. cl_object_size_lock() protects consistency between cl_attr's of
118 * top-object and sub-objects.
120 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
121 struct cl_io *io, loff_t start, size_t count,
124 struct cl_attr *attr = vvp_env_thread_attr(env);
125 struct inode *inode = vvp_object_inode(obj);
126 loff_t pos = start + count - 1;
131 * Consistency guarantees: following possibilities exist for the
132 * relation between region being accessed and real file size at this
135 * (A): the region is completely inside of the file;
137 * (B-x): x bytes of region are inside of the file, the rest is
140 * (C): the region is completely outside of the file.
142 * This classification is stable under DLM lock already acquired by
143 * the caller, because to change the class, other client has to take
144 * DLM lock conflicting with our lock. Also, any updates to ->i_size
145 * by other threads on this client are serialized by
146 * ll_inode_size_lock(). This guarantees that short reads are handled
147 * correctly in the face of concurrent writes and truncates.
149 vvp_object_size_lock(obj);
150 result = cl_object_attr_get(env, obj, attr);
153 if (pos > kms || !attr->cat_kms_valid) {
155 * A glimpse is necessary to determine whether we
156 * return a short read (B) or some zeroes at the end
159 vvp_object_size_unlock(obj);
160 result = cl_glimpse_lock(env, io, inode, obj, 0);
161 if (result == 0 && exceed != NULL) {
162 /* If objective page index exceed end-of-file
163 * page index, return directly. Do not expect
164 * kernel will check such case correctly.
165 * linux-2.6.18-128.1.1 miss to do that.
167 loff_t size = i_size_read(inode);
168 unsigned long cur_index = start >>
171 if ((size == 0 && cur_index != 0) ||
172 (((size - 1) >> PAGE_SHIFT) <
180 * region is within kms and, hence, within real file
181 * size (A). We need to increase i_size to cover the
182 * read region so that generic_file_read() will do its
183 * job, but that doesn't mean the kms size is
184 * _correct_, it is only the _minimum_ size. If
185 * someone does a stat they will get the correct size
186 * which will always be >= the kms value here.
189 if (i_size_read(inode) < kms) {
190 i_size_write(inode, kms);
192 DFID" updating i_size %llu\n",
193 PFID(lu_object_fid(&obj->co_lu)),
194 (__u64)i_size_read(inode));
199 vvp_object_size_unlock(obj);
204 /*****************************************************************************
210 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
211 __u32 enqflags, enum cl_lock_mode mode,
212 pgoff_t start, pgoff_t end)
214 struct vvp_io *vio = vvp_env_io(env);
215 struct cl_lock_descr *descr = &vio->vui_link.cill_descr;
216 struct cl_object *obj = io->ci_obj;
218 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
221 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
223 memset(&vio->vui_link, 0, sizeof vio->vui_link);
225 if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
226 descr->cld_mode = CLM_GROUP;
227 descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid;
228 enqflags |= CEF_LOCK_MATCH;
230 descr->cld_mode = mode;
233 descr->cld_obj = obj;
234 descr->cld_start = start;
235 descr->cld_end = end;
236 descr->cld_enq_flags = enqflags;
238 cl_io_lock_add(env, io, &vio->vui_link);
243 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
244 __u32 enqflags, enum cl_lock_mode mode,
245 loff_t start, loff_t end)
247 struct cl_object *obj = io->ci_obj;
249 return vvp_io_one_lock_index(env, io, enqflags, mode,
250 cl_index(obj, start), cl_index(obj, end));
253 static int vvp_io_write_iter_init(const struct lu_env *env,
254 const struct cl_io_slice *ios)
256 struct vvp_io *vio = cl2vvp_io(env, ios);
258 cl_page_list_init(&vio->u.readwrite.vui_queue);
259 vio->u.readwrite.vui_written = 0;
260 vio->u.readwrite.vui_from = 0;
261 vio->u.readwrite.vui_to = PAGE_SIZE;
266 static int vvp_io_read_iter_init(const struct lu_env *env,
267 const struct cl_io_slice *ios)
269 struct vvp_io *vio = cl2vvp_io(env, ios);
271 vio->u.readwrite.vui_read = 0;
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.readwrite.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 clear_bit(LLIF_FILE_RESTORING,
356 &ll_i2info(vvp_object_inode(obj))->lli_flags);
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 || cl_io_is_fallocate(io) ||
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 /* nfs or loop back device write */
450 if (vio->vui_iter == NULL)
453 /* No MM (e.g. NFS)? No vmas too. */
457 if (!iter_is_iovec(vio->vui_iter) && !iov_iter_is_kvec(vio->vui_iter))
460 for (i = *vio->vui_iter;
462 iov_iter_advance(&i, iov.iov_len)) {
463 iov = iov_iter_iovec(&i);
464 addr = (unsigned long)iov.iov_base;
470 count += addr & ~PAGE_MASK;
474 while ((vma = our_vma(mm, addr, count)) != NULL) {
475 struct dentry *de = file_dentry(vma->vm_file);
476 struct inode *inode = de->d_inode;
477 int flags = CEF_MUST;
479 if (ll_file_nolock(vma->vm_file)) {
481 * For no lock case is not allowed for mmap
488 * XXX: Required lock mode can be weakened: CIT_WRITE
489 * io only ever reads user level buffer, and CIT_READ
492 policy_from_vma(&policy, vma, addr, count);
493 descr->cld_mode = vvp_mode_from_vma(vma);
494 descr->cld_obj = ll_i2info(inode)->lli_clob;
495 descr->cld_start = cl_index(descr->cld_obj,
496 policy.l_extent.start);
497 descr->cld_end = cl_index(descr->cld_obj,
498 policy.l_extent.end);
499 descr->cld_enq_flags = flags;
500 result = cl_io_lock_alloc_add(env, io, descr);
502 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
503 descr->cld_mode, descr->cld_start,
509 if (vma->vm_end - addr >= count)
512 count -= vma->vm_end - addr;
515 mmap_read_unlock(mm);
522 static void vvp_io_advance(const struct lu_env *env,
523 const struct cl_io_slice *ios,
526 struct cl_object *obj = ios->cis_io->ci_obj;
527 struct vvp_io *vio = cl2vvp_io(env, ios);
529 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
532 * Since 3.16(26978b8b4) vfs revert iov iter to
533 * original position even io succeed, so instead
534 * of relying on VFS, we move iov iter by ourselves.
536 iov_iter_advance(vio->vui_iter, nob);
537 CDEBUG(D_VFSTRACE, "advancing %ld bytes\n", nob);
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;
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 /* Group lock held means no lockless any more */
571 if (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)
574 if (ll_file_nolock(vio->vui_fd->fd_file) ||
575 (vio->vui_fd->fd_file->f_flags & O_DIRECT &&
577 ast_flags |= CEF_NEVER;
580 result = vvp_mmap_locks(env, vio, io);
582 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
587 static int vvp_io_read_lock(const struct lu_env *env,
588 const struct cl_io_slice *ios)
590 struct cl_io *io = ios->cis_io;
591 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
595 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
596 rd->crw_pos + rd->crw_count - 1);
600 static int vvp_io_fault_lock(const struct lu_env *env,
601 const struct cl_io_slice *ios)
603 struct cl_io *io = ios->cis_io;
604 struct vvp_io *vio = cl2vvp_io(env, ios);
606 * XXX LDLM_FL_CBPENDING
608 return vvp_io_one_lock_index(env,
610 vvp_mode_from_vma(vio->u.fault.ft_vma),
611 io->u.ci_fault.ft_index,
612 io->u.ci_fault.ft_index);
615 static int vvp_io_write_lock(const struct lu_env *env,
616 const struct cl_io_slice *ios)
618 struct cl_io *io = ios->cis_io;
622 if (io->u.ci_wr.wr_append) {
624 end = OBD_OBJECT_EOF;
626 start = io->u.ci_wr.wr.crw_pos;
627 end = start + io->u.ci_wr.wr.crw_count - 1;
630 RETURN(vvp_io_rw_lock(env, io, CLM_WRITE, start, end));
633 static int vvp_io_setattr_iter_init(const struct lu_env *env,
634 const struct cl_io_slice *ios)
641 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
643 * Handles "lockless io" mode when extent locking is done by server.
645 static int vvp_io_setattr_lock(const struct lu_env *env,
646 const struct cl_io_slice *ios)
648 struct cl_io *io = ios->cis_io;
649 __u64 lock_start = 0;
650 __u64 lock_end = OBD_OBJECT_EOF;
653 if (cl_io_is_trunc(io)) {
654 struct inode *inode = vvp_object_inode(io->ci_obj);
656 /* set enqueue flags to CEF_MUST in case of encrypted file,
657 * to prevent lockless truncate
659 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
661 else if (io->u.ci_setattr.sa_attr.lvb_size == 0)
662 enqflags = CEF_DISCARD_DATA;
663 } else if (cl_io_is_fallocate(io)) {
664 lock_start = io->u.ci_setattr.sa_falloc_offset;
665 lock_end = io->u.ci_setattr.sa_falloc_end - 1;
667 unsigned int valid = io->u.ci_setattr.sa_avalid;
669 if (!(valid & TIMES_SET_FLAGS))
672 if ((!(valid & ATTR_MTIME) ||
673 io->u.ci_setattr.sa_attr.lvb_mtime >=
674 io->u.ci_setattr.sa_attr.lvb_ctime) &&
675 (!(valid & ATTR_ATIME) ||
676 io->u.ci_setattr.sa_attr.lvb_atime >=
677 io->u.ci_setattr.sa_attr.lvb_ctime))
681 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
682 lock_start, lock_end);
685 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
690 * Only ll_inode_size_lock is taken at this level.
692 ll_inode_size_lock(inode);
693 result = inode_newsize_ok(inode, size);
695 ll_inode_size_unlock(inode);
698 i_size_write(inode, size);
700 ll_truncate_pagecache(inode, size);
701 ll_inode_size_unlock(inode);
705 static int vvp_io_setattr_time(const struct lu_env *env,
706 const struct cl_io_slice *ios)
708 struct cl_io *io = ios->cis_io;
709 struct cl_object *obj = io->ci_obj;
710 struct cl_attr *attr = vvp_env_thread_attr(env);
712 unsigned valid = CAT_CTIME;
714 cl_object_attr_lock(obj);
715 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
716 if (io->u.ci_setattr.sa_avalid & ATTR_ATIME_SET) {
717 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
720 if (io->u.ci_setattr.sa_avalid & ATTR_MTIME_SET) {
721 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
724 result = cl_object_attr_update(env, obj, attr, valid);
725 cl_object_attr_unlock(obj);
730 static int vvp_io_setattr_start(const struct lu_env *env,
731 const struct cl_io_slice *ios)
733 struct cl_io *io = ios->cis_io;
734 struct inode *inode = vvp_object_inode(io->ci_obj);
735 struct ll_inode_info *lli = ll_i2info(inode);
736 int mode = io->u.ci_setattr.sa_falloc_mode;
738 if (cl_io_is_trunc(io)) {
739 trunc_sem_down_write(&lli->lli_trunc_sem);
740 mutex_lock(&lli->lli_setattr_mutex);
741 inode_dio_wait(inode);
742 } else if (cl_io_is_fallocate(io)) {
745 trunc_sem_down_write(&lli->lli_trunc_sem);
746 mutex_lock(&lli->lli_setattr_mutex);
747 inode_dio_wait(inode);
749 ll_merge_attr(env, inode);
750 size = i_size_read(inode);
751 if (io->u.ci_setattr.sa_falloc_end > size &&
752 !(mode & FALLOC_FL_KEEP_SIZE)) {
753 size = io->u.ci_setattr.sa_falloc_end;
754 io->u.ci_setattr.sa_avalid |= ATTR_SIZE;
756 io->u.ci_setattr.sa_attr.lvb_size = size;
758 mutex_lock(&lli->lli_setattr_mutex);
761 if (io->u.ci_setattr.sa_avalid & TIMES_SET_FLAGS)
762 return vvp_io_setattr_time(env, ios);
767 static void vvp_io_setattr_end(const struct lu_env *env,
768 const struct cl_io_slice *ios)
770 struct cl_io *io = ios->cis_io;
771 struct inode *inode = vvp_object_inode(io->ci_obj);
772 struct ll_inode_info *lli = ll_i2info(inode);
773 loff_t size = io->u.ci_setattr.sa_attr.lvb_size;
775 if (cl_io_is_trunc(io)) {
776 /* Truncate in memory pages - they must be clean pages
777 * because osc has already notified to destroy osc_extents. */
778 vvp_do_vmtruncate(inode, size);
779 mutex_unlock(&lli->lli_setattr_mutex);
780 trunc_sem_up_write(&lli->lli_trunc_sem);
781 } else if (cl_io_is_fallocate(io)) {
782 int mode = io->u.ci_setattr.sa_falloc_mode;
784 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
785 size > i_size_read(inode)) {
786 ll_inode_size_lock(inode);
787 i_size_write(inode, size);
788 ll_inode_size_unlock(inode);
790 inode->i_ctime = current_time(inode);
791 mutex_unlock(&lli->lli_setattr_mutex);
792 trunc_sem_up_write(&lli->lli_trunc_sem);
794 mutex_unlock(&lli->lli_setattr_mutex);
798 static void vvp_io_setattr_fini(const struct lu_env *env,
799 const struct cl_io_slice *ios)
801 bool restore_needed = ios->cis_io->ci_restore_needed;
802 struct inode *inode = vvp_object_inode(ios->cis_obj);
804 vvp_io_fini(env, ios);
806 if (restore_needed && !ios->cis_io->ci_restore_needed) {
807 /* restore finished, set data modified flag for HSM */
808 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
812 static int vvp_io_read_start(const struct lu_env *env,
813 const struct cl_io_slice *ios)
815 struct vvp_io *vio = cl2vvp_io(env, ios);
816 struct cl_io *io = ios->cis_io;
817 struct cl_object *obj = io->ci_obj;
818 struct inode *inode = vvp_object_inode(obj);
819 struct ll_inode_info *lli = ll_i2info(inode);
820 struct file *file = vio->vui_fd->fd_file;
821 loff_t pos = io->u.ci_rd.rd.crw_pos;
822 size_t cnt = io->u.ci_rd.rd.crw_count;
823 size_t tot = vio->vui_tot_count;
826 struct iov_iter iter;
831 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
833 CDEBUG(D_VFSTRACE, "%s: read [%llu, %llu)\n",
834 file_dentry(file)->d_name.name,
837 trunc_sem_down_read(&lli->lli_trunc_sem);
839 if (io->ci_async_readahead) {
844 if (!can_populate_pages(env, io, inode))
847 if (!(file->f_flags & O_DIRECT)) {
848 result = cl_io_lru_reserve(env, io, pos, cnt);
853 /* Unless this is reading a sparse file, otherwise the lock has already
854 * been acquired so vvp_prep_size() is an empty op. */
855 result = vvp_prep_size(env, obj, io, pos, cnt, &exceed);
858 else if (exceed != 0)
861 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
862 "Read ino %lu, %zu bytes, offset %lld, size %llu\n",
863 inode->i_ino, cnt, pos, i_size_read(inode));
865 /* initialize read-ahead window once per syscall */
866 if (!vio->vui_ra_valid) {
867 vio->vui_ra_valid = true;
868 vio->vui_ra_start_idx = cl_index(obj, pos);
869 vio->vui_ra_pages = 0;
870 page_offset = pos & ~PAGE_MASK;
873 if (tot > PAGE_SIZE - page_offset)
874 tot -= (PAGE_SIZE - page_offset);
878 vio->vui_ra_pages += (tot + PAGE_SIZE - 1) >> PAGE_SHIFT;
880 CDEBUG(D_READA, "tot %zu, ra_start %lu, ra_count %lu\n",
881 vio->vui_tot_count, vio->vui_ra_start_idx,
887 LASSERT(vio->vui_iocb->ki_pos == pos);
888 iter = *vio->vui_iter;
889 result = generic_file_read_iter(vio->vui_iocb, &iter);
894 io->ci_nob += result;
896 } else if (result == -EIOCBQUEUED) {
897 io->ci_nob += vio->u.readwrite.vui_read;
898 vio->vui_iocb->ki_pos = pos + vio->u.readwrite.vui_read;
904 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
905 struct cl_page_list *plist, int from, int to)
907 struct cl_2queue *queue = &io->ci_queue;
908 struct cl_page *page;
909 unsigned int bytes = 0;
913 if (plist->pl_nr == 0)
916 if (from > 0 || to != PAGE_SIZE) {
917 page = cl_page_list_first(plist);
918 if (plist->pl_nr == 1) {
919 cl_page_clip(env, page, from, to);
922 cl_page_clip(env, page, from, PAGE_SIZE);
923 if (to != PAGE_SIZE) {
924 page = cl_page_list_last(plist);
925 cl_page_clip(env, page, 0, to);
930 cl_2queue_init(queue);
931 cl_page_list_splice(plist, &queue->c2_qin);
932 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
934 /* plist is not sorted any more */
935 cl_page_list_splice(&queue->c2_qin, plist);
936 cl_page_list_splice(&queue->c2_qout, plist);
937 cl_2queue_fini(env, queue);
940 /* calculate bytes */
941 bytes = plist->pl_nr << PAGE_SHIFT;
942 bytes -= from + PAGE_SIZE - to;
944 while (plist->pl_nr > 0) {
945 page = cl_page_list_first(plist);
946 cl_page_list_del(env, plist, page);
948 cl_page_clip(env, page, 0, PAGE_SIZE);
950 SetPageUptodate(cl_page_vmpage(page));
951 cl_page_disown(env, io, page);
953 /* held in ll_cl_init() */
954 lu_ref_del(&page->cp_reference, "cl_io", io);
955 cl_page_put(env, page);
959 RETURN(bytes > 0 ? bytes : rc);
963 * From kernel v4.19-rc5-248-g9b89a0355144 use XArrary
964 * Prior kernels use radix_tree for tags
966 static inline void ll_page_tag_dirty(struct page *page,
967 struct address_space *mapping)
969 #ifndef HAVE_RADIX_TREE_TAG_SET
970 __xa_set_mark(&mapping->i_pages, page_index(page), PAGECACHE_TAG_DIRTY);
972 radix_tree_tag_set(&mapping->page_tree, page_index(page),
973 PAGECACHE_TAG_DIRTY);
978 * Kernels 4.2 - 4.5 pass memcg argument to account_page_dirtied()
979 * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
981 static inline void ll_account_page_dirtied(struct page *page,
982 struct address_space *mapping)
984 #ifdef HAVE_ACCOUNT_PAGE_DIRTIED_3ARGS
985 struct mem_cgroup *memcg = mem_cgroup_begin_page_stat(page);
987 account_page_dirtied(page, mapping, memcg);
988 mem_cgroup_end_page_stat(memcg);
989 #elif defined(HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT)
990 account_page_dirtied(page, mapping);
992 vvp_account_page_dirtied(page, mapping);
994 ll_page_tag_dirty(page, mapping);
997 /* Taken from kernel set_page_dirty, __set_page_dirty_nobuffers
998 * Last change to this area: b93b016313b3ba8003c3b8bb71f569af91f19fc7
1000 * Current with Linus tip of tree (7/13/2019):
1001 * v5.2-rc4-224-ge01e060fe0
1003 * Backwards compat for 3.x, 5.x kernels relating to memcg handling
1004 * & rename of radix tree to xarray.
1006 void vvp_set_pagevec_dirty(struct pagevec *pvec)
1008 struct page *page = pvec->pages[0];
1009 int count = pagevec_count(pvec);
1011 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
1012 struct address_space *mapping = page->mapping;
1013 unsigned long flags;
1014 unsigned long skip_pages = 0;
1020 BUILD_BUG_ON(PAGEVEC_SIZE > BITS_PER_LONG);
1021 LASSERTF(page->mapping,
1022 "mapping must be set. page %p, page->private (cl_page) %p\n",
1023 page, (void *) page->private);
1025 /* kernels without HAVE_KALLSYMS_LOOKUP_NAME also don't have account_dirty_page
1026 * exported, and if we can't access that symbol, we can't do page dirtying in
1027 * batch (taking the xarray lock only once) so we just fall back to a looped
1028 * call to __set_page_dirty_nobuffers
1030 #ifndef HAVE_KALLSYMS_LOOKUP_NAME
1031 for (i = 0; i < count; i++)
1032 __set_page_dirty_nobuffers(pvec->pages[i]);
1035 * In kernel 5.14.21, kallsyms_lookup_name is defined but
1036 * account_page_dirtied is not exported.
1038 if (!vvp_account_page_dirtied) {
1039 for (i = 0; i < count; i++)
1040 __set_page_dirty_nobuffers(pvec->pages[i]);
1044 for (i = 0; i < count; i++) {
1045 page = pvec->pages[i];
1047 ClearPageReclaim(page);
1049 lock_page_memcg(page);
1050 if (TestSetPageDirty(page)) {
1051 /* page is already dirty .. no extra work needed
1052 * set a flag for the i'th page to be skipped
1054 unlock_page_memcg(page);
1055 skip_pages |= (1 << i);
1059 ll_xa_lock_irqsave(&mapping->i_pages, flags);
1061 /* Notes on differences with __set_page_dirty_nobuffers:
1062 * 1. We don't need to call page_mapping because we know this is a page
1064 * 2. We have the pages locked, so there is no need for the careful
1065 * mapping/mapping2 dance.
1066 * 3. No mapping is impossible. (Race w/truncate mentioned in
1067 * dirty_nobuffers should be impossible because we hold the page lock.)
1068 * 4. All mappings are the same because i/o is only to one file.
1070 for (i = 0; i < count; i++) {
1071 page = pvec->pages[i];
1072 /* if the i'th page was unlocked above, skip it here */
1073 if ((skip_pages >> i) & 1)
1076 LASSERTF(page->mapping == mapping,
1077 "all pages must have the same mapping. page %p, mapping %p, first mapping %p\n",
1078 page, page->mapping, mapping);
1079 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
1080 ll_account_page_dirtied(page, mapping);
1082 unlock_page_memcg(page);
1084 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1086 CDEBUG(D_VFSTRACE, "mapping %p, count %d, dirtied %d\n", mapping,
1089 if (mapping->host && dirtied) {
1090 /* !PageAnon && !swapper_space */
1091 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1098 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
1099 struct pagevec *pvec)
1106 count = pagevec_count(pvec);
1109 for (i = 0; i < count; i++) {
1110 struct page *vmpage = pvec->pages[i];
1111 SetPageUptodate(vmpage);
1114 vvp_set_pagevec_dirty(pvec);
1116 for (i = 0; i < count; i++) {
1117 struct page *vmpage = pvec->pages[i];
1118 struct cl_page *page = (struct cl_page *) vmpage->private;
1119 cl_page_disown(env, io, page);
1120 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
1121 cl_page_put(env, page);
1127 /* make sure the page list is contiguous */
1128 static bool page_list_sanity_check(struct cl_object *obj,
1129 struct cl_page_list *plist)
1131 struct cl_page *page;
1132 pgoff_t index = CL_PAGE_EOF;
1134 cl_page_list_for_each(page, plist) {
1135 if (index == CL_PAGE_EOF) {
1136 index = cl_page_index(page);
1141 if (index == cl_page_index(page))
1149 /* Return how many bytes have queued or written */
1150 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
1152 struct cl_object *obj = io->ci_obj;
1153 struct inode *inode = vvp_object_inode(obj);
1154 struct vvp_io *vio = vvp_env_io(env);
1155 struct cl_page_list *queue = &vio->u.readwrite.vui_queue;
1156 struct cl_page *page;
1159 unsigned int npages = vio->u.readwrite.vui_queue.pl_nr;
1165 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
1166 npages, vio->u.readwrite.vui_from, vio->u.readwrite.vui_to);
1168 LASSERT(page_list_sanity_check(obj, queue));
1170 /* submit IO with async write */
1171 rc = cl_io_commit_async(env, io, queue,
1172 vio->u.readwrite.vui_from,
1173 vio->u.readwrite.vui_to,
1174 write_commit_callback);
1175 npages -= queue->pl_nr; /* already committed pages */
1177 /* calculate how many bytes were written */
1178 bytes = npages << PAGE_SHIFT;
1181 bytes -= vio->u.readwrite.vui_from;
1182 if (queue->pl_nr == 0) /* last page */
1183 bytes -= PAGE_SIZE - vio->u.readwrite.vui_to;
1184 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
1186 vio->u.readwrite.vui_written += bytes;
1188 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
1189 npages, bytes, vio->u.readwrite.vui_written);
1191 /* the first page must have been written. */
1192 vio->u.readwrite.vui_from = 0;
1194 LASSERT(page_list_sanity_check(obj, queue));
1195 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
1197 /* out of quota, try sync write */
1198 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
1199 struct ll_inode_info *lli = ll_i2info(inode);
1201 rc = vvp_io_commit_sync(env, io, queue,
1202 vio->u.readwrite.vui_from,
1203 vio->u.readwrite.vui_to);
1205 vio->u.readwrite.vui_written += rc;
1208 if (lli->lli_clob != NULL)
1209 lov_read_and_clear_async_rc(lli->lli_clob);
1210 lli->lli_async_rc = 0;
1213 /* update inode size */
1214 ll_merge_attr(env, inode);
1216 /* Now the pages in queue were failed to commit, discard them
1217 * unless they were dirtied before. */
1218 while (queue->pl_nr > 0) {
1219 page = cl_page_list_first(queue);
1220 cl_page_list_del(env, queue, page);
1222 if (!PageDirty(cl_page_vmpage(page)))
1223 cl_page_discard(env, io, page);
1225 cl_page_disown(env, io, page);
1227 /* held in ll_cl_init() */
1228 lu_ref_del(&page->cp_reference, "cl_io", io);
1229 cl_page_put(env, page);
1231 cl_page_list_fini(env, queue);
1236 static int vvp_io_write_start(const struct lu_env *env,
1237 const struct cl_io_slice *ios)
1239 struct vvp_io *vio = cl2vvp_io(env, ios);
1240 struct cl_io *io = ios->cis_io;
1241 struct cl_object *obj = io->ci_obj;
1242 struct inode *inode = vvp_object_inode(obj);
1243 struct ll_inode_info *lli = ll_i2info(inode);
1244 struct file *file = vio->vui_fd->fd_file;
1246 loff_t pos = io->u.ci_wr.wr.crw_pos;
1247 size_t cnt = io->u.ci_wr.wr.crw_count;
1248 bool lock_inode = !IS_NOSEC(inode);
1249 size_t nob = io->ci_nob;
1250 struct iov_iter iter;
1255 trunc_sem_down_read(&lli->lli_trunc_sem);
1257 if (!can_populate_pages(env, io, inode))
1260 if (cl_io_is_append(io)) {
1262 * PARALLEL IO This has to be changed for parallel IO doing
1263 * out-of-order writes.
1265 ll_merge_attr(env, inode);
1266 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1267 vio->vui_iocb->ki_pos = pos;
1269 LASSERTF(vio->vui_iocb->ki_pos == pos,
1270 "ki_pos %lld [%lld, %lld)\n",
1271 vio->vui_iocb->ki_pos,
1275 CDEBUG(D_VFSTRACE, "%s: write [%llu, %llu)\n",
1276 file_dentry(file)->d_name.name,
1279 /* The maximum Lustre file size is variable, based on the OST maximum
1280 * object size and number of stripes. This needs another check in
1281 * addition to the VFS checks earlier. */
1282 if (pos + cnt > ll_file_maxbytes(inode)) {
1284 "%s: file %s ("DFID") offset %llu > maxbytes %llu\n",
1285 ll_i2sbi(inode)->ll_fsname,
1286 file_dentry(file)->d_name.name,
1287 PFID(ll_inode2fid(inode)), pos + cnt,
1288 ll_file_maxbytes(inode));
1292 /* Tests to verify we take the i_mutex correctly */
1293 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_SEC) && !lock_inode)
1296 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_NOSEC) && lock_inode)
1299 if (!(file->f_flags & O_DIRECT)) {
1300 result = cl_io_lru_reserve(env, io, pos, cnt);
1305 if (vio->vui_iter == NULL) {
1306 /* from a temp io in ll_cl_init(). */
1310 * When using the locked AIO function (generic_file_aio_write())
1311 * testing has shown the inode mutex to be a limiting factor
1312 * with multi-threaded single shared file performance. To get
1313 * around this, we now use the lockless version. To maintain
1314 * consistency, proper locking to protect against writes,
1315 * trucates, etc. is handled in the higher layers of lustre.
1317 lock_inode = !IS_NOSEC(inode);
1318 iter = *vio->vui_iter;
1320 if (unlikely(lock_inode))
1322 result = __generic_file_write_iter(vio->vui_iocb, &iter);
1323 if (unlikely(lock_inode))
1324 inode_unlock(inode);
1328 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1329 result = generic_write_sync(vio->vui_iocb, result);
1334 err = generic_write_sync(vio->vui_iocb->ki_filp, pos,
1336 if (err < 0 && result > 0)
1343 result = vvp_io_write_commit(env, io);
1344 /* Simulate short commit */
1345 if (CFS_FAULT_CHECK(OBD_FAIL_LLITE_SHORT_COMMIT)) {
1346 vio->u.readwrite.vui_written >>= 1;
1347 if (vio->u.readwrite.vui_written > 0)
1348 io->ci_need_restart = 1;
1350 if (vio->u.readwrite.vui_written > 0) {
1351 result = vio->u.readwrite.vui_written;
1352 CDEBUG(D_VFSTRACE, "%s: write nob %zd, result: %zd\n",
1353 file_dentry(file)->d_name.name,
1354 io->ci_nob, result);
1355 io->ci_nob += result;
1357 io->ci_continue = 0;
1360 if (vio->vui_iocb->ki_pos != (pos + io->ci_nob - nob)) {
1362 "%s: write position mismatch: ki_pos %lld vs. pos %lld, written %zd, commit %zd: rc = %zd\n",
1363 file_dentry(file)->d_name.name,
1364 vio->vui_iocb->ki_pos, pos + io->ci_nob - nob,
1365 written, io->ci_nob - nob, result);
1367 * Rewind ki_pos and vui_iter to where it has
1368 * successfully committed.
1370 vio->vui_iocb->ki_pos = pos + io->ci_nob - nob;
1372 if (result > 0 || result == -EIOCBQUEUED) {
1373 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
1375 if (result != -EIOCBQUEUED && result < cnt)
1376 io->ci_continue = 0;
1380 if (result == -EIOCBQUEUED) {
1381 io->ci_nob += vio->u.readwrite.vui_written;
1382 vio->vui_iocb->ki_pos = pos +
1383 vio->u.readwrite.vui_written;
1390 static void vvp_io_rw_end(const struct lu_env *env,
1391 const struct cl_io_slice *ios)
1393 struct inode *inode = vvp_object_inode(ios->cis_obj);
1394 struct ll_inode_info *lli = ll_i2info(inode);
1396 trunc_sem_up_read(&lli->lli_trunc_sem);
1399 static void detach_and_deref_page(struct cl_page *clp, struct page *vmpage)
1401 if (!clp->cp_defer_detach)
1405 * cl_page_delete0() took a vmpage reference, but not unlink the vmpage
1408 clp->cp_defer_detach = 0;
1409 ClearPagePrivate(vmpage);
1410 vmpage->private = 0;
1413 refcount_dec(&clp->cp_ref);
1416 static int vvp_io_kernel_fault(const struct lu_env *env,
1417 struct vvp_fault_io *cfio)
1419 struct vm_fault *vmf = cfio->ft_vmf;
1420 struct file *vmff = cfio->ft_vma->vm_file;
1421 struct address_space *mapping = vmff->f_mapping;
1422 struct inode *inode = mapping->host;
1423 struct page *vmpage = NULL;
1424 struct cl_page *clp = NULL;
1428 ll_inode_size_lock(inode);
1430 cfio->ft_flags = ll_filemap_fault(cfio->ft_vma, vmf);
1431 cfio->ft_flags_valid = 1;
1434 /* success, vmpage is locked */
1435 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1436 get_vmf_address(vmf));
1437 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1438 lock_page(vmf->page);
1439 cfio->ft_flags |= VM_FAULT_LOCKED;
1442 cfio->ft_vmpage = vmf->page;
1445 * ll_filemap_fault()->ll_readpage() could get an extra cl_page
1446 * reference. So we have to get the cl_page's to check its
1447 * cp_fault_ref and drop the reference later.
1449 clp = cl_vmpage_page(vmf->page, NULL);
1451 GOTO(unlock, rc = 0);
1454 /* filemap_fault() fails, vmpage is not locked */
1456 vmpage = find_get_page(mapping, vmf->pgoff);
1459 clp = cl_vmpage_page(vmpage, NULL);
1460 unlock_page(vmpage);
1464 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1468 * ll_filemap_fault()->ll_readpage() could fill vmpage
1469 * correctly, and unlock the vmpage, while memory pressure or
1470 * truncate could detach cl_page from vmpage, and kernel
1471 * filemap_fault() will wait_on_page_locked(vmpage) and find
1472 * out that the vmpage has been cleared its uptodate bit,
1473 * so it returns VM_FAULT_SIGBUS.
1475 * In this case, we'd retry the filemap_fault()->ll_readpage()
1476 * to rebuild the cl_page and fill vmpage with uptodated data.
1478 if (likely(vmpage)) {
1479 bool need_retry = false;
1482 if (clp->cp_defer_detach) {
1483 detach_and_deref_page(clp, vmpage);
1485 * check i_size to make sure it's not
1486 * over EOF, we don't want to call
1487 * filemap_fault() repeatedly since it
1488 * returns VM_FAULT_SIGBUS without even
1489 * trying if vmf->pgoff is over EOF.
1491 max_idx = DIV_ROUND_UP(
1492 i_size_read(inode), PAGE_SIZE);
1493 if (vmf->pgoff < max_idx)
1496 if (clp->cp_fault_ref) {
1497 clp->cp_fault_ref = 0;
1498 /* ref not released in ll_readpage() */
1499 cl_page_put(env, clp);
1506 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", get_vmf_address(vmf));
1507 GOTO(unlock, rc = -EFAULT);
1510 if (cfio->ft_flags & VM_FAULT_OOM) {
1511 CDEBUG(D_PAGE, "got addr %p - OOM\n", get_vmf_address(vmf));
1512 GOTO(unlock, rc = -ENOMEM);
1515 if (cfio->ft_flags & VM_FAULT_RETRY)
1516 GOTO(unlock, rc = -EAGAIN);
1518 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1521 ll_inode_size_unlock(inode);
1523 if (clp->cp_defer_detach && vmpage)
1524 detach_and_deref_page(clp, vmpage);
1526 /* additional cl_page ref has been taken in ll_readpage() */
1527 if (clp->cp_fault_ref) {
1528 clp->cp_fault_ref = 0;
1529 /* ref not released in ll_readpage() */
1530 cl_page_put(env, clp);
1532 /* ref taken in this function */
1533 cl_page_put(env, clp);
1540 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1541 struct pagevec *pvec)
1543 vvp_set_pagevec_dirty(pvec);
1546 static int vvp_io_fault_start(const struct lu_env *env,
1547 const struct cl_io_slice *ios)
1549 struct vvp_io *vio = cl2vvp_io(env, ios);
1550 struct cl_io *io = ios->cis_io;
1551 struct cl_object *obj = io->ci_obj;
1552 struct inode *inode = vvp_object_inode(obj);
1553 struct ll_inode_info *lli = ll_i2info(inode);
1554 struct cl_fault_io *fio = &io->u.ci_fault;
1555 struct vvp_fault_io *cfio = &vio->u.fault;
1558 struct page *vmpage = NULL;
1559 struct cl_page *page;
1564 trunc_sem_down_read_nowait(&lli->lli_trunc_sem);
1566 /* offset of the last byte on the page */
1567 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1568 LASSERT(cl_index(obj, offset) == fio->ft_index);
1569 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1573 /* must return locked page */
1574 if (fio->ft_mkwrite) {
1575 LASSERT(cfio->ft_vmpage != NULL);
1576 lock_page(cfio->ft_vmpage);
1578 result = vvp_io_kernel_fault(env, cfio);
1583 vmpage = cfio->ft_vmpage;
1584 LASSERT(PageLocked(vmpage));
1586 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1587 generic_error_remove_page(vmpage->mapping, vmpage);
1589 size = i_size_read(inode);
1590 /* Though we have already held a cl_lock upon this page, but
1591 * it still can be truncated locally. */
1592 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1593 (page_offset(vmpage) > size))) {
1594 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1596 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1598 GOTO(out, result = +1);
1601 last_index = cl_index(obj, size - 1);
1603 if (fio->ft_mkwrite ) {
1605 * Capture the size while holding the lli_trunc_sem from above
1606 * we want to make sure that we complete the mkwrite action
1607 * while holding this lock. We need to make sure that we are
1608 * not past the end of the file.
1610 if (last_index < fio->ft_index) {
1612 "llite: mkwrite and truncate race happened: "
1613 "%p: 0x%lx 0x%lx\n",
1614 vmpage->mapping,fio->ft_index,last_index);
1616 * We need to return if we are
1617 * passed the end of the file. This will propagate
1618 * up the call stack to ll_page_mkwrite where
1619 * we will return VM_FAULT_NOPAGE. Any non-negative
1620 * value returned here will be silently
1621 * converted to 0. If the vmpage->mapping is null
1622 * the error code would be converted back to ENODATA
1623 * in ll_page_mkwrite0. Thus we return -ENODATA
1624 * to handle both cases
1626 GOTO(out, result = -ENODATA);
1630 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1632 GOTO(out, result = PTR_ERR(page));
1634 /* if page is going to be written, we should add this page into cache
1636 if (fio->ft_mkwrite) {
1637 wait_on_page_writeback(vmpage);
1638 if (!PageDirty(vmpage)) {
1639 struct cl_page_list *plist = &vio->u.fault.ft_queue;
1642 /* vvp_page_assume() calls wait_on_page_writeback(). */
1643 cl_page_assume(env, io, page);
1645 cl_page_list_init(plist);
1646 cl_page_list_add(plist, page, true);
1649 if (last_index == cl_page_index(page))
1650 to = ((size - 1) & ~PAGE_MASK) + 1;
1652 /* Do not set Dirty bit here so that in case IO is
1653 * started before the page is really made dirty, we
1654 * still have chance to detect it. */
1655 result = cl_io_commit_async(env, io, plist, 0, to,
1656 mkwrite_commit_callback);
1657 /* Have overquota flag, trying sync write to check
1658 * whether indeed out of quota */
1659 if (result == -EDQUOT) {
1661 result = vvp_io_commit_sync(env, io,
1665 cl_page_own(env, io, page);
1666 cl_page_list_add(plist, page, true);
1667 lu_ref_add(&page->cp_reference,
1669 result = cl_io_commit_async(env, io,
1671 mkwrite_commit_callback);
1674 cl_page_put(env, page);
1678 LASSERT(cl_page_is_owned(page, io));
1679 cl_page_list_fini(env, plist);
1683 cl_page_discard(env, io, page);
1684 cl_page_disown(env, io, page);
1686 cl_page_put(env, page);
1688 /* we're in big trouble, what can we do now? */
1689 if (result == -EDQUOT)
1693 cl_page_disown(env, io, page);
1699 * The ft_index is only used in the case of
1700 * a mkwrite action. We need to check
1701 * our assertions are correct, since
1702 * we should have caught this above
1704 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1705 if (fio->ft_index == last_index)
1707 * Last page is mapped partially.
1709 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1711 fio->ft_nob = cl_page_size(obj);
1713 lu_ref_add(&page->cp_reference, "fault", io);
1714 fio->ft_page = page;
1718 /* return unlocked vmpage to avoid deadlocking */
1720 unlock_page(vmpage);
1722 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1727 static void vvp_io_fault_end(const struct lu_env *env,
1728 const struct cl_io_slice *ios)
1730 struct inode *inode = vvp_object_inode(ios->cis_obj);
1731 struct ll_inode_info *lli = ll_i2info(inode);
1733 CLOBINVRNT(env, ios->cis_io->ci_obj,
1734 vvp_object_invariant(ios->cis_io->ci_obj));
1735 trunc_sem_up_read(&lli->lli_trunc_sem);
1738 static int vvp_io_fsync_start(const struct lu_env *env,
1739 const struct cl_io_slice *ios)
1741 /* we should mark TOWRITE bit to each dirty page in radix tree to
1742 * verify pages have been written, but this is difficult because of
1747 static int vvp_io_read_ahead(const struct lu_env *env,
1748 const struct cl_io_slice *ios,
1749 pgoff_t start, struct cl_read_ahead *ra)
1754 if (ios->cis_io->ci_type == CIT_READ ||
1755 ios->cis_io->ci_type == CIT_FAULT) {
1756 struct vvp_io *vio = cl2vvp_io(env, ios);
1758 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1759 ra->cra_end_idx = CL_PAGE_EOF;
1760 result = +1; /* no need to call down */
1767 static int vvp_io_lseek_lock(const struct lu_env *env,
1768 const struct cl_io_slice *ios)
1770 struct cl_io *io = ios->cis_io;
1771 __u64 lock_start = io->u.ci_lseek.ls_start;
1772 __u64 lock_end = OBD_OBJECT_EOF;
1773 __u32 enqflags = CEF_MUST; /* always take client lock */
1775 return vvp_io_one_lock(env, io, enqflags, CLM_READ,
1776 lock_start, lock_end);
1779 static int vvp_io_lseek_start(const struct lu_env *env,
1780 const struct cl_io_slice *ios)
1782 struct cl_io *io = ios->cis_io;
1783 struct inode *inode = vvp_object_inode(io->ci_obj);
1784 __u64 start = io->u.ci_lseek.ls_start;
1787 inode_dio_wait(inode);
1789 /* At the moment we have DLM lock so just update inode
1790 * to know the file size.
1792 ll_merge_attr(env, inode);
1793 if (start >= i_size_read(inode)) {
1794 io->u.ci_lseek.ls_result = -ENXIO;
1800 static void vvp_io_lseek_end(const struct lu_env *env,
1801 const struct cl_io_slice *ios)
1803 struct cl_io *io = ios->cis_io;
1804 struct inode *inode = vvp_object_inode(io->ci_obj);
1806 if (io->u.ci_lseek.ls_result > i_size_read(inode))
1807 io->u.ci_lseek.ls_result = -ENXIO;
1809 inode_unlock(inode);
1812 static const struct cl_io_operations vvp_io_ops = {
1815 .cio_fini = vvp_io_fini,
1816 .cio_iter_init = vvp_io_read_iter_init,
1817 .cio_lock = vvp_io_read_lock,
1818 .cio_start = vvp_io_read_start,
1819 .cio_end = vvp_io_rw_end,
1820 .cio_advance = vvp_io_advance,
1823 .cio_fini = vvp_io_fini,
1824 .cio_iter_init = vvp_io_write_iter_init,
1825 .cio_iter_fini = vvp_io_write_iter_fini,
1826 .cio_lock = vvp_io_write_lock,
1827 .cio_start = vvp_io_write_start,
1828 .cio_end = vvp_io_rw_end,
1829 .cio_advance = vvp_io_advance,
1832 .cio_fini = vvp_io_setattr_fini,
1833 .cio_iter_init = vvp_io_setattr_iter_init,
1834 .cio_lock = vvp_io_setattr_lock,
1835 .cio_start = vvp_io_setattr_start,
1836 .cio_end = vvp_io_setattr_end
1839 .cio_fini = vvp_io_fault_fini,
1840 .cio_iter_init = vvp_io_fault_iter_init,
1841 .cio_lock = vvp_io_fault_lock,
1842 .cio_start = vvp_io_fault_start,
1843 .cio_end = vvp_io_fault_end,
1846 .cio_start = vvp_io_fsync_start,
1847 .cio_fini = vvp_io_fini
1850 .cio_fini = vvp_io_fini
1853 .cio_fini = vvp_io_fini
1856 .cio_fini = vvp_io_fini
1859 .cio_fini = vvp_io_fini,
1860 .cio_lock = vvp_io_lseek_lock,
1861 .cio_start = vvp_io_lseek_start,
1862 .cio_end = vvp_io_lseek_end,
1865 .cio_read_ahead = vvp_io_read_ahead
1868 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1871 struct vvp_io *vio = vvp_env_io(env);
1872 struct inode *inode = vvp_object_inode(obj);
1875 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1878 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1879 "restore needed %d\n",
1880 PFID(lu_object_fid(&obj->co_lu)),
1881 io->ci_ignore_layout, io->ci_verify_layout,
1882 vio->vui_layout_gen, io->ci_restore_needed);
1884 CL_IO_SLICE_CLEAN(vio, vui_cl);
1885 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1886 vio->vui_ra_valid = false;
1888 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1890 struct ll_inode_info *lli = ll_i2info(inode);
1892 count = io->u.ci_rw.crw_count;
1893 /* "If nbyte is 0, read() will return 0 and have no other
1894 * results." -- Single Unix Spec */
1898 vio->vui_tot_count = count;
1900 /* for read/write, we store the jobid in the inode, and
1901 * it'll be fetched by osc when building RPC.
1903 * it's not accurate if the file is shared by different
1906 lustre_get_jobid(lli->lli_jobid, sizeof(lli->lli_jobid));
1907 } else if (io->ci_type == CIT_SETATTR) {
1908 if (!cl_io_is_trunc(io))
1909 io->ci_lockreq = CILR_MANDATORY;
1912 /* Enqueue layout lock and get layout version. We need to do this
1913 * even for operations requiring to open file, such as read and write,
1914 * because it might not grant layout lock in IT_OPEN. */
1915 if (result == 0 && !io->ci_ignore_layout) {
1916 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1917 if (result == -ENOENT)
1918 /* If the inode on MDS has been removed, but the objects
1919 * on OSTs haven't been destroyed (async unlink), layout
1920 * fetch will return -ENOENT, we'd ingore this error
1921 * and continue with dirty flush. LU-3230. */
1924 CERROR("%s: refresh file layout " DFID " error %d.\n",
1925 ll_i2sbi(inode)->ll_fsname,
1926 PFID(lu_object_fid(&obj->co_lu)), result);
1929 io->ci_result = result < 0 ? result : 0;