Whamcloud - gitweb
LU-5062 llite: Solve a race to access lli_has_smd in read case
[fs/lustre-release.git] / lustre / llite / vvp_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_io for VVP layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #ifndef __KERNEL__
45 # error This file is kernel only.
46 #endif
47
48 #include <obd.h>
49 #include <lustre_lite.h>
50
51 #include "vvp_internal.h"
52
53 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
54                                 const struct cl_io_slice *slice);
55
56 /**
57  * True, if \a io is a normal io, False for splice_{read,write}
58  */
59 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
60 {
61         struct vvp_io *vio = vvp_env_io(env);
62
63         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
64
65         return vio->cui_io_subtype == IO_NORMAL;
66 }
67
68 /**
69  * For swapping layout. The file's layout may have changed.
70  * To avoid populating pages to a wrong stripe, we have to verify the
71  * correctness of layout. It works because swapping layout processes
72  * have to acquire group lock.
73  */
74 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
75                                 struct inode *inode)
76 {
77         struct ll_inode_info    *lli = ll_i2info(inode);
78         struct ccc_io           *cio = ccc_env_io(env);
79         bool rc = true;
80
81         switch (io->ci_type) {
82         case CIT_READ:
83         case CIT_WRITE:
84                 /* don't need lock here to check lli_layout_gen as we have held
85                  * extent lock and GROUP lock has to hold to swap layout */
86                 if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
87                         io->ci_need_restart = 1;
88                         /* this will return application a short read/write */
89                         io->ci_continue = 0;
90                         rc = false;
91                 }
92         case CIT_FAULT:
93                 /* fault is okay because we've already had a page. */
94         default:
95                 break;
96         }
97
98         return rc;
99 }
100
101 /*****************************************************************************
102  *
103  * io operations.
104  *
105  */
106
107 static int vvp_io_write_iter_init(const struct lu_env *env,
108                                   const struct cl_io_slice *ios)
109 {
110         struct ccc_io *cio = cl2ccc_io(env, ios);
111
112         cl_page_list_init(&cio->u.write.cui_queue);
113         cio->u.write.cui_written = 0;
114         cio->u.write.cui_from = 0;
115         cio->u.write.cui_to = PAGE_SIZE;
116
117         return 0;
118 }
119
120 static void vvp_io_write_iter_fini(const struct lu_env *env,
121                                    const struct cl_io_slice *ios)
122 {
123         struct ccc_io *cio = cl2ccc_io(env, ios);
124
125         LASSERT(cio->u.write.cui_queue.pl_nr == 0);
126 }
127
128 static int vvp_io_fault_iter_init(const struct lu_env *env,
129                                   const struct cl_io_slice *ios)
130 {
131         struct vvp_io *vio   = cl2vvp_io(env, ios);
132         struct inode  *inode = ccc_object_inode(ios->cis_obj);
133
134         LASSERT(inode ==
135                 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
136         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
137         return 0;
138 }
139
140 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
141 {
142         struct cl_io     *io  = ios->cis_io;
143         struct cl_object *obj = io->ci_obj;
144         struct ccc_io    *cio = cl2ccc_io(env, ios);
145         struct inode     *inode = ccc_object_inode(obj);
146
147         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
148
149         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
150                            "restore needed %d\n",
151                PFID(lu_object_fid(&obj->co_lu)),
152                io->ci_ignore_layout, io->ci_verify_layout,
153                cio->cui_layout_gen, io->ci_restore_needed);
154
155         if (io->ci_restore_needed == 1) {
156                 int     rc;
157
158                 /* file was detected release, we need to restore it
159                  * before finishing the io
160                  */
161                 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
162                 /* if restore registration failed, no restart,
163                  * we will return -ENODATA */
164                 /* The layout will change after restore, so we need to
165                  * block on layout lock hold by the MDT
166                  * as MDT will not send new layout in lvb (see LU-3124)
167                  * we have to explicitly fetch it, all this will be done
168                  * by ll_layout_refresh()
169                  */
170                 if (rc == 0) {
171                         io->ci_restore_needed = 0;
172                         io->ci_need_restart = 1;
173                         io->ci_verify_layout = 1;
174                 } else {
175                         io->ci_restore_needed = 1;
176                         io->ci_need_restart = 0;
177                         io->ci_verify_layout = 0;
178                         io->ci_result = rc;
179                 }
180         }
181
182         if (!io->ci_ignore_layout && io->ci_verify_layout) {
183                 __u32 gen = 0;
184
185                 /* check layout version */
186                 ll_layout_refresh(inode, &gen);
187                 io->ci_need_restart = cio->cui_layout_gen != gen;
188                 if (io->ci_need_restart) {
189                         CDEBUG(D_VFSTRACE,
190                                DFID" layout changed from %d to %d.\n",
191                                PFID(lu_object_fid(&obj->co_lu)),
192                                cio->cui_layout_gen, gen);
193                         /* today successful restore is the only possible
194                          * case */
195                         /* restore was done, clear restoring state */
196                         ll_i2info(ccc_object_inode(obj))->lli_flags &=
197                                 ~LLIF_FILE_RESTORING;
198                 }
199         }
200 }
201
202 static void vvp_io_fault_fini(const struct lu_env *env,
203                               const struct cl_io_slice *ios)
204 {
205         struct cl_io   *io   = ios->cis_io;
206         struct cl_page *page = io->u.ci_fault.ft_page;
207
208         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
209
210         if (page != NULL) {
211                 lu_ref_del(&page->cp_reference, "fault", io);
212                 cl_page_put(env, page);
213                 io->u.ci_fault.ft_page = NULL;
214         }
215         vvp_io_fini(env, ios);
216 }
217
218 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
219 {
220         /*
221          * we only want to hold PW locks if the mmap() can generate
222          * writes back to the file and that only happens in shared
223          * writable vmas
224          */
225         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
226                 return CLM_WRITE;
227         return CLM_READ;
228 }
229
230 static int vvp_mmap_locks(const struct lu_env *env,
231                           struct ccc_io *vio, struct cl_io *io)
232 {
233         struct ccc_thread_info *cti = ccc_env_info(env);
234         struct mm_struct       *mm = current->mm;
235         struct vm_area_struct  *vma;
236         struct cl_lock_descr   *descr = &cti->cti_descr;
237         ldlm_policy_data_t      policy;
238         unsigned long           addr;
239         unsigned long           seg;
240         ssize_t                 count;
241         int                     result;
242         ENTRY;
243
244         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
245
246         if (!cl_is_normalio(env, io))
247                 RETURN(0);
248
249         if (vio->cui_iov == NULL) /* nfs or loop back device write */
250                 RETURN(0);
251
252         /* No MM (e.g. NFS)? No vmas too. */
253         if (mm == NULL)
254                 RETURN(0);
255
256         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
257                 const struct iovec *iv = &vio->cui_iov[seg];
258
259                 addr = (unsigned long)iv->iov_base;
260                 count = iv->iov_len;
261                 if (count == 0)
262                         continue;
263
264                 count += addr & (~CFS_PAGE_MASK);
265                 addr &= CFS_PAGE_MASK;
266
267                 down_read(&mm->mmap_sem);
268                 while((vma = our_vma(mm, addr, count)) != NULL) {
269                         struct inode *inode = vma->vm_file->f_dentry->d_inode;
270                         int flags = CEF_MUST;
271
272                         if (ll_file_nolock(vma->vm_file)) {
273                                 /*
274                                  * For no lock case, a lockless lock will be
275                                  * generated.
276                                  */
277                                 flags = CEF_NEVER;
278                         }
279
280                         /*
281                          * XXX: Required lock mode can be weakened: CIT_WRITE
282                          * io only ever reads user level buffer, and CIT_READ
283                          * only writes on it.
284                          */
285                         policy_from_vma(&policy, vma, addr, count);
286                         descr->cld_mode = vvp_mode_from_vma(vma);
287                         descr->cld_obj = ll_i2info(inode)->lli_clob;
288                         descr->cld_start = cl_index(descr->cld_obj,
289                                                     policy.l_extent.start);
290                         descr->cld_end = cl_index(descr->cld_obj,
291                                                   policy.l_extent.end);
292                         descr->cld_enq_flags = flags;
293                         result = cl_io_lock_alloc_add(env, io, descr);
294
295                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
296                                descr->cld_mode, descr->cld_start,
297                                descr->cld_end);
298
299                         if (result < 0) {
300                                 up_read(&mm->mmap_sem);
301                                 RETURN(result);
302                         }
303
304                         if (vma->vm_end - addr >= count)
305                                 break;
306
307                         count -= vma->vm_end - addr;
308                         addr = vma->vm_end;
309                 }
310                 up_read(&mm->mmap_sem);
311         }
312         RETURN(0);
313 }
314
315 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
316                           enum cl_lock_mode mode, loff_t start, loff_t end)
317 {
318         struct ccc_io *cio = ccc_env_io(env);
319         int result;
320         int ast_flags = 0;
321
322         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
323         ENTRY;
324
325         ccc_io_update_iov(env, cio, io);
326
327         if (io->u.ci_rw.crw_nonblock)
328                 ast_flags |= CEF_NONBLOCK;
329         result = vvp_mmap_locks(env, cio, io);
330         if (result == 0)
331                 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
332         RETURN(result);
333 }
334
335 static int vvp_io_read_lock(const struct lu_env *env,
336                             const struct cl_io_slice *ios)
337 {
338         struct cl_io            *io = ios->cis_io;
339         struct cl_io_rw_common  *rd = &io->u.ci_rd.rd;
340         int result;
341
342         ENTRY;
343         result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos,
344                                 rd->crw_pos + rd->crw_count - 1);
345         RETURN(result);
346 }
347
348 static int vvp_io_fault_lock(const struct lu_env *env,
349                              const struct cl_io_slice *ios)
350 {
351         struct cl_io *io   = ios->cis_io;
352         struct vvp_io *vio = cl2vvp_io(env, ios);
353         /*
354          * XXX LDLM_FL_CBPENDING
355          */
356         return ccc_io_one_lock_index
357                 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
358                  io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
359 }
360
361 static int vvp_io_write_lock(const struct lu_env *env,
362                              const struct cl_io_slice *ios)
363 {
364         struct cl_io *io = ios->cis_io;
365         loff_t start;
366         loff_t end;
367
368         if (io->u.ci_wr.wr_append) {
369                 start = 0;
370                 end   = OBD_OBJECT_EOF;
371         } else {
372                 start = io->u.ci_wr.wr.crw_pos;
373                 end   = start + io->u.ci_wr.wr.crw_count - 1;
374         }
375         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
376 }
377
378 static int vvp_io_setattr_iter_init(const struct lu_env *env,
379                                     const struct cl_io_slice *ios)
380 {
381         return 0;
382 }
383
384 /**
385  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
386  *
387  * Handles "lockless io" mode when extent locking is done by server.
388  */
389 static int vvp_io_setattr_lock(const struct lu_env *env,
390                                const struct cl_io_slice *ios)
391 {
392         struct ccc_io *cio = ccc_env_io(env);
393         struct cl_io  *io  = ios->cis_io;
394         __u64 new_size;
395         __u32 enqflags = 0;
396
397         if (cl_io_is_trunc(io)) {
398                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
399                 if (new_size == 0)
400                         enqflags = CEF_DISCARD_DATA;
401         } else {
402                 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
403                      io->u.ci_setattr.sa_attr.lvb_ctime) ||
404                     (io->u.ci_setattr.sa_attr.lvb_atime >=
405                      io->u.ci_setattr.sa_attr.lvb_ctime))
406                         return 0;
407                 new_size = 0;
408         }
409         cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
410         return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
411                                new_size, OBD_OBJECT_EOF);
412 }
413
414 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
415 {
416         int     result;
417
418         /*
419          * Only ll_inode_size_lock is taken at this level.
420          */
421         ll_inode_size_lock(inode);
422         result = inode_newsize_ok(inode, size);
423         if (result < 0) {
424                 ll_inode_size_unlock(inode);
425                 return result;
426         }
427         i_size_write(inode, size);
428
429         ll_truncate_pagecache(inode, size);
430         ll_inode_size_unlock(inode);
431         return result;
432 }
433
434 static int vvp_io_setattr_trunc(const struct lu_env *env,
435                                 const struct cl_io_slice *ios,
436                                 struct inode *inode, loff_t size)
437 {
438         inode_dio_wait(inode);
439         return 0;
440 }
441
442 static int vvp_io_setattr_time(const struct lu_env *env,
443                                const struct cl_io_slice *ios)
444 {
445         struct cl_io       *io    = ios->cis_io;
446         struct cl_object   *obj   = io->ci_obj;
447         struct cl_attr     *attr  = ccc_env_thread_attr(env);
448         int result;
449         unsigned valid = CAT_CTIME;
450
451         cl_object_attr_lock(obj);
452         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
453         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
454                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
455                 valid |= CAT_ATIME;
456         }
457         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
458                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
459                 valid |= CAT_MTIME;
460         }
461         result = cl_object_attr_set(env, obj, attr, valid);
462         cl_object_attr_unlock(obj);
463
464         return result;
465 }
466
467 static int vvp_io_setattr_start(const struct lu_env *env,
468                                 const struct cl_io_slice *ios)
469 {
470         struct cl_io    *io    = ios->cis_io;
471         struct inode    *inode = ccc_object_inode(io->ci_obj);
472         int result = 0;
473
474         mutex_lock(&inode->i_mutex);
475         if (cl_io_is_trunc(io))
476                 result = vvp_io_setattr_trunc(env, ios, inode,
477                                         io->u.ci_setattr.sa_attr.lvb_size);
478         if (result == 0)
479                 result = vvp_io_setattr_time(env, ios);
480         return result;
481 }
482
483 static void vvp_io_setattr_end(const struct lu_env *env,
484                                const struct cl_io_slice *ios)
485 {
486         struct cl_io *io    = ios->cis_io;
487         struct inode *inode = ccc_object_inode(io->ci_obj);
488
489         if (cl_io_is_trunc(io)) {
490                 /* Truncate in memory pages - they must be clean pages
491                  * because osc has already notified to destroy osc_extents. */
492                 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
493                 inode_dio_write_done(inode);
494         }
495         mutex_unlock(&inode->i_mutex);
496 }
497
498 static void vvp_io_setattr_fini(const struct lu_env *env,
499                                 const struct cl_io_slice *ios)
500 {
501         vvp_io_fini(env, ios);
502 }
503
504 static int vvp_io_read_start(const struct lu_env *env,
505                              const struct cl_io_slice *ios)
506 {
507         struct vvp_io     *vio   = cl2vvp_io(env, ios);
508         struct ccc_io     *cio   = cl2ccc_io(env, ios);
509         struct cl_io      *io    = ios->cis_io;
510         struct cl_object  *obj   = io->ci_obj;
511         struct inode      *inode = ccc_object_inode(obj);
512         struct ll_ra_read *bead  = &vio->cui_bead;
513         struct file       *file  = cio->cui_fd->fd_file;
514
515         int     result;
516         loff_t  pos = io->u.ci_rd.rd.crw_pos;
517         long    cnt = io->u.ci_rd.rd.crw_count;
518         long    tot = cio->cui_tot_count;
519         int     exceed = 0;
520
521         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
522
523         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
524
525         if (!can_populate_pages(env, io, inode))
526                 return 0;
527
528         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
529         if (result != 0)
530                 return result;
531         else if (exceed != 0)
532                 goto out;
533
534         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
535                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
536                         inode->i_ino, cnt, pos, i_size_read(inode));
537
538         /* turn off the kernel's read-ahead */
539         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
540
541         /* initialize read-ahead window once per syscall */
542         if (!vio->cui_ra_window_set) {
543                 vio->cui_ra_window_set = 1;
544                 bead->lrr_start = cl_index(obj, pos);
545                 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
546                 ll_ra_read_in(file, bead);
547         }
548
549         /* BUG: 5972 */
550         file_accessed(file);
551         switch (vio->cui_io_subtype) {
552         case IO_NORMAL:
553                 LASSERT(cio->cui_iocb->ki_pos == pos);
554                 result = generic_file_aio_read(cio->cui_iocb,
555                                                cio->cui_iov, cio->cui_nrsegs,
556                                                cio->cui_iocb->ki_pos);
557                 break;
558         case IO_SPLICE:
559                 result = generic_file_splice_read(file, &pos,
560                                 vio->u.splice.cui_pipe, cnt,
561                                 vio->u.splice.cui_flags);
562                 /* LU-1109: do splice read stripe by stripe otherwise if it
563                  * may make nfsd stuck if this read occupied all internal pipe
564                  * buffers. */
565                 io->ci_continue = 0;
566                 break;
567         default:
568                 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
569                 LBUG();
570         }
571
572 out:
573         if (result >= 0) {
574                 if (result < cnt)
575                         io->ci_continue = 0;
576                 io->ci_nob += result;
577                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
578                                   pos, result, READ);
579                 result = 0;
580         }
581
582         return result;
583 }
584
585 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
586 {
587         struct vvp_io *vio = cl2vvp_io(env, ios);
588         struct ccc_io *cio = cl2ccc_io(env, ios);
589
590         if (vio->cui_ra_window_set)
591                 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
592
593         vvp_io_fini(env, ios);
594 }
595
596 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
597                               struct cl_page_list *plist, int from, int to)
598 {
599         struct cl_2queue *queue = &io->ci_queue;
600         struct cl_page *page;
601         unsigned int bytes = 0;
602         int rc = 0;
603         ENTRY;
604
605         if (plist->pl_nr == 0)
606                 RETURN(0);
607
608         if (from > 0 || to != PAGE_SIZE) {
609                 page = cl_page_list_first(plist);
610                 if (plist->pl_nr == 1) {
611                         cl_page_clip(env, page, from, to);
612                 } else if (from > 0) {
613                         cl_page_clip(env, page, from, PAGE_SIZE);
614                 } else {
615                         page = cl_page_list_last(plist);
616                         cl_page_clip(env, page, 0, to);
617                 }
618         }
619
620         cl_2queue_init(queue);
621         cl_page_list_splice(plist, &queue->c2_qin);
622         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
623
624         /* plist is not sorted any more */
625         cl_page_list_splice(&queue->c2_qin, plist);
626         cl_page_list_splice(&queue->c2_qout, plist);
627         cl_2queue_fini(env, queue);
628
629         if (rc == 0) {
630                 /* calculate bytes */
631                 bytes = plist->pl_nr << PAGE_SHIFT;
632                 bytes -= from + PAGE_SIZE - to;
633
634                 while (plist->pl_nr > 0) {
635                         page = cl_page_list_first(plist);
636                         cl_page_list_del(env, plist, page);
637
638                         cl_page_clip(env, page, 0, PAGE_SIZE);
639
640                         SetPageUptodate(cl_page_vmpage(page));
641                         cl_page_disown(env, io, page);
642
643                         /* held in ll_cl_init() */
644                         lu_ref_del(&page->cp_reference, "cl_io", io);
645                         cl_page_put(env, page);
646                 }
647         }
648
649         RETURN(bytes > 0 ? bytes : rc);
650 }
651
652 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
653                                 struct cl_page *page)
654 {
655         struct ccc_page *cp;
656         struct page *vmpage = page->cp_vmpage;
657         struct cl_object *clob = cl_io_top(io)->ci_obj;
658
659         SetPageUptodate(vmpage);
660         set_page_dirty(vmpage);
661
662         cp = cl2ccc_page(cl_object_page_slice(clob, page));
663         vvp_write_pending(cl2ccc(clob), cp);
664
665         cl_page_disown(env, io, page);
666
667         /* held in ll_cl_init() */
668         lu_ref_del(&page->cp_reference, "cl_io", io);
669         cl_page_put(env, page);
670 }
671
672 /* make sure the page list is contiguous */
673 static bool page_list_sanity_check(struct cl_object *obj,
674                                    struct cl_page_list *plist)
675 {
676         struct cl_page *page;
677         pgoff_t index = CL_PAGE_EOF;
678
679         cl_page_list_for_each(page, plist) {
680                 struct ccc_page *cp = cl_object_page_slice(obj, page);
681
682                 if (index == CL_PAGE_EOF) {
683                         index = ccc_index(cp);
684                         continue;
685                 }
686
687                 ++index;
688                 if (index == ccc_index(cp))
689                         continue;
690
691                 return false;
692         }
693         return true;
694 }
695
696 /* Return how many bytes have queued or written */
697 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
698 {
699         struct cl_object *obj = io->ci_obj;
700         struct inode *inode = ccc_object_inode(obj);
701         struct ccc_io *cio = ccc_env_io(env);
702         struct cl_page_list *queue = &cio->u.write.cui_queue;
703         struct cl_page *page;
704         int rc = 0;
705         int bytes = 0;
706         unsigned int npages = cio->u.write.cui_queue.pl_nr;
707         ENTRY;
708
709         if (npages == 0)
710                 RETURN(0);
711
712         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
713                 npages, cio->u.write.cui_from, cio->u.write.cui_to);
714
715         LASSERT(page_list_sanity_check(obj, queue));
716
717         /* submit IO with async write */
718         rc = cl_io_commit_async(env, io, queue,
719                                 cio->u.write.cui_from, cio->u.write.cui_to,
720                                 write_commit_callback);
721         npages -= queue->pl_nr; /* already committed pages */
722         if (npages > 0) {
723                 /* calculate how many bytes were written */
724                 bytes = npages << PAGE_SHIFT;
725
726                 /* first page */
727                 bytes -= cio->u.write.cui_from;
728                 if (queue->pl_nr == 0) /* last page */
729                         bytes -= PAGE_SIZE - cio->u.write.cui_to;
730                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
731
732                 cio->u.write.cui_written += bytes;
733
734                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
735                         npages, bytes, cio->u.write.cui_written);
736
737                 /* the first page must have been written. */
738                 cio->u.write.cui_from = 0;
739         }
740         LASSERT(page_list_sanity_check(obj, queue));
741         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
742
743         /* out of quota, try sync write */
744         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
745                 rc = vvp_io_commit_sync(env, io, queue,
746                                         cio->u.write.cui_from,
747                                         cio->u.write.cui_to);
748                 if (rc > 0) {
749                         cio->u.write.cui_written += rc;
750                         rc = 0;
751                 }
752         }
753
754         /* update inode size */
755         ll_merge_lvb(env, inode);
756
757         /* Now the pages in queue were failed to commit, discard them
758          * unless they were dirtied before. */
759         while (queue->pl_nr > 0) {
760                 page = cl_page_list_first(queue);
761                 cl_page_list_del(env, queue, page);
762
763                 if (!PageDirty(cl_page_vmpage(page)))
764                         cl_page_discard(env, io, page);
765
766                 cl_page_disown(env, io, page);
767
768                 /* held in ll_cl_init() */
769                 lu_ref_del(&page->cp_reference, "cl_io", io);
770                 cl_page_put(env, page);
771         }
772         cl_page_list_fini(env, queue);
773
774         RETURN(rc);
775 }
776
777 static int vvp_io_write_start(const struct lu_env *env,
778                               const struct cl_io_slice *ios)
779 {
780         struct ccc_io      *cio   = cl2ccc_io(env, ios);
781         struct cl_io       *io    = ios->cis_io;
782         struct cl_object   *obj   = io->ci_obj;
783         struct inode       *inode = ccc_object_inode(obj);
784         ssize_t result = 0;
785         loff_t pos = io->u.ci_wr.wr.crw_pos;
786         size_t cnt = io->u.ci_wr.wr.crw_count;
787
788         ENTRY;
789
790         if (!can_populate_pages(env, io, inode))
791                 RETURN(0);
792
793         if (cl_io_is_append(io)) {
794                 /*
795                  * PARALLEL IO This has to be changed for parallel IO doing
796                  * out-of-order writes.
797                  */
798                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
799                 cio->cui_iocb->ki_pos = pos;
800         } else {
801                 LASSERT(cio->cui_iocb->ki_pos == pos);
802         }
803
804         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
805
806         if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
807                 result = 0;
808         else
809                 result = generic_file_aio_write(cio->cui_iocb,
810                                                 cio->cui_iov, cio->cui_nrsegs,
811                                                 cio->cui_iocb->ki_pos);
812         if (result > 0) {
813                 result = vvp_io_write_commit(env, io);
814                 if (cio->u.write.cui_written > 0) {
815                         result = cio->u.write.cui_written;
816                         io->ci_nob += result;
817
818                         CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
819                                 io->ci_nob, result);
820                 }
821         }
822         if (result > 0) {
823                 struct ll_inode_info *lli = ll_i2info(inode);
824
825                 spin_lock(&lli->lli_lock);
826                 lli->lli_flags |= LLIF_DATA_MODIFIED;
827                 spin_unlock(&lli->lli_lock);
828
829                 if (result < cnt)
830                         io->ci_continue = 0;
831                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
832                                   cio->cui_fd, pos, result, WRITE);
833                 result = 0;
834         }
835
836         RETURN(result);
837 }
838
839 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
840 {
841         struct vm_fault *vmf = cfio->fault.ft_vmf;
842
843         cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
844
845         if (vmf->page) {
846                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
847                                vmf->virtual_address);
848                 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
849                         lock_page(vmf->page);
850                         cfio->fault.ft_flags |= VM_FAULT_LOCKED;
851                 }
852
853                 cfio->ft_vmpage = vmf->page;
854                 return 0;
855         }
856
857         if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
858                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
859                 return -EFAULT;
860         }
861
862         if (cfio->fault.ft_flags & VM_FAULT_OOM) {
863                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
864                 return -ENOMEM;
865         }
866
867         if (cfio->fault.ft_flags & VM_FAULT_RETRY)
868                 return -EAGAIN;
869
870         CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
871         return -EINVAL;
872 }
873
874 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
875                                     struct cl_page *page)
876 {
877         struct ccc_page *cp;
878         struct cl_object *clob = cl_io_top(io)->ci_obj;
879
880         set_page_dirty(page->cp_vmpage);
881
882         cp = cl2ccc_page(cl_object_page_slice(clob, page));
883         vvp_write_pending(cl2ccc(clob), cp);
884 }
885
886 static int vvp_io_fault_start(const struct lu_env *env,
887                               const struct cl_io_slice *ios)
888 {
889         struct vvp_io       *vio     = cl2vvp_io(env, ios);
890         struct cl_io        *io      = ios->cis_io;
891         struct cl_object    *obj     = io->ci_obj;
892         struct inode        *inode   = ccc_object_inode(obj);
893         struct cl_fault_io  *fio     = &io->u.ci_fault;
894         struct vvp_fault_io *cfio    = &vio->u.fault;
895         loff_t               offset;
896         int                  result  = 0;
897         struct page          *vmpage  = NULL;
898         struct cl_page      *page;
899         loff_t               size;
900         pgoff_t              last_index;
901         ENTRY;
902
903         if (fio->ft_executable &&
904             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
905                 CWARN("binary "DFID
906                       " changed while waiting for the page fault lock\n",
907                       PFID(lu_object_fid(&obj->co_lu)));
908
909         /* offset of the last byte on the page */
910         offset = cl_offset(obj, fio->ft_index + 1) - 1;
911         LASSERT(cl_index(obj, offset) == fio->ft_index);
912         result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
913         if (result != 0)
914                 RETURN(result);
915
916         /* must return locked page */
917         if (fio->ft_mkwrite) {
918                 LASSERT(cfio->ft_vmpage != NULL);
919                 lock_page(cfio->ft_vmpage);
920         } else {
921                 result = vvp_io_kernel_fault(cfio);
922                 if (result != 0)
923                         RETURN(result);
924         }
925
926         vmpage = cfio->ft_vmpage;
927         LASSERT(PageLocked(vmpage));
928
929         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
930                 ll_invalidate_page(vmpage);
931
932         size = i_size_read(inode);
933         /* Though we have already held a cl_lock upon this page, but
934          * it still can be truncated locally. */
935         if (unlikely((vmpage->mapping != inode->i_mapping) ||
936                      (page_offset(vmpage) > size))) {
937                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
938
939                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
940                  * and retry. */
941                 GOTO(out, result = +1);
942         }
943
944         last_index = cl_index(obj, size - 1);
945
946         if (fio->ft_mkwrite ) {
947                 /*
948                  * Capture the size while holding the lli_trunc_sem from above
949                  * we want to make sure that we complete the mkwrite action
950                  * while holding this lock. We need to make sure that we are
951                  * not past the end of the file.
952                  */
953                 if (last_index < fio->ft_index) {
954                         CDEBUG(D_PAGE,
955                                 "llite: mkwrite and truncate race happened: "
956                                 "%p: 0x%lx 0x%lx\n",
957                                 vmpage->mapping,fio->ft_index,last_index);
958                         /*
959                          * We need to return if we are
960                          * passed the end of the file. This will propagate
961                          * up the call stack to ll_page_mkwrite where
962                          * we will return VM_FAULT_NOPAGE. Any non-negative
963                          * value returned here will be silently
964                          * converted to 0. If the vmpage->mapping is null
965                          * the error code would be converted back to ENODATA
966                          * in ll_page_mkwrite0. Thus we return -ENODATA
967                          * to handle both cases
968                          */
969                         GOTO(out, result = -ENODATA);
970                 }
971         }
972
973         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
974         if (IS_ERR(page))
975                 GOTO(out, result = PTR_ERR(page));
976
977         /* if page is going to be written, we should add this page into cache
978          * earlier. */
979         if (fio->ft_mkwrite) {
980                 wait_on_page_writeback(vmpage);
981                 if (!PageDirty(vmpage)) {
982                         struct cl_page_list *plist = &io->ci_queue.c2_qin;
983                         struct ccc_page *cp = cl_object_page_slice(obj, page);
984                         int to = PAGE_SIZE;
985
986                         /* vvp_page_assume() calls wait_on_page_writeback(). */
987                         cl_page_assume(env, io, page);
988
989                         cl_page_list_init(plist);
990                         cl_page_list_add(plist, page);
991
992                         /* size fixup */
993                         if (last_index == ccc_index(cp))
994                                 to = size & ~CFS_PAGE_MASK;
995
996                         /* Do not set Dirty bit here so that in case IO is
997                          * started before the page is really made dirty, we
998                          * still have chance to detect it. */
999                         result = cl_io_commit_async(env, io, plist, 0, to,
1000                                                     mkwrite_commit_callback);
1001                         LASSERT(cl_page_is_owned(page, io));
1002                         cl_page_list_fini(env, plist);
1003
1004                         vmpage = NULL;
1005                         if (result < 0) {
1006                                 cl_page_discard(env, io, page);
1007                                 cl_page_disown(env, io, page);
1008
1009                                 cl_page_put(env, page);
1010
1011                                 /* we're in big trouble, what can we do now? */
1012                                 if (result == -EDQUOT)
1013                                         result = -ENOSPC;
1014                                 GOTO(out, result);
1015                         } else
1016                                 cl_page_disown(env, io, page);
1017                 }
1018         }
1019
1020         /*
1021          * The ft_index is only used in the case of
1022          * a mkwrite action. We need to check
1023          * our assertions are correct, since
1024          * we should have caught this above
1025          */
1026         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1027         if (fio->ft_index == last_index)
1028                 /*
1029                  * Last page is mapped partially.
1030                  */
1031                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1032         else
1033                 fio->ft_nob = cl_page_size(obj);
1034
1035         lu_ref_add(&page->cp_reference, "fault", io);
1036         fio->ft_page = page;
1037         EXIT;
1038
1039 out:
1040         /* return unlocked vmpage to avoid deadlocking */
1041         if (vmpage != NULL)
1042                 unlock_page(vmpage);
1043         cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1044         return result;
1045 }
1046
1047 static int vvp_io_fsync_start(const struct lu_env *env,
1048                               const struct cl_io_slice *ios)
1049 {
1050         /* we should mark TOWRITE bit to each dirty page in radix tree to
1051          * verify pages have been written, but this is difficult because of
1052          * race. */
1053         return 0;
1054 }
1055
1056 static int vvp_io_read_page(const struct lu_env *env,
1057                             const struct cl_io_slice *ios,
1058                             const struct cl_page_slice *slice)
1059 {
1060         struct cl_io              *io     = ios->cis_io;
1061         struct ccc_page           *cp     = cl2ccc_page(slice);
1062         struct cl_page            *page   = slice->cpl_page;
1063         struct inode              *inode  = ccc_object_inode(slice->cpl_obj);
1064         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1065         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
1066         struct ll_readahead_state *ras    = &fd->fd_ras;
1067         struct cl_2queue          *queue  = &io->ci_queue;
1068
1069         ENTRY;
1070
1071         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1072             sbi->ll_ra_info.ra_max_pages > 0)
1073                 ras_update(sbi, inode, ras, ccc_index(cp),
1074                            cp->cpg_defer_uptodate);
1075
1076         if (cp->cpg_defer_uptodate) {
1077                 cp->cpg_ra_used = 1;
1078                 cl_page_export(env, page, 1);
1079         }
1080
1081         /*
1082          * Add page into the queue even when it is marked uptodate above.
1083          * this will unlock it automatically as part of cl_page_list_disown().
1084          */
1085         cl_2queue_add(queue, page);
1086         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1087             sbi->ll_ra_info.ra_max_pages > 0)
1088                 ll_readahead(env, io, &queue->c2_qin, ras,
1089                              cp->cpg_defer_uptodate);
1090
1091         RETURN(0);
1092 }
1093
1094 static const struct cl_io_operations vvp_io_ops = {
1095         .op = {
1096                 [CIT_READ] = {
1097                         .cio_fini      = vvp_io_read_fini,
1098                         .cio_lock      = vvp_io_read_lock,
1099                         .cio_start     = vvp_io_read_start,
1100                         .cio_advance   = ccc_io_advance
1101                 },
1102                 [CIT_WRITE] = {
1103                         .cio_fini      = vvp_io_fini,
1104                         .cio_iter_init = vvp_io_write_iter_init,
1105                         .cio_iter_fini = vvp_io_write_iter_fini,
1106                         .cio_lock      = vvp_io_write_lock,
1107                         .cio_start     = vvp_io_write_start,
1108                         .cio_advance   = ccc_io_advance
1109                 },
1110                 [CIT_SETATTR] = {
1111                         .cio_fini       = vvp_io_setattr_fini,
1112                         .cio_iter_init  = vvp_io_setattr_iter_init,
1113                         .cio_lock       = vvp_io_setattr_lock,
1114                         .cio_start      = vvp_io_setattr_start,
1115                         .cio_end        = vvp_io_setattr_end
1116                 },
1117                 [CIT_FAULT] = {
1118                         .cio_fini      = vvp_io_fault_fini,
1119                         .cio_iter_init = vvp_io_fault_iter_init,
1120                         .cio_lock      = vvp_io_fault_lock,
1121                         .cio_start     = vvp_io_fault_start,
1122                         .cio_end       = ccc_io_end
1123                 },
1124                 [CIT_FSYNC] = {
1125                         .cio_start  = vvp_io_fsync_start,
1126                         .cio_fini   = vvp_io_fini
1127                 },
1128                 [CIT_MISC] = {
1129                         .cio_fini   = vvp_io_fini
1130                 }
1131         },
1132         .cio_read_page     = vvp_io_read_page,
1133 };
1134
1135 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1136                 struct cl_io *io)
1137 {
1138         struct vvp_io      *vio   = vvp_env_io(env);
1139         struct ccc_io      *cio   = ccc_env_io(env);
1140         struct inode       *inode = ccc_object_inode(obj);
1141         int                 result;
1142
1143         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1144         ENTRY;
1145
1146         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1147                            "restore needed %d\n",
1148                PFID(lu_object_fid(&obj->co_lu)),
1149                io->ci_ignore_layout, io->ci_verify_layout,
1150                cio->cui_layout_gen, io->ci_restore_needed);
1151
1152         CL_IO_SLICE_CLEAN(cio, cui_cl);
1153         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1154         vio->cui_ra_window_set = 0;
1155         result = 0;
1156         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1157                 size_t count;
1158                 struct ll_inode_info *lli = ll_i2info(inode);
1159
1160                 count = io->u.ci_rw.crw_count;
1161                 /* "If nbyte is 0, read() will return 0 and have no other
1162                  *  results."  -- Single Unix Spec */
1163                 if (count == 0)
1164                         result = 1;
1165                 else {
1166                         cio->cui_tot_count = count;
1167                         cio->cui_tot_nrsegs = 0;
1168                 }
1169
1170                 /* for read/write, we store the jobid in the inode, and
1171                  * it'll be fetched by osc when building RPC.
1172                  *
1173                  * it's not accurate if the file is shared by different
1174                  * jobs.
1175                  */
1176                 lustre_get_jobid(lli->lli_jobid);
1177         } else if (io->ci_type == CIT_SETATTR) {
1178                 if (!cl_io_is_trunc(io))
1179                         io->ci_lockreq = CILR_MANDATORY;
1180         }
1181
1182         /* ignore layout change for generic CIT_MISC but not for glimpse.
1183          * io context for glimpse must set ci_verify_layout to true,
1184          * see cl_glimpse_size0() for details. */
1185         if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1186                 io->ci_ignore_layout = 1;
1187
1188         /* Enqueue layout lock and get layout version. We need to do this
1189          * even for operations requiring to open file, such as read and write,
1190          * because it might not grant layout lock in IT_OPEN. */
1191         if (result == 0 && !io->ci_ignore_layout) {
1192                 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1193                 if (result == -ENOENT)
1194                         /* If the inode on MDS has been removed, but the objects
1195                          * on OSTs haven't been destroyed (async unlink), layout
1196                          * fetch will return -ENOENT, we'd ingore this error
1197                          * and continue with dirty flush. LU-3230. */
1198                         result = 0;
1199                 if (result < 0)
1200                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1201                                 ll_get_fsname(inode->i_sb, NULL, 0),
1202                                 PFID(lu_object_fid(&obj->co_lu)), result);
1203         }
1204
1205         RETURN(result);
1206 }
1207
1208 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1209                                 const struct cl_io_slice *slice)
1210 {
1211         /* Caling just for assertion */
1212         cl2ccc_io(env, slice);
1213         return vvp_env_io(env);
1214 }