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