Whamcloud - gitweb
vvp: always write page from the beginning: sptlrpc depends on kiov_offset being 0.
[fs/lustre-release.git] / lustre / llite / vvp_io.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #ifndef __KERNEL__
44 # error This file is kernel only.
45 #endif
46
47 #include <obd.h>
48 #include <lustre_lite.h>
49
50 #include "vvp_internal.h"
51
52 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
53                                 const struct cl_io_slice *slice);
54
55 /*****************************************************************************
56  *
57  * io operations.
58  *
59  */
60
61 static int vvp_io_fault_iter_init(const struct lu_env *env,
62                                   const struct cl_io_slice *ios)
63 {
64         struct vvp_io *vio   = cl2vvp_io(env, ios);
65         struct inode  *inode = ccc_object_inode(ios->cis_obj);
66
67         LASSERT(inode ==
68                 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
69         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
70         return 0;
71 }
72
73 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
74 {
75         struct cl_io     *io  = ios->cis_io;
76         struct cl_object *obj = io->ci_obj;
77
78         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
79         if (io->ci_type == CIT_WRITE)
80                 up(&ll_i2info(ccc_object_inode(obj))->lli_write_sem);
81         else {
82                 struct vvp_io     *vio  = cl2vvp_io(env, ios);
83                 struct ccc_io     *cio  = cl2ccc_io(env, ios);
84
85                 if (vio->cui_ra_window_set)
86                         ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
87         }
88
89 }
90
91 static void vvp_io_fault_fini(const struct lu_env *env,
92                               const struct cl_io_slice *ios)
93 {
94         struct cl_io   *io   = ios->cis_io;
95         struct cl_page *page = io->u.ci_fault.ft_page;
96
97         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
98
99         if (page != NULL) {
100                 lu_ref_del(&page->cp_reference, "fault", io);
101                 cl_page_put(env, page);
102                 io->u.ci_fault.ft_page = NULL;
103         }
104         vvp_io_fini(env, ios);
105 }
106
107 enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
108 {
109         /*
110          * we only want to hold PW locks if the mmap() can generate
111          * writes back to the file and that only happens in shared
112          * writable vmas
113          */
114         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
115                 return CLM_WRITE;
116         return CLM_READ;
117 }
118
119 static int vvp_mmap_locks(const struct lu_env *env,
120                           struct ccc_io *vio, struct cl_io *io)
121 {
122         struct ccc_thread_info *cti = ccc_env_info(env);
123         struct vm_area_struct  *vma;
124         struct cl_lock_descr   *descr = &cti->cti_descr;
125         ldlm_policy_data_t      policy;
126         struct inode           *inode;
127         unsigned long           addr;
128         unsigned long           seg;
129         ssize_t                 count;
130         int                     result;
131         ENTRY;
132
133         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
134
135         if (cl_io_is_sendfile(io))
136                 RETURN(0);
137
138         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
139                 const struct iovec *iv = &vio->cui_iov[seg];
140
141                 addr = (unsigned long)iv->iov_base;
142                 count = iv->iov_len;
143                 if (count == 0)
144                         continue;
145
146                 count += addr & (~CFS_PAGE_MASK);
147                 addr &= CFS_PAGE_MASK;
148                 while((vma = our_vma(addr, count)) != NULL) {
149                         LASSERT(vma->vm_file);
150
151                         inode = vma->vm_file->f_dentry->d_inode;
152                         /*
153                          * XXX: Required lock mode can be weakened: CIT_WRITE
154                          * io only ever reads user level buffer, and CIT_READ
155                          * only writes on it.
156                          */
157                         policy_from_vma(&policy, vma, addr, count);
158                         descr->cld_mode = vvp_mode_from_vma(vma);
159                         descr->cld_obj = ll_i2info(inode)->lli_clob;
160                         descr->cld_start = cl_index(descr->cld_obj,
161                                                     policy.l_extent.start);
162                         descr->cld_end = cl_index(descr->cld_obj,
163                                                   policy.l_extent.end);
164                         result = cl_io_lock_alloc_add(env, io, descr);
165                         if (result < 0)
166                                 RETURN(result);
167                         if (vma->vm_end - addr >= count)
168                                 break;
169                         count -= vma->vm_end - addr;
170                         addr = vma->vm_end;
171                 }
172         }
173         RETURN(0);
174 }
175
176 static void vvp_io_update_iov(const struct lu_env *env,
177                               struct ccc_io *vio, struct cl_io *io)
178 {
179         int i;
180         size_t size = io->u.ci_rw.crw_count;
181
182         vio->cui_iov_olen = 0;
183         if (cl_io_is_sendfile(io) || size == vio->cui_tot_count)
184                 return;
185
186         if (vio->cui_tot_nrsegs == 0)
187                 vio->cui_tot_nrsegs =  vio->cui_nrsegs;
188
189         for (i = 0; i < vio->cui_tot_nrsegs; i++) {
190                 struct iovec *iv = &vio->cui_iov[i];
191
192                 if (iv->iov_len < size)
193                         size -= iv->iov_len;
194                 else {
195                         if (iv->iov_len > size) {
196                                 vio->cui_iov_olen = iv->iov_len;
197                                 iv->iov_len = size;
198                         }
199                         break;
200                 }
201         }
202
203         vio->cui_nrsegs = i + 1;
204 }
205
206 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
207                           enum cl_lock_mode mode, loff_t start, loff_t end)
208 {
209         struct ccc_io *cio = ccc_env_io(env);
210         int result;
211         int ast_flags = 0;
212
213         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
214         LASSERT(vvp_env_io(env)->cui_oneshot == 0);
215         ENTRY;
216
217         vvp_io_update_iov(env, cio, io);
218
219         if (io->u.ci_rw.crw_nonblock)
220                 ast_flags |= CEF_NONBLOCK;
221         result = vvp_mmap_locks(env, cio, io);
222         if (result == 0)
223                 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
224         RETURN(result);
225 }
226
227 static int vvp_io_read_lock(const struct lu_env *env,
228                             const struct cl_io_slice *ios)
229 {
230         struct cl_io         *io  = ios->cis_io;
231         struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
232         int result;
233
234         ENTRY;
235         /* XXX: Layer violation, we shouldn't see lsm at llite level. */
236         if (lli->lli_smd != NULL) /* lsm-less file, don't need to lock */
237                 result = vvp_io_rw_lock(env, io, CLM_READ,
238                                         io->u.ci_rd.rd.crw_pos,
239                                         io->u.ci_rd.rd.crw_pos +
240                                         io->u.ci_rd.rd.crw_count - 1);
241         else
242                 result = 0;
243         RETURN(result);
244 }
245
246 static int vvp_io_fault_lock(const struct lu_env *env,
247                              const struct cl_io_slice *ios)
248 {
249         struct cl_io *io   = ios->cis_io;
250         struct vvp_io *vio = cl2vvp_io(env, ios);
251         /*
252          * XXX LDLM_FL_CBPENDING
253          */
254         return ccc_io_one_lock_index
255                 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
256                  io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
257 }
258
259 static int vvp_io_write_lock(const struct lu_env *env,
260                              const struct cl_io_slice *ios)
261 {
262         struct cl_io *io = ios->cis_io;
263         loff_t start;
264         loff_t end;
265         int    result;
266
267         if (cl2vvp_io(env, ios)->cui_oneshot == 0) {
268                 if (io->u.ci_wr.wr_append) {
269                         start = 0;
270                         end   = OBD_OBJECT_EOF;
271                 } else {
272                         start = io->u.ci_wr.wr.crw_pos;
273                         end   = start + io->u.ci_wr.wr.crw_count - 1;
274                 }
275                 result = vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
276         } else
277                 result = 0;
278         return result;
279 }
280
281 static int vvp_io_trunc_iter_init(const struct lu_env *env,
282                                   const struct cl_io_slice *ios)
283 {
284         struct ccc_io *vio   = cl2ccc_io(env, ios);
285         struct inode  *inode = ccc_object_inode(ios->cis_obj);
286
287         /*
288          * We really need to get our PW lock before we change inode->i_size.
289          * If we don't we can race with other i_size updaters on our node,
290          * like ll_file_read.  We can also race with i_size propogation to
291          * other nodes through dirtying and writeback of final cached pages.
292          * This last one is especially bad for racing o_append users on other
293          * nodes.
294          */
295
296         UNLOCK_INODE_MUTEX(inode);
297         UP_WRITE_I_ALLOC_SEM(inode);
298         vio->u.trunc.cui_locks_released = 1;
299         return 0;
300 }
301
302 /**
303  * Implementation of cl_io_operations::vio_lock() method for CIT_TRUNC io.
304  *
305  * Handles "lockless io" mode when extent locking is done by server.
306  */
307 static int vvp_io_trunc_lock(const struct lu_env *env,
308                              const struct cl_io_slice *ios)
309 {
310         struct ccc_io     *vio   = cl2ccc_io(env, ios);
311         struct cl_io      *io    = ios->cis_io;
312         size_t new_size          = io->u.ci_truncate.tr_size;
313         __u32 enqflags = new_size == 0 ? CEF_DISCARD_DATA : 0;
314         int result;
315
316         vio->u.trunc.cui_local_lock = TRUNC_EXTENT;
317         result = ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
318                                  new_size, OBD_OBJECT_EOF);
319         return result;
320 }
321
322 static int vvp_io_trunc_start(const struct lu_env *env,
323                               const struct cl_io_slice *ios)
324 {
325         struct ccc_io        *cio   = cl2ccc_io(env, ios);
326         struct vvp_io        *vio   = cl2vvp_io(env, ios);
327         struct cl_io         *io    = ios->cis_io;
328         struct inode         *inode = ccc_object_inode(io->ci_obj);
329         struct cl_object     *obj   = ios->cis_obj;
330         size_t                size  = io->u.ci_truncate.tr_size;
331         pgoff_t               start = cl_index(obj, size);
332         int                   result;
333
334         LASSERT(cio->u.trunc.cui_locks_released);
335         LASSERT(vio->cui_oneshot == 0);
336
337         LOCK_INODE_MUTEX(inode);
338         DOWN_WRITE_I_ALLOC_SEM(inode);
339         cio->u.trunc.cui_locks_released = 0;
340
341         /*
342          * Only ll_inode_size_lock is taken at this level. lov_stripe_lock()
343          * is grabbed by ll_truncate() only over call to obd_adjust_kms().  If
344          * vmtruncate returns 0, then ll_truncate dropped ll_inode_size_lock()
345          */
346         ll_inode_size_lock(inode, 0);
347         result = vmtruncate(inode, size);
348         if (result != 0)
349                 ll_inode_size_unlock(inode, 0);
350         /*
351          * If a page is partially truncated, keep it owned across truncate to
352          * prevent... races.
353          *
354          * XXX this properly belongs to osc, because races in question are OST
355          * specific.
356          */
357         if (cl_offset(obj, start) != size) {
358                 struct cl_object_header *hdr;
359
360                 hdr = cl_object_header(obj);
361                 spin_lock(&hdr->coh_page_guard);
362                 vio->cui_partpage = cl_page_lookup(hdr, start);
363                 spin_unlock(&hdr->coh_page_guard);
364
365                 if (vio->cui_partpage != NULL)
366                         /*
367                          * Wait for the transfer completion for a partially
368                          * truncated page to avoid dead-locking an OST with
369                          * the concurrent page-wise overlapping WRITE and
370                          * PUNCH requests.
371                          *
372                          * Partial page is disowned in vvp_io_trunc_end().
373                          */
374                         cl_page_own(env, io, vio->cui_partpage);
375         } else
376                 vio->cui_partpage = NULL;
377         return result;
378 }
379
380 static void vvp_io_trunc_end(const struct lu_env *env,
381                              const struct cl_io_slice *ios)
382 {
383         struct vvp_io *vio = cl2vvp_io(env, ios);
384
385         if (vio->cui_partpage != NULL) {
386                 cl_page_disown(env, ios->cis_io, vio->cui_partpage);
387                 cl_page_put(env, vio->cui_partpage);
388                 vio->cui_partpage = NULL;
389         }
390 }
391
392 static void vvp_io_trunc_fini(const struct lu_env *env,
393                               const struct cl_io_slice *ios)
394 {
395         struct ccc_io *cio   = ccc_env_io(env);
396         struct inode  *inode = ccc_object_inode(ios->cis_io->ci_obj);
397
398         if (cio->u.trunc.cui_locks_released) {
399                 LOCK_INODE_MUTEX(inode);
400                 DOWN_WRITE_I_ALLOC_SEM(inode);
401                 cio->u.trunc.cui_locks_released = 0;
402         }
403         vvp_io_fini(env, ios);
404 }
405
406 #ifdef HAVE_FILE_READV
407 static ssize_t lustre_generic_file_read(struct file *file,
408                                         struct ccc_io *vio, loff_t *ppos)
409 {
410         return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
411 }
412
413 static ssize_t lustre_generic_file_write(struct file *file,
414                                          struct ccc_io *vio, loff_t *ppos)
415 {
416         return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
417 }
418 #else
419 static ssize_t lustre_generic_file_read(struct file *file,
420                                         struct ccc_io *vio, loff_t *ppos)
421 {
422         return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
423                                      vio->cui_nrsegs, *ppos);
424 }
425
426 static ssize_t lustre_generic_file_write(struct file *file,
427                                         struct ccc_io *vio, loff_t *ppos)
428 {
429         return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
430                                       vio->cui_nrsegs, *ppos);
431 }
432 #endif
433
434 static int vvp_io_read_start(const struct lu_env *env,
435                              const struct cl_io_slice *ios)
436 {
437         struct vvp_io     *vio   = cl2vvp_io(env, ios);
438         struct ccc_io     *cio   = cl2ccc_io(env, ios);
439         struct cl_io      *io    = ios->cis_io;
440         struct cl_object  *obj   = io->ci_obj;
441         struct inode      *inode = ccc_object_inode(obj);
442         struct ll_ra_read *bead  = &vio->cui_bead;
443         struct file       *file  = cio->cui_fd->fd_file;
444
445         int     result;
446         loff_t  pos = io->u.ci_rd.rd.crw_pos;
447         size_t  cnt = io->u.ci_rd.rd.crw_count;
448         size_t  tot = cio->cui_tot_count;
449
450         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
451         LASSERT(vio->cui_oneshot == 0);
452
453         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
454
455         result = ccc_prep_size(env, obj, io, pos + tot - 1, 1);
456         if (result != 0)
457                 return result;
458
459         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
460                         "Read ino %lu, "LPSZ" bytes, offset %lld, size %llu\n",
461                         inode->i_ino, cnt, pos, i_size_read(inode));
462
463         /* turn off the kernel's read-ahead */
464         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
465
466         /* initialize read-ahead window once per syscall */
467         if (!vio->cui_ra_window_set) {
468                 vio->cui_ra_window_set = 1;
469                 bead->lrr_start = cl_index(obj, pos);
470                 /*
471                  * XXX: explicit CFS_PAGE_SIZE
472                  */
473                 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
474                 ll_ra_read_in(file, bead);
475         }
476
477         /* BUG: 5972 */
478         file_accessed(file);
479         if (cl_io_is_sendfile(io)) {
480                 result = generic_file_sendfile(file, &pos, cnt,
481                                 vio->u.read.cui_actor, vio->u.read.cui_target);
482         } else {
483                 result = lustre_generic_file_read(file, cio, &pos);
484         }
485
486         if (result >= 0) {
487                 if (result < cnt)
488                         io->ci_continue = 0;
489                 io->ci_nob += result;
490                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
491                                   cio->cui_fd, pos, result, 0);
492                 result = 0;
493         }
494         return result;
495 }
496
497 static int vvp_io_write_start(const struct lu_env *env,
498                               const struct cl_io_slice *ios)
499 {
500         struct ccc_io      *cio   = cl2ccc_io(env, ios);
501         struct cl_io       *io    = ios->cis_io;
502         struct cl_object   *obj   = io->ci_obj;
503         struct inode       *inode = ccc_object_inode(obj);
504         struct file        *file  = cio->cui_fd->fd_file;
505         ssize_t result = 0;
506         loff_t pos = io->u.ci_wr.wr.crw_pos;
507         size_t cnt = io->u.ci_wr.wr.crw_count;
508
509         ENTRY;
510
511         if (cl_io_is_append(io))
512                 /*
513                  * PARALLEL IO This has to be changed for parallel IO doing
514                  * out-of-order writes.
515                  */
516                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
517
518         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + cnt);
519
520         if (cl2vvp_io(env, ios)->cui_oneshot > 0)
521                 result = 0;
522         else
523                 result = lustre_generic_file_write(file, cio, &pos);
524
525         if (result > 0) {
526                 if (result < cnt)
527                         io->ci_continue = 0;
528                 io->ci_nob += result;
529                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
530                                   cio->cui_fd, pos, result, 0);
531                 result = 0;
532         }
533         RETURN(result);
534 }
535
536 static int vvp_io_fault_start(const struct lu_env *env,
537                               const struct cl_io_slice *ios)
538 {
539         struct vvp_io       *vio     = cl2vvp_io(env, ios);
540         struct cl_io        *io      = ios->cis_io;
541         struct cl_object    *obj     = io->ci_obj;
542         struct inode        *inode   = ccc_object_inode(obj);
543         struct cl_fault_io  *fio     = &io->u.ci_fault;
544         struct vvp_fault_io *cfio    = &vio->u.fault;
545         cfs_page_t          *vmpage;
546         loff_t               offset;
547         int                  result  = 0;
548
549         LASSERT(vio->cui_oneshot == 0);
550
551         if (fio->ft_executable &&
552             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
553                 CWARN("binary "DFID
554                       " changed while waiting for the page fault lock\n",
555                       PFID(lu_object_fid(&obj->co_lu)));
556
557         /* offset of the last byte on the page */
558         offset = cl_offset(obj, fio->ft_index + 1) - 1;
559         LASSERT(cl_index(obj, offset) == fio->ft_index);
560         result = ccc_prep_size(env, obj, io, offset, 0);
561         if (result != 0)
562                 return result;
563
564         vmpage = filemap_nopage(cfio->ft_vma, cfio->ft_address, cfio->ft_type);
565         if (vmpage != NOPAGE_SIGBUS && vmpage != NOPAGE_OOM)
566                 LL_CDEBUG_PAGE(D_PAGE, vmpage,
567                                "got addr %lu type %lx\n",
568                                cfio->ft_address, (long)cfio->ft_type);
569         else
570                 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
571                        cfio->ft_address, (long)cfio->ft_type);
572
573         if (vmpage == NOPAGE_SIGBUS)
574                 result = -EFAULT;
575         else if (vmpage == NOPAGE_OOM)
576                 result = -ENOMEM;
577         else {
578                 struct cl_page *page;
579                 loff_t          size;
580                 pgoff_t         last; /* last page in a file data region */
581
582                 /* Temporarily lock vmpage to keep cl_page_find() happy. */
583                 lock_page(vmpage);
584                 page = cl_page_find(env, obj, fio->ft_index, vmpage,
585                                     CPT_CACHEABLE);
586                 unlock_page(vmpage);
587                 if (!IS_ERR(page)) {
588                         size = i_size_read(inode);
589                         last = cl_index(obj, size - 1);
590                         if (fio->ft_index == last)
591                                 /*
592                                  * Last page is mapped partially.
593                                  */
594                                 fio->ft_nob = size - cl_offset(obj,
595                                                                fio->ft_index);
596                         else
597                                 fio->ft_nob = cl_page_size(obj);
598                         lu_ref_add(&page->cp_reference, "fault", io);
599                         fio->ft_page = page;
600                         /*
601                          * Certain 2.6 kernels return not-NULL from
602                          * filemap_nopage() when page is beyond the file size,
603                          * on the grounds that "An external ptracer can access
604                          * pages that normally aren't accessible.." Don't
605                          * propagate such page fault to the lower layers to
606                          * avoid side-effects like KMS updates.
607                          */
608                         if (fio->ft_index > last)
609                                 result = +1;
610                 } else
611                         result = PTR_ERR(page);
612         }
613         return result;
614 }
615
616 static void vvp_io_advance(const struct lu_env *env,
617                            const struct cl_io_slice *ios, size_t nob)
618 {
619         struct ccc_io    *vio = cl2ccc_io(env, ios);
620         struct cl_io     *io  = ios->cis_io;
621         struct cl_object *obj = ios->cis_io->ci_obj;
622
623         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
624
625         if (!cl_io_is_sendfile(io) && io->ci_continue) {
626                 /* update the iov */
627                 LASSERT(vio->cui_tot_nrsegs >= vio->cui_nrsegs);
628                 LASSERT(vio->cui_tot_count  >= nob);
629
630                 vio->cui_iov        += vio->cui_nrsegs;
631                 vio->cui_tot_nrsegs -= vio->cui_nrsegs;
632                 vio->cui_tot_count  -= nob;
633
634                 if (vio->cui_iov_olen) {
635                         struct iovec *iv;
636
637                         vio->cui_iov--;
638                         vio->cui_tot_nrsegs++;
639                         iv = &vio->cui_iov[0];
640                         iv->iov_base += iv->iov_len;
641                         LASSERT(vio->cui_iov_olen > iv->iov_len);
642                         iv->iov_len = vio->cui_iov_olen - iv->iov_len;
643                 }
644         }
645 }
646
647 static int vvp_io_read_page(const struct lu_env *env,
648                             const struct cl_io_slice *ios,
649                             const struct cl_page_slice *slice)
650 {
651         struct cl_io              *io     = ios->cis_io;
652         struct cl_object          *obj    = slice->cpl_obj;
653         struct ccc_page           *cp     = cl2ccc_page(slice);
654         struct cl_page            *page   = slice->cpl_page;
655         struct inode              *inode  = ccc_object_inode(obj);
656         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
657         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
658         struct ll_readahead_state *ras    = &fd->fd_ras;
659         cfs_page_t                *vmpage = cp->cpg_page;
660         struct cl_2queue          *queue  = &io->ci_queue;
661
662         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
663         LASSERT(cl2vvp_io(env, ios)->cui_oneshot == 0);
664         LASSERT(slice->cpl_obj == obj);
665
666         ENTRY;
667
668         if (sbi->ll_ra_info.ra_max_pages)
669                 ras_update(sbi, inode, ras, page->cp_index,
670                            cp->cpg_defer_uptodate);
671
672         /* Sanity check whether the page is protected by a lock. */
673         if (likely(!(fd->fd_flags & LL_FILE_IGNORE_LOCK))) {
674                 int rc;
675
676                 rc = cl_page_is_under_lock(env, io, page);
677                 if (rc != -EBUSY) {
678                         CL_PAGE_HEADER(D_WARNING, env, page, "%s: %i\n",
679                                        rc == -ENODATA ? "without a lock" :
680                                        "match failed", rc);
681                         if (rc != -ENODATA)
682                                 RETURN(rc);
683                 }
684         }
685
686         if (cp->cpg_defer_uptodate) {
687                 cp->cpg_ra_used = 1;
688                 cl_page_export(env, page);
689         }
690         /*
691          * Add page into the queue even when it is marked uptodate above.
692          * this will unlock it automatically as part of cl_page_list_disown().
693          */
694         cl_2queue_add(queue, page);
695         if (sbi->ll_ra_info.ra_max_pages)
696                 ll_readahead(env, io, ras,
697                              vmpage->mapping, &queue->c2_qin, fd->fd_flags);
698
699         RETURN(0);
700 }
701
702 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
703                             struct cl_page *page, struct ccc_page *cp,
704                             int to, enum cl_req_type crt)
705 {
706         struct cl_2queue  *queue;
707         struct ccc_object *cobo   = cl2ccc(page->cp_obj);
708         struct cl_sync_io *anchor = &ccc_env_info(env)->cti_sync_io;
709
710         int writing = io->ci_type == CIT_WRITE;
711         int result;
712
713         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
714
715         queue = &io->ci_queue;
716
717         cl_2queue_init_page(queue, page);
718
719         if (writing)
720                 /* Do not pass llap here as it is sync write. */
721                 vvp_write_pending(cobo, cp);
722
723         cl_sync_io_init(anchor, 1);
724         cp->cpg_sync_io = anchor;
725         cl_page_clip(env, page, 0, to);
726         result = cl_io_submit_rw(env, io, crt, queue);
727         if (result == 0)
728                 result = cl_sync_io_wait(env, io, &queue->c2_qout, anchor);
729         else
730                 cp->cpg_sync_io = NULL;
731         LASSERT(cl_page_is_owned(page, io));
732         cl_page_clip(env, page, 0, CFS_PAGE_SIZE);
733
734         if (crt == CRT_READ)
735                 /*
736                  * in CRT_WRITE case page is left locked even in case of
737                  * error.
738                  */
739                 cl_page_list_disown(env, io, &queue->c2_qin);
740         cl_2queue_fini(env, queue);
741
742         return result;
743 }
744
745 /**
746  * Prepare partially written-to page for a write.
747  */
748 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
749                                   struct cl_object *obj, struct cl_page *pg,
750                                   struct ccc_page *cp,
751                                   unsigned from, unsigned to)
752 {
753         struct cl_attr *attr   = &ccc_env_info(env)->cti_attr;
754         loff_t          offset = cl_offset(obj, pg->cp_index);
755         int             result;
756
757         cl_object_attr_lock(obj);
758         result = cl_object_attr_get(env, obj, attr);
759         cl_object_attr_unlock(obj);
760         if (result == 0) {
761                 /*
762                  * If are writing to a new page, no need to read old data.
763                  * The extent locking will have updated the KMS, and for our
764                  * purposes here we can treat it like i_size.
765                  */
766                 if (attr->cat_kms <= offset) {
767                         char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
768
769                         memset(kaddr, 0, cl_page_size(obj));
770                         kunmap_atomic(kaddr, KM_USER0);
771                 } else if (cp->cpg_defer_uptodate)
772                         cp->cpg_ra_used = 1;
773                 else
774                         result = vvp_page_sync_io(env, io, pg, cp,
775                                                   CFS_PAGE_SIZE, CRT_READ);
776                 /*
777                  * In older implementations, obdo_refresh_inode is called here
778                  * to update the inode because the write might modify the
779                  * object info at OST. However, this has been proven useless,
780                  * since LVB functions will be called when user space program
781                  * tries to retrieve inode attribute.  Also, see bug 15909 for
782                  * details. -jay
783                  */
784                 if (result == 0)
785                         cl_page_export(env, pg);
786         }
787         return result;
788 }
789
790 static int vvp_io_prepare_write(const struct lu_env *env,
791                                 const struct cl_io_slice *ios,
792                                 const struct cl_page_slice *slice,
793                                 unsigned from, unsigned to)
794 {
795         struct cl_object *obj    = slice->cpl_obj;
796         struct ccc_page  *cp     = cl2ccc_page(slice);
797         struct cl_page   *pg     = slice->cpl_page;
798         cfs_page_t       *vmpage = cp->cpg_page;
799
800         int result;
801
802         ENTRY;
803
804         LINVRNT(cl_page_is_vmlocked(env, pg));
805         LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
806
807         result = 0;
808
809         CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
810         if (!PageUptodate(vmpage)) {
811                 /*
812                  * We're completely overwriting an existing page, so _don't_
813                  * set it up to date until commit_write
814                  */
815                 if (from == 0 && to == CFS_PAGE_SIZE) {
816                         CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
817                         POISON_PAGE(page, 0x11);
818                 } else
819                         result = vvp_io_prepare_partial(env, ios->cis_io, obj,
820                                                         pg, cp, from, to);
821         } else
822                 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
823         RETURN(result);
824 }
825
826 static int vvp_io_commit_write(const struct lu_env *env,
827                                const struct cl_io_slice *ios,
828                                const struct cl_page_slice *slice,
829                                unsigned from, unsigned to)
830 {
831         struct cl_object  *obj    = slice->cpl_obj;
832         struct cl_io      *io     = ios->cis_io;
833         struct ccc_page   *cp     = cl2ccc_page(slice);
834         struct cl_page    *pg     = slice->cpl_page;
835         struct inode      *inode  = ccc_object_inode(obj);
836         struct ll_sb_info *sbi    = ll_i2sbi(inode);
837         cfs_page_t        *vmpage = cp->cpg_page;
838
839         int    result;
840         int    tallyop;
841         loff_t size;
842
843         ENTRY;
844
845         LINVRNT(cl_page_is_vmlocked(env, pg));
846         LASSERT(vmpage->mapping->host == inode);
847
848         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
849         CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
850
851         /*
852          * queue a write for some time in the future the first time we
853          * dirty the page.
854          *
855          * This is different from what other file systems do: they usually
856          * just mark page (and some of its buffers) dirty and rely on
857          * balance_dirty_pages() to start a write-back. Lustre wants write-back
858          * to be started earlier for the following reasons:
859          *
860          *     (1) with a large number of clients we need to limit the amount
861          *     of cached data on the clients a lot;
862          *
863          *     (2) large compute jobs generally want compute-only then io-only
864          *     and the IO should complete as quickly as possible;
865          *
866          *     (3) IO is batched up to the RPC size and is async until the
867          *     client max cache is hit
868          *     (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
869          *
870          */
871         if (!PageDirty(vmpage)) {
872                 tallyop = LPROC_LL_DIRTY_MISSES;
873                 vvp_write_pending(cl2ccc(obj), cp);
874                 set_page_dirty(vmpage);
875                 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
876                 if (result == -EDQUOT)
877                         /*
878                          * Client ran out of disk space grant. Possible
879                          * strategies are:
880                          *
881                          *     (a) do a sync write, renewing grant;
882                          *
883                          *     (b) stop writing on this stripe, switch to the
884                          *     next one.
885                          *
886                          * (b) is a part of "parallel io" design that is the
887                          * ultimate goal. (a) is what "old" client did, and
888                          * what the new code continues to do for the time
889                          * being.
890                          */
891                         result = vvp_page_sync_io(env, io, pg, cp,
892                                                   to, CRT_WRITE);
893         } else {
894                 tallyop = LPROC_LL_DIRTY_HITS;
895                 result = 0;
896         }
897         ll_stats_ops_tally(sbi, tallyop, 1);
898
899         size = cl_offset(obj, pg->cp_index) + to;
900
901         if (result == 0) {
902                 if (size > i_size_read(inode))
903                         i_size_write(inode, size);
904                 cl_page_export(env, pg);
905         } else if (size > i_size_read(inode))
906                 cl_page_discard(env, io, pg);
907         RETURN(result);
908 }
909
910 static const struct cl_io_operations vvp_io_ops = {
911         .op = {
912                 [CIT_READ] = {
913                         .cio_fini      = vvp_io_fini,
914                         .cio_lock      = vvp_io_read_lock,
915                         .cio_start     = vvp_io_read_start,
916                         .cio_advance   = vvp_io_advance
917                 },
918                 [CIT_WRITE] = {
919                         .cio_fini      = vvp_io_fini,
920                         .cio_lock      = vvp_io_write_lock,
921                         .cio_start     = vvp_io_write_start,
922                         .cio_advance   = vvp_io_advance
923                 },
924                 [CIT_TRUNC] = {
925                         .cio_fini       = vvp_io_trunc_fini,
926                         .cio_iter_init  = vvp_io_trunc_iter_init,
927                         .cio_lock       = vvp_io_trunc_lock,
928                         .cio_start      = vvp_io_trunc_start,
929                         .cio_end        = vvp_io_trunc_end
930                 },
931                 [CIT_FAULT] = {
932                         .cio_fini      = vvp_io_fault_fini,
933                         .cio_iter_init = vvp_io_fault_iter_init,
934                         .cio_lock      = vvp_io_fault_lock,
935                         .cio_start     = vvp_io_fault_start,
936                         .cio_end       = ccc_io_end
937                 },
938                 [CIT_MISC] = {
939                         .cio_fini   = vvp_io_fini
940                 }
941         },
942         .cio_read_page     = vvp_io_read_page,
943         .cio_prepare_write = vvp_io_prepare_write,
944         .cio_commit_write  = vvp_io_commit_write
945 };
946
947 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
948                 struct cl_io *io)
949 {
950         struct vvp_io      *vio   = vvp_env_io(env);
951         struct ccc_io      *cio   = ccc_env_io(env);
952         struct inode       *inode = ccc_object_inode(obj);
953         struct ll_sb_info  *sbi   = ll_i2sbi(inode);
954         int                 result;
955
956         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
957         ENTRY;
958
959         CL_IO_SLICE_CLEAN(cio, cui_cl);
960         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
961         vio->cui_oneshot = 0;
962         vio->cui_ra_window_set = 0;
963         result = 0;
964         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
965                 int    op;
966                 size_t count;
967
968                 count = io->u.ci_rw.crw_count;
969                 op    = io->ci_type == CIT_READ ?
970                         LPROC_LL_READ_BYTES : LPROC_LL_WRITE_BYTES;
971                 if (io->ci_type == CIT_WRITE)
972                         down(&ll_i2info(inode)->lli_write_sem);
973                 /* "If nbyte is 0, read() will return 0 and have no other
974                  *  results."  -- Single Unix Spec */
975                 if (count == 0)
976                         result = 1;
977                 else {
978                         cio->cui_tot_count = count;
979                         cio->cui_tot_nrsegs = 0;
980                         ll_stats_ops_tally(sbi, op, count);
981                 }
982         } else if (io->ci_type == CIT_TRUNC) {
983                 /* lockless truncate? */
984                 ll_stats_ops_tally(sbi, LPROC_LL_TRUNC, 1);
985         }
986         RETURN(result);
987 }
988
989 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
990                                 const struct cl_io_slice *slice)
991 {
992         /* Caling just for assertion */
993         cl2ccc_io(env, slice);
994         return vvp_env_io(env);
995 }
996