Whamcloud - gitweb
50a19c98d416e4fc823cb66a83bfcd4adff6e85d
[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, 2012, Whamcloud, Inc.
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 sendfile() / 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  *
70  * io operations.
71  *
72  */
73
74 static int vvp_io_fault_iter_init(const struct lu_env *env,
75                                   const struct cl_io_slice *ios)
76 {
77         struct vvp_io *vio   = cl2vvp_io(env, ios);
78         struct inode  *inode = ccc_object_inode(ios->cis_obj);
79
80         LASSERT(inode ==
81                 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
82         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
83         return 0;
84 }
85
86 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
87 {
88         struct cl_io     *io  = ios->cis_io;
89         struct cl_object *obj = io->ci_obj;
90         struct ccc_io    *cio = cl2ccc_io(env, ios);
91         __u32 gen;
92         int   result;
93
94         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
95
96         /* check layout version */
97         result = ll_layout_refresh(ccc_object_inode(obj), &gen);
98         if (cio->cui_layout_gen > 0)
99                 io->ci_need_restart = cio->cui_layout_gen == gen;
100 }
101
102 static void vvp_io_fault_fini(const struct lu_env *env,
103                               const struct cl_io_slice *ios)
104 {
105         struct cl_io   *io   = ios->cis_io;
106         struct cl_page *page = io->u.ci_fault.ft_page;
107
108         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
109
110         if (page != NULL) {
111                 lu_ref_del(&page->cp_reference, "fault", io);
112                 cl_page_put(env, page);
113                 io->u.ci_fault.ft_page = NULL;
114         }
115         vvp_io_fini(env, ios);
116 }
117
118 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
119 {
120         /*
121          * we only want to hold PW locks if the mmap() can generate
122          * writes back to the file and that only happens in shared
123          * writable vmas
124          */
125         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
126                 return CLM_WRITE;
127         return CLM_READ;
128 }
129
130 static int vvp_mmap_locks(const struct lu_env *env,
131                           struct ccc_io *vio, struct cl_io *io)
132 {
133         struct ccc_thread_info *cti = ccc_env_info(env);
134         struct mm_struct       *mm = current->mm;
135         struct vm_area_struct  *vma;
136         struct cl_lock_descr   *descr = &cti->cti_descr;
137         ldlm_policy_data_t      policy;
138         unsigned long           addr;
139         unsigned long           seg;
140         ssize_t                 count;
141         int                     result;
142         ENTRY;
143
144         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
145
146         if (!cl_is_normalio(env, io))
147                 RETURN(0);
148
149         if (vio->cui_iov == NULL) /* nfs or loop back device write */
150                 RETURN(0);
151
152         /* No MM (e.g. NFS)? No vmas too. */
153         if (mm == NULL)
154                 RETURN(0);
155
156         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
157                 const struct iovec *iv = &vio->cui_iov[seg];
158
159                 addr = (unsigned long)iv->iov_base;
160                 count = iv->iov_len;
161                 if (count == 0)
162                         continue;
163
164                 count += addr & (~CFS_PAGE_MASK);
165                 addr &= CFS_PAGE_MASK;
166
167                 down_read(&mm->mmap_sem);
168                 while((vma = our_vma(mm, addr, count)) != NULL) {
169                         struct inode *inode = vma->vm_file->f_dentry->d_inode;
170                         int flags = CEF_MUST;
171
172                         if (ll_file_nolock(vma->vm_file)) {
173                                 /*
174                                  * For no lock case, a lockless lock will be
175                                  * generated.
176                                  */
177                                 flags = CEF_NEVER;
178                         }
179
180                         /*
181                          * XXX: Required lock mode can be weakened: CIT_WRITE
182                          * io only ever reads user level buffer, and CIT_READ
183                          * only writes on it.
184                          */
185                         policy_from_vma(&policy, vma, addr, count);
186                         descr->cld_mode = vvp_mode_from_vma(vma);
187                         descr->cld_obj = ll_i2info(inode)->lli_clob;
188                         descr->cld_start = cl_index(descr->cld_obj,
189                                                     policy.l_extent.start);
190                         descr->cld_end = cl_index(descr->cld_obj,
191                                                   policy.l_extent.end);
192                         descr->cld_enq_flags = flags;
193                         result = cl_io_lock_alloc_add(env, io, descr);
194
195                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
196                                descr->cld_mode, descr->cld_start,
197                                descr->cld_end);
198
199                         if (result < 0)
200                                 RETURN(result);
201
202                         if (vma->vm_end - addr >= count)
203                                 break;
204
205                         count -= vma->vm_end - addr;
206                         addr = vma->vm_end;
207                 }
208                 up_read(&mm->mmap_sem);
209         }
210         RETURN(0);
211 }
212
213 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
214                           enum cl_lock_mode mode, loff_t start, loff_t end)
215 {
216         struct ccc_io *cio = ccc_env_io(env);
217         int result;
218         int ast_flags = 0;
219
220         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
221         ENTRY;
222
223         ccc_io_update_iov(env, cio, io);
224
225         if (io->u.ci_rw.crw_nonblock)
226                 ast_flags |= CEF_NONBLOCK;
227         result = vvp_mmap_locks(env, cio, io);
228         if (result == 0)
229                 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
230         RETURN(result);
231 }
232
233 static int vvp_io_read_lock(const struct lu_env *env,
234                             const struct cl_io_slice *ios)
235 {
236         struct cl_io         *io  = ios->cis_io;
237         struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
238         int result;
239
240         ENTRY;
241         /* XXX: Layer violation, we shouldn't see lsm at llite level. */
242         if (lli->lli_has_smd) /* lsm-less file doesn't need to lock */
243                 result = vvp_io_rw_lock(env, io, CLM_READ,
244                                         io->u.ci_rd.rd.crw_pos,
245                                         io->u.ci_rd.rd.crw_pos +
246                                         io->u.ci_rd.rd.crw_count - 1);
247         else
248                 result = 0;
249         RETURN(result);
250 }
251
252 static int vvp_io_fault_lock(const struct lu_env *env,
253                              const struct cl_io_slice *ios)
254 {
255         struct cl_io *io   = ios->cis_io;
256         struct vvp_io *vio = cl2vvp_io(env, ios);
257         /*
258          * XXX LDLM_FL_CBPENDING
259          */
260         return ccc_io_one_lock_index
261                 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
262                  io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
263 }
264
265 static int vvp_io_write_lock(const struct lu_env *env,
266                              const struct cl_io_slice *ios)
267 {
268         struct cl_io *io = ios->cis_io;
269         loff_t start;
270         loff_t end;
271
272         if (io->u.ci_wr.wr_append) {
273                 start = 0;
274                 end   = OBD_OBJECT_EOF;
275         } else {
276                 start = io->u.ci_wr.wr.crw_pos;
277                 end   = start + io->u.ci_wr.wr.crw_count - 1;
278         }
279         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
280 }
281
282 static int vvp_io_setattr_iter_init(const struct lu_env *env,
283                                     const struct cl_io_slice *ios)
284 {
285         struct ccc_io *cio   = ccc_env_io(env);
286         struct inode  *inode = ccc_object_inode(ios->cis_obj);
287
288         /*
289          * We really need to get our PW lock before we change inode->i_size.
290          * If we don't we can race with other i_size updaters on our node,
291          * like ll_file_read.  We can also race with i_size propogation to
292          * other nodes through dirtying and writeback of final cached pages.
293          * This last one is especially bad for racing o_append users on other
294          * nodes.
295          */
296         mutex_unlock(&inode->i_mutex);
297         if (cl_io_is_trunc(ios->cis_io))
298                 UP_WRITE_I_ALLOC_SEM(inode);
299         cio->u.setattr.cui_locks_released = 1;
300         return 0;
301 }
302
303 /**
304  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
305  *
306  * Handles "lockless io" mode when extent locking is done by server.
307  */
308 static int vvp_io_setattr_lock(const struct lu_env *env,
309                                const struct cl_io_slice *ios)
310 {
311         struct ccc_io      *cio       = ccc_env_io(env);
312         struct cl_io       *io        = ios->cis_io;
313         size_t              new_size;
314         __u32               enqflags = 0;
315
316         if (cl_io_is_trunc(io)) {
317                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
318                 if (new_size == 0)
319                         enqflags = CEF_DISCARD_DATA;
320         } else {
321                 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
322                      io->u.ci_setattr.sa_attr.lvb_ctime) ||
323                     (io->u.ci_setattr.sa_attr.lvb_atime >=
324                      io->u.ci_setattr.sa_attr.lvb_ctime))
325                         return 0;
326                 new_size = 0;
327         }
328         cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
329         return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
330                                new_size, OBD_OBJECT_EOF);
331 }
332
333 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
334 {
335         int     result;
336         /*
337          * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
338          * is grabbed by ll_truncate() only over call to obd_adjust_kms().
339          */
340         ll_inode_size_lock(inode);
341         result = vmtruncate(inode, size);
342         ll_inode_size_unlock(inode);
343
344         return result;
345 }
346
347 static int vvp_io_setattr_trunc(const struct lu_env *env,
348                                 const struct cl_io_slice *ios,
349                                 struct inode *inode, loff_t size)
350 {
351         DOWN_WRITE_I_ALLOC_SEM(inode);
352         return 0;
353 }
354
355 static int vvp_io_setattr_time(const struct lu_env *env,
356                                const struct cl_io_slice *ios)
357 {
358         struct cl_io       *io    = ios->cis_io;
359         struct cl_object   *obj   = io->ci_obj;
360         struct cl_attr     *attr  = ccc_env_thread_attr(env);
361         int result;
362         unsigned valid = CAT_CTIME;
363
364         cl_object_attr_lock(obj);
365         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
366         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
367                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
368                 valid |= CAT_ATIME;
369         }
370         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
371                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
372                 valid |= CAT_MTIME;
373         }
374         result = cl_object_attr_set(env, obj, attr, valid);
375         cl_object_attr_unlock(obj);
376
377         return result;
378 }
379
380 static int vvp_io_setattr_start(const struct lu_env *env,
381                                 const struct cl_io_slice *ios)
382 {
383         struct ccc_io   *cio   = cl2ccc_io(env, ios);
384         struct cl_io    *io    = ios->cis_io;
385         struct inode    *inode = ccc_object_inode(io->ci_obj);
386
387         LASSERT(cio->u.setattr.cui_locks_released);
388
389         mutex_lock(&inode->i_mutex);
390         cio->u.setattr.cui_locks_released = 0;
391
392         if (cl_io_is_trunc(io))
393                 return vvp_io_setattr_trunc(env, ios, inode,
394                                             io->u.ci_setattr.sa_attr.lvb_size);
395         else
396                 return vvp_io_setattr_time(env, ios);
397 }
398
399 static void vvp_io_setattr_end(const struct lu_env *env,
400                                const struct cl_io_slice *ios)
401 {
402         struct cl_io         *io    = ios->cis_io;
403         struct inode         *inode = ccc_object_inode(io->ci_obj);
404
405         if (!cl_io_is_trunc(io))
406                 return;
407
408         /* Truncate in memory pages - they must be clean pages because osc
409          * has already notified to destroy osc_extents. */
410         vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
411 }
412
413 static void vvp_io_setattr_fini(const struct lu_env *env,
414                                 const struct cl_io_slice *ios)
415 {
416         struct ccc_io *cio   = ccc_env_io(env);
417         struct cl_io  *io    = ios->cis_io;
418         struct inode  *inode = ccc_object_inode(ios->cis_io->ci_obj);
419
420         if (cio->u.setattr.cui_locks_released) {
421                 mutex_lock(&inode->i_mutex);
422                 if (cl_io_is_trunc(io))
423                         DOWN_WRITE_I_ALLOC_SEM(inode);
424                 cio->u.setattr.cui_locks_released = 0;
425         }
426         vvp_io_fini(env, ios);
427 }
428
429 #ifdef HAVE_FILE_READV
430 static ssize_t lustre_generic_file_read(struct file *file,
431                                         struct ccc_io *vio, loff_t *ppos)
432 {
433         return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
434 }
435
436 static ssize_t lustre_generic_file_write(struct file *file,
437                                          struct ccc_io *vio, loff_t *ppos)
438 {
439         return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
440 }
441 #else
442 static ssize_t lustre_generic_file_read(struct file *file,
443                                         struct ccc_io *vio, loff_t *ppos)
444 {
445         return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
446                                      vio->cui_nrsegs, *ppos);
447 }
448
449 static ssize_t lustre_generic_file_write(struct file *file,
450                                         struct ccc_io *vio, loff_t *ppos)
451 {
452         return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
453                                       vio->cui_nrsegs, *ppos);
454 }
455 #endif
456
457 static int vvp_io_read_start(const struct lu_env *env,
458                              const struct cl_io_slice *ios)
459 {
460         struct vvp_io     *vio   = cl2vvp_io(env, ios);
461         struct ccc_io     *cio   = cl2ccc_io(env, ios);
462         struct cl_io      *io    = ios->cis_io;
463         struct cl_object  *obj   = io->ci_obj;
464         struct inode      *inode = ccc_object_inode(obj);
465         struct ll_ra_read *bead  = &vio->cui_bead;
466         struct file       *file  = cio->cui_fd->fd_file;
467
468         int     result;
469         loff_t  pos = io->u.ci_rd.rd.crw_pos;
470         long    cnt = io->u.ci_rd.rd.crw_count;
471         long    tot = cio->cui_tot_count;
472         int     exceed = 0;
473
474         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
475
476         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
477
478         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
479         if (result != 0)
480                 return result;
481         else if (exceed != 0)
482                 goto out;
483
484         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
485                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
486                         inode->i_ino, cnt, pos, i_size_read(inode));
487
488         /* turn off the kernel's read-ahead */
489         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
490
491         /* initialize read-ahead window once per syscall */
492         if (!vio->cui_ra_window_set) {
493                 vio->cui_ra_window_set = 1;
494                 bead->lrr_start = cl_index(obj, pos);
495                 /*
496                  * XXX: explicit CFS_PAGE_SIZE
497                  */
498                 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
499                 ll_ra_read_in(file, bead);
500         }
501
502         /* BUG: 5972 */
503         file_accessed(file);
504         switch (vio->cui_io_subtype) {
505         case IO_NORMAL:
506                  result = lustre_generic_file_read(file, cio, &pos);
507                  break;
508 #ifdef HAVE_KERNEL_SENDFILE
509         case IO_SENDFILE:
510                 result = generic_file_sendfile(file, &pos, cnt,
511                                 vio->u.sendfile.cui_actor,
512                                 vio->u.sendfile.cui_target);
513                 break;
514 #endif
515 #ifdef HAVE_KERNEL_SPLICE_READ
516         case IO_SPLICE:
517                 result = generic_file_splice_read(file, &pos,
518                                 vio->u.splice.cui_pipe, cnt,
519                                 vio->u.splice.cui_flags);
520                 /* LU-1109: do splice read stripe by stripe otherwise if it
521                  * may make nfsd stuck if this read occupied all internal pipe
522                  * buffers. */
523                 io->ci_continue = 0;
524                 break;
525 #endif
526         default:
527                 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
528                 LBUG();
529         }
530
531 out:
532         if (result >= 0) {
533                 if (result < cnt)
534                         io->ci_continue = 0;
535                 io->ci_nob += result;
536                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
537                                   cio->cui_fd, pos, result, 0);
538                 result = 0;
539         }
540         return result;
541 }
542
543 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
544 {
545         struct vvp_io *vio = cl2vvp_io(env, ios);
546         struct ccc_io *cio = cl2ccc_io(env, ios);
547
548         if (vio->cui_ra_window_set)
549                 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
550
551         vvp_io_fini(env, ios);
552 }
553
554 static int vvp_io_write_start(const struct lu_env *env,
555                               const struct cl_io_slice *ios)
556 {
557         struct ccc_io      *cio   = cl2ccc_io(env, ios);
558         struct cl_io       *io    = ios->cis_io;
559         struct cl_object   *obj   = io->ci_obj;
560         struct inode       *inode = ccc_object_inode(obj);
561         struct file        *file  = cio->cui_fd->fd_file;
562         ssize_t result = 0;
563         loff_t pos = io->u.ci_wr.wr.crw_pos;
564         size_t cnt = io->u.ci_wr.wr.crw_count;
565
566         ENTRY;
567
568         if (cl_io_is_append(io)) {
569                 /*
570                  * PARALLEL IO This has to be changed for parallel IO doing
571                  * out-of-order writes.
572                  */
573                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
574 #ifndef HAVE_FILE_WRITEV
575                 cio->cui_iocb->ki_pos = pos;
576 #endif
577         }
578
579         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
580
581         if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
582                 result = 0;
583         else
584                 result = lustre_generic_file_write(file, cio, &pos);
585
586         if (result > 0) {
587                 if (result < cnt)
588                         io->ci_continue = 0;
589                 io->ci_nob += result;
590                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
591                                   cio->cui_fd, pos, result, 0);
592                 result = 0;
593         }
594         RETURN(result);
595 }
596
597 #ifndef HAVE_VM_OP_FAULT
598 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
599 {
600         cfs_page_t *vmpage;
601
602         vmpage = filemap_nopage(cfio->ft_vma, cfio->nopage.ft_address,
603                                 cfio->nopage.ft_type);
604
605         if (vmpage == NOPAGE_SIGBUS) {
606                 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
607                        cfio->nopage.ft_address,(long)cfio->nopage.ft_type);
608                 return -EFAULT;
609         } else if (vmpage == NOPAGE_OOM) {
610                 CDEBUG(D_PAGE, "got addr %lu type %lx - OOM\n",
611                        cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
612                 return -ENOMEM;
613         }
614
615         LL_CDEBUG_PAGE(D_PAGE, vmpage, "got addr %lu type %lx\n",
616                        cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
617
618         cfio->ft_vmpage = vmpage;
619         lock_page(vmpage);
620
621         return 0;
622 }
623 #else
624 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
625 {
626         struct vm_fault *vmf = cfio->fault.ft_vmf;
627
628         cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
629
630         if (vmf->page) {
631                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
632                                vmf->virtual_address);
633                 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
634                         lock_page(vmf->page);
635                         cfio->fault.ft_flags &= VM_FAULT_LOCKED;
636                 }
637
638                 cfio->ft_vmpage = vmf->page;
639                 return 0;
640         }
641
642         if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
643                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
644                 return -EFAULT;
645         }
646
647         if (cfio->fault.ft_flags & VM_FAULT_OOM) {
648                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
649                 return -ENOMEM;
650         }
651
652         if (cfio->fault.ft_flags & VM_FAULT_RETRY)
653                 return -EAGAIN;
654
655         CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
656         return -EINVAL;
657 }
658
659 #endif
660
661 static int vvp_io_fault_start(const struct lu_env *env,
662                               const struct cl_io_slice *ios)
663 {
664         struct vvp_io       *vio     = cl2vvp_io(env, ios);
665         struct cl_io        *io      = ios->cis_io;
666         struct cl_object    *obj     = io->ci_obj;
667         struct inode        *inode   = ccc_object_inode(obj);
668         struct cl_fault_io  *fio     = &io->u.ci_fault;
669         struct vvp_fault_io *cfio    = &vio->u.fault;
670         loff_t               offset;
671         int                  result  = 0;
672         cfs_page_t          *vmpage  = NULL;
673         struct cl_page      *page;
674         loff_t               size;
675         pgoff_t              last; /* last page in a file data region */
676
677         if (fio->ft_executable &&
678             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
679                 CWARN("binary "DFID
680                       " changed while waiting for the page fault lock\n",
681                       PFID(lu_object_fid(&obj->co_lu)));
682
683         /* offset of the last byte on the page */
684         offset = cl_offset(obj, fio->ft_index + 1) - 1;
685         LASSERT(cl_index(obj, offset) == fio->ft_index);
686         result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
687         if (result != 0)
688                 return result;
689
690         /* must return locked page */
691         if (fio->ft_mkwrite) {
692                 /* we grab alloc_sem to exclude truncate case.
693                  * Otherwise, we could add dirty pages into osc cache
694                  * while truncate is on-going. */
695                 DOWN_READ_I_ALLOC_SEM(inode);
696
697                 LASSERT(cfio->ft_vmpage != NULL);
698                 lock_page(cfio->ft_vmpage);
699         } else {
700                 result = vvp_io_kernel_fault(cfio);
701                 if (result != 0)
702                         return result;
703         }
704
705         vmpage = cfio->ft_vmpage;
706         LASSERT(PageLocked(vmpage));
707
708         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
709                 ll_invalidate_page(vmpage);
710
711         /* Though we have already held a cl_lock upon this page, but
712          * it still can be truncated locally. */
713         if (unlikely(vmpage->mapping == NULL)) {
714                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
715
716                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
717                  * and retry. */
718                 GOTO(out, result = +1);
719         }
720
721         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
722         if (IS_ERR(page))
723                 GOTO(out, result = PTR_ERR(page));
724
725         /* if page is going to be written, we should add this page into cache
726          * earlier. */
727         if (fio->ft_mkwrite) {
728                 wait_on_page_writeback(vmpage);
729                 if (set_page_dirty(vmpage)) {
730                         struct ccc_page *cp;
731
732                         /* vvp_page_assume() calls wait_on_page_writeback(). */
733                         cl_page_assume(env, io, page);
734
735                         cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
736                         vvp_write_pending(cl2ccc(obj), cp);
737
738                         /* Do not set Dirty bit here so that in case IO is
739                          * started before the page is really made dirty, we
740                          * still have chance to detect it. */
741                         result = cl_page_cache_add(env, io, page, CRT_WRITE);
742                         LASSERT(cl_page_is_owned(page, io));
743
744                         vmpage = NULL;
745                         if (result < 0) {
746                                 cl_page_unmap(env, io, page);
747                                 cl_page_discard(env, io, page);
748                                 cl_page_disown(env, io, page);
749
750                                 cl_page_put(env, page);
751
752                                 /* we're in big trouble, what can we do now? */
753                                 if (result == -EDQUOT)
754                                         result = -ENOSPC;
755                                 GOTO(out, result);
756                         } else
757                                 cl_page_disown(env, io, page);
758                 }
759         }
760
761         size = i_size_read(inode);
762         last = cl_index(obj, size - 1);
763         LASSERT(fio->ft_index <= last);
764         if (fio->ft_index == last)
765                 /*
766                  * Last page is mapped partially.
767                  */
768                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
769         else
770                 fio->ft_nob = cl_page_size(obj);
771
772         lu_ref_add(&page->cp_reference, "fault", io);
773         fio->ft_page = page;
774         EXIT;
775
776 out:
777         /* return unlocked vmpage to avoid deadlocking */
778         if (vmpage != NULL)
779                 unlock_page(vmpage);
780         if (fio->ft_mkwrite)
781                 UP_READ_I_ALLOC_SEM(inode);
782 #ifdef HAVE_VM_OP_FAULT
783         cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
784 #endif
785         return result;
786 }
787
788 static int vvp_io_fsync_start(const struct lu_env *env,
789                               const struct cl_io_slice *ios)
790 {
791         /* we should mark TOWRITE bit to each dirty page in radix tree to
792          * verify pages have been written, but this is difficult because of
793          * race. */
794         return 0;
795 }
796
797 static int vvp_io_read_page(const struct lu_env *env,
798                             const struct cl_io_slice *ios,
799                             const struct cl_page_slice *slice)
800 {
801         struct cl_io              *io     = ios->cis_io;
802         struct cl_object          *obj    = slice->cpl_obj;
803         struct ccc_page           *cp     = cl2ccc_page(slice);
804         struct cl_page            *page   = slice->cpl_page;
805         struct inode              *inode  = ccc_object_inode(obj);
806         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
807         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
808         struct ll_readahead_state *ras    = &fd->fd_ras;
809         cfs_page_t                *vmpage = cp->cpg_page;
810         struct cl_2queue          *queue  = &io->ci_queue;
811         int rc;
812
813         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
814         LASSERT(slice->cpl_obj == obj);
815
816         ENTRY;
817
818         if (sbi->ll_ra_info.ra_max_pages_per_file &&
819             sbi->ll_ra_info.ra_max_pages)
820                 ras_update(sbi, inode, ras, page->cp_index,
821                            cp->cpg_defer_uptodate);
822
823         /* Sanity check whether the page is protected by a lock. */
824         rc = cl_page_is_under_lock(env, io, page);
825         if (rc != -EBUSY) {
826                 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
827                                rc == -ENODATA ? "without a lock" :
828                                "match failed", rc);
829                 if (rc != -ENODATA)
830                         RETURN(rc);
831         }
832
833         if (cp->cpg_defer_uptodate) {
834                 cp->cpg_ra_used = 1;
835                 cl_page_export(env, page, 1);
836         }
837         /*
838          * Add page into the queue even when it is marked uptodate above.
839          * this will unlock it automatically as part of cl_page_list_disown().
840          */
841         cl_2queue_add(queue, page);
842         if (sbi->ll_ra_info.ra_max_pages_per_file &&
843             sbi->ll_ra_info.ra_max_pages)
844                 ll_readahead(env, io, ras,
845                              vmpage->mapping, &queue->c2_qin, fd->fd_flags);
846
847         RETURN(0);
848 }
849
850 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
851                             struct cl_page *page, struct ccc_page *cp,
852                             enum cl_req_type crt)
853 {
854         struct cl_2queue  *queue;
855         int result;
856
857         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
858
859         queue = &io->ci_queue;
860         cl_2queue_init_page(queue, page);
861
862         result = cl_io_submit_sync(env, io, crt, queue, 0);
863         LASSERT(cl_page_is_owned(page, io));
864
865         if (crt == CRT_READ)
866                 /*
867                  * in CRT_WRITE case page is left locked even in case of
868                  * error.
869                  */
870                 cl_page_list_disown(env, io, &queue->c2_qin);
871         cl_2queue_fini(env, queue);
872
873         return result;
874 }
875
876 /**
877  * Prepare partially written-to page for a write.
878  */
879 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
880                                   struct cl_object *obj, struct cl_page *pg,
881                                   struct ccc_page *cp,
882                                   unsigned from, unsigned to)
883 {
884         struct cl_attr *attr   = ccc_env_thread_attr(env);
885         loff_t          offset = cl_offset(obj, pg->cp_index);
886         int             result;
887
888         cl_object_attr_lock(obj);
889         result = cl_object_attr_get(env, obj, attr);
890         cl_object_attr_unlock(obj);
891         if (result == 0) {
892                 /*
893                  * If are writing to a new page, no need to read old data.
894                  * The extent locking will have updated the KMS, and for our
895                  * purposes here we can treat it like i_size.
896                  */
897                 if (attr->cat_kms <= offset) {
898                         char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
899
900                         memset(kaddr, 0, cl_page_size(obj));
901                         kunmap_atomic(kaddr, KM_USER0);
902                 } else if (cp->cpg_defer_uptodate)
903                         cp->cpg_ra_used = 1;
904                 else
905                         result = vvp_page_sync_io(env, io, pg, cp, CRT_READ);
906                 /*
907                  * In older implementations, obdo_refresh_inode is called here
908                  * to update the inode because the write might modify the
909                  * object info at OST. However, this has been proven useless,
910                  * since LVB functions will be called when user space program
911                  * tries to retrieve inode attribute.  Also, see bug 15909 for
912                  * details. -jay
913                  */
914                 if (result == 0)
915                         cl_page_export(env, pg, 1);
916         }
917         return result;
918 }
919
920 static int vvp_io_prepare_write(const struct lu_env *env,
921                                 const struct cl_io_slice *ios,
922                                 const struct cl_page_slice *slice,
923                                 unsigned from, unsigned to)
924 {
925         struct cl_object *obj    = slice->cpl_obj;
926         struct ccc_page  *cp     = cl2ccc_page(slice);
927         struct cl_page   *pg     = slice->cpl_page;
928         cfs_page_t       *vmpage = cp->cpg_page;
929
930         int result;
931
932         ENTRY;
933
934         LINVRNT(cl_page_is_vmlocked(env, pg));
935         LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
936
937         result = 0;
938
939         CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
940         if (!PageUptodate(vmpage)) {
941                 /*
942                  * We're completely overwriting an existing page, so _don't_
943                  * set it up to date until commit_write
944                  */
945                 if (from == 0 && to == CFS_PAGE_SIZE) {
946                         CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
947                         POISON_PAGE(page, 0x11);
948                 } else
949                         result = vvp_io_prepare_partial(env, ios->cis_io, obj,
950                                                         pg, cp, from, to);
951         } else
952                 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
953         RETURN(result);
954 }
955
956 static int vvp_io_commit_write(const struct lu_env *env,
957                                const struct cl_io_slice *ios,
958                                const struct cl_page_slice *slice,
959                                unsigned from, unsigned to)
960 {
961         struct cl_object  *obj    = slice->cpl_obj;
962         struct cl_io      *io     = ios->cis_io;
963         struct ccc_page   *cp     = cl2ccc_page(slice);
964         struct cl_page    *pg     = slice->cpl_page;
965         struct inode      *inode  = ccc_object_inode(obj);
966         struct ll_sb_info *sbi    = ll_i2sbi(inode);
967         cfs_page_t        *vmpage = cp->cpg_page;
968
969         int    result;
970         int    tallyop;
971         loff_t size;
972
973         ENTRY;
974
975         LINVRNT(cl_page_is_vmlocked(env, pg));
976         LASSERT(vmpage->mapping->host == inode);
977
978         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
979         CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
980
981         /*
982          * queue a write for some time in the future the first time we
983          * dirty the page.
984          *
985          * This is different from what other file systems do: they usually
986          * just mark page (and some of its buffers) dirty and rely on
987          * balance_dirty_pages() to start a write-back. Lustre wants write-back
988          * to be started earlier for the following reasons:
989          *
990          *     (1) with a large number of clients we need to limit the amount
991          *     of cached data on the clients a lot;
992          *
993          *     (2) large compute jobs generally want compute-only then io-only
994          *     and the IO should complete as quickly as possible;
995          *
996          *     (3) IO is batched up to the RPC size and is async until the
997          *     client max cache is hit
998          *     (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
999          *
1000          */
1001         if (!PageDirty(vmpage)) {
1002                 tallyop = LPROC_LL_DIRTY_MISSES;
1003                 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
1004                 if (result == 0) {
1005                         /* page was added into cache successfully. */
1006                         set_page_dirty(vmpage);
1007                         vvp_write_pending(cl2ccc(obj), cp);
1008                 } else if (result == -EDQUOT) {
1009                         pgoff_t last_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
1010                         bool need_clip = true;
1011
1012                         /*
1013                          * Client ran out of disk space grant. Possible
1014                          * strategies are:
1015                          *
1016                          *     (a) do a sync write, renewing grant;
1017                          *
1018                          *     (b) stop writing on this stripe, switch to the
1019                          *     next one.
1020                          *
1021                          * (b) is a part of "parallel io" design that is the
1022                          * ultimate goal. (a) is what "old" client did, and
1023                          * what the new code continues to do for the time
1024                          * being.
1025                          */
1026                         if (last_index > pg->cp_index) {
1027                                 to = CFS_PAGE_SIZE;
1028                                 need_clip = false;
1029                         } else if (last_index == pg->cp_index) {
1030                                 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
1031                                 if (to < size_to)
1032                                         to = size_to;
1033                         }
1034                         if (need_clip)
1035                                 cl_page_clip(env, pg, 0, to);
1036                         result = vvp_page_sync_io(env, io, pg, cp, CRT_WRITE);
1037                         if (result)
1038                                 CERROR("Write page %lu of inode %p failed %d\n",
1039                                        pg->cp_index, inode, result);
1040                 }
1041         } else {
1042                 tallyop = LPROC_LL_DIRTY_HITS;
1043                 result = 0;
1044         }
1045         ll_stats_ops_tally(sbi, tallyop, 1);
1046
1047         size = cl_offset(obj, pg->cp_index) + to;
1048
1049         ll_inode_size_lock(inode);
1050         if (result == 0) {
1051                 if (size > i_size_read(inode)) {
1052                         cl_isize_write_nolock(inode, size);
1053                         CDEBUG(D_VFSTRACE, DFID" updating i_size %lu\n",
1054                                PFID(lu_object_fid(&obj->co_lu)),
1055                                (unsigned long)size);
1056                 }
1057                 cl_page_export(env, pg, 1);
1058         } else {
1059                 if (size > i_size_read(inode))
1060                         cl_page_discard(env, io, pg);
1061         }
1062         ll_inode_size_unlock(inode);
1063         RETURN(result);
1064 }
1065
1066 static const struct cl_io_operations vvp_io_ops = {
1067         .op = {
1068                 [CIT_READ] = {
1069                         .cio_fini      = vvp_io_read_fini,
1070                         .cio_lock      = vvp_io_read_lock,
1071                         .cio_start     = vvp_io_read_start,
1072                         .cio_advance   = ccc_io_advance
1073                 },
1074                 [CIT_WRITE] = {
1075                         .cio_fini      = vvp_io_fini,
1076                         .cio_lock      = vvp_io_write_lock,
1077                         .cio_start     = vvp_io_write_start,
1078                         .cio_advance   = ccc_io_advance
1079                 },
1080                 [CIT_SETATTR] = {
1081                         .cio_fini       = vvp_io_setattr_fini,
1082                         .cio_iter_init  = vvp_io_setattr_iter_init,
1083                         .cio_lock       = vvp_io_setattr_lock,
1084                         .cio_start      = vvp_io_setattr_start,
1085                         .cio_end        = vvp_io_setattr_end
1086                 },
1087                 [CIT_FAULT] = {
1088                         .cio_fini      = vvp_io_fault_fini,
1089                         .cio_iter_init = vvp_io_fault_iter_init,
1090                         .cio_lock      = vvp_io_fault_lock,
1091                         .cio_start     = vvp_io_fault_start,
1092                         .cio_end       = ccc_io_end
1093                 },
1094                 [CIT_FSYNC] = {
1095                         .cio_start  = vvp_io_fsync_start,
1096                         .cio_fini   = vvp_io_fini
1097                 },
1098                 [CIT_MISC] = {
1099                         .cio_fini   = vvp_io_fini
1100                 }
1101         },
1102         .cio_read_page     = vvp_io_read_page,
1103         .cio_prepare_write = vvp_io_prepare_write,
1104         .cio_commit_write  = vvp_io_commit_write
1105 };
1106
1107 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1108                 struct cl_io *io)
1109 {
1110         struct vvp_io      *vio   = vvp_env_io(env);
1111         struct ccc_io      *cio   = ccc_env_io(env);
1112         struct inode       *inode = ccc_object_inode(obj);
1113         int                 result;
1114
1115         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1116         ENTRY;
1117
1118         CL_IO_SLICE_CLEAN(cio, cui_cl);
1119         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1120         vio->cui_ra_window_set = 0;
1121         result = 0;
1122         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1123                 size_t count;
1124                 struct ll_inode_info *lli = ll_i2info(inode);
1125
1126                 count = io->u.ci_rw.crw_count;
1127                 /* "If nbyte is 0, read() will return 0 and have no other
1128                  *  results."  -- Single Unix Spec */
1129                 if (count == 0)
1130                         result = 1;
1131                 else {
1132                         cio->cui_tot_count = count;
1133                         cio->cui_tot_nrsegs = 0;
1134                 }
1135                 /* for read/write, we store the jobid in the inode, and
1136                  * it'll be fetched by osc when building RPC.
1137                  *
1138                  * it's not accurate if the file is shared by different
1139                  * jobs.
1140                  */
1141                 lustre_get_jobid(lli->lli_jobid);
1142         } else if (io->ci_type == CIT_SETATTR) {
1143                 if (!cl_io_is_trunc(io))
1144                         io->ci_lockreq = CILR_MANDATORY;
1145         }
1146
1147         /* Enqueue layout lock and get layout version. We need to do this
1148          * even for operations requiring to open file, such as read and write,
1149          * because it might not grant layout lock in IT_OPEN. */
1150         if (result == 0 && !io->ci_ignore_layout)
1151                 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1152
1153         RETURN(result);
1154 }
1155
1156 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1157                                 const struct cl_io_slice *slice)
1158 {
1159         /* Caling just for assertion */
1160         cl2ccc_io(env, slice);
1161         return vvp_env_io(env);
1162 }
1163