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