Whamcloud - gitweb
LU-613 clio: Client dead-lock during binary exec
[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().
330          */
331         ll_inode_size_lock(inode, 0);
332         result = vmtruncate(inode, size);
333         ll_inode_size_unlock(inode, 0);
334
335         return result;
336 }
337
338 static int vvp_io_setattr_trunc(const struct lu_env *env,
339                                 const struct cl_io_slice *ios,
340                                 struct inode *inode, loff_t size)
341 {
342         struct vvp_io        *vio   = cl2vvp_io(env, ios);
343         struct cl_io         *io    = ios->cis_io;
344         struct cl_object     *obj   = ios->cis_obj;
345         pgoff_t               start = cl_index(obj, size);
346         int                   result;
347
348         DOWN_WRITE_I_ALLOC_SEM(inode);
349
350         result = vvp_do_vmtruncate(inode, size);
351
352         /*
353          * If a page is partially truncated, keep it owned across truncate to
354          * prevent... races.
355          *
356          * XXX this properly belongs to osc, because races in question are OST
357          * specific.
358          */
359         if (cl_offset(obj, start) != size) {
360                 struct cl_object_header *hdr;
361
362                 hdr = cl_object_header(obj);
363                 cfs_spin_lock(&hdr->coh_page_guard);
364                 vio->cui_partpage = cl_page_lookup(hdr, start);
365                 cfs_spin_unlock(&hdr->coh_page_guard);
366
367                 if (vio->cui_partpage != NULL)
368                         /*
369                          * Wait for the transfer completion for a partially
370                          * truncated page to avoid dead-locking an OST with
371                          * the concurrent page-wise overlapping WRITE and
372                          * PUNCH requests. BUG:17397.
373                          *
374                          * Partial page is disowned in vvp_io_trunc_end().
375                          */
376                         cl_page_own(env, io, vio->cui_partpage);
377         } else
378                 vio->cui_partpage = NULL;
379         return result;
380 }
381
382 static int vvp_io_setattr_time(const struct lu_env *env,
383                                const struct cl_io_slice *ios)
384 {
385         struct cl_io       *io    = ios->cis_io;
386         struct cl_object   *obj   = io->ci_obj;
387         struct cl_attr     *attr  = ccc_env_thread_attr(env);
388         int result;
389         unsigned valid = CAT_CTIME;
390
391         cl_object_attr_lock(obj);
392         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
393         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
394                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
395                 valid |= CAT_ATIME;
396         }
397         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
398                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
399                 valid |= CAT_MTIME;
400         }
401         result = cl_object_attr_set(env, obj, attr, valid);
402         cl_object_attr_unlock(obj);
403
404         return result;
405 }
406
407 static int vvp_io_setattr_start(const struct lu_env *env,
408                                 const struct cl_io_slice *ios)
409 {
410         struct ccc_io        *cio   = cl2ccc_io(env, ios);
411         struct cl_io         *io    = ios->cis_io;
412         struct inode         *inode = ccc_object_inode(io->ci_obj);
413
414         LASSERT(cio->u.setattr.cui_locks_released);
415
416         LOCK_INODE_MUTEX(inode);
417         cio->u.setattr.cui_locks_released = 0;
418
419         if (cl_io_is_trunc(io))
420                 return vvp_io_setattr_trunc(env, ios, inode,
421                                             io->u.ci_setattr.sa_attr.lvb_size);
422         else
423                 return vvp_io_setattr_time(env, ios);
424 }
425
426 static void vvp_io_setattr_end(const struct lu_env *env,
427                                const struct cl_io_slice *ios)
428 {
429         struct vvp_io        *vio   = cl2vvp_io(env, ios);
430         struct cl_io         *io    = ios->cis_io;
431         struct inode         *inode = ccc_object_inode(io->ci_obj);
432
433         if (!cl_io_is_trunc(io))
434                 return;
435         if (vio->cui_partpage != NULL) {
436                 cl_page_disown(env, ios->cis_io, vio->cui_partpage);
437                 cl_page_put(env, vio->cui_partpage);
438                 vio->cui_partpage = NULL;
439         }
440
441         /*
442          * Do vmtruncate again, to remove possible stale pages populated by
443          * competing read threads. bz20645.
444          */
445         vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
446 }
447
448 static void vvp_io_setattr_fini(const struct lu_env *env,
449                                 const struct cl_io_slice *ios)
450 {
451         struct ccc_io *cio   = ccc_env_io(env);
452         struct cl_io  *io    = ios->cis_io;
453         struct inode  *inode = ccc_object_inode(ios->cis_io->ci_obj);
454
455         if (cio->u.setattr.cui_locks_released) {
456                 LOCK_INODE_MUTEX(inode);
457                 if (cl_io_is_trunc(io))
458                         DOWN_WRITE_I_ALLOC_SEM(inode);
459                 cio->u.setattr.cui_locks_released = 0;
460         }
461         vvp_io_fini(env, ios);
462 }
463
464 #ifdef HAVE_FILE_READV
465 static ssize_t lustre_generic_file_read(struct file *file,
466                                         struct ccc_io *vio, loff_t *ppos)
467 {
468         return generic_file_readv(file, vio->cui_iov, vio->cui_nrsegs, ppos);
469 }
470
471 static ssize_t lustre_generic_file_write(struct file *file,
472                                          struct ccc_io *vio, loff_t *ppos)
473 {
474         return generic_file_writev(file, vio->cui_iov, vio->cui_nrsegs, ppos);
475 }
476 #else
477 static ssize_t lustre_generic_file_read(struct file *file,
478                                         struct ccc_io *vio, loff_t *ppos)
479 {
480         return generic_file_aio_read(vio->cui_iocb, vio->cui_iov,
481                                      vio->cui_nrsegs, *ppos);
482 }
483
484 static ssize_t lustre_generic_file_write(struct file *file,
485                                         struct ccc_io *vio, loff_t *ppos)
486 {
487         return generic_file_aio_write(vio->cui_iocb, vio->cui_iov,
488                                       vio->cui_nrsegs, *ppos);
489 }
490 #endif
491
492 static int vvp_io_read_start(const struct lu_env *env,
493                              const struct cl_io_slice *ios)
494 {
495         struct vvp_io     *vio   = cl2vvp_io(env, ios);
496         struct ccc_io     *cio   = cl2ccc_io(env, ios);
497         struct cl_io      *io    = ios->cis_io;
498         struct cl_object  *obj   = io->ci_obj;
499         struct inode      *inode = ccc_object_inode(obj);
500         struct ll_ra_read *bead  = &vio->cui_bead;
501         struct file       *file  = cio->cui_fd->fd_file;
502
503         int     result;
504         loff_t  pos = io->u.ci_rd.rd.crw_pos;
505         long    cnt = io->u.ci_rd.rd.crw_count;
506         long    tot = cio->cui_tot_count;
507         int     exceed = 0;
508
509         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
510
511         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
512
513         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
514         if (result != 0)
515                 return result;
516         else if (exceed != 0)
517                 goto out;
518
519         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
520                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
521                         inode->i_ino, cnt, pos, i_size_read(inode));
522
523         /* turn off the kernel's read-ahead */
524         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
525
526         /* initialize read-ahead window once per syscall */
527         if (!vio->cui_ra_window_set) {
528                 vio->cui_ra_window_set = 1;
529                 bead->lrr_start = cl_index(obj, pos);
530                 /*
531                  * XXX: explicit CFS_PAGE_SIZE
532                  */
533                 bead->lrr_count = cl_index(obj, tot + CFS_PAGE_SIZE - 1);
534                 ll_ra_read_in(file, bead);
535         }
536
537         /* BUG: 5972 */
538         file_accessed(file);
539         switch (vio->cui_io_subtype) {
540         case IO_NORMAL:
541                  result = lustre_generic_file_read(file, cio, &pos);
542                  break;
543 #ifdef HAVE_KERNEL_SENDFILE
544         case IO_SENDFILE:
545                 result = generic_file_sendfile(file, &pos, cnt,
546                                 vio->u.sendfile.cui_actor,
547                                 vio->u.sendfile.cui_target);
548                 break;
549 #endif
550 #ifdef HAVE_KERNEL_SPLICE_READ
551         case IO_SPLICE:
552                 result = generic_file_splice_read(file, &pos,
553                                 vio->u.splice.cui_pipe, cnt,
554                                 vio->u.splice.cui_flags);
555                 break;
556 #endif
557         default:
558                 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
559                 LBUG();
560         }
561
562 out:
563         if (result >= 0) {
564                 if (result < cnt)
565                         io->ci_continue = 0;
566                 io->ci_nob += result;
567                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
568                                   cio->cui_fd, pos, result, 0);
569                 result = 0;
570         }
571         return result;
572 }
573
574 static int vvp_io_write_start(const struct lu_env *env,
575                               const struct cl_io_slice *ios)
576 {
577         struct ccc_io      *cio   = cl2ccc_io(env, ios);
578         struct cl_io       *io    = ios->cis_io;
579         struct cl_object   *obj   = io->ci_obj;
580         struct inode       *inode = ccc_object_inode(obj);
581         struct file        *file  = cio->cui_fd->fd_file;
582         ssize_t result = 0;
583         loff_t pos = io->u.ci_wr.wr.crw_pos;
584         size_t cnt = io->u.ci_wr.wr.crw_count;
585
586         ENTRY;
587
588         if (cl_io_is_append(io)) {
589                 /*
590                  * PARALLEL IO This has to be changed for parallel IO doing
591                  * out-of-order writes.
592                  */
593                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
594 #ifndef HAVE_FILE_WRITEV
595                 cio->cui_iocb->ki_pos = pos;
596 #endif
597         }
598
599         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
600
601         if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
602                 result = 0;
603         else
604                 result = lustre_generic_file_write(file, cio, &pos);
605
606         if (result > 0) {
607                 if (result < cnt)
608                         io->ci_continue = 0;
609                 io->ci_nob += result;
610                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
611                                   cio->cui_fd, pos, result, 0);
612                 result = 0;
613         }
614         RETURN(result);
615 }
616
617 #ifndef HAVE_VM_OP_FAULT
618 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
619 {
620         cfs_page_t *vmpage;
621
622         vmpage = filemap_nopage(cfio->ft_vma, cfio->nopage.ft_address,
623                                 cfio->nopage.ft_type);
624
625         if (vmpage == NOPAGE_SIGBUS) {
626                 CDEBUG(D_PAGE, "got addr %lu type %lx - SIGBUS\n",
627                        cfio->nopage.ft_address,(long)cfio->nopage.ft_type);
628                 return -EFAULT;
629         } else if (vmpage == NOPAGE_OOM) {
630                 CDEBUG(D_PAGE, "got addr %lu type %lx - OOM\n",
631                        cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
632                 return -ENOMEM;
633         }
634
635         LL_CDEBUG_PAGE(D_PAGE, vmpage, "got addr %lu type %lx\n",
636                        cfio->nopage.ft_address, (long)cfio->nopage.ft_type);
637
638         cfio->ft_vmpage = vmpage;
639         lock_page(vmpage);
640
641         return 0;
642 }
643 #else
644 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
645 {
646         struct vm_fault *vmf = cfio->fault.ft_vmf;
647
648         cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
649
650         if (vmf->page) {
651                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
652                                vmf->virtual_address);
653                 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
654                         lock_page(vmf->page);
655                         cfio->fault.ft_flags &= VM_FAULT_LOCKED;
656                 }
657
658                 cfio->ft_vmpage = vmf->page;
659                 return 0;
660         }
661
662         if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
663                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
664                 return -EFAULT;
665         }
666
667         if (cfio->fault.ft_flags & VM_FAULT_OOM) {
668                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
669                 return -ENOMEM;
670         }
671
672         if (cfio->fault.ft_flags & VM_FAULT_RETRY)
673                 return -EAGAIN;
674
675         CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
676         return -EINVAL;
677 }
678
679 #endif
680
681 static int vvp_io_fault_start(const struct lu_env *env,
682                               const struct cl_io_slice *ios)
683 {
684         struct vvp_io       *vio     = cl2vvp_io(env, ios);
685         struct cl_io        *io      = ios->cis_io;
686         struct cl_object    *obj     = io->ci_obj;
687         struct inode        *inode   = ccc_object_inode(obj);
688         struct cl_fault_io  *fio     = &io->u.ci_fault;
689         struct vvp_fault_io *cfio    = &vio->u.fault;
690         loff_t               offset;
691         int                  result  = 0;
692         cfs_page_t          *vmpage  = NULL;
693         struct cl_page      *page;
694         loff_t               size;
695         pgoff_t              last; /* last page in a file data region */
696
697         if (fio->ft_executable &&
698             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
699                 CWARN("binary "DFID
700                       " changed while waiting for the page fault lock\n",
701                       PFID(lu_object_fid(&obj->co_lu)));
702
703         /* offset of the last byte on the page */
704         offset = cl_offset(obj, fio->ft_index + 1) - 1;
705         LASSERT(cl_index(obj, offset) == fio->ft_index);
706         result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
707         if (result != 0)
708                 return result;
709
710         /* must return locked page */
711         if (fio->ft_mkwrite) {
712                 LASSERT(cfio->ft_vmpage != NULL);
713                 lock_page(cfio->ft_vmpage);
714         } else {
715                 result = vvp_io_kernel_fault(cfio);
716                 if (result != 0)
717                         return result;
718         }
719
720         vmpage = cfio->ft_vmpage;
721         LASSERT(PageLocked(vmpage));
722
723         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
724                 ll_invalidate_page(vmpage);
725
726         /* Though we have already held a cl_lock upon this page, but
727          * it still can be truncated locally. */
728         if (unlikely(vmpage->mapping == NULL)) {
729                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
730
731                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
732                  * and retry. */
733                 GOTO(out, result = +1);
734         }
735
736         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
737         if (IS_ERR(page))
738                 GOTO(out, result = PTR_ERR(page));
739
740         /* if page is going to be written, we should add this page into cache
741          * earlier. */
742         if (fio->ft_mkwrite) {
743                 wait_on_page_writeback(vmpage);
744                 if (set_page_dirty(vmpage)) {
745                         struct ccc_page *cp;
746
747                         /* vvp_page_assume() calls wait_on_page_writeback(). */
748                         cl_page_assume(env, io, page);
749
750                         cp = cl2ccc_page(cl_page_at(page, &vvp_device_type));
751                         vvp_write_pending(cl2ccc(obj), cp);
752
753                         /* Do not set Dirty bit here so that in case IO is
754                          * started before the page is really made dirty, we
755                          * still have chance to detect it. */
756                         result = cl_page_cache_add(env, io, page, CRT_WRITE);
757                         if (result < 0) {
758                                 cl_page_unassume(env, io, page);
759                                 cl_page_put(env, page);
760
761                                 /* we're in big trouble, what can we do now? */
762                                 if (result == -EDQUOT)
763                                         result = -ENOSPC;
764                                 GOTO(out, result);
765                         }
766                 }
767         }
768
769         size = i_size_read(inode);
770         last = cl_index(obj, size - 1);
771         LASSERT(fio->ft_index <= last);
772         if (fio->ft_index == last)
773                 /*
774                  * Last page is mapped partially.
775                  */
776                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
777         else
778                 fio->ft_nob = cl_page_size(obj);
779
780         lu_ref_add(&page->cp_reference, "fault", io);
781         fio->ft_page = page;
782         EXIT;
783
784 out:
785         /* return unlocked vmpage to avoid deadlocking */
786         unlock_page(vmpage);
787 #ifdef HAVE_VM_OP_FAULT
788         cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
789 #endif
790         return result;
791 }
792
793 static int vvp_io_read_page(const struct lu_env *env,
794                             const struct cl_io_slice *ios,
795                             const struct cl_page_slice *slice)
796 {
797         struct cl_io              *io     = ios->cis_io;
798         struct cl_object          *obj    = slice->cpl_obj;
799         struct ccc_page           *cp     = cl2ccc_page(slice);
800         struct cl_page            *page   = slice->cpl_page;
801         struct inode              *inode  = ccc_object_inode(obj);
802         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
803         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
804         struct ll_readahead_state *ras    = &fd->fd_ras;
805         cfs_page_t                *vmpage = cp->cpg_page;
806         struct cl_2queue          *queue  = &io->ci_queue;
807         int rc;
808
809         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
810         LASSERT(slice->cpl_obj == obj);
811
812         ENTRY;
813
814         if (sbi->ll_ra_info.ra_max_pages_per_file &&
815             sbi->ll_ra_info.ra_max_pages)
816                 ras_update(sbi, inode, ras, page->cp_index,
817                            cp->cpg_defer_uptodate);
818
819         /* Sanity check whether the page is protected by a lock. */
820         rc = cl_page_is_under_lock(env, io, page);
821         if (rc != -EBUSY) {
822                 CL_PAGE_HEADER(D_WARNING, env, page, "%s: %d\n",
823                                rc == -ENODATA ? "without a lock" :
824                                "match failed", rc);
825                 if (rc != -ENODATA)
826                         RETURN(rc);
827         }
828
829         if (cp->cpg_defer_uptodate) {
830                 cp->cpg_ra_used = 1;
831                 cl_page_export(env, page, 1);
832         }
833         /*
834          * Add page into the queue even when it is marked uptodate above.
835          * this will unlock it automatically as part of cl_page_list_disown().
836          */
837         cl_2queue_add(queue, page);
838         if (sbi->ll_ra_info.ra_max_pages_per_file &&
839             sbi->ll_ra_info.ra_max_pages)
840                 ll_readahead(env, io, ras,
841                              vmpage->mapping, &queue->c2_qin, fd->fd_flags);
842
843         RETURN(0);
844 }
845
846 static int vvp_page_sync_io(const struct lu_env *env, struct cl_io *io,
847                             struct cl_page *page, struct ccc_page *cp,
848                             enum cl_req_type crt)
849 {
850         struct cl_2queue  *queue;
851         int result;
852
853         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
854
855         queue = &io->ci_queue;
856         cl_2queue_init_page(queue, page);
857
858         result = cl_io_submit_sync(env, io, crt, queue, CRP_NORMAL, 0);
859         LASSERT(cl_page_is_owned(page, io));
860
861         if (crt == CRT_READ)
862                 /*
863                  * in CRT_WRITE case page is left locked even in case of
864                  * error.
865                  */
866                 cl_page_list_disown(env, io, &queue->c2_qin);
867         cl_2queue_fini(env, queue);
868
869         return result;
870 }
871
872 /**
873  * Prepare partially written-to page for a write.
874  */
875 static int vvp_io_prepare_partial(const struct lu_env *env, struct cl_io *io,
876                                   struct cl_object *obj, struct cl_page *pg,
877                                   struct ccc_page *cp,
878                                   unsigned from, unsigned to)
879 {
880         struct cl_attr *attr   = ccc_env_thread_attr(env);
881         loff_t          offset = cl_offset(obj, pg->cp_index);
882         int             result;
883
884         cl_object_attr_lock(obj);
885         result = cl_object_attr_get(env, obj, attr);
886         cl_object_attr_unlock(obj);
887         if (result == 0) {
888                 /*
889                  * If are writing to a new page, no need to read old data.
890                  * The extent locking will have updated the KMS, and for our
891                  * purposes here we can treat it like i_size.
892                  */
893                 if (attr->cat_kms <= offset) {
894                         char *kaddr = kmap_atomic(cp->cpg_page, KM_USER0);
895
896                         memset(kaddr, 0, cl_page_size(obj));
897                         kunmap_atomic(kaddr, KM_USER0);
898                 } else if (cp->cpg_defer_uptodate)
899                         cp->cpg_ra_used = 1;
900                 else
901                         result = vvp_page_sync_io(env, io, pg, cp, CRT_READ);
902                 /*
903                  * In older implementations, obdo_refresh_inode is called here
904                  * to update the inode because the write might modify the
905                  * object info at OST. However, this has been proven useless,
906                  * since LVB functions will be called when user space program
907                  * tries to retrieve inode attribute.  Also, see bug 15909 for
908                  * details. -jay
909                  */
910                 if (result == 0)
911                         cl_page_export(env, pg, 1);
912         }
913         return result;
914 }
915
916 static int vvp_io_prepare_write(const struct lu_env *env,
917                                 const struct cl_io_slice *ios,
918                                 const struct cl_page_slice *slice,
919                                 unsigned from, unsigned to)
920 {
921         struct cl_object *obj    = slice->cpl_obj;
922         struct ccc_page  *cp     = cl2ccc_page(slice);
923         struct cl_page   *pg     = slice->cpl_page;
924         cfs_page_t       *vmpage = cp->cpg_page;
925
926         int result;
927
928         ENTRY;
929
930         LINVRNT(cl_page_is_vmlocked(env, pg));
931         LASSERT(vmpage->mapping->host == ccc_object_inode(obj));
932
933         result = 0;
934
935         CL_PAGE_HEADER(D_PAGE, env, pg, "preparing: [%d, %d]\n", from, to);
936         if (!PageUptodate(vmpage)) {
937                 /*
938                  * We're completely overwriting an existing page, so _don't_
939                  * set it up to date until commit_write
940                  */
941                 if (from == 0 && to == CFS_PAGE_SIZE) {
942                         CL_PAGE_HEADER(D_PAGE, env, pg, "full page write\n");
943                         POISON_PAGE(page, 0x11);
944                 } else
945                         result = vvp_io_prepare_partial(env, ios->cis_io, obj,
946                                                         pg, cp, from, to);
947         } else
948                 CL_PAGE_HEADER(D_PAGE, env, pg, "uptodate\n");
949         RETURN(result);
950 }
951
952 static int vvp_io_commit_write(const struct lu_env *env,
953                                const struct cl_io_slice *ios,
954                                const struct cl_page_slice *slice,
955                                unsigned from, unsigned to)
956 {
957         struct cl_object  *obj    = slice->cpl_obj;
958         struct cl_io      *io     = ios->cis_io;
959         struct ccc_page   *cp     = cl2ccc_page(slice);
960         struct cl_page    *pg     = slice->cpl_page;
961         struct inode      *inode  = ccc_object_inode(obj);
962         struct ll_sb_info *sbi    = ll_i2sbi(inode);
963         cfs_page_t        *vmpage = cp->cpg_page;
964
965         int    result;
966         int    tallyop;
967         loff_t size;
968
969         ENTRY;
970
971         LINVRNT(cl_page_is_vmlocked(env, pg));
972         LASSERT(vmpage->mapping->host == inode);
973
974         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, "commiting page write\n");
975         CL_PAGE_HEADER(D_PAGE, env, pg, "committing: [%d, %d]\n", from, to);
976
977         /*
978          * queue a write for some time in the future the first time we
979          * dirty the page.
980          *
981          * This is different from what other file systems do: they usually
982          * just mark page (and some of its buffers) dirty and rely on
983          * balance_dirty_pages() to start a write-back. Lustre wants write-back
984          * to be started earlier for the following reasons:
985          *
986          *     (1) with a large number of clients we need to limit the amount
987          *     of cached data on the clients a lot;
988          *
989          *     (2) large compute jobs generally want compute-only then io-only
990          *     and the IO should complete as quickly as possible;
991          *
992          *     (3) IO is batched up to the RPC size and is async until the
993          *     client max cache is hit
994          *     (/proc/fs/lustre/osc/OSC.../max_dirty_mb)
995          *
996          */
997         if (!PageDirty(vmpage)) {
998                 tallyop = LPROC_LL_DIRTY_MISSES;
999                 vvp_write_pending(cl2ccc(obj), cp);
1000                 set_page_dirty(vmpage);
1001                 /* ll_set_page_dirty() does the same for now, but
1002                  * it will not soon. */
1003                 vvp_write_pending(cl2ccc(obj), cp);
1004                 result = cl_page_cache_add(env, io, pg, CRT_WRITE);
1005                 if (result == -EDQUOT) {
1006                         pgoff_t last_index = i_size_read(inode) >> CFS_PAGE_SHIFT;
1007                         bool need_clip = true;
1008
1009                         /*
1010                          * Client ran out of disk space grant. Possible
1011                          * strategies are:
1012                          *
1013                          *     (a) do a sync write, renewing grant;
1014                          *
1015                          *     (b) stop writing on this stripe, switch to the
1016                          *     next one.
1017                          *
1018                          * (b) is a part of "parallel io" design that is the
1019                          * ultimate goal. (a) is what "old" client did, and
1020                          * what the new code continues to do for the time
1021                          * being.
1022                          */
1023                         if (last_index > pg->cp_index) {
1024                                 to = CFS_PAGE_SIZE;
1025                                 need_clip = false;
1026                         } else if (last_index == pg->cp_index) {
1027                                 int size_to = i_size_read(inode) & ~CFS_PAGE_MASK;
1028                                 if (to < size_to)
1029                                         to = size_to;
1030                         }
1031                         if (need_clip)
1032                                 cl_page_clip(env, pg, 0, to);
1033                         result = vvp_page_sync_io(env, io, pg, cp, CRT_WRITE);
1034                         if (result)
1035                                 CERROR("Write page %lu of inode %p failed %d\n",
1036                                        pg->cp_index, inode, result);
1037                 }
1038         } else {
1039                 tallyop = LPROC_LL_DIRTY_HITS;
1040                 result = 0;
1041         }
1042         ll_stats_ops_tally(sbi, tallyop, 1);
1043
1044         size = cl_offset(obj, pg->cp_index) + to;
1045
1046         ll_inode_size_lock(inode, 0);
1047         if (result == 0) {
1048                 if (size > i_size_read(inode)) {
1049                         cl_isize_write_nolock(inode, size);
1050                         CDEBUG(D_VFSTRACE, DFID" updating i_size %lu\n",
1051                                PFID(lu_object_fid(&obj->co_lu)),
1052                                (unsigned long)size);
1053                 }
1054                 cl_page_export(env, pg, 1);
1055         } else {
1056                 if (size > i_size_read(inode))
1057                         cl_page_discard(env, io, pg);
1058         }
1059         ll_inode_size_unlock(inode, 0);
1060         RETURN(result);
1061 }
1062
1063 static const struct cl_io_operations vvp_io_ops = {
1064         .op = {
1065                 [CIT_READ] = {
1066                         .cio_fini      = vvp_io_fini,
1067                         .cio_lock      = vvp_io_read_lock,
1068                         .cio_start     = vvp_io_read_start,
1069                         .cio_advance   = ccc_io_advance
1070                 },
1071                 [CIT_WRITE] = {
1072                         .cio_fini      = vvp_io_fini,
1073                         .cio_lock      = vvp_io_write_lock,
1074                         .cio_start     = vvp_io_write_start,
1075                         .cio_advance   = ccc_io_advance
1076                 },
1077                 [CIT_SETATTR] = {
1078                         .cio_fini       = vvp_io_setattr_fini,
1079                         .cio_iter_init  = vvp_io_setattr_iter_init,
1080                         .cio_lock       = vvp_io_setattr_lock,
1081                         .cio_start      = vvp_io_setattr_start,
1082                         .cio_end        = vvp_io_setattr_end
1083                 },
1084                 [CIT_FAULT] = {
1085                         .cio_fini      = vvp_io_fault_fini,
1086                         .cio_iter_init = vvp_io_fault_iter_init,
1087                         .cio_lock      = vvp_io_fault_lock,
1088                         .cio_start     = vvp_io_fault_start,
1089                         .cio_end       = ccc_io_end
1090                 },
1091                 [CIT_MISC] = {
1092                         .cio_fini   = vvp_io_fini
1093                 }
1094         },
1095         .cio_read_page     = vvp_io_read_page,
1096         .cio_prepare_write = vvp_io_prepare_write,
1097         .cio_commit_write  = vvp_io_commit_write
1098 };
1099
1100 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1101                 struct cl_io *io)
1102 {
1103         struct vvp_io      *vio   = vvp_env_io(env);
1104         struct ccc_io      *cio   = ccc_env_io(env);
1105         struct inode       *inode = ccc_object_inode(obj);
1106         struct ll_sb_info  *sbi   = ll_i2sbi(inode);
1107         int                 result;
1108
1109         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1110         ENTRY;
1111
1112         CL_IO_SLICE_CLEAN(cio, cui_cl);
1113         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1114         vio->cui_ra_window_set = 0;
1115         result = 0;
1116         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1117                 size_t count;
1118
1119                 count = io->u.ci_rw.crw_count;
1120                 /* "If nbyte is 0, read() will return 0 and have no other
1121                  *  results."  -- Single Unix Spec */
1122                 if (count == 0)
1123                         result = 1;
1124                 else {
1125                         cio->cui_tot_count = count;
1126                         cio->cui_tot_nrsegs = 0;
1127                 }
1128         } else if (io->ci_type == CIT_SETATTR) {
1129                 if (cl_io_is_trunc(io))
1130                         /* lockless truncate? */
1131                         ll_stats_ops_tally(sbi, LPROC_LL_TRUNC, 1);
1132                 else
1133                         io->ci_lockreq = CILR_MANDATORY;
1134         }
1135         RETURN(result);
1136 }
1137
1138 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1139                                 const struct cl_io_slice *slice)
1140 {
1141         /* Caling just for assertion */
1142         cl2ccc_io(env, slice);
1143         return vvp_env_io(env);
1144 }
1145