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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_io for VVP layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
46 #include "llite_internal.h"
47 #include "vvp_internal.h"
49 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
50 const struct cl_io_slice *slice)
54 vio = container_of(slice, struct vvp_io, vui_cl);
55 LASSERT(vio == vvp_env_io(env));
61 * True, if \a io is a normal io, False for splice_{read,write}
63 static int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
65 struct vvp_io *vio = vvp_env_io(env);
67 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
69 return vio->vui_io_subtype == IO_NORMAL;
73 * For swapping layout. The file's layout may have changed.
74 * To avoid populating pages to a wrong stripe, we have to verify the
75 * correctness of layout. It works because swapping layout processes
76 * have to acquire group lock.
78 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
81 struct ll_inode_info *lli = ll_i2info(inode);
82 struct vvp_io *vio = vvp_env_io(env);
85 switch (io->ci_type) {
88 /* don't need lock here to check lli_layout_gen as we have held
89 * extent lock and GROUP lock has to hold to swap layout */
90 if (ll_layout_version_get(lli) != vio->vui_layout_gen ||
91 OBD_FAIL_CHECK_RESET(OBD_FAIL_LLITE_LOST_LAYOUT, 0)) {
92 io->ci_need_restart = 1;
93 /* this will cause a short read/write */
98 /* fault is okay because we've already had a page. */
106 static void vvp_object_size_lock(struct cl_object *obj)
108 struct inode *inode = vvp_object_inode(obj);
110 ll_inode_size_lock(inode);
111 cl_object_attr_lock(obj);
114 static void vvp_object_size_unlock(struct cl_object *obj)
116 struct inode *inode = vvp_object_inode(obj);
118 cl_object_attr_unlock(obj);
119 ll_inode_size_unlock(inode);
123 * Helper function that if necessary adjusts file size (inode->i_size), when
124 * position at the offset \a pos is accessed. File size can be arbitrary stale
125 * on a Lustre client, but client at least knows KMS. If accessed area is
126 * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
128 * Locking: i_size_lock is used to serialize changes to inode size and to
129 * protect consistency between inode size and cl_object
130 * attributes. cl_object_size_lock() protects consistency between cl_attr's of
131 * top-object and sub-objects.
133 static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj,
134 struct cl_io *io, loff_t start, size_t count,
137 struct cl_attr *attr = vvp_env_thread_attr(env);
138 struct inode *inode = vvp_object_inode(obj);
139 loff_t pos = start + count - 1;
144 * Consistency guarantees: following possibilities exist for the
145 * relation between region being accessed and real file size at this
148 * (A): the region is completely inside of the file;
150 * (B-x): x bytes of region are inside of the file, the rest is
153 * (C): the region is completely outside of the file.
155 * This classification is stable under DLM lock already acquired by
156 * the caller, because to change the class, other client has to take
157 * DLM lock conflicting with our lock. Also, any updates to ->i_size
158 * by other threads on this client are serialized by
159 * ll_inode_size_lock(). This guarantees that short reads are handled
160 * correctly in the face of concurrent writes and truncates.
162 vvp_object_size_lock(obj);
163 result = cl_object_attr_get(env, obj, attr);
168 * A glimpse is necessary to determine whether we
169 * return a short read (B) or some zeroes at the end
172 vvp_object_size_unlock(obj);
173 result = cl_glimpse_lock(env, io, inode, obj, 0);
174 if (result == 0 && exceed != NULL) {
175 /* If objective page index exceed end-of-file
176 * page index, return directly. Do not expect
177 * kernel will check such case correctly.
178 * linux-2.6.18-128.1.1 miss to do that.
180 loff_t size = i_size_read(inode);
181 unsigned long cur_index = start >>
184 if ((size == 0 && cur_index != 0) ||
185 (((size - 1) >> PAGE_CACHE_SHIFT) <
193 * region is within kms and, hence, within real file
194 * size (A). We need to increase i_size to cover the
195 * read region so that generic_file_read() will do its
196 * job, but that doesn't mean the kms size is
197 * _correct_, it is only the _minimum_ size. If
198 * someone does a stat they will get the correct size
199 * which will always be >= the kms value here.
202 if (i_size_read(inode) < kms) {
203 i_size_write(inode, kms);
205 DFID" updating i_size "LPU64"\n",
206 PFID(lu_object_fid(&obj->co_lu)),
207 (__u64)i_size_read(inode));
212 vvp_object_size_unlock(obj);
217 /*****************************************************************************
223 static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
224 __u32 enqflags, enum cl_lock_mode mode,
225 pgoff_t start, pgoff_t end)
227 struct vvp_io *vio = vvp_env_io(env);
228 struct cl_lock_descr *descr = &vio->vui_link.cill_descr;
229 struct cl_object *obj = io->ci_obj;
231 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
234 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
236 memset(&vio->vui_link, 0, sizeof vio->vui_link);
238 if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
239 descr->cld_mode = CLM_GROUP;
240 descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid;
241 enqflags |= CEF_LOCK_MATCH;
243 descr->cld_mode = mode;
246 descr->cld_obj = obj;
247 descr->cld_start = start;
248 descr->cld_end = end;
249 descr->cld_enq_flags = enqflags;
251 cl_io_lock_add(env, io, &vio->vui_link);
256 static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io,
257 __u32 enqflags, enum cl_lock_mode mode,
258 loff_t start, loff_t end)
260 struct cl_object *obj = io->ci_obj;
262 return vvp_io_one_lock_index(env, io, enqflags, mode,
263 cl_index(obj, start), cl_index(obj, end));
266 static int vvp_io_write_iter_init(const struct lu_env *env,
267 const struct cl_io_slice *ios)
269 struct vvp_io *vio = cl2vvp_io(env, ios);
271 cl_page_list_init(&vio->u.write.vui_queue);
272 vio->u.write.vui_written = 0;
273 vio->u.write.vui_from = 0;
274 vio->u.write.vui_to = PAGE_SIZE;
279 static void vvp_io_write_iter_fini(const struct lu_env *env,
280 const struct cl_io_slice *ios)
282 struct vvp_io *vio = cl2vvp_io(env, ios);
284 LASSERT(vio->u.write.vui_queue.pl_nr == 0);
287 static int vvp_io_fault_iter_init(const struct lu_env *env,
288 const struct cl_io_slice *ios)
290 struct vvp_io *vio = cl2vvp_io(env, ios);
291 struct inode *inode = vvp_object_inode(ios->cis_obj);
293 LASSERT(inode == vio->vui_fd->fd_file->f_path.dentry->d_inode);
294 vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
299 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
301 struct cl_io *io = ios->cis_io;
302 struct cl_object *obj = io->ci_obj;
303 struct vvp_io *vio = cl2vvp_io(env, ios);
304 struct inode *inode = vvp_object_inode(obj);
306 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
308 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
309 "restore needed %d\n",
310 PFID(lu_object_fid(&obj->co_lu)),
311 io->ci_ignore_layout, io->ci_verify_layout,
312 vio->vui_layout_gen, io->ci_restore_needed);
314 if (io->ci_restore_needed) {
317 /* file was detected release, we need to restore it
318 * before finishing the io
320 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
321 /* if restore registration failed, no restart,
322 * we will return -ENODATA */
323 /* The layout will change after restore, so we need to
324 * block on layout lock held by the MDT
325 * as MDT will not send new layout in lvb (see LU-3124)
326 * we have to explicitly fetch it, all this will be done
327 * by ll_layout_refresh()
330 io->ci_restore_needed = 0;
331 io->ci_need_restart = 1;
332 io->ci_verify_layout = 1;
334 io->ci_restore_needed = 1;
335 io->ci_need_restart = 0;
336 io->ci_verify_layout = 0;
341 if (!io->ci_ignore_layout && io->ci_verify_layout) {
344 /* check layout version */
345 ll_layout_refresh(inode, &gen);
346 io->ci_need_restart = vio->vui_layout_gen != gen;
347 if (io->ci_need_restart) {
349 DFID" layout changed from %d to %d.\n",
350 PFID(lu_object_fid(&obj->co_lu)),
351 vio->vui_layout_gen, gen);
352 /* today successful restore is the only possible
354 /* restore was done, clear restoring state */
355 ll_file_clear_flag(ll_i2info(vvp_object_inode(obj)),
356 LLIF_FILE_RESTORING);
361 static void vvp_io_fault_fini(const struct lu_env *env,
362 const struct cl_io_slice *ios)
364 struct cl_io *io = ios->cis_io;
365 struct cl_page *page = io->u.ci_fault.ft_page;
367 CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj));
370 lu_ref_del(&page->cp_reference, "fault", io);
371 cl_page_put(env, page);
372 io->u.ci_fault.ft_page = NULL;
374 vvp_io_fini(env, ios);
377 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
380 * we only want to hold PW locks if the mmap() can generate
381 * writes back to the file and that only happens in shared
384 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
389 static int vvp_mmap_locks(const struct lu_env *env,
390 struct vvp_io *vio, struct cl_io *io)
392 struct vvp_thread_info *vti = vvp_env_info(env);
393 struct mm_struct *mm = current->mm;
394 struct vm_area_struct *vma;
395 struct cl_lock_descr *descr = &vti->vti_descr;
396 union ldlm_policy_data policy;
402 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
404 if (!cl_is_normalio(env, io))
407 /* nfs or loop back device write */
408 if (vio->vui_iter == NULL)
411 /* No MM (e.g. NFS)? No vmas too. */
415 iov_for_each(iov, i, *(vio->vui_iter)) {
416 unsigned long addr = (unsigned long)iov.iov_base;
417 size_t count = iov.iov_len;
422 count += addr & ~PAGE_MASK;
425 down_read(&mm->mmap_sem);
426 while((vma = our_vma(mm, addr, count)) != NULL) {
427 struct dentry *de = vma->vm_file->f_path.dentry;
428 struct inode *inode = de->d_inode;
429 int flags = CEF_MUST;
431 if (ll_file_nolock(vma->vm_file)) {
433 * For no lock case is not allowed for mmap
440 * XXX: Required lock mode can be weakened: CIT_WRITE
441 * io only ever reads user level buffer, and CIT_READ
444 policy_from_vma(&policy, vma, addr, count);
445 descr->cld_mode = vvp_mode_from_vma(vma);
446 descr->cld_obj = ll_i2info(inode)->lli_clob;
447 descr->cld_start = cl_index(descr->cld_obj,
448 policy.l_extent.start);
449 descr->cld_end = cl_index(descr->cld_obj,
450 policy.l_extent.end);
451 descr->cld_enq_flags = flags;
452 result = cl_io_lock_alloc_add(env, io, descr);
454 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
455 descr->cld_mode, descr->cld_start,
461 if (vma->vm_end - addr >= count)
464 count -= vma->vm_end - addr;
467 up_read(&mm->mmap_sem);
474 static void vvp_io_advance(const struct lu_env *env,
475 const struct cl_io_slice *ios,
478 struct vvp_io *vio = cl2vvp_io(env, ios);
479 struct cl_io *io = ios->cis_io;
480 struct cl_object *obj = ios->cis_io->ci_obj;
482 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
484 if (!cl_is_normalio(env, io))
487 vio->vui_tot_count -= nob;
488 iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
491 static void vvp_io_update_iov(const struct lu_env *env,
492 struct vvp_io *vio, struct cl_io *io)
494 size_t size = io->u.ci_rw.crw_count;
496 if (!cl_is_normalio(env, io) || vio->vui_iter == NULL)
499 iov_iter_truncate(vio->vui_iter, size);
502 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
503 enum cl_lock_mode mode, loff_t start, loff_t end)
505 struct vvp_io *vio = vvp_env_io(env);
509 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
512 vvp_io_update_iov(env, vio, io);
514 if (io->u.ci_rw.crw_nonblock)
515 ast_flags |= CEF_NONBLOCK;
517 result = vvp_mmap_locks(env, vio, io);
519 result = vvp_io_one_lock(env, io, ast_flags, mode, start, end);
524 static int vvp_io_read_lock(const struct lu_env *env,
525 const struct cl_io_slice *ios)
527 struct cl_io *io = ios->cis_io;
528 struct cl_io_rw_common *rd = &io->u.ci_rd.rd;
532 result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
533 rd->crw_pos + rd->crw_count - 1);
537 static int vvp_io_fault_lock(const struct lu_env *env,
538 const struct cl_io_slice *ios)
540 struct cl_io *io = ios->cis_io;
541 struct vvp_io *vio = cl2vvp_io(env, ios);
543 * XXX LDLM_FL_CBPENDING
545 return vvp_io_one_lock_index(env,
547 vvp_mode_from_vma(vio->u.fault.ft_vma),
548 io->u.ci_fault.ft_index,
549 io->u.ci_fault.ft_index);
552 static int vvp_io_write_lock(const struct lu_env *env,
553 const struct cl_io_slice *ios)
555 struct cl_io *io = ios->cis_io;
559 if (io->u.ci_wr.wr_append) {
561 end = OBD_OBJECT_EOF;
563 start = io->u.ci_wr.wr.crw_pos;
564 end = start + io->u.ci_wr.wr.crw_count - 1;
566 return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
569 static int vvp_io_setattr_iter_init(const struct lu_env *env,
570 const struct cl_io_slice *ios)
576 * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
578 * Handles "lockless io" mode when extent locking is done by server.
580 static int vvp_io_setattr_lock(const struct lu_env *env,
581 const struct cl_io_slice *ios)
583 struct cl_io *io = ios->cis_io;
587 if (cl_io_is_trunc(io)) {
588 new_size = io->u.ci_setattr.sa_attr.lvb_size;
590 enqflags = CEF_DISCARD_DATA;
592 unsigned int valid = io->u.ci_setattr.sa_valid;
594 if (!(valid & TIMES_SET_FLAGS))
597 if ((!(valid & ATTR_MTIME) ||
598 io->u.ci_setattr.sa_attr.lvb_mtime >=
599 io->u.ci_setattr.sa_attr.lvb_ctime) &&
600 (!(valid & ATTR_ATIME) ||
601 io->u.ci_setattr.sa_attr.lvb_atime >=
602 io->u.ci_setattr.sa_attr.lvb_ctime))
608 return vvp_io_one_lock(env, io, enqflags, CLM_WRITE,
609 new_size, OBD_OBJECT_EOF);
612 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
617 * Only ll_inode_size_lock is taken at this level.
619 ll_inode_size_lock(inode);
620 result = inode_newsize_ok(inode, size);
622 ll_inode_size_unlock(inode);
625 i_size_write(inode, size);
627 ll_truncate_pagecache(inode, size);
628 ll_inode_size_unlock(inode);
632 static int vvp_io_setattr_time(const struct lu_env *env,
633 const struct cl_io_slice *ios)
635 struct cl_io *io = ios->cis_io;
636 struct cl_object *obj = io->ci_obj;
637 struct cl_attr *attr = vvp_env_thread_attr(env);
639 unsigned valid = CAT_CTIME;
641 cl_object_attr_lock(obj);
642 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
643 if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
644 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
647 if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
648 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
651 result = cl_object_attr_update(env, obj, attr, valid);
652 cl_object_attr_unlock(obj);
657 static int vvp_io_setattr_start(const struct lu_env *env,
658 const struct cl_io_slice *ios)
660 struct cl_io *io = ios->cis_io;
661 struct inode *inode = vvp_object_inode(io->ci_obj);
662 struct ll_inode_info *lli = ll_i2info(inode);
664 mutex_lock(&inode->i_mutex);
665 if (cl_io_is_trunc(io)) {
666 down_write(&lli->lli_trunc_sem);
667 inode_dio_wait(inode);
670 if (io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS)
671 return vvp_io_setattr_time(env, ios);
676 static void vvp_io_setattr_end(const struct lu_env *env,
677 const struct cl_io_slice *ios)
679 struct cl_io *io = ios->cis_io;
680 struct inode *inode = vvp_object_inode(io->ci_obj);
681 struct ll_inode_info *lli = ll_i2info(inode);
683 if (cl_io_is_trunc(io)) {
684 /* Truncate in memory pages - they must be clean pages
685 * because osc has already notified to destroy osc_extents. */
686 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
687 inode_dio_write_done(inode);
688 up_write(&lli->lli_trunc_sem);
690 mutex_unlock(&inode->i_mutex);
693 static void vvp_io_setattr_fini(const struct lu_env *env,
694 const struct cl_io_slice *ios)
696 bool restore_needed = ios->cis_io->ci_restore_needed;
697 struct inode *inode = vvp_object_inode(ios->cis_obj);
699 vvp_io_fini(env, ios);
701 if (restore_needed && !ios->cis_io->ci_restore_needed) {
702 /* restore finished, set data modified flag for HSM */
703 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
707 static int vvp_io_read_start(const struct lu_env *env,
708 const struct cl_io_slice *ios)
710 struct vvp_io *vio = cl2vvp_io(env, ios);
711 struct cl_io *io = ios->cis_io;
712 struct cl_object *obj = io->ci_obj;
713 struct inode *inode = vvp_object_inode(obj);
714 struct ll_inode_info *lli = ll_i2info(inode);
715 struct file *file = vio->vui_fd->fd_file;
718 loff_t pos = io->u.ci_rd.rd.crw_pos;
719 long cnt = io->u.ci_rd.rd.crw_count;
720 long tot = vio->vui_tot_count;
723 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
725 CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
727 if (vio->vui_io_subtype == IO_NORMAL)
728 down_read(&lli->lli_trunc_sem);
730 if (!can_populate_pages(env, io, inode))
733 result = vvp_prep_size(env, obj, io, pos, tot, &exceed);
736 else if (exceed != 0)
739 LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
740 "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
741 inode->i_ino, cnt, pos, i_size_read(inode));
743 /* turn off the kernel's read-ahead */
744 vio->vui_fd->fd_file->f_ra.ra_pages = 0;
746 /* initialize read-ahead window once per syscall */
747 if (!vio->vui_ra_valid) {
748 vio->vui_ra_valid = true;
749 vio->vui_ra_start = cl_index(obj, pos);
750 vio->vui_ra_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
756 switch (vio->vui_io_subtype) {
758 LASSERT(vio->vui_iocb->ki_pos == pos);
759 result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter);
762 result = generic_file_splice_read(file, &pos,
763 vio->u.splice.vui_pipe, cnt,
764 vio->u.splice.vui_flags);
765 /* LU-1109: do splice read stripe by stripe otherwise if it
766 * may make nfsd stuck if this read occupied all internal pipe
771 CERROR("Wrong IO type %u\n", vio->vui_io_subtype);
779 io->ci_nob += result;
780 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, vio->vui_fd,
788 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
789 struct cl_page_list *plist, int from, int to)
791 struct cl_2queue *queue = &io->ci_queue;
792 struct cl_page *page;
793 unsigned int bytes = 0;
797 if (plist->pl_nr == 0)
800 if (from > 0 || to != PAGE_SIZE) {
801 page = cl_page_list_first(plist);
802 if (plist->pl_nr == 1) {
803 cl_page_clip(env, page, from, to);
806 cl_page_clip(env, page, from, PAGE_SIZE);
807 if (to != PAGE_SIZE) {
808 page = cl_page_list_last(plist);
809 cl_page_clip(env, page, 0, to);
814 cl_2queue_init(queue);
815 cl_page_list_splice(plist, &queue->c2_qin);
816 rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
818 /* plist is not sorted any more */
819 cl_page_list_splice(&queue->c2_qin, plist);
820 cl_page_list_splice(&queue->c2_qout, plist);
821 cl_2queue_fini(env, queue);
824 /* calculate bytes */
825 bytes = plist->pl_nr << PAGE_SHIFT;
826 bytes -= from + PAGE_SIZE - to;
828 while (plist->pl_nr > 0) {
829 page = cl_page_list_first(plist);
830 cl_page_list_del(env, plist, page);
832 cl_page_clip(env, page, 0, PAGE_SIZE);
834 SetPageUptodate(cl_page_vmpage(page));
835 cl_page_disown(env, io, page);
837 /* held in ll_cl_init() */
838 lu_ref_del(&page->cp_reference, "cl_io", io);
839 cl_page_put(env, page);
843 RETURN(bytes > 0 ? bytes : rc);
846 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
847 struct cl_page *page)
849 struct page *vmpage = page->cp_vmpage;
851 SetPageUptodate(vmpage);
852 set_page_dirty(vmpage);
854 cl_page_disown(env, io, page);
856 /* held in ll_cl_init() */
857 lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io));
858 cl_page_put(env, page);
861 /* make sure the page list is contiguous */
862 static bool page_list_sanity_check(struct cl_object *obj,
863 struct cl_page_list *plist)
865 struct cl_page *page;
866 pgoff_t index = CL_PAGE_EOF;
868 cl_page_list_for_each(page, plist) {
869 struct vvp_page *vpg = cl_object_page_slice(obj, page);
871 if (index == CL_PAGE_EOF) {
872 index = vvp_index(vpg);
877 if (index == vvp_index(vpg))
885 /* Return how many bytes have queued or written */
886 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
888 struct cl_object *obj = io->ci_obj;
889 struct inode *inode = vvp_object_inode(obj);
890 struct vvp_io *vio = vvp_env_io(env);
891 struct cl_page_list *queue = &vio->u.write.vui_queue;
892 struct cl_page *page;
895 unsigned int npages = vio->u.write.vui_queue.pl_nr;
901 CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
902 npages, vio->u.write.vui_from, vio->u.write.vui_to);
904 LASSERT(page_list_sanity_check(obj, queue));
906 /* submit IO with async write */
907 rc = cl_io_commit_async(env, io, queue,
908 vio->u.write.vui_from, vio->u.write.vui_to,
909 write_commit_callback);
910 npages -= queue->pl_nr; /* already committed pages */
912 /* calculate how many bytes were written */
913 bytes = npages << PAGE_SHIFT;
916 bytes -= vio->u.write.vui_from;
917 if (queue->pl_nr == 0) /* last page */
918 bytes -= PAGE_SIZE - vio->u.write.vui_to;
919 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
921 vio->u.write.vui_written += bytes;
923 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
924 npages, bytes, vio->u.write.vui_written);
926 /* the first page must have been written. */
927 vio->u.write.vui_from = 0;
929 LASSERT(page_list_sanity_check(obj, queue));
930 LASSERT(ergo(rc == 0, queue->pl_nr == 0));
932 /* out of quota, try sync write */
933 if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
934 rc = vvp_io_commit_sync(env, io, queue,
935 vio->u.write.vui_from,
936 vio->u.write.vui_to);
938 vio->u.write.vui_written += rc;
943 /* update inode size */
944 ll_merge_attr(env, inode);
946 /* Now the pages in queue were failed to commit, discard them
947 * unless they were dirtied before. */
948 while (queue->pl_nr > 0) {
949 page = cl_page_list_first(queue);
950 cl_page_list_del(env, queue, page);
952 if (!PageDirty(cl_page_vmpage(page)))
953 cl_page_discard(env, io, page);
955 cl_page_disown(env, io, page);
957 /* held in ll_cl_init() */
958 lu_ref_del(&page->cp_reference, "cl_io", io);
959 cl_page_put(env, page);
961 cl_page_list_fini(env, queue);
966 static int vvp_io_write_start(const struct lu_env *env,
967 const struct cl_io_slice *ios)
969 struct vvp_io *vio = cl2vvp_io(env, ios);
970 struct cl_io *io = ios->cis_io;
971 struct cl_object *obj = io->ci_obj;
972 struct inode *inode = vvp_object_inode(obj);
973 struct ll_inode_info *lli = ll_i2info(inode);
975 loff_t pos = io->u.ci_wr.wr.crw_pos;
976 size_t cnt = io->u.ci_wr.wr.crw_count;
980 if (vio->vui_io_subtype == IO_NORMAL)
981 down_read(&lli->lli_trunc_sem);
983 if (!can_populate_pages(env, io, inode))
986 if (cl_io_is_append(io)) {
988 * PARALLEL IO This has to be changed for parallel IO doing
989 * out-of-order writes.
991 ll_merge_attr(env, inode);
992 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
993 vio->vui_iocb->ki_pos = pos;
995 LASSERT(vio->vui_iocb->ki_pos == pos);
998 CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
1000 /* The maximum Lustre file size is variable, based on the OST maximum
1001 * object size and number of stripes. This needs another check in
1002 * addition to the VFS checks earlier. */
1003 if (pos + cnt > ll_file_maxbytes(inode)) {
1005 "%s: file "DFID" offset %llu > maxbytes "LPU64"\n",
1006 ll_get_fsname(inode->i_sb, NULL, 0),
1007 PFID(ll_inode2fid(inode)), pos + cnt,
1008 ll_file_maxbytes(inode));
1012 if (vio->vui_iter == NULL) {
1013 /* from a temp io in ll_cl_init(). */
1017 * When using the locked AIO function (generic_file_aio_write())
1018 * testing has shown the inode mutex to be a limiting factor
1019 * with multi-threaded single shared file performance. To get
1020 * around this, we now use the lockless version. To maintain
1021 * consistency, proper locking to protect against writes,
1022 * trucates, etc. is handled in the higher layers of lustre.
1024 result = generic_file_write_iter(vio->vui_iocb, vio->vui_iter);
1025 if (result > 0 || result == -EIOCBQUEUED) {
1028 err = generic_write_sync(vio->vui_iocb->ki_filp,
1030 if (err < 0 && result > 0)
1036 result = vvp_io_write_commit(env, io);
1037 if (vio->u.write.vui_written > 0) {
1038 result = vio->u.write.vui_written;
1039 io->ci_nob += result;
1041 CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
1042 io->ci_nob, result);
1046 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1049 io->ci_continue = 0;
1050 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
1051 vio->vui_fd, pos, result, WRITE);
1058 static void vvp_io_rw_end(const struct lu_env *env,
1059 const struct cl_io_slice *ios)
1061 struct vvp_io *vio = cl2vvp_io(env, ios);
1062 struct inode *inode = vvp_object_inode(ios->cis_obj);
1063 struct ll_inode_info *lli = ll_i2info(inode);
1065 if (vio->vui_io_subtype == IO_NORMAL)
1066 up_read(&lli->lli_trunc_sem);
1069 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
1071 struct vm_fault *vmf = cfio->ft_vmf;
1073 cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf);
1074 cfio->ft_flags_valid = 1;
1077 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
1078 vmf->virtual_address);
1079 if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) {
1080 lock_page(vmf->page);
1081 cfio->ft_flags |= VM_FAULT_LOCKED;
1084 cfio->ft_vmpage = vmf->page;
1089 if (cfio->ft_flags & VM_FAULT_SIGBUS) {
1090 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
1094 if (cfio->ft_flags & VM_FAULT_OOM) {
1095 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
1099 if (cfio->ft_flags & VM_FAULT_RETRY)
1102 CERROR("unknown error in page fault %d\n", cfio->ft_flags);
1107 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
1108 struct cl_page *page)
1110 set_page_dirty(page->cp_vmpage);
1113 static int vvp_io_fault_start(const struct lu_env *env,
1114 const struct cl_io_slice *ios)
1116 struct vvp_io *vio = cl2vvp_io(env, ios);
1117 struct cl_io *io = ios->cis_io;
1118 struct cl_object *obj = io->ci_obj;
1119 struct inode *inode = vvp_object_inode(obj);
1120 struct ll_inode_info *lli = ll_i2info(inode);
1121 struct cl_fault_io *fio = &io->u.ci_fault;
1122 struct vvp_fault_io *cfio = &vio->u.fault;
1125 struct page *vmpage = NULL;
1126 struct cl_page *page;
1131 down_read(&lli->lli_trunc_sem);
1133 /* offset of the last byte on the page */
1134 offset = cl_offset(obj, fio->ft_index + 1) - 1;
1135 LASSERT(cl_index(obj, offset) == fio->ft_index);
1136 result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL);
1140 /* must return locked page */
1141 if (fio->ft_mkwrite) {
1142 LASSERT(cfio->ft_vmpage != NULL);
1143 lock_page(cfio->ft_vmpage);
1145 result = vvp_io_kernel_fault(cfio);
1150 vmpage = cfio->ft_vmpage;
1151 LASSERT(PageLocked(vmpage));
1153 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
1154 ll_invalidate_page(vmpage);
1156 size = i_size_read(inode);
1157 /* Though we have already held a cl_lock upon this page, but
1158 * it still can be truncated locally. */
1159 if (unlikely((vmpage->mapping != inode->i_mapping) ||
1160 (page_offset(vmpage) > size))) {
1161 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
1163 /* return +1 to stop cl_io_loop() and ll_fault() will catch
1165 GOTO(out, result = +1);
1168 last_index = cl_index(obj, size - 1);
1170 if (fio->ft_mkwrite ) {
1172 * Capture the size while holding the lli_trunc_sem from above
1173 * we want to make sure that we complete the mkwrite action
1174 * while holding this lock. We need to make sure that we are
1175 * not past the end of the file.
1177 if (last_index < fio->ft_index) {
1179 "llite: mkwrite and truncate race happened: "
1180 "%p: 0x%lx 0x%lx\n",
1181 vmpage->mapping,fio->ft_index,last_index);
1183 * We need to return if we are
1184 * passed the end of the file. This will propagate
1185 * up the call stack to ll_page_mkwrite where
1186 * we will return VM_FAULT_NOPAGE. Any non-negative
1187 * value returned here will be silently
1188 * converted to 0. If the vmpage->mapping is null
1189 * the error code would be converted back to ENODATA
1190 * in ll_page_mkwrite0. Thus we return -ENODATA
1191 * to handle both cases
1193 GOTO(out, result = -ENODATA);
1197 page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
1199 GOTO(out, result = PTR_ERR(page));
1201 /* if page is going to be written, we should add this page into cache
1203 if (fio->ft_mkwrite) {
1204 wait_on_page_writeback(vmpage);
1205 if (!PageDirty(vmpage)) {
1206 struct cl_page_list *plist = &io->ci_queue.c2_qin;
1207 struct vvp_page *vpg = cl_object_page_slice(obj, page);
1210 /* vvp_page_assume() calls wait_on_page_writeback(). */
1211 cl_page_assume(env, io, page);
1213 cl_page_list_init(plist);
1214 cl_page_list_add(plist, page);
1217 if (last_index == vvp_index(vpg))
1218 to = size & ~PAGE_MASK;
1220 /* Do not set Dirty bit here so that in case IO is
1221 * started before the page is really made dirty, we
1222 * still have chance to detect it. */
1223 result = cl_io_commit_async(env, io, plist, 0, to,
1224 mkwrite_commit_callback);
1225 LASSERT(cl_page_is_owned(page, io));
1226 cl_page_list_fini(env, plist);
1230 cl_page_discard(env, io, page);
1231 cl_page_disown(env, io, page);
1233 cl_page_put(env, page);
1235 /* we're in big trouble, what can we do now? */
1236 if (result == -EDQUOT)
1240 cl_page_disown(env, io, page);
1245 * The ft_index is only used in the case of
1246 * a mkwrite action. We need to check
1247 * our assertions are correct, since
1248 * we should have caught this above
1250 LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1251 if (fio->ft_index == last_index)
1253 * Last page is mapped partially.
1255 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1257 fio->ft_nob = cl_page_size(obj);
1259 lu_ref_add(&page->cp_reference, "fault", io);
1260 fio->ft_page = page;
1264 /* return unlocked vmpage to avoid deadlocking */
1266 unlock_page(vmpage);
1268 cfio->ft_flags &= ~VM_FAULT_LOCKED;
1273 static void vvp_io_fault_end(const struct lu_env *env,
1274 const struct cl_io_slice *ios)
1276 struct inode *inode = vvp_object_inode(ios->cis_obj);
1277 struct ll_inode_info *lli = ll_i2info(inode);
1279 CLOBINVRNT(env, ios->cis_io->ci_obj,
1280 vvp_object_invariant(ios->cis_io->ci_obj));
1281 up_read(&lli->lli_trunc_sem);
1284 static int vvp_io_fsync_start(const struct lu_env *env,
1285 const struct cl_io_slice *ios)
1287 /* we should mark TOWRITE bit to each dirty page in radix tree to
1288 * verify pages have been written, but this is difficult because of
1293 static int vvp_io_read_ahead(const struct lu_env *env,
1294 const struct cl_io_slice *ios,
1295 pgoff_t start, struct cl_read_ahead *ra)
1300 if (ios->cis_io->ci_type == CIT_READ ||
1301 ios->cis_io->ci_type == CIT_FAULT) {
1302 struct vvp_io *vio = cl2vvp_io(env, ios);
1304 if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1305 ra->cra_end = CL_PAGE_EOF;
1306 result = +1; /* no need to call down */
1313 static const struct cl_io_operations vvp_io_ops = {
1316 .cio_fini = vvp_io_fini,
1317 .cio_lock = vvp_io_read_lock,
1318 .cio_start = vvp_io_read_start,
1319 .cio_end = vvp_io_rw_end,
1320 .cio_advance = vvp_io_advance,
1323 .cio_fini = vvp_io_fini,
1324 .cio_iter_init = vvp_io_write_iter_init,
1325 .cio_iter_fini = vvp_io_write_iter_fini,
1326 .cio_lock = vvp_io_write_lock,
1327 .cio_start = vvp_io_write_start,
1328 .cio_end = vvp_io_rw_end,
1329 .cio_advance = vvp_io_advance,
1332 .cio_fini = vvp_io_setattr_fini,
1333 .cio_iter_init = vvp_io_setattr_iter_init,
1334 .cio_lock = vvp_io_setattr_lock,
1335 .cio_start = vvp_io_setattr_start,
1336 .cio_end = vvp_io_setattr_end
1339 .cio_fini = vvp_io_fault_fini,
1340 .cio_iter_init = vvp_io_fault_iter_init,
1341 .cio_lock = vvp_io_fault_lock,
1342 .cio_start = vvp_io_fault_start,
1343 .cio_end = vvp_io_fault_end,
1346 .cio_start = vvp_io_fsync_start,
1347 .cio_fini = vvp_io_fini
1350 .cio_fini = vvp_io_fini
1353 .cio_fini = vvp_io_fini
1356 .cio_read_ahead = vvp_io_read_ahead
1359 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1362 struct vvp_io *vio = vvp_env_io(env);
1363 struct inode *inode = vvp_object_inode(obj);
1366 CLOBINVRNT(env, obj, vvp_object_invariant(obj));
1369 CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1370 "restore needed %d\n",
1371 PFID(lu_object_fid(&obj->co_lu)),
1372 io->ci_ignore_layout, io->ci_verify_layout,
1373 vio->vui_layout_gen, io->ci_restore_needed);
1375 CL_IO_SLICE_CLEAN(vio, vui_cl);
1376 cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops);
1377 vio->vui_ra_valid = false;
1379 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1381 struct ll_inode_info *lli = ll_i2info(inode);
1383 count = io->u.ci_rw.crw_count;
1384 /* "If nbyte is 0, read() will return 0 and have no other
1385 * results." -- Single Unix Spec */
1389 vio->vui_tot_count = count;
1391 /* for read/write, we store the jobid in the inode, and
1392 * it'll be fetched by osc when building RPC.
1394 * it's not accurate if the file is shared by different
1397 lustre_get_jobid(lli->lli_jobid);
1398 } else if (io->ci_type == CIT_SETATTR) {
1399 if (!cl_io_is_trunc(io))
1400 io->ci_lockreq = CILR_MANDATORY;
1403 /* ignore layout change for generic CIT_MISC but not for glimpse.
1404 * io context for glimpse must set ci_verify_layout to true,
1405 * see cl_glimpse_size0() for details. */
1406 if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1407 io->ci_ignore_layout = 1;
1409 /* Enqueue layout lock and get layout version. We need to do this
1410 * even for operations requiring to open file, such as read and write,
1411 * because it might not grant layout lock in IT_OPEN. */
1412 if (result == 0 && !io->ci_ignore_layout) {
1413 result = ll_layout_refresh(inode, &vio->vui_layout_gen);
1414 if (result == -ENOENT)
1415 /* If the inode on MDS has been removed, but the objects
1416 * on OSTs haven't been destroyed (async unlink), layout
1417 * fetch will return -ENOENT, we'd ingore this error
1418 * and continue with dirty flush. LU-3230. */
1421 CERROR("%s: refresh file layout " DFID " error %d.\n",
1422 ll_get_fsname(inode->i_sb, NULL, 0),
1423 PFID(lu_object_fid(&obj->co_lu)), result);