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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_io for VVP layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include "../include/obd.h"
42 #include "llite_internal.h"
43 #include "vvp_internal.h"
45 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
46 const struct cl_io_slice *slice)
50 vio = container_of(slice, struct vvp_io, vui_cl);
51 LASSERT(vio == vvp_env_io(env));
57 * For swapping layout. The file's layout may have changed.
58 * To avoid populating pages to a wrong stripe, we have to verify the
59 * correctness of layout. It works because swapping layout processes
60 * have to acquire group lock.
62 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
65 struct ll_inode_info *lli = ll_i2info(inode);
66 struct vvp_io *vio = vvp_env_io(env);
69 switch (io->ci_type) {
72 /* don't need lock here to check lli_layout_gen as we have held
73 * extent lock and GROUP lock has to hold to swap layout
75 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
76 OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
77 io->ci_need_restart = 1;
78 /* this will cause a short read/write */
83 /* fault is okay because we've already had a page. */
91 static void vvp_object_size_lock(struct cl_object *obj)
93 struct inode *inode = vvp_object_inode(obj);
95 ll_inode_size_lock(inode);
96 cl_object_attr_lock(obj);
99 static void vvp_object_size_unlock(struct cl_object *obj)
101 struct inode *inode = vvp_object_inode(obj);
103 cl_object_attr_unlock(obj);
104 ll_inode_size_unlock(inode);
108 * Helper function that if necessary adjusts file size (inode->i_size), when
109 * position at the offset \a pos is accessed. File size can be arbitrary stale
110 * on a Lustre client, but client at least knows KMS. If accessed area is
111 * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
113 * Locking: cl_isize_lock is used to serialize changes to inode size and to
114 * protect consistency between inode size and cl_object
115 * attributes. cl_object_size_lock() protects consistency between cl_attr's of
116 * top-object and sub-objects.
118 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
119 struct cl_io *io, loff_t start, size_t count,
122 struct cl_attr *attr = vvp_env_thread_attr(env);
123 struct inode *inode = vvp_object_inode(obj);
124 loff_t pos = start + count - 1;
129 * Consistency guarantees: following possibilities exist for the
130 * relation between region being accessed and real file size at this
133 * (A): the region is completely inside of the file;
135 * (B-x): x bytes of region are inside of the file, the rest is
138 * (C): the region is completely outside of the file.
140 * This classification is stable under DLM lock already acquired by
141 * the caller, because to change the class, other client has to take
142 * DLM lock conflicting with our lock. Also, any updates to ->i_size
143 * by other threads on this client are serialized by
144 * ll_inode_size_lock(). This guarantees that short reads are handled
145 * correctly in the face of concurrent writes and truncates.
147 vvp_object_size_lock(obj);
148 result = cl_object_attr_get(env, obj, attr);
153 * A glimpse is necessary to determine whether we
154 * return a short read (B) or some zeroes at the end
157 vvp_object_size_unlock(obj);
158 result = cl_glimpse_lock(env, io, inode, obj, 0);
159 if (result == 0 && exceed) {
160 /* If objective page index exceed end-of-file
161 * page index, return directly. Do not expect
162 * kernel will check such case correctly.
163 * linux-2.6.18-128.1.1 miss to do that.
166 loff_t size = i_size_read(inode);
167 loff_t cur_index = start >> PAGE_SHIFT;
168 loff_t size_index = (size - 1) >> PAGE_SHIFT;
170 if ((size == 0 && cur_index != 0) ||
171 size_index < cur_index)
177 * region is within kms and, hence, within real file
178 * size (A). We need to increase i_size to cover the
179 * read region so that generic_file_read() will do its
180 * job, but that doesn't mean the kms size is
181 * _correct_, it is only the _minimum_ size. If
182 * someone does a stat they will get the correct size
183 * which will always be >= the kms value here.
186 if (i_size_read(inode) < kms) {
187 i_size_write(inode, kms);
188 CDEBUG(D_VFSTRACE, DFID " updating i_size %llu\n",
189 PFID(lu_object_fid(&obj->co_lu)),
190 (__u64)i_size_read(inode));
194 vvp_object_size_unlock(obj);
199 /*****************************************************************************
205 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
206 __u32 enqflags, enum cl_lock_mode mode,
207 pgoff_t start, pgoff_t end)
209 struct vvp_io *vio = vvp_env_io(env);
210 struct cl_lock_descr *descr = &vio->vui_link.cill_descr;
211 struct cl_object *obj = io->ci_obj;
213 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
215 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
217 memset(&vio->vui_link, 0, sizeof(vio->vui_link));
219 if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
220 descr->cld_mode = CLM_GROUP;
221 descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid;
223 descr->cld_mode = mode;
225 descr->cld_obj = obj;
226 descr->cld_start = start;
227 descr->cld_end = end;
228 descr->cld_enq_flags = enqflags;
230 cl_io_lock_add(env, io, &vio->vui_link);
234 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
235 __u32 enqflags, enum cl_lock_mode mode,
236 loff_t start, loff_t end)
238 struct cl_object *obj = io->ci_obj;
240 return vvp_io_one_lock_index(env, io, enqflags, mode,
241 cl_index(obj, start), cl_index(obj, end));
244 static int vvp_io_write_iter_init(const struct lu_env *env,
245 const struct cl_io_slice *ios)
247 struct vvp_io *vio = cl2vvp_io(env, ios);
249 cl_page_list_init(&vio->u.write.vui_queue);
250 vio->u.write.vui_written = 0;
251 vio->u.write.vui_from = 0;
252 vio->u.write.vui_to = PAGE_SIZE;
257 static void vvp_io_write_iter_fini(const struct lu_env *env,
258 const struct cl_io_slice *ios)
260 struct vvp_io *vio = cl2vvp_io(env, ios);
262 LASSERT(vio->u.write.vui_queue.pl_nr == 0);
265 static int vvp_io_fault_iter_init(const struct lu_env *env,
266 const struct cl_io_slice *ios)
268 struct vvp_io *vio = cl2vvp_io(env, ios);
269 struct inode *inode = vvp_object_inode(ios->cis_obj);
271 LASSERT(inode == file_inode(vio->vui_fd->fd_file));
272 vio->u.fault.ft_mtime = inode->i_mtime.tv_sec;
276 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
278 struct cl_io *io = ios->cis_io;
279 struct cl_object *obj = io->ci_obj;
280 struct vvp_io *vio = cl2vvp_io(env, ios);
281 struct inode *inode = vvp_object_inode(obj);
283 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
285 CDEBUG(D_VFSTRACE, DFID
286 " ignore/verify layout %d/%d, layout version %d restore needed %d\n",
287 PFID(lu_object_fid(&obj->co_lu)),
288 io->ci_ignore_layout, io->ci_verify_layout,
289 vio->vui_layout_gen, io->ci_restore_needed);
291 if (io->ci_restore_needed) {
294 /* file was detected release, we need to restore it
295 * before finishing the io
297 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
298 /* if restore registration failed, no restart,
299 * we will return -ENODATA
301 /* The layout will change after restore, so we need to
302 * block on layout lock hold by the MDT
303 * as MDT will not send new layout in lvb (see LU-3124)
304 * we have to explicitly fetch it, all this will be done
305 * by ll_layout_refresh()
308 io->ci_restore_needed = 0;
309 io->ci_need_restart = 1;
310 io->ci_verify_layout = 1;
312 io->ci_restore_needed = 1;
313 io->ci_need_restart = 0;
314 io->ci_verify_layout = 0;
319 if (!io->ci_ignore_layout && io->ci_verify_layout) {
322 /* check layout version */
323 ll_layout_refresh(inode, &gen);
324 io->ci_need_restart = vio->vui_layout_gen != gen;
325 if (io->ci_need_restart) {
327 DFID" layout changed from %d to %d.\n",
328 PFID(lu_object_fid(&obj->co_lu)),
329 vio->vui_layout_gen, gen);
330 /* today successful restore is the only possible case */
331 /* restore was done, clear restoring state */
332 clear_bit(LLIF_FILE_RESTORING,
333 &ll_i2info(inode)->lli_flags);
338 static void vvp_io_fault_fini(const struct lu_env *env,
339 const struct cl_io_slice *ios)
341 struct cl_io *io = ios->cis_io;
342 struct cl_page *page = io->u.ci_fault.ft_page;
344 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
347 lu_ref_del(&page->cp_reference, "fault", io);
348 cl_page_put(env, page);
349 io->u.ci_fault.ft_page = NULL;
351 vvp_io_fini(env, ios);
354 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
357 * we only want to hold PW locks if the mmap() can generate
358 * writes back to the file and that only happens in shared
361 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
366 static int vvp_mmap_locks(const struct lu_env *env,
367 struct vvp_io *vio, struct cl_io *io)
369 struct vvp_thread_info *cti = vvp_env_info(env);
370 struct mm_struct *mm = current->mm;
371 struct vm_area_struct *vma;
372 struct cl_lock_descr *descr = &cti->vti_descr;
373 union ldlm_policy_data policy;
380 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
382 if (!vio->vui_iter) /* nfs or loop back device write */
385 /* No MM (e.g. NFS)? No vmas too. */
389 iov_for_each(iov, i, *vio->vui_iter) {
390 addr = (unsigned long)iov.iov_base;
395 count += addr & (~PAGE_MASK);
398 down_read(&mm->mmap_sem);
399 while ((vma = our_vma(mm, addr, count)) != NULL) {
400 struct inode *inode = file_inode(vma->vm_file);
401 int flags = CEF_MUST;
403 if (ll_file_nolock(vma->vm_file)) {
405 * For no lock case is not allowed for mmap
412 * XXX: Required lock mode can be weakened: CIT_WRITE
413 * io only ever reads user level buffer, and CIT_READ
416 policy_from_vma(&policy, vma, addr, count);
417 descr->cld_mode = vvp_mode_from_vma(vma);
418 descr->cld_obj = ll_i2info(inode)->lli_clob;
419 descr->cld_start = cl_index(descr->cld_obj,
420 policy.l_extent.start);
421 descr->cld_end = cl_index(descr->cld_obj,
422 policy.l_extent.end);
423 descr->cld_enq_flags = flags;
424 result = cl_io_lock_alloc_add(env, io, descr);
426 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
427 descr->cld_mode, descr->cld_start,
433 if (vma->vm_end - addr >= count)
436 count -= vma->vm_end - addr;
439 up_read(&mm->mmap_sem);
446 static void vvp_io_advance(const struct lu_env *env,
447 const struct cl_io_slice *ios,
450 struct cl_object *obj = ios->cis_io->ci_obj;
451 struct vvp_io *vio = cl2vvp_io(env, ios);
452 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
454 vio->vui_tot_count -= nob;
455 iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
458 static void vvp_io_update_iov(const struct lu_env *env,
459 struct vvp_io *vio, struct cl_io *io)
461 size_t size = io->u.ci_rw.crw_count;
466 iov_iter_truncate(vio->vui_iter, size);
469 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
470 enum cl_lock_mode mode, loff_t start, loff_t end)
472 struct vvp_io *vio = vvp_env_io(env);
476 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
478 vvp_io_update_iov(env, vio, io);
480 if (io->u.ci_rw.crw_nonblock)
481 ast_flags |= CEF_NONBLOCK;
482 result = vvp_mmap_locks(env, vio, io);
484 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
488 static int vvp_io_read_lock(const struct lu_env *env,
489 const struct cl_io_slice *ios)
491 struct cl_io *io = ios->cis_io;
492 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
495 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
496 rd->crw_pos + rd->crw_count - 1);
501 static int vvp_io_fault_lock(const struct lu_env *env,
502 const struct cl_io_slice *ios)
504 struct cl_io *io = ios->cis_io;
505 struct vvp_io *vio = cl2vvp_io(env, ios);
507 * XXX LDLM_FL_CBPENDING
509 return vvp_io_one_lock_index(env,
511 vvp_mode_from_vma(vio->u.fault.ft_vma),
512 io->u.ci_fault.ft_index,
513 io->u.ci_fault.ft_index);
516 static int vvp_io_write_lock(const struct lu_env *env,
517 const struct cl_io_slice *ios)
519 struct cl_io *io = ios->cis_io;
523 if (io->u.ci_wr.wr_append) {
525 end = OBD_OBJECT_EOF;
527 start = io->u.ci_wr.wr.crw_pos;
528 end = start + io->u.ci_wr.wr.crw_count - 1;
530 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
533 static int vvp_io_setattr_iter_init(const struct lu_env *env,
534 const struct cl_io_slice *ios)
540 * Implementation of cl_io_operations::vio_lock() method for CIT_SETATTR io.
542 * Handles "lockless io" mode when extent locking is done by server.
544 static int vvp_io_setattr_lock(const struct lu_env *env,
545 const struct cl_io_slice *ios)
547 struct cl_io *io = ios->cis_io;
551 if (cl_io_is_trunc(io)) {
552 new_size = io->u.ci_setattr.sa_attr.lvb_size;
554 enqflags = CEF_DISCARD_DATA;
556 unsigned int valid = io->u.ci_setattr.sa_valid;
558 if (!(valid & TIMES_SET_FLAGS))
561 if ((!(valid & ATTR_MTIME) ||
562 io->u.ci_setattr.sa_attr.lvb_mtime >=
563 io->u.ci_setattr.sa_attr.lvb_ctime) &&
564 (!(valid & ATTR_ATIME) ||
565 io->u.ci_setattr.sa_attr.lvb_atime >=
566 io->u.ci_setattr.sa_attr.lvb_ctime))
571 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
572 new_size, OBD_OBJECT_EOF);
575 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
579 * Only ll_inode_size_lock is taken at this level.
581 ll_inode_size_lock(inode);
582 result = inode_newsize_ok(inode, size);
584 ll_inode_size_unlock(inode);
587 truncate_setsize(inode, size);
588 ll_inode_size_unlock(inode);
592 static int vvp_io_setattr_time(const struct lu_env *env,
593 const struct cl_io_slice *ios)
595 struct cl_io *io = ios->cis_io;
596 struct cl_object *obj = io->ci_obj;
597 struct cl_attr *attr = vvp_env_thread_attr(env);
599 unsigned valid = CAT_CTIME;
601 cl_object_attr_lock(obj);
602 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
603 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
604 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
607 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
608 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
611 result = cl_object_attr_update(env, obj, attr, valid);
612 cl_object_attr_unlock(obj);
617 static int vvp_io_setattr_start(const struct lu_env *env,
618 const struct cl_io_slice *ios)
620 struct cl_io *io = ios->cis_io;
621 struct inode *inode = vvp_object_inode(io->ci_obj);
622 struct ll_inode_info *lli = ll_i2info(inode);
624 if (cl_io_is_trunc(io)) {
625 down_write(&lli->lli_trunc_sem);
627 inode_dio_wait(inode);
632 if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
633 return vvp_io_setattr_time(env, ios);
638 static void vvp_io_setattr_end(const struct lu_env *env,
639 const struct cl_io_slice *ios)
641 struct cl_io *io = ios->cis_io;
642 struct inode *inode = vvp_object_inode(io->ci_obj);
643 struct ll_inode_info *lli = ll_i2info(inode);
645 if (cl_io_is_trunc(io)) {
646 /* Truncate in memory pages - they must be clean pages
647 * because osc has already notified to destroy osc_extents.
649 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
651 up_write(&lli->lli_trunc_sem);
657 static void vvp_io_setattr_fini(const struct lu_env *env,
658 const struct cl_io_slice *ios)
660 bool restore_needed = ios->cis_io->ci_restore_needed;
661 struct inode *inode = vvp_object_inode(ios->cis_obj);
663 vvp_io_fini(env, ios);
665 if (restore_needed && !ios->cis_io->ci_restore_needed) {
666 /* restore finished, set data modified flag for HSM */
667 set_bit(LLIF_DATA_MODIFIED, &(ll_i2info(inode))->lli_flags);
671 static int vvp_io_read_start(const struct lu_env *env,
672 const struct cl_io_slice *ios)
674 struct vvp_io *vio = cl2vvp_io(env, ios);
675 struct cl_io *io = ios->cis_io;
676 struct cl_object *obj = io->ci_obj;
677 struct inode *inode = vvp_object_inode(obj);
678 struct ll_inode_info *lli = ll_i2info(inode);
679 struct file *file = vio->vui_fd->fd_file;
682 loff_t pos = io->u.ci_rd.rd.crw_pos;
683 long cnt = io->u.ci_rd.rd.crw_count;
684 long tot = vio->vui_tot_count;
687 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
689 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
691 down_read(&lli->lli_trunc_sem);
693 if (!can_populate_pages(env, io, inode))
696 result = vvp_prep_size(env, obj, io, pos, tot, &exceed);
699 else if (exceed != 0)
702 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
703 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
704 inode->i_ino, cnt, pos, i_size_read(inode));
706 /* turn off the kernel's read-ahead */
707 vio->vui_fd->fd_file->f_ra.ra_pages = 0;
709 /* initialize read-ahead window once per syscall */
710 if (!vio->vui_ra_valid) {
711 vio->vui_ra_valid = true;
712 vio->vui_ra_start = cl_index(obj, pos);
713 vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1);
719 LASSERT(vio->vui_iocb->ki_pos == pos);
720 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
726 io->ci_nob += result;
727 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
728 vio->vui_fd, pos, result, READ);
734 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
735 struct cl_page_list *plist, int from, int to)
737 struct cl_2queue *queue = &io->ci_queue;
738 struct cl_page *page;
739 unsigned int bytes = 0;
742 if (plist->pl_nr == 0)
745 if (from > 0 || to != PAGE_SIZE) {
746 page = cl_page_list_first(plist);
747 if (plist->pl_nr == 1) {
748 cl_page_clip(env, page, from, to);
751 cl_page_clip(env, page, from, PAGE_SIZE);
752 if (to != PAGE_SIZE) {
753 page = cl_page_list_last(plist);
754 cl_page_clip(env, page, 0, to);
759 cl_2queue_init(queue);
760 cl_page_list_splice(plist, &queue->c2_qin);
761 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
763 /* plist is not sorted any more */
764 cl_page_list_splice(&queue->c2_qin, plist);
765 cl_page_list_splice(&queue->c2_qout, plist);
766 cl_2queue_fini(env, queue);
769 /* calculate bytes */
770 bytes = plist->pl_nr << PAGE_SHIFT;
771 bytes -= from + PAGE_SIZE - to;
773 while (plist->pl_nr > 0) {
774 page = cl_page_list_first(plist);
775 cl_page_list_del(env, plist, page);
777 cl_page_clip(env, page, 0, PAGE_SIZE);
779 SetPageUptodate(cl_page_vmpage(page));
780 cl_page_disown(env, io, page);
782 /* held in ll_cl_init() */
783 lu_ref_del(&page->cp_reference, "cl_io", io);
784 cl_page_put(env, page);
788 return bytes > 0 ? bytes : rc;
791 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
792 struct cl_page *page)
794 struct page *vmpage = page->cp_vmpage;
796 SetPageUptodate(vmpage);
797 set_page_dirty(vmpage);
799 cl_page_disown(env, io, page);
801 /* held in ll_cl_init() */
802 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
803 cl_page_put(env, page);
806 /* make sure the page list is contiguous */
807 static bool page_list_sanity_check(struct cl_object *obj,
808 struct cl_page_list *plist)
810 struct cl_page *page;
811 pgoff_t index = CL_PAGE_EOF;
813 cl_page_list_for_each(page, plist) {
814 struct vvp_page *vpg = cl_object_page_slice(obj, page);
816 if (index == CL_PAGE_EOF) {
817 index = vvp_index(vpg);
822 if (index == vvp_index(vpg))
830 /* Return how many bytes have queued or written */
831 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
833 struct cl_object *obj = io->ci_obj;
834 struct inode *inode = vvp_object_inode(obj);
835 struct vvp_io *vio = vvp_env_io(env);
836 struct cl_page_list *queue = &vio->u.write.vui_queue;
837 struct cl_page *page;
840 unsigned int npages = vio->u.write.vui_queue.pl_nr;
845 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
846 npages, vio->u.write.vui_from, vio->u.write.vui_to);
848 LASSERT(page_list_sanity_check(obj, queue));
850 /* submit IO with async write */
851 rc = cl_io_commit_async(env, io, queue,
852 vio->u.write.vui_from, vio->u.write.vui_to,
853 write_commit_callback);
854 npages -= queue->pl_nr; /* already committed pages */
856 /* calculate how many bytes were written */
857 bytes = npages << PAGE_SHIFT;
860 bytes -= vio->u.write.vui_from;
861 if (queue->pl_nr == 0) /* last page */
862 bytes -= PAGE_SIZE - vio->u.write.vui_to;
863 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
865 vio->u.write.vui_written += bytes;
867 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
868 npages, bytes, vio->u.write.vui_written);
870 /* the first page must have been written. */
871 vio->u.write.vui_from = 0;
873 LASSERT(page_list_sanity_check(obj, queue));
874 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
876 /* out of quota, try sync write */
877 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
878 rc = vvp_io_commit_sync(env, io, queue,
879 vio->u.write.vui_from,
880 vio->u.write.vui_to);
882 vio->u.write.vui_written += rc;
887 /* update inode size */
888 ll_merge_attr(env, inode);
890 /* Now the pages in queue were failed to commit, discard them
891 * unless they were dirtied before.
893 while (queue->pl_nr > 0) {
894 page = cl_page_list_first(queue);
895 cl_page_list_del(env, queue, page);
897 if (!PageDirty(cl_page_vmpage(page)))
898 cl_page_discard(env, io, page);
900 cl_page_disown(env, io, page);
902 /* held in ll_cl_init() */
903 lu_ref_del(&page->cp_reference, "cl_io", io);
904 cl_page_put(env, page);
906 cl_page_list_fini(env, queue);
911 static int vvp_io_write_start(const struct lu_env *env,
912 const struct cl_io_slice *ios)
914 struct vvp_io *vio = cl2vvp_io(env, ios);
915 struct cl_io *io = ios->cis_io;
916 struct cl_object *obj = io->ci_obj;
917 struct inode *inode = vvp_object_inode(obj);
918 struct ll_inode_info *lli = ll_i2info(inode);
920 loff_t pos = io->u.ci_wr.wr.crw_pos;
921 size_t cnt = io->u.ci_wr.wr.crw_count;
923 down_read(&lli->lli_trunc_sem);
925 if (!can_populate_pages(env, io, inode))
928 if (cl_io_is_append(io)) {
930 * PARALLEL IO This has to be changed for parallel IO doing
931 * out-of-order writes.
933 ll_merge_attr(env, inode);
934 pos = i_size_read(inode);
935 io->u.ci_wr.wr.crw_pos = pos;
936 vio->vui_iocb->ki_pos = pos;
938 LASSERT(vio->vui_iocb->ki_pos == pos);
941 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
944 * The maximum Lustre file size is variable, based on the OST maximum
945 * object size and number of stripes. This needs another check in
946 * addition to the VFS checks earlier.
948 if (pos + cnt > ll_file_maxbytes(inode)) {
950 "%s: file " DFID " offset %llu > maxbytes %llu\n",
951 ll_get_fsname(inode->i_sb, NULL, 0),
952 PFID(ll_inode2fid(inode)), pos + cnt,
953 ll_file_maxbytes(inode));
957 if (!vio->vui_iter) {
958 /* from a temp io in ll_cl_init(). */
962 * When using the locked AIO function (generic_file_aio_write())
963 * testing has shown the inode mutex to be a limiting factor
964 * with multi-threaded single shared file performance. To get
965 * around this, we now use the lockless version. To maintain
966 * consistency, proper locking to protect against writes,
967 * trucates, etc. is handled in the higher layers of lustre.
969 bool lock_node = !IS_NOSEC(inode);
973 result = __generic_file_write_iter(vio->vui_iocb,
978 if (result > 0 || result == -EIOCBQUEUED)
979 result = generic_write_sync(vio->vui_iocb, result);
983 result = vvp_io_write_commit(env, io);
984 if (vio->u.write.vui_written > 0) {
985 result = vio->u.write.vui_written;
986 io->ci_nob += result;
988 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
993 set_bit(LLIF_DATA_MODIFIED, &(ll_i2info(inode))->lli_flags);
997 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
998 vio->vui_fd, pos, result, WRITE);
1004 static void vvp_io_rw_end(const struct lu_env *env,
1005 const struct cl_io_slice *ios)
1007 struct inode *inode = vvp_object_inode(ios->cis_obj);
1008 struct ll_inode_info *lli = ll_i2info(inode);
1010 up_read(&lli->lli_trunc_sem);
1013 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1015 struct vm_fault *vmf = cfio->ft_vmf;
1017 cfio->ft_flags = filemap_fault(vmf);
1018 cfio->ft_flags_valid = 1;
1022 "page %p map %p index %lu flags %lx count %u priv %0lx: got addr %p type NOPAGE\n",
1023 vmf->page, vmf->page->mapping, vmf->page->index,
1024 (long)vmf->page->flags, page_count(vmf->page),
1025 page_private(vmf->page), (void *)vmf->address);
1026 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1027 lock_page(vmf->page);
1028 cfio->ft_flags |= VM_FAULT_LOCKED;
1031 cfio->ft_vmpage = vmf->page;
1035 if (cfio->ft_flags & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) {
1036 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", (void *)vmf->address);
1040 if (cfio->ft_flags & VM_FAULT_OOM) {
1041 CDEBUG(D_PAGE, "got addr %p - OOM\n", (void *)vmf->address);
1045 if (cfio->ft_flags & VM_FAULT_RETRY)
1048 CERROR("Unknown error in page fault %d!\n", cfio->ft_flags);
1052 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1053 struct cl_page *page)
1055 set_page_dirty(page->cp_vmpage);
1058 static int vvp_io_fault_start(const struct lu_env *env,
1059 const struct cl_io_slice *ios)
1061 struct vvp_io *vio = cl2vvp_io(env, ios);
1062 struct cl_io *io = ios->cis_io;
1063 struct cl_object *obj = io->ci_obj;
1064 struct inode *inode = vvp_object_inode(obj);
1065 struct ll_inode_info *lli = ll_i2info(inode);
1066 struct cl_fault_io *fio = &io->u.ci_fault;
1067 struct vvp_fault_io *cfio = &vio->u.fault;
1070 struct page *vmpage = NULL;
1071 struct cl_page *page;
1075 down_read(&lli->lli_trunc_sem);
1077 /* offset of the last byte on the page */
1078 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1079 LASSERT(cl_index(obj, offset) == fio->ft_index);
1080 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1084 /* must return locked page */
1085 if (fio->ft_mkwrite) {
1086 LASSERT(cfio->ft_vmpage);
1087 lock_page(cfio->ft_vmpage);
1089 result = vvp_io_kernel_fault(cfio);
1094 vmpage = cfio->ft_vmpage;
1095 LASSERT(PageLocked(vmpage));
1097 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1098 ll_invalidate_page(vmpage);
1100 size = i_size_read(inode);
1101 /* Though we have already held a cl_lock upon this page, but
1102 * it still can be truncated locally.
1104 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1105 (page_offset(vmpage) > size))) {
1106 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1108 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1115 last_index = cl_index(obj, size - 1);
1117 if (fio->ft_mkwrite) {
1119 * Capture the size while holding the lli_trunc_sem from above
1120 * we want to make sure that we complete the mkwrite action
1121 * while holding this lock. We need to make sure that we are
1122 * not past the end of the file.
1124 if (last_index < fio->ft_index) {
1126 "llite: mkwrite and truncate race happened: %p: 0x%lx 0x%lx\n",
1127 vmpage->mapping, fio->ft_index, last_index);
1129 * We need to return if we are
1130 * passed the end of the file. This will propagate
1131 * up the call stack to ll_page_mkwrite where
1132 * we will return VM_FAULT_NOPAGE. Any non-negative
1133 * value returned here will be silently
1134 * converted to 0. If the vmpage->mapping is null
1135 * the error code would be converted back to ENODATA
1136 * in ll_page_mkwrite0. Thus we return -ENODATA
1137 * to handle both cases
1144 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1146 result = PTR_ERR(page);
1150 /* if page is going to be written, we should add this page into cache
1153 if (fio->ft_mkwrite) {
1154 wait_on_page_writeback(vmpage);
1155 if (!PageDirty(vmpage)) {
1156 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1157 struct vvp_page *vpg = cl_object_page_slice(obj, page);
1160 /* vvp_page_assume() calls wait_on_page_writeback(). */
1161 cl_page_assume(env, io, page);
1163 cl_page_list_init(plist);
1164 cl_page_list_add(plist, page);
1167 if (last_index == vvp_index(vpg))
1168 to = size & ~PAGE_MASK;
1170 /* Do not set Dirty bit here so that in case IO is
1171 * started before the page is really made dirty, we
1172 * still have chance to detect it.
1174 result = cl_io_commit_async(env, io, plist, 0, to,
1175 mkwrite_commit_callback);
1176 LASSERT(cl_page_is_owned(page, io));
1177 cl_page_list_fini(env, plist);
1181 cl_page_discard(env, io, page);
1182 cl_page_disown(env, io, page);
1184 cl_page_put(env, page);
1186 /* we're in big trouble, what can we do now? */
1187 if (result == -EDQUOT)
1191 cl_page_disown(env, io, page);
1197 * The ft_index is only used in the case of
1198 * a mkwrite action. We need to check
1199 * our assertions are correct, since
1200 * we should have caught this above
1202 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1203 if (fio->ft_index == last_index)
1205 * Last page is mapped partially.
1207 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1209 fio->ft_nob = cl_page_size(obj);
1211 lu_ref_add(&page->cp_reference, "fault", io);
1212 fio->ft_page = page;
1215 /* return unlocked vmpage to avoid deadlocking */
1217 unlock_page(vmpage);
1219 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1224 static void vvp_io_fault_end(const struct lu_env *env,
1225 const struct cl_io_slice *ios)
1227 struct inode *inode = vvp_object_inode(ios->cis_obj);
1228 struct ll_inode_info *lli = ll_i2info(inode);
1230 CLOBINVRNT(env, ios->cis_io->ci_obj,
1231 vvp_object_invariant(ios->cis_io->ci_obj));
1232 up_read(&lli->lli_trunc_sem);
1235 static int vvp_io_fsync_start(const struct lu_env *env,
1236 const struct cl_io_slice *ios)
1238 /* we should mark TOWRITE bit to each dirty page in radix tree to
1239 * verify pages have been written, but this is difficult because of
1245 static int vvp_io_read_ahead(const struct lu_env *env,
1246 const struct cl_io_slice *ios,
1247 pgoff_t start, struct cl_read_ahead *ra)
1251 if (ios->cis_io->ci_type == CIT_READ ||
1252 ios->cis_io->ci_type == CIT_FAULT) {
1253 struct vvp_io *vio = cl2vvp_io(env, ios);
1255 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1256 ra->cra_end = CL_PAGE_EOF;
1257 result = 1; /* no need to call down */
1264 static const struct cl_io_operations vvp_io_ops = {
1267 .cio_fini = vvp_io_fini,
1268 .cio_lock = vvp_io_read_lock,
1269 .cio_start = vvp_io_read_start,
1270 .cio_end = vvp_io_rw_end,
1271 .cio_advance = vvp_io_advance,
1274 .cio_fini = vvp_io_fini,
1275 .cio_iter_init = vvp_io_write_iter_init,
1276 .cio_iter_fini = vvp_io_write_iter_fini,
1277 .cio_lock = vvp_io_write_lock,
1278 .cio_start = vvp_io_write_start,
1279 .cio_end = vvp_io_rw_end,
1280 .cio_advance = vvp_io_advance,
1283 .cio_fini = vvp_io_setattr_fini,
1284 .cio_iter_init = vvp_io_setattr_iter_init,
1285 .cio_lock = vvp_io_setattr_lock,
1286 .cio_start = vvp_io_setattr_start,
1287 .cio_end = vvp_io_setattr_end
1290 .cio_fini = vvp_io_fault_fini,
1291 .cio_iter_init = vvp_io_fault_iter_init,
1292 .cio_lock = vvp_io_fault_lock,
1293 .cio_start = vvp_io_fault_start,
1294 .cio_end = vvp_io_fault_end,
1297 .cio_start = vvp_io_fsync_start,
1298 .cio_fini = vvp_io_fini
1301 .cio_fini = vvp_io_fini
1304 .cio_read_ahead = vvp_io_read_ahead,
1307 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1310 struct vvp_io *vio = vvp_env_io(env);
1311 struct inode *inode = vvp_object_inode(obj);
1314 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1316 CDEBUG(D_VFSTRACE, DFID
1317 " ignore/verify layout %d/%d, layout version %d restore needed %d\n",
1318 PFID(lu_object_fid(&obj->co_lu)),
1319 io->ci_ignore_layout, io->ci_verify_layout,
1320 vio->vui_layout_gen, io->ci_restore_needed);
1322 CL_IO_SLICE_CLEAN(vio, vui_cl);
1323 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1324 vio->vui_ra_valid = false;
1326 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1328 struct ll_inode_info *lli = ll_i2info(inode);
1330 count = io->u.ci_rw.crw_count;
1331 /* "If nbyte is 0, read() will return 0 and have no other
1332 * results." -- Single Unix Spec
1337 vio->vui_tot_count = count;
1339 /* for read/write, we store the jobid in the inode, and
1340 * it'll be fetched by osc when building RPC.
1342 * it's not accurate if the file is shared by different
1345 lustre_get_jobid(lli->lli_jobid);
1346 } else if (io->ci_type == CIT_SETATTR) {
1347 if (!cl_io_is_trunc(io))
1348 io->ci_lockreq = CILR_MANDATORY;
1351 /* Enqueue layout lock and get layout version. We need to do this
1352 * even for operations requiring to open file, such as read and write,
1353 * because it might not grant layout lock in IT_OPEN.
1355 if (result == 0 && !io->ci_ignore_layout) {
1356 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1357 if (result == -ENOENT)
1358 /* If the inode on MDS has been removed, but the objects
1359 * on OSTs haven't been destroyed (async unlink), layout
1360 * fetch will return -ENOENT, we'd ignore this error
1361 * and continue with dirty flush. LU-3230.
1365 CERROR("%s: refresh file layout " DFID " error %d.\n",
1366 ll_get_fsname(inode->i_sb, NULL, 0),
1367 PFID(lu_object_fid(&obj->co_lu)), result);