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