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 CFS_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 bytes,
124 struct cl_attr *attr = vvp_env_thread_attr(env);
125 struct inode *inode = vvp_object_inode(obj);
126 loff_t pos = start + bytes - 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 return vvp_io_one_lock_index(env, io, enqflags, mode,
248 start >> PAGE_SHIFT, end >> PAGE_SHIFT);
251 static int vvp_io_write_iter_init(const struct lu_env *env,
252 const struct cl_io_slice *ios)
254 struct vvp_io *vio = cl2vvp_io(env, ios);
256 cl_page_list_init(&vio->u.readwrite.vui_queue);
257 vio->u.readwrite.vui_written = 0;
258 vio->u.readwrite.vui_from = 0;
259 vio->u.readwrite.vui_to = PAGE_SIZE;
264 static int vvp_io_read_iter_init(const struct lu_env *env,
265 const struct cl_io_slice *ios)
267 struct vvp_io *vio = cl2vvp_io(env, ios);
269 vio->u.readwrite.vui_read = 0;
274 static void vvp_io_write_iter_fini(const struct lu_env *env,
275 const struct cl_io_slice *ios)
277 struct vvp_io *vio = cl2vvp_io(env, ios);
279 LASSERT(vio->u.readwrite.vui_queue.pl_nr == 0);
282 static int vvp_io_fault_iter_init(const struct lu_env *env,
283 const struct cl_io_slice *ios)
285 struct vvp_io *vio = cl2vvp_io(env, ios);
286 struct inode *inode = vvp_object_inode(ios->cis_obj);
288 LASSERT(inode == file_inode(vio->vui_fd->fd_file));
293 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
295 struct cl_io *io = ios->cis_io;
296 struct cl_object *obj = io->ci_obj;
297 struct vvp_io *vio = cl2vvp_io(env, ios);
298 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, invalidate_lock %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, io->ci_invalidate_page_cache);
312 #ifdef HAVE_INVALIDATE_LOCK
313 if (io->ci_invalidate_page_cache) {
314 filemap_invalidate_unlock(inode->i_mapping);
315 io->ci_invalidate_page_cache = 0;
317 #endif /* HAVE_INVALIDATE_LOCK */
319 if (io->ci_restore_needed) {
320 /* file was detected release, we need to restore it
321 * before finishing the io
323 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
324 /* if restore registration failed, no restart,
325 * we will return -ENODATA */
326 /* The layout will change after restore, so we need to
327 * block on layout lock held by the MDT
328 * as MDT will not send new layout in lvb (see LU-3124)
329 * we have to explicitly fetch it, all this will be done
330 * by ll_layout_refresh().
331 * Even if ll_layout_restore() returns zero, it doesn't mean
332 * that restore has been successful. Therefore it sets
333 * ci_verify_layout so that it will check layout at the end
337 io->ci_restore_needed = 1;
338 io->ci_need_restart = 0;
339 io->ci_verify_layout = 0;
344 io->ci_restore_needed = 0;
346 /* Even if ll_layout_restore() returns zero, it doesn't mean
347 * that restore has been successful. Therefore it should verify
348 * if there was layout change and restart I/O correspondingly.
350 ll_layout_refresh(inode, &gen);
351 io->ci_need_restart = vio->vui_layout_gen != gen;
352 if (io->ci_need_restart) {
354 DFID" layout changed from %d to %d.\n",
355 PFID(lu_object_fid(&obj->co_lu)),
356 vio->vui_layout_gen, gen);
357 /* today successful restore is the only possible
359 /* restore was done, clear restoring state */
360 clear_bit(LLIF_FILE_RESTORING,
361 &ll_i2info(vvp_object_inode(obj))->lli_flags);
367 * dynamic layout change needed, send layout intent
370 if (io->ci_need_write_intent) {
371 enum layout_intent_opc opc = LAYOUT_INTENT_WRITE;
373 io->ci_need_write_intent = 0;
375 LASSERT(io->ci_type == CIT_WRITE || cl_io_is_fallocate(io) ||
376 cl_io_is_trunc(io) || cl_io_is_mkwrite(io));
378 CDEBUG(D_VFSTRACE, DFID" write layout, type %u "DEXT"\n",
379 PFID(lu_object_fid(&obj->co_lu)), io->ci_type,
380 PEXT(&io->ci_write_intent));
382 if (cl_io_is_trunc(io))
383 opc = LAYOUT_INTENT_TRUNC;
385 rc = ll_layout_write_intent(inode, opc, &io->ci_write_intent);
388 io->ci_need_restart = 1;
392 if (!io->ci_need_restart &&
393 !io->ci_ignore_layout && io->ci_verify_layout) {
394 /* check layout version */
395 ll_layout_refresh(inode, &gen);
396 io->ci_need_restart = vio->vui_layout_gen != gen;
397 if (io->ci_need_restart) {
399 DFID" layout changed from %d to %d.\n",
400 PFID(lu_object_fid(&obj->co_lu)),
401 vio->vui_layout_gen, gen);
409 static void vvp_io_fault_fini(const struct lu_env *env,
410 const struct cl_io_slice *ios)
412 struct cl_io *io = ios->cis_io;
413 struct cl_page *page = io->u.ci_fault.ft_page;
415 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
418 lu_ref_del(&page->cp_reference, "fault", io);
419 cl_page_put(env, page);
420 io->u.ci_fault.ft_page = NULL;
422 vvp_io_fini(env, ios);
425 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
428 * we only want to hold PW locks if the mmap() can generate
429 * writes back to the file and that only happens in shared
432 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
437 static int vvp_mmap_locks(const struct lu_env *env,
438 struct vvp_io *vio, struct cl_io *io)
440 struct vvp_thread_info *vti = vvp_env_info(env);
441 struct mm_struct *mm = current->mm;
442 struct vm_area_struct *vma;
443 struct cl_lock_descr *descr = &vti->vti_descr;
444 union ldlm_policy_data policy;
452 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
454 /* nfs or loop back device write */
455 if (vio->vui_iter == NULL)
458 /* No MM (e.g. NFS)? No vmas too. */
462 if (!iter_is_iovec(vio->vui_iter) && !iov_iter_is_kvec(vio->vui_iter))
465 for (i = *vio->vui_iter;
467 iov_iter_advance(&i, iov.iov_len)) {
468 iov = iov_iter_iovec(&i);
469 addr = (unsigned long)iov.iov_base;
475 bytes += addr & ~PAGE_MASK;
479 while ((vma = our_vma(mm, addr, bytes)) != NULL) {
480 struct dentry *de = file_dentry(vma->vm_file);
481 struct inode *inode = de->d_inode;
482 int flags = CEF_MUST;
484 if (ll_file_nolock(vma->vm_file)) {
486 * For no lock case is not allowed for mmap
493 * XXX: Required lock mode can be weakened: CIT_WRITE
494 * io only ever reads user level buffer, and CIT_READ
497 policy_from_vma(&policy, vma, addr, bytes);
498 descr->cld_mode = vvp_mode_from_vma(vma);
499 descr->cld_obj = ll_i2info(inode)->lli_clob;
500 descr->cld_start = policy.l_extent.start >> PAGE_SHIFT;
501 descr->cld_end = policy.l_extent.end >> PAGE_SHIFT;
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 >= bytes)
515 bytes -= vma->vm_end - addr;
518 mmap_read_unlock(mm);
525 static void vvp_io_advance(const struct lu_env *env,
526 const struct cl_io_slice *ios, size_t bytes)
528 struct cl_object *obj = ios->cis_io->ci_obj;
529 struct vvp_io *vio = cl2vvp_io(env, ios);
531 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
534 * Since 3.16(26978b8b4) vfs revert iov iter to
535 * original position even io succeed, so instead
536 * of relying on VFS, we move iov iter by ourselves.
538 iov_iter_advance(vio->vui_iter, bytes);
539 CDEBUG(D_VFSTRACE, "advancing %ld bytes\n", bytes);
540 vio->vui_tot_bytes -= bytes;
541 iov_iter_reexpand(vio->vui_iter, vio->vui_tot_bytes);
544 static void vvp_io_update_iov(const struct lu_env *env,
545 struct vvp_io *vio, struct cl_io *io)
547 size_t size = io->u.ci_rw.crw_bytes;
552 iov_iter_truncate(vio->vui_iter, size);
555 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
556 enum cl_lock_mode mode, loff_t start, loff_t end)
558 struct vvp_io *vio = vvp_env_io(env);
562 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
565 vvp_io_update_iov(env, vio, io);
567 if (io->u.ci_rw.crw_nonblock)
568 ast_flags |= CEF_NONBLOCK;
569 if (io->ci_lock_no_expand)
570 ast_flags |= CEF_LOCK_NO_EXPAND;
572 /* Group lock held means no lockless any more */
573 if (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)
576 if (ll_file_nolock(vio->vui_fd->fd_file) ||
577 (vio->vui_fd->fd_file->f_flags & O_DIRECT &&
579 ast_flags |= CEF_NEVER;
582 result = vvp_mmap_locks(env, vio, io);
584 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
589 static int vvp_io_read_lock(const struct lu_env *env,
590 const struct cl_io_slice *ios)
592 struct cl_io *io = ios->cis_io;
593 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
597 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
598 rd->crw_pos + rd->crw_bytes - 1);
602 static int vvp_io_fault_lock(const struct lu_env *env,
603 const struct cl_io_slice *ios)
605 struct cl_io *io = ios->cis_io;
606 struct vvp_io *vio = cl2vvp_io(env, ios);
608 * XXX LDLM_FL_CBPENDING
610 return vvp_io_one_lock_index(env,
612 vvp_mode_from_vma(vio->u.fault.ft_vma),
613 io->u.ci_fault.ft_index,
614 io->u.ci_fault.ft_index);
617 static int vvp_io_write_lock(const struct lu_env *env,
618 const struct cl_io_slice *ios)
620 struct cl_io *io = ios->cis_io;
624 if (io->u.ci_wr.wr_append) {
626 end = OBD_OBJECT_EOF;
628 start = io->u.ci_wr.wr.crw_pos;
629 end = start + io->u.ci_wr.wr.crw_bytes - 1;
632 RETURN(vvp_io_rw_lock(env, io, CLM_WRITE, start, end));
635 static int vvp_io_setattr_iter_init(const struct lu_env *env,
636 const struct cl_io_slice *ios)
643 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
645 * Handles "lockless io" mode when extent locking is done by server.
647 static int vvp_io_setattr_lock(const struct lu_env *env,
648 const struct cl_io_slice *ios)
650 struct cl_io *io = ios->cis_io;
651 __u64 lock_start = 0;
652 __u64 lock_end = OBD_OBJECT_EOF;
655 if (cl_io_is_trunc(io)) {
656 struct inode *inode = vvp_object_inode(io->ci_obj);
658 /* set enqueue flags to CEF_MUST in case of encrypted file,
659 * to prevent lockless truncate
661 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
663 else if (io->u.ci_setattr.sa_attr.lvb_size == 0)
664 enqflags = CEF_DISCARD_DATA;
665 } else if (cl_io_is_fallocate(io)) {
666 lock_start = io->u.ci_setattr.sa_falloc_offset;
667 lock_end = io->u.ci_setattr.sa_falloc_end - 1;
669 unsigned int valid = io->u.ci_setattr.sa_avalid;
671 if (!(valid & TIMES_SET_FLAGS))
674 if ((!(valid & ATTR_MTIME) ||
675 io->u.ci_setattr.sa_attr.lvb_mtime >=
676 io->u.ci_setattr.sa_attr.lvb_ctime) &&
677 (!(valid & ATTR_ATIME) ||
678 io->u.ci_setattr.sa_attr.lvb_atime >=
679 io->u.ci_setattr.sa_attr.lvb_ctime))
683 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
684 lock_start, lock_end);
687 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
692 * Only ll_inode_size_lock is taken at this level.
694 ll_inode_size_lock(inode);
695 result = inode_newsize_ok(inode, size);
697 ll_inode_size_unlock(inode);
700 i_size_write(inode, size);
702 ll_truncate_pagecache(inode, size);
703 ll_inode_size_unlock(inode);
707 static int vvp_io_setattr_time(const struct lu_env *env,
708 const struct cl_io_slice *ios)
710 struct cl_io *io = ios->cis_io;
711 struct cl_object *obj = io->ci_obj;
712 struct cl_attr *attr = vvp_env_thread_attr(env);
714 unsigned valid = CAT_CTIME;
716 cl_object_attr_lock(obj);
717 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
718 if (io->u.ci_setattr.sa_avalid & ATTR_ATIME_SET) {
719 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
722 if (io->u.ci_setattr.sa_avalid & ATTR_MTIME_SET) {
723 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
726 result = cl_object_attr_update(env, obj, attr, valid);
727 cl_object_attr_unlock(obj);
732 static int vvp_io_setattr_start(const struct lu_env *env,
733 const struct cl_io_slice *ios)
735 struct cl_io *io = ios->cis_io;
736 struct inode *inode = vvp_object_inode(io->ci_obj);
737 struct ll_inode_info *lli = ll_i2info(inode);
738 int mode = io->u.ci_setattr.sa_falloc_mode;
740 if (cl_io_is_trunc(io)) {
741 trunc_sem_down_write(&lli->lli_trunc_sem);
742 mutex_lock(&lli->lli_setattr_mutex);
743 inode_dio_wait(inode);
744 } else if (cl_io_is_fallocate(io)) {
747 trunc_sem_down_write(&lli->lli_trunc_sem);
748 mutex_lock(&lli->lli_setattr_mutex);
749 inode_dio_wait(inode);
751 ll_merge_attr(env, inode);
752 size = i_size_read(inode);
753 if (io->u.ci_setattr.sa_falloc_end > size &&
754 !(mode & FALLOC_FL_KEEP_SIZE)) {
755 size = io->u.ci_setattr.sa_falloc_end;
756 io->u.ci_setattr.sa_avalid |= ATTR_SIZE;
758 io->u.ci_setattr.sa_attr.lvb_size = size;
760 mutex_lock(&lli->lli_setattr_mutex);
763 if (io->u.ci_setattr.sa_avalid & TIMES_SET_FLAGS)
764 return vvp_io_setattr_time(env, ios);
769 static void vvp_io_setattr_end(const struct lu_env *env,
770 const struct cl_io_slice *ios)
772 struct cl_io *io = ios->cis_io;
773 struct inode *inode = vvp_object_inode(io->ci_obj);
774 struct ll_inode_info *lli = ll_i2info(inode);
775 loff_t size = io->u.ci_setattr.sa_attr.lvb_size;
777 if (cl_io_is_trunc(io)) {
778 /* Truncate in memory pages - they must be clean pages
779 * because osc has already notified to destroy osc_extents. */
780 vvp_do_vmtruncate(inode, size);
781 mutex_unlock(&lli->lli_setattr_mutex);
782 trunc_sem_up_write(&lli->lli_trunc_sem);
784 /* Update size and blocks for LSOM */
785 if (!io->ci_ignore_layout)
786 ll_merge_attr(env, inode);
787 } else if (cl_io_is_fallocate(io)) {
788 int mode = io->u.ci_setattr.sa_falloc_mode;
790 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
791 size > i_size_read(inode)) {
792 ll_inode_size_lock(inode);
793 i_size_write(inode, size);
794 ll_inode_size_unlock(inode);
796 inode->i_ctime = current_time(inode);
797 mutex_unlock(&lli->lli_setattr_mutex);
798 trunc_sem_up_write(&lli->lli_trunc_sem);
800 mutex_unlock(&lli->lli_setattr_mutex);
804 static void vvp_io_setattr_fini(const struct lu_env *env,
805 const struct cl_io_slice *ios)
807 bool restore_needed = ios->cis_io->ci_restore_needed;
808 struct inode *inode = vvp_object_inode(ios->cis_obj);
810 vvp_io_fini(env, ios);
812 if (restore_needed && !ios->cis_io->ci_restore_needed) {
813 /* restore finished, set data modified flag for HSM */
814 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
818 static int vvp_io_read_start(const struct lu_env *env,
819 const struct cl_io_slice *ios)
821 struct vvp_io *vio = cl2vvp_io(env, ios);
822 struct cl_io *io = ios->cis_io;
823 struct cl_object *obj = io->ci_obj;
824 struct inode *inode = vvp_object_inode(obj);
825 struct ll_inode_info *lli = ll_i2info(inode);
826 struct file *file = vio->vui_fd->fd_file;
827 loff_t pos = io->u.ci_rd.rd.crw_pos;
828 size_t crw_bytes = io->u.ci_rd.rd.crw_bytes;
829 size_t tot_bytes = vio->vui_tot_bytes;
830 struct ll_cl_context *lcc;
834 int total_bytes_read = 0;
835 struct iov_iter iter;
840 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
842 CDEBUG(D_VFSTRACE, "%s: read [%llu, %llu)\n",
843 file_dentry(file)->d_name.name,
844 pos, pos + crw_bytes);
846 trunc_sem_down_read(&lli->lli_trunc_sem);
848 if (io->ci_async_readahead) {
853 if (!can_populate_pages(env, io, inode))
856 if (!(file->f_flags & O_DIRECT)) {
857 result = cl_io_lru_reserve(env, io, pos, crw_bytes);
862 /* Unless this is reading a sparse file, otherwise the lock has already
863 * been acquired so vvp_prep_size() is an empty op. */
864 result = vvp_prep_size(env, obj, io, pos, crw_bytes, &exceed);
867 else if (exceed != 0)
870 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
871 "Read ino %lu, %zu bytes, offset %lld, size %llu\n",
872 inode->i_ino, crw_bytes, pos, i_size_read(inode));
874 /* initialize read-ahead window once per syscall */
875 if (!vio->vui_ra_valid) {
876 vio->vui_ra_valid = true;
877 vio->vui_ra_start_idx = pos >> PAGE_SHIFT;
878 vio->vui_ra_pages = 0;
879 page_offset = pos & ~PAGE_MASK;
882 if (tot_bytes > PAGE_SIZE - page_offset)
883 tot_bytes -= (PAGE_SIZE - page_offset);
887 vio->vui_ra_pages += (tot_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
889 CDEBUG(D_READA, "tot %zu, ra_start %lu, ra_count %lu\n",
890 vio->vui_tot_bytes, vio->vui_ra_start_idx,
896 LASSERT(vio->vui_iocb->ki_pos == pos);
897 iter = *vio->vui_iter;
899 lcc = ll_cl_find(inode);
900 lcc->lcc_end_index = DIV_ROUND_UP(pos + iter.count, PAGE_SIZE);
901 CDEBUG(D_VFSTRACE, "count:%ld iocb pos:%lld\n", iter.count, pos);
903 /* this seqlock lets us notice if a page has been deleted on this inode
904 * during the fault process, allowing us to catch an erroneous short
909 seq = read_seqbegin(&ll_i2info(inode)->lli_page_inv_lock);
910 result = generic_file_read_iter(vio->vui_iocb, &iter);
912 io->ci_bytes += result;
913 total_bytes_read += result;
915 /* if we got a short read or -EIO and we raced with page invalidation,
918 } while (read_seqretry(&ll_i2info(inode)->lli_page_inv_lock, seq) &&
919 ((result >= 0 && iov_iter_count(&iter) > 0)
924 if (total_bytes_read < crw_bytes)
927 } else if (result == -EIOCBQUEUED) {
928 io->ci_bytes += vio->u.readwrite.vui_read;
929 vio->vui_iocb->ki_pos = pos + vio->u.readwrite.vui_read;
935 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
936 struct cl_page_list *plist, int from, int to)
938 struct cl_2queue *queue = &io->ci_queue;
939 struct cl_page *page;
940 unsigned int bytes = 0;
944 if (plist->pl_nr == 0)
947 if (from > 0 || to != PAGE_SIZE) {
948 page = cl_page_list_first(plist);
949 if (plist->pl_nr == 1) {
950 cl_page_clip(env, page, from, to);
953 cl_page_clip(env, page, from, PAGE_SIZE);
954 if (to != PAGE_SIZE) {
955 page = cl_page_list_last(plist);
956 cl_page_clip(env, page, 0, to);
961 cl_2queue_init(queue);
962 cl_page_list_splice(plist, &queue->c2_qin);
963 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
965 /* plist is not sorted any more */
966 cl_page_list_splice(&queue->c2_qin, plist);
967 cl_page_list_splice(&queue->c2_qout, plist);
968 cl_2queue_fini(env, queue);
971 /* calculate bytes */
972 bytes = plist->pl_nr << PAGE_SHIFT;
973 bytes -= from + PAGE_SIZE - to;
975 while (plist->pl_nr > 0) {
976 page = cl_page_list_first(plist);
977 cl_page_list_del(env, plist, page);
979 cl_page_clip(env, page, 0, PAGE_SIZE);
981 SetPageUptodate(cl_page_vmpage(page));
982 cl_page_disown(env, io, page);
984 /* held in ll_cl_init() */
985 lu_ref_del(&page->cp_reference, "cl_io", io);
986 cl_page_put(env, page);
990 RETURN(bytes > 0 ? bytes : rc);
994 * From kernel v4.19-rc5-248-g9b89a0355144 use XArrary
995 * Prior kernels use radix_tree for tags
997 static inline void ll_page_tag_dirty(struct page *page,
998 struct address_space *mapping)
1000 #ifndef HAVE_RADIX_TREE_TAG_SET
1001 __xa_set_mark(&mapping->i_pages, page_index(page), PAGECACHE_TAG_DIRTY);
1003 radix_tree_tag_set(&mapping->page_tree, page_index(page),
1004 PAGECACHE_TAG_DIRTY);
1009 * Kernels 4.2 - 4.5 pass memcg argument to account_page_dirtied()
1010 * Kernel v5.2-5678-gac1c3e4 no longer exports account_page_dirtied
1012 static inline void ll_account_page_dirtied(struct page *page,
1013 struct address_space *mapping)
1015 #ifdef HAVE_ACCOUNT_PAGE_DIRTIED_3ARGS
1016 struct mem_cgroup *memcg = mem_cgroup_begin_page_stat(page);
1018 account_page_dirtied(page, mapping, memcg);
1019 mem_cgroup_end_page_stat(memcg);
1020 #elif defined(HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT)
1021 account_page_dirtied(page, mapping);
1023 vvp_account_page_dirtied(page, mapping);
1025 ll_page_tag_dirty(page, mapping);
1028 /* Taken from kernel set_page_dirty, __set_page_dirty_nobuffers
1029 * Last change to this area: b93b016313b3ba8003c3b8bb71f569af91f19fc7
1031 * Current with Linus tip of tree (7/13/2019):
1032 * v5.2-rc4-224-ge01e060fe0
1034 * Backwards compat for 3.x, 5.x kernels relating to memcg handling
1035 * & rename of radix tree to xarray.
1037 static void vvp_set_pagevec_dirty(struct pagevec *pvec)
1039 struct page *page = pvec->pages[0];
1040 int count = pagevec_count(pvec);
1042 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
1043 struct address_space *mapping = page->mapping;
1044 unsigned long flags;
1045 unsigned long skip_pages = 0;
1051 BUILD_BUG_ON(PAGEVEC_SIZE > BITS_PER_LONG);
1052 LASSERTF(page->mapping,
1053 "mapping must be set. page %p, page->private (cl_page) %p\n",
1054 page, (void *) page->private);
1057 * kernels without HAVE_KALLSYMS_LOOKUP_NAME also don't have
1058 * account_dirty_page exported, and if we can't access that symbol,
1059 * we can't do page dirtying in batch (taking the xarray lock only once)
1060 * so we just fall back to a looped call to __set_page_dirty_nobuffers
1062 #ifndef HAVE_ACCOUNT_PAGE_DIRTIED_EXPORT
1063 if (!vvp_account_page_dirtied) {
1064 for (i = 0; i < count; i++)
1065 __set_page_dirty_nobuffers(pvec->pages[i]);
1070 #ifdef HAVE_KALLSYMS_LOOKUP_NAME
1071 for (i = 0; i < count; i++) {
1072 page = pvec->pages[i];
1074 ClearPageReclaim(page);
1076 vvp_lock_page_memcg(page);
1077 if (TestSetPageDirty(page)) {
1078 /* page is already dirty .. no extra work needed
1079 * set a flag for the i'th page to be skipped
1081 vvp_unlock_page_memcg(page);
1082 skip_pages |= (1 << i);
1086 ll_xa_lock_irqsave(&mapping->i_pages, flags);
1088 /* Notes on differences with __set_page_dirty_nobuffers:
1089 * 1. We don't need to call page_mapping because we know this is a page
1091 * 2. We have the pages locked, so there is no need for the careful
1092 * mapping/mapping2 dance.
1093 * 3. No mapping is impossible. (Race w/truncate mentioned in
1094 * dirty_nobuffers should be impossible because we hold the page lock.)
1095 * 4. All mappings are the same because i/o is only to one file.
1097 for (i = 0; i < count; i++) {
1098 page = pvec->pages[i];
1099 /* if the i'th page was unlocked above, skip it here */
1100 if ((skip_pages >> i) & 1)
1103 LASSERTF(page->mapping == mapping,
1104 "all pages must have the same mapping. page %p, mapping %p, first mapping %p\n",
1105 page, page->mapping, mapping);
1106 WARN_ON_ONCE(!PagePrivate(page) && !PageUptodate(page));
1107 ll_account_page_dirtied(page, mapping);
1109 vvp_unlock_page_memcg(page);
1111 ll_xa_unlock_irqrestore(&mapping->i_pages, flags);
1113 CDEBUG(D_VFSTRACE, "mapping %p, count %d, dirtied %d\n", mapping,
1116 if (mapping->host && dirtied) {
1117 /* !PageAnon && !swapper_space */
1118 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1124 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
1125 struct pagevec *pvec)
1132 count = pagevec_count(pvec);
1135 for (i = 0; i < count; i++) {
1136 struct page *vmpage = pvec->pages[i];
1137 SetPageUptodate(vmpage);
1140 vvp_set_pagevec_dirty(pvec);
1142 for (i = 0; i < count; i++) {
1143 struct page *vmpage = pvec->pages[i];
1144 struct cl_page *page = (struct cl_page *) vmpage->private;
1145 cl_page_disown(env, io, page);
1146 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
1147 cl_page_put(env, page);
1153 /* make sure the page list is contiguous */
1154 static bool page_list_sanity_check(struct cl_object *obj,
1155 struct cl_page_list *plist)
1157 struct cl_page *page;
1158 pgoff_t index = CL_PAGE_EOF;
1160 cl_page_list_for_each(page, plist) {
1161 if (index == CL_PAGE_EOF) {
1162 index = cl_page_index(page);
1167 if (index == cl_page_index(page))
1175 /* Return how many bytes have queued or written */
1176 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
1178 struct cl_object *obj = io->ci_obj;
1179 struct inode *inode = vvp_object_inode(obj);
1180 struct vvp_io *vio = vvp_env_io(env);
1181 struct cl_page_list *queue = &vio->u.readwrite.vui_queue;
1182 struct cl_page *page;
1185 unsigned int npages = vio->u.readwrite.vui_queue.pl_nr;
1191 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
1192 npages, vio->u.readwrite.vui_from, vio->u.readwrite.vui_to);
1194 LASSERT(page_list_sanity_check(obj, queue));
1196 /* submit IO with async write */
1197 rc = cl_io_commit_async(env, io, queue,
1198 vio->u.readwrite.vui_from,
1199 vio->u.readwrite.vui_to,
1200 write_commit_callback);
1201 npages -= queue->pl_nr; /* already committed pages */
1203 /* calculate how many bytes were written */
1204 bytes = npages << PAGE_SHIFT;
1207 bytes -= vio->u.readwrite.vui_from;
1208 if (queue->pl_nr == 0) /* last page */
1209 bytes -= PAGE_SIZE - vio->u.readwrite.vui_to;
1210 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
1212 vio->u.readwrite.vui_written += bytes;
1214 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
1215 npages, bytes, vio->u.readwrite.vui_written);
1217 /* the first page must have been written. */
1218 vio->u.readwrite.vui_from = 0;
1220 LASSERT(page_list_sanity_check(obj, queue));
1221 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
1223 /* out of quota, try sync write */
1224 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
1225 struct ll_inode_info *lli = ll_i2info(inode);
1227 rc = vvp_io_commit_sync(env, io, queue,
1228 vio->u.readwrite.vui_from,
1229 vio->u.readwrite.vui_to);
1231 vio->u.readwrite.vui_written += rc;
1234 if (lli->lli_clob != NULL)
1235 lov_read_and_clear_async_rc(lli->lli_clob);
1236 lli->lli_async_rc = 0;
1239 /* update inode size */
1240 ll_merge_attr(env, inode);
1242 /* Now the pages in queue were failed to commit, discard them
1243 * unless they were dirtied before. */
1244 while (queue->pl_nr > 0) {
1245 page = cl_page_list_first(queue);
1246 cl_page_list_del(env, queue, page);
1248 if (!PageDirty(cl_page_vmpage(page)))
1249 cl_page_discard(env, io, page);
1251 cl_page_disown(env, io, page);
1253 /* held in ll_cl_init() */
1254 lu_ref_del(&page->cp_reference, "cl_io", io);
1255 cl_page_put(env, page);
1257 cl_page_list_fini(env, queue);
1262 static int vvp_io_write_start(const struct lu_env *env,
1263 const struct cl_io_slice *ios)
1265 struct vvp_io *vio = cl2vvp_io(env, ios);
1266 struct cl_io *io = ios->cis_io;
1267 struct cl_object *obj = io->ci_obj;
1268 struct inode *inode = vvp_object_inode(obj);
1269 struct ll_inode_info *lli = ll_i2info(inode);
1270 struct file *file = vio->vui_fd->fd_file;
1272 loff_t pos = io->u.ci_wr.wr.crw_pos;
1273 size_t crw_bytes = io->u.ci_wr.wr.crw_bytes;
1274 bool lock_inode = !IS_NOSEC(inode);
1275 size_t ci_bytes = io->ci_bytes;
1276 struct iov_iter iter;
1281 trunc_sem_down_read(&lli->lli_trunc_sem);
1283 if (!can_populate_pages(env, io, inode))
1286 if (cl_io_is_append(io)) {
1288 * PARALLEL IO This has to be changed for parallel IO doing
1289 * out-of-order writes.
1291 ll_merge_attr(env, inode);
1292 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
1293 vio->vui_iocb->ki_pos = pos;
1295 LASSERTF(vio->vui_iocb->ki_pos == pos,
1296 "ki_pos %lld [%lld, %lld)\n",
1297 vio->vui_iocb->ki_pos,
1298 pos, pos + crw_bytes);
1301 CDEBUG(D_VFSTRACE, "%s: write [%llu, %llu)\n",
1302 file_dentry(file)->d_name.name, pos, pos + crw_bytes);
1304 /* The maximum Lustre file size is variable, based on the OST maximum
1305 * object size and number of stripes. This needs another check in
1306 * addition to the VFS checks earlier. */
1307 if (pos + crw_bytes > ll_file_maxbytes(inode)) {
1309 "%s: file %s ("DFID") offset %llu > maxbytes %llu\n",
1310 ll_i2sbi(inode)->ll_fsname,
1311 file_dentry(file)->d_name.name,
1312 PFID(ll_inode2fid(inode)), pos + crw_bytes,
1313 ll_file_maxbytes(inode));
1317 /* Tests to verify we take the i_mutex correctly */
1318 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_SEC) && !lock_inode)
1321 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_IMUTEX_NOSEC) && lock_inode)
1324 if (!(file->f_flags & O_DIRECT)) {
1325 result = cl_io_lru_reserve(env, io, pos, crw_bytes);
1330 if (vio->vui_iter == NULL) {
1331 /* from a temp io in ll_cl_init(). */
1335 * When using the locked AIO function (generic_file_aio_write())
1336 * testing has shown the inode mutex to be a limiting factor
1337 * with multi-threaded single shared file performance. To get
1338 * around this, we now use the lockless version. To maintain
1339 * consistency, proper locking to protect against writes,
1340 * trucates, etc. is handled in the higher layers of lustre.
1342 lock_inode = !IS_NOSEC(inode);
1343 iter = *vio->vui_iter;
1345 if (unlikely(lock_inode))
1346 ll_inode_lock(inode);
1347 result = __generic_file_write_iter(vio->vui_iocb, &iter);
1348 if (unlikely(lock_inode))
1349 ll_inode_unlock(inode);
1353 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1354 result = generic_write_sync(vio->vui_iocb, result);
1359 err = generic_write_sync(vio->vui_iocb->ki_filp, pos,
1361 if (err < 0 && result > 0)
1368 result = vvp_io_write_commit(env, io);
1369 /* Simulate short commit */
1370 if (CFS_FAULT_CHECK(OBD_FAIL_LLITE_SHORT_COMMIT)) {
1371 vio->u.readwrite.vui_written >>= 1;
1372 if (vio->u.readwrite.vui_written > 0)
1373 io->ci_need_restart = 1;
1375 if (vio->u.readwrite.vui_written > 0) {
1376 result = vio->u.readwrite.vui_written;
1377 CDEBUG(D_VFSTRACE, "%s: write bytes %zd, result: %zd\n",
1378 file_dentry(file)->d_name.name,
1379 io->ci_bytes, result);
1380 io->ci_bytes += result;
1382 io->ci_continue = 0;
1385 if (vio->vui_iocb->ki_pos != (pos + io->ci_bytes - ci_bytes)) {
1387 "%s: write position mismatch: ki_pos %lld vs. pos %lld, written %zd, commit %ld: rc = %zd\n",
1388 file_dentry(file)->d_name.name,
1389 vio->vui_iocb->ki_pos, pos + io->ci_bytes - ci_bytes,
1390 written, io->ci_bytes - ci_bytes, result);
1392 * Rewind ki_pos and vui_iter to where it has
1393 * successfully committed.
1395 vio->vui_iocb->ki_pos = pos + io->ci_bytes - ci_bytes;
1397 if (result > 0 || result == -EIOCBQUEUED) {
1398 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
1400 if (result != -EIOCBQUEUED && result < crw_bytes)
1401 io->ci_continue = 0;
1405 if (result == -EIOCBQUEUED) {
1406 io->ci_bytes += vio->u.readwrite.vui_written;
1407 vio->vui_iocb->ki_pos = pos +
1408 vio->u.readwrite.vui_written;
1415 static void vvp_io_rw_end(const struct lu_env *env,
1416 const struct cl_io_slice *ios)
1418 struct inode *inode = vvp_object_inode(ios->cis_obj);
1419 struct ll_inode_info *lli = ll_i2info(inode);
1421 trunc_sem_up_read(&lli->lli_trunc_sem);
1424 static void vvp_io_write_end(const struct lu_env *env,
1425 const struct cl_io_slice *ios)
1427 struct inode *inode = vvp_object_inode(ios->cis_obj);
1428 struct cl_io *io = ios->cis_io;
1430 vvp_io_rw_end(env, ios);
1432 /* Update size and blocks for LSOM (best effort) */
1433 if (!io->ci_ignore_layout && cl_io_is_sync_write(io))
1434 ll_merge_attr_try(env, inode);
1438 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1440 struct vm_fault *vmf = cfio->ft_vmf;
1442 cfio->ft_flags = ll_filemap_fault(cfio->ft_vma, vmf);
1443 cfio->ft_flags_valid = 1;
1446 /* success, vmpage is locked */
1447 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1448 get_vmf_address(vmf));
1449 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1450 lock_page(vmf->page);
1451 cfio->ft_flags |= VM_FAULT_LOCKED;
1454 cfio->ft_vmpage = vmf->page;
1459 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1460 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", get_vmf_address(vmf));
1464 if (cfio->ft_flags & VM_FAULT_OOM) {
1465 CDEBUG(D_PAGE, "got addr %p - OOM\n", get_vmf_address(vmf));
1469 if (cfio->ft_flags & VM_FAULT_RETRY)
1472 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1477 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1478 struct pagevec *pvec)
1480 vvp_set_pagevec_dirty(pvec);
1483 static int vvp_io_fault_start(const struct lu_env *env,
1484 const struct cl_io_slice *ios)
1486 struct vvp_io *vio = cl2vvp_io(env, ios);
1487 struct cl_io *io = ios->cis_io;
1488 struct cl_object *obj = io->ci_obj;
1489 struct inode *inode = vvp_object_inode(obj);
1490 struct ll_inode_info *lli = ll_i2info(inode);
1491 struct cl_fault_io *fio = &io->u.ci_fault;
1492 struct vvp_fault_io *cfio = &vio->u.fault;
1495 struct page *vmpage = NULL;
1496 struct cl_page *page;
1501 trunc_sem_down_read_nowait(&lli->lli_trunc_sem);
1503 /* offset of the last byte on the page */
1504 offset = ((fio->ft_index + 1) << PAGE_SHIFT) - 1;
1505 LASSERT((offset >> PAGE_SHIFT) == fio->ft_index);
1506 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1510 /* must return locked page */
1511 if (fio->ft_mkwrite) {
1512 LASSERT(cfio->ft_vmpage != NULL);
1513 lock_page(cfio->ft_vmpage);
1515 result = vvp_io_kernel_fault(cfio);
1520 vmpage = cfio->ft_vmpage;
1521 LASSERT(PageLocked(vmpage));
1523 if (CFS_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1524 generic_error_remove_page(vmpage->mapping, vmpage);
1526 size = i_size_read(inode);
1527 /* Though we have already held a cl_lock upon this page, but
1528 * it still can be truncated locally. */
1529 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1530 (page_offset(vmpage) > size))) {
1531 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1533 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1535 GOTO(out, result = +1);
1538 last_index = (size - 1) >> PAGE_SHIFT;
1540 if (fio->ft_mkwrite ) {
1542 * Capture the size while holding the lli_trunc_sem from above
1543 * we want to make sure that we complete the mkwrite action
1544 * while holding this lock. We need to make sure that we are
1545 * not past the end of the file.
1547 if (last_index < fio->ft_index) {
1549 "llite: mkwrite and truncate race happened: "
1550 "%p: 0x%lx 0x%lx\n",
1551 vmpage->mapping,fio->ft_index,last_index);
1553 * We need to return if we are
1554 * passed the end of the file. This will propagate
1555 * up the call stack to ll_page_mkwrite where
1556 * we will return VM_FAULT_NOPAGE. Any non-negative
1557 * value returned here will be silently
1558 * converted to 0. If the vmpage->mapping is null
1559 * the error code would be converted back to ENODATA
1560 * in ll_page_mkwrite0. Thus we return -ENODATA
1561 * to handle both cases
1563 GOTO(out, result = -ENODATA);
1567 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1569 GOTO(out, result = PTR_ERR(page));
1571 /* if page is going to be written, we should add this page into cache
1573 if (fio->ft_mkwrite) {
1574 wait_on_page_writeback(vmpage);
1575 if (!PageDirty(vmpage)) {
1576 struct cl_page_list *plist = &vio->u.fault.ft_queue;
1579 /* vvp_page_assume() calls wait_on_page_writeback(). */
1580 cl_page_assume(env, io, page);
1582 cl_page_list_init(plist);
1583 cl_page_list_add(plist, page, true);
1586 if (last_index == cl_page_index(page))
1587 to = ((size - 1) & ~PAGE_MASK) + 1;
1589 /* Do not set Dirty bit here so that in case IO is
1590 * started before the page is really made dirty, we
1591 * still have chance to detect it. */
1592 result = cl_io_commit_async(env, io, plist, 0, to,
1593 mkwrite_commit_callback);
1594 /* Have overquota flag, trying sync write to check
1595 * whether indeed out of quota */
1596 if (result == -EDQUOT) {
1598 result = vvp_io_commit_sync(env, io,
1602 cl_page_own(env, io, page);
1603 cl_page_list_add(plist, page, true);
1604 lu_ref_add(&page->cp_reference,
1606 result = cl_io_commit_async(env, io,
1608 mkwrite_commit_callback);
1611 cl_page_put(env, page);
1615 LASSERT(cl_page_is_owned(page, io));
1616 cl_page_list_fini(env, plist);
1620 cl_page_discard(env, io, page);
1621 cl_page_disown(env, io, page);
1623 cl_page_put(env, page);
1625 /* we're in big trouble, what can we do now? */
1626 if (result == -EDQUOT)
1630 cl_page_disown(env, io, page);
1636 * The ft_index is only used in the case of mkwrite action. We need to
1637 * check our assertions are correct, since we should have caught this
1640 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1641 if (fio->ft_index == last_index)
1643 * Last page is mapped partially.
1645 fio->ft_bytes = size - (fio->ft_index << PAGE_SHIFT);
1647 fio->ft_bytes = PAGE_SIZE;
1649 lu_ref_add(&page->cp_reference, "fault", io);
1650 fio->ft_page = page;
1654 /* return unlocked vmpage to avoid deadlocking */
1656 unlock_page(vmpage);
1658 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1663 static void vvp_io_fault_end(const struct lu_env *env,
1664 const struct cl_io_slice *ios)
1666 struct inode *inode = vvp_object_inode(ios->cis_obj);
1667 struct ll_inode_info *lli = ll_i2info(inode);
1669 CLOBINVRNT(env, ios->cis_io->ci_obj,
1670 vvp_object_invariant(ios->cis_io->ci_obj));
1671 trunc_sem_up_read(&lli->lli_trunc_sem);
1674 static int vvp_io_fsync_start(const struct lu_env *env,
1675 const struct cl_io_slice *ios)
1677 /* we should mark TOWRITE bit to each dirty page in radix tree to
1678 * verify pages have been written, but this is difficult because of
1683 static void vvp_io_fsync_end(const struct lu_env *env,
1684 const struct cl_io_slice *ios)
1686 struct inode *inode = vvp_object_inode(ios->cis_obj);
1687 struct cl_io *io = ios->cis_io;
1689 /* Update size and blocks for LSOM (best effort) */
1690 if (!io->ci_ignore_layout)
1691 ll_merge_attr_try(env, inode);
1694 static int vvp_io_read_ahead(const struct lu_env *env,
1695 const struct cl_io_slice *ios,
1696 pgoff_t start, struct cl_read_ahead *ra)
1701 if (ios->cis_io->ci_type == CIT_READ ||
1702 ios->cis_io->ci_type == CIT_FAULT) {
1703 struct vvp_io *vio = cl2vvp_io(env, ios);
1705 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1706 ra->cra_end_idx = CL_PAGE_EOF;
1707 result = +1; /* no need to call down */
1714 static int vvp_io_lseek_lock(const struct lu_env *env,
1715 const struct cl_io_slice *ios)
1717 struct cl_io *io = ios->cis_io;
1718 __u64 lock_start = io->u.ci_lseek.ls_start;
1719 __u64 lock_end = OBD_OBJECT_EOF;
1720 __u32 enqflags = CEF_MUST; /* always take client lock */
1722 return vvp_io_one_lock(env, io, enqflags, CLM_READ,
1723 lock_start, lock_end);
1726 static int vvp_io_lseek_start(const struct lu_env *env,
1727 const struct cl_io_slice *ios)
1729 struct cl_io *io = ios->cis_io;
1730 struct inode *inode = vvp_object_inode(io->ci_obj);
1731 __u64 start = io->u.ci_lseek.ls_start;
1733 ll_inode_lock(inode);
1734 inode_dio_wait(inode);
1736 /* At the moment we have DLM lock so just update inode
1737 * to know the file size.
1739 ll_merge_attr(env, inode);
1740 if (start >= i_size_read(inode)) {
1741 io->u.ci_lseek.ls_result = -ENXIO;
1747 static void vvp_io_lseek_end(const struct lu_env *env,
1748 const struct cl_io_slice *ios)
1750 struct cl_io *io = ios->cis_io;
1751 struct inode *inode = vvp_object_inode(io->ci_obj);
1753 if (io->u.ci_lseek.ls_result > i_size_read(inode))
1754 io->u.ci_lseek.ls_result = -ENXIO;
1756 ll_inode_unlock(inode);
1759 static const struct cl_io_operations vvp_io_ops = {
1762 .cio_fini = vvp_io_fini,
1763 .cio_iter_init = vvp_io_read_iter_init,
1764 .cio_lock = vvp_io_read_lock,
1765 .cio_start = vvp_io_read_start,
1766 .cio_end = vvp_io_rw_end,
1767 .cio_advance = vvp_io_advance,
1770 .cio_fini = vvp_io_fini,
1771 .cio_iter_init = vvp_io_write_iter_init,
1772 .cio_iter_fini = vvp_io_write_iter_fini,
1773 .cio_lock = vvp_io_write_lock,
1774 .cio_start = vvp_io_write_start,
1775 .cio_end = vvp_io_write_end,
1776 .cio_advance = vvp_io_advance,
1779 .cio_fini = vvp_io_setattr_fini,
1780 .cio_iter_init = vvp_io_setattr_iter_init,
1781 .cio_lock = vvp_io_setattr_lock,
1782 .cio_start = vvp_io_setattr_start,
1783 .cio_end = vvp_io_setattr_end
1786 .cio_fini = vvp_io_fault_fini,
1787 .cio_iter_init = vvp_io_fault_iter_init,
1788 .cio_lock = vvp_io_fault_lock,
1789 .cio_start = vvp_io_fault_start,
1790 .cio_end = vvp_io_fault_end,
1793 .cio_start = vvp_io_fsync_start,
1794 .cio_fini = vvp_io_fini,
1795 .cio_end = vvp_io_fsync_end,
1798 .cio_fini = vvp_io_fini
1801 .cio_fini = vvp_io_fini
1804 .cio_fini = vvp_io_fini
1807 .cio_fini = vvp_io_fini,
1808 .cio_lock = vvp_io_lseek_lock,
1809 .cio_start = vvp_io_lseek_start,
1810 .cio_end = vvp_io_lseek_end,
1813 .cio_read_ahead = vvp_io_read_ahead
1816 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1819 struct vvp_io *vio = vvp_env_io(env);
1820 struct inode *inode = vvp_object_inode(obj);
1823 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1826 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1827 "restore needed %d\n",
1828 PFID(lu_object_fid(&obj->co_lu)),
1829 io->ci_ignore_layout, io->ci_verify_layout,
1830 vio->vui_layout_gen, io->ci_restore_needed);
1832 CL_IO_SLICE_CLEAN(vio, vui_cl);
1833 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1834 vio->vui_ra_valid = false;
1836 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1838 struct ll_inode_info *lli = ll_i2info(inode);
1840 bytes = io->u.ci_rw.crw_bytes;
1841 /* "If nbyte is 0, read() will return 0 and have no other
1842 * results." -- Single Unix Spec */
1846 vio->vui_tot_bytes = bytes;
1848 /* for read/write, we store the process jobid/gid/uid in the
1849 * inode, and it'll be fetched by osc when building RPC.
1851 * it's not accurate if the file is shared by different
1854 lustre_get_jobid(lli->lli_jobid, sizeof(lli->lli_jobid));
1855 lli->lli_uid = from_kuid(&init_user_ns, current_uid());
1856 lli->lli_gid = from_kgid(&init_user_ns, current_gid());
1857 } else if (io->ci_type == CIT_SETATTR) {
1858 if (!cl_io_is_trunc(io))
1859 io->ci_lockreq = CILR_MANDATORY;
1862 /* Enqueue layout lock and get layout version. We need to do this
1863 * even for operations requiring to open file, such as read and write,
1864 * because it might not grant layout lock in IT_OPEN. */
1865 if (result == 0 && !io->ci_ignore_layout) {
1866 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1867 if (result == -ENOENT)
1868 /* If the inode on MDS has been removed, but the objects
1869 * on OSTs haven't been destroyed (async unlink), layout
1870 * fetch will return -ENOENT, we'd ingore this error
1871 * and continue with dirty flush. LU-3230. */
1874 CERROR("%s: refresh file layout " DFID " error %d.\n",
1875 ll_i2sbi(inode)->ll_fsname,
1876 PFID(lu_object_fid(&obj->co_lu)), result);
1879 #ifdef HAVE_INVALIDATE_LOCK
1880 if (io->ci_invalidate_page_cache)
1881 filemap_invalidate_lock(inode->i_mapping);
1882 #endif /* HAVE_INVALIDATE_LOCK */
1884 io->ci_result = result < 0 ? result : 0;