Whamcloud - gitweb
LU-5221 vvp: release mmap_sem in error case
[fs/lustre-release.git] / lustre / llite / vvp_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_io for VVP layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #ifndef __KERNEL__
45 # error This file is kernel only.
46 #endif
47
48 #include <obd.h>
49 #include <lustre_lite.h>
50
51 #include "vvp_internal.h"
52
53 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
54                                 const struct cl_io_slice *slice);
55
56 /**
57  * True, if \a io is a normal io, False for splice_{read,write}
58  */
59 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
60 {
61         struct vvp_io *vio = vvp_env_io(env);
62
63         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
64
65         return vio->cui_io_subtype == IO_NORMAL;
66 }
67
68 /**
69  * For swapping layout. The file's layout may have changed.
70  * To avoid populating pages to a wrong stripe, we have to verify the
71  * correctness of layout. It works because swapping layout processes
72  * have to acquire group lock.
73  */
74 static bool can_populate_pages(const struct lu_env *env, struct cl_io *io,
75                                 struct inode *inode)
76 {
77         struct ll_inode_info    *lli = ll_i2info(inode);
78         struct ccc_io           *cio = ccc_env_io(env);
79         bool rc = true;
80
81         switch (io->ci_type) {
82         case CIT_READ:
83         case CIT_WRITE:
84                 /* don't need lock here to check lli_layout_gen as we have held
85                  * extent lock and GROUP lock has to hold to swap layout */
86                 if (ll_layout_version_get(lli) != cio->cui_layout_gen) {
87                         io->ci_need_restart = 1;
88                         /* this will return application a short read/write */
89                         io->ci_continue = 0;
90                         rc = false;
91                 }
92         case CIT_FAULT:
93                 /* fault is okay because we've already had a page. */
94         default:
95                 break;
96         }
97
98         return rc;
99 }
100
101 /*****************************************************************************
102  *
103  * io operations.
104  *
105  */
106
107 static int vvp_io_write_iter_init(const struct lu_env *env,
108                                   const struct cl_io_slice *ios)
109 {
110         struct ccc_io *cio = cl2ccc_io(env, ios);
111
112         cl_page_list_init(&cio->u.write.cui_queue);
113         cio->u.write.cui_written = 0;
114         cio->u.write.cui_from = 0;
115         cio->u.write.cui_to = PAGE_SIZE;
116
117         return 0;
118 }
119
120 static void vvp_io_write_iter_fini(const struct lu_env *env,
121                                    const struct cl_io_slice *ios)
122 {
123         struct ccc_io *cio = cl2ccc_io(env, ios);
124
125         LASSERT(cio->u.write.cui_queue.pl_nr == 0);
126 }
127
128 static int vvp_io_fault_iter_init(const struct lu_env *env,
129                                   const struct cl_io_slice *ios)
130 {
131         struct vvp_io *vio   = cl2vvp_io(env, ios);
132         struct inode  *inode = ccc_object_inode(ios->cis_obj);
133
134         LASSERT(inode ==
135                 cl2ccc_io(env, ios)->cui_fd->fd_file->f_dentry->d_inode);
136         vio->u.fault.ft_mtime = LTIME_S(inode->i_mtime);
137         return 0;
138 }
139
140 static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
141 {
142         struct cl_io     *io  = ios->cis_io;
143         struct cl_object *obj = io->ci_obj;
144         struct ccc_io    *cio = cl2ccc_io(env, ios);
145         struct inode     *inode = ccc_object_inode(obj);
146
147         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
148
149         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
150                            "restore needed %d\n",
151                PFID(lu_object_fid(&obj->co_lu)),
152                io->ci_ignore_layout, io->ci_verify_layout,
153                cio->cui_layout_gen, io->ci_restore_needed);
154
155         if (io->ci_restore_needed == 1) {
156                 int     rc;
157
158                 /* file was detected release, we need to restore it
159                  * before finishing the io
160                  */
161                 rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF);
162                 /* if restore registration failed, no restart,
163                  * we will return -ENODATA */
164                 /* The layout will change after restore, so we need to
165                  * block on layout lock hold by the MDT
166                  * as MDT will not send new layout in lvb (see LU-3124)
167                  * we have to explicitly fetch it, all this will be done
168                  * by ll_layout_refresh()
169                  */
170                 if (rc == 0) {
171                         io->ci_restore_needed = 0;
172                         io->ci_need_restart = 1;
173                         io->ci_verify_layout = 1;
174                 } else {
175                         io->ci_restore_needed = 1;
176                         io->ci_need_restart = 0;
177                         io->ci_verify_layout = 0;
178                         io->ci_result = rc;
179                 }
180         }
181
182         if (!io->ci_ignore_layout && io->ci_verify_layout) {
183                 __u32 gen = 0;
184
185                 /* check layout version */
186                 ll_layout_refresh(inode, &gen);
187                 io->ci_need_restart = cio->cui_layout_gen != gen;
188                 if (io->ci_need_restart) {
189                         CDEBUG(D_VFSTRACE,
190                                DFID" layout changed from %d to %d.\n",
191                                PFID(lu_object_fid(&obj->co_lu)),
192                                cio->cui_layout_gen, gen);
193                         /* today successful restore is the only possible
194                          * case */
195                         /* restore was done, clear restoring state */
196                         ll_i2info(ccc_object_inode(obj))->lli_flags &=
197                                 ~LLIF_FILE_RESTORING;
198                 }
199         }
200 }
201
202 static void vvp_io_fault_fini(const struct lu_env *env,
203                               const struct cl_io_slice *ios)
204 {
205         struct cl_io   *io   = ios->cis_io;
206         struct cl_page *page = io->u.ci_fault.ft_page;
207
208         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
209
210         if (page != NULL) {
211                 lu_ref_del(&page->cp_reference, "fault", io);
212                 cl_page_put(env, page);
213                 io->u.ci_fault.ft_page = NULL;
214         }
215         vvp_io_fini(env, ios);
216 }
217
218 static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma)
219 {
220         /*
221          * we only want to hold PW locks if the mmap() can generate
222          * writes back to the file and that only happens in shared
223          * writable vmas
224          */
225         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
226                 return CLM_WRITE;
227         return CLM_READ;
228 }
229
230 static int vvp_mmap_locks(const struct lu_env *env,
231                           struct ccc_io *vio, struct cl_io *io)
232 {
233         struct ccc_thread_info *cti = ccc_env_info(env);
234         struct mm_struct       *mm = current->mm;
235         struct vm_area_struct  *vma;
236         struct cl_lock_descr   *descr = &cti->cti_descr;
237         ldlm_policy_data_t      policy;
238         unsigned long           addr;
239         unsigned long           seg;
240         ssize_t                 count;
241         int                     result;
242         ENTRY;
243
244         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
245
246         if (!cl_is_normalio(env, io))
247                 RETURN(0);
248
249         if (vio->cui_iov == NULL) /* nfs or loop back device write */
250                 RETURN(0);
251
252         /* No MM (e.g. NFS)? No vmas too. */
253         if (mm == NULL)
254                 RETURN(0);
255
256         for (seg = 0; seg < vio->cui_nrsegs; seg++) {
257                 const struct iovec *iv = &vio->cui_iov[seg];
258
259                 addr = (unsigned long)iv->iov_base;
260                 count = iv->iov_len;
261                 if (count == 0)
262                         continue;
263
264                 count += addr & (~CFS_PAGE_MASK);
265                 addr &= CFS_PAGE_MASK;
266
267                 down_read(&mm->mmap_sem);
268                 while((vma = our_vma(mm, addr, count)) != NULL) {
269                         struct inode *inode = vma->vm_file->f_dentry->d_inode;
270                         int flags = CEF_MUST;
271
272                         if (ll_file_nolock(vma->vm_file)) {
273                                 /*
274                                  * For no lock case, a lockless lock will be
275                                  * generated.
276                                  */
277                                 flags = CEF_NEVER;
278                         }
279
280                         /*
281                          * XXX: Required lock mode can be weakened: CIT_WRITE
282                          * io only ever reads user level buffer, and CIT_READ
283                          * only writes on it.
284                          */
285                         policy_from_vma(&policy, vma, addr, count);
286                         descr->cld_mode = vvp_mode_from_vma(vma);
287                         descr->cld_obj = ll_i2info(inode)->lli_clob;
288                         descr->cld_start = cl_index(descr->cld_obj,
289                                                     policy.l_extent.start);
290                         descr->cld_end = cl_index(descr->cld_obj,
291                                                   policy.l_extent.end);
292                         descr->cld_enq_flags = flags;
293                         result = cl_io_lock_alloc_add(env, io, descr);
294
295                         CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
296                                descr->cld_mode, descr->cld_start,
297                                descr->cld_end);
298
299                         if (result < 0) {
300                                 up_read(&mm->mmap_sem);
301                                 RETURN(result);
302                         }
303
304                         if (vma->vm_end - addr >= count)
305                                 break;
306
307                         count -= vma->vm_end - addr;
308                         addr = vma->vm_end;
309                 }
310                 up_read(&mm->mmap_sem);
311         }
312         RETURN(0);
313 }
314
315 static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io,
316                           enum cl_lock_mode mode, loff_t start, loff_t end)
317 {
318         struct ccc_io *cio = ccc_env_io(env);
319         int result;
320         int ast_flags = 0;
321
322         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
323         ENTRY;
324
325         ccc_io_update_iov(env, cio, io);
326
327         if (io->u.ci_rw.crw_nonblock)
328                 ast_flags |= CEF_NONBLOCK;
329         result = vvp_mmap_locks(env, cio, io);
330         if (result == 0)
331                 result = ccc_io_one_lock(env, io, ast_flags, mode, start, end);
332         RETURN(result);
333 }
334
335 static int vvp_io_read_lock(const struct lu_env *env,
336                             const struct cl_io_slice *ios)
337 {
338         struct cl_io         *io  = ios->cis_io;
339         struct ll_inode_info *lli = ll_i2info(ccc_object_inode(io->ci_obj));
340         int result;
341
342         ENTRY;
343         /* XXX: Layer violation, we shouldn't see lsm at llite level. */
344         if (lli->lli_has_smd) /* lsm-less file doesn't need to lock */
345                 result = vvp_io_rw_lock(env, io, CLM_READ,
346                                         io->u.ci_rd.rd.crw_pos,
347                                         io->u.ci_rd.rd.crw_pos +
348                                         io->u.ci_rd.rd.crw_count - 1);
349         else
350                 result = 0;
351         RETURN(result);
352 }
353
354 static int vvp_io_fault_lock(const struct lu_env *env,
355                              const struct cl_io_slice *ios)
356 {
357         struct cl_io *io   = ios->cis_io;
358         struct vvp_io *vio = cl2vvp_io(env, ios);
359         /*
360          * XXX LDLM_FL_CBPENDING
361          */
362         return ccc_io_one_lock_index
363                 (env, io, 0, vvp_mode_from_vma(vio->u.fault.ft_vma),
364                  io->u.ci_fault.ft_index, io->u.ci_fault.ft_index);
365 }
366
367 static int vvp_io_write_lock(const struct lu_env *env,
368                              const struct cl_io_slice *ios)
369 {
370         struct cl_io *io = ios->cis_io;
371         loff_t start;
372         loff_t end;
373
374         if (io->u.ci_wr.wr_append) {
375                 start = 0;
376                 end   = OBD_OBJECT_EOF;
377         } else {
378                 start = io->u.ci_wr.wr.crw_pos;
379                 end   = start + io->u.ci_wr.wr.crw_count - 1;
380         }
381         return vvp_io_rw_lock(env, io, CLM_WRITE, start, end);
382 }
383
384 static int vvp_io_setattr_iter_init(const struct lu_env *env,
385                                     const struct cl_io_slice *ios)
386 {
387         return 0;
388 }
389
390 /**
391  * Implementation of cl_io_operations::cio_lock() method for CIT_SETATTR io.
392  *
393  * Handles "lockless io" mode when extent locking is done by server.
394  */
395 static int vvp_io_setattr_lock(const struct lu_env *env,
396                                const struct cl_io_slice *ios)
397 {
398         struct ccc_io *cio = ccc_env_io(env);
399         struct cl_io  *io  = ios->cis_io;
400         __u64 new_size;
401         __u32 enqflags = 0;
402
403         if (cl_io_is_trunc(io)) {
404                 new_size = io->u.ci_setattr.sa_attr.lvb_size;
405                 if (new_size == 0)
406                         enqflags = CEF_DISCARD_DATA;
407         } else {
408                 if ((io->u.ci_setattr.sa_attr.lvb_mtime >=
409                      io->u.ci_setattr.sa_attr.lvb_ctime) ||
410                     (io->u.ci_setattr.sa_attr.lvb_atime >=
411                      io->u.ci_setattr.sa_attr.lvb_ctime))
412                         return 0;
413                 new_size = 0;
414         }
415         cio->u.setattr.cui_local_lock = SETATTR_EXTENT_LOCK;
416         return ccc_io_one_lock(env, io, enqflags, CLM_WRITE,
417                                new_size, OBD_OBJECT_EOF);
418 }
419
420 static int vvp_do_vmtruncate(struct inode *inode, size_t size)
421 {
422         int     result;
423
424         /*
425          * Only ll_inode_size_lock is taken at this level.
426          */
427         ll_inode_size_lock(inode);
428         result = inode_newsize_ok(inode, size);
429         if (result < 0) {
430                 ll_inode_size_unlock(inode);
431                 return result;
432         }
433         i_size_write(inode, size);
434
435         ll_truncate_pagecache(inode, size);
436         ll_inode_size_unlock(inode);
437         return result;
438 }
439
440 static int vvp_io_setattr_trunc(const struct lu_env *env,
441                                 const struct cl_io_slice *ios,
442                                 struct inode *inode, loff_t size)
443 {
444         inode_dio_wait(inode);
445         return 0;
446 }
447
448 static int vvp_io_setattr_time(const struct lu_env *env,
449                                const struct cl_io_slice *ios)
450 {
451         struct cl_io       *io    = ios->cis_io;
452         struct cl_object   *obj   = io->ci_obj;
453         struct cl_attr     *attr  = ccc_env_thread_attr(env);
454         int result;
455         unsigned valid = CAT_CTIME;
456
457         cl_object_attr_lock(obj);
458         attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
459         if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) {
460                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
461                 valid |= CAT_ATIME;
462         }
463         if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) {
464                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
465                 valid |= CAT_MTIME;
466         }
467         result = cl_object_attr_set(env, obj, attr, valid);
468         cl_object_attr_unlock(obj);
469
470         return result;
471 }
472
473 static int vvp_io_setattr_start(const struct lu_env *env,
474                                 const struct cl_io_slice *ios)
475 {
476         struct cl_io    *io    = ios->cis_io;
477         struct inode    *inode = ccc_object_inode(io->ci_obj);
478         int result = 0;
479
480         mutex_lock(&inode->i_mutex);
481         if (cl_io_is_trunc(io))
482                 result = vvp_io_setattr_trunc(env, ios, inode,
483                                         io->u.ci_setattr.sa_attr.lvb_size);
484         if (result == 0)
485                 result = vvp_io_setattr_time(env, ios);
486         return result;
487 }
488
489 static void vvp_io_setattr_end(const struct lu_env *env,
490                                const struct cl_io_slice *ios)
491 {
492         struct cl_io *io    = ios->cis_io;
493         struct inode *inode = ccc_object_inode(io->ci_obj);
494
495         if (cl_io_is_trunc(io)) {
496                 /* Truncate in memory pages - they must be clean pages
497                  * because osc has already notified to destroy osc_extents. */
498                 vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size);
499                 inode_dio_write_done(inode);
500         }
501         mutex_unlock(&inode->i_mutex);
502 }
503
504 static void vvp_io_setattr_fini(const struct lu_env *env,
505                                 const struct cl_io_slice *ios)
506 {
507         vvp_io_fini(env, ios);
508 }
509
510 static int vvp_io_read_start(const struct lu_env *env,
511                              const struct cl_io_slice *ios)
512 {
513         struct vvp_io     *vio   = cl2vvp_io(env, ios);
514         struct ccc_io     *cio   = cl2ccc_io(env, ios);
515         struct cl_io      *io    = ios->cis_io;
516         struct cl_object  *obj   = io->ci_obj;
517         struct inode      *inode = ccc_object_inode(obj);
518         struct ll_ra_read *bead  = &vio->cui_bead;
519         struct file       *file  = cio->cui_fd->fd_file;
520
521         int     result;
522         loff_t  pos = io->u.ci_rd.rd.crw_pos;
523         long    cnt = io->u.ci_rd.rd.crw_count;
524         long    tot = cio->cui_tot_count;
525         int     exceed = 0;
526
527         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
528
529         CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt);
530
531         if (!can_populate_pages(env, io, inode))
532                 return 0;
533
534         result = ccc_prep_size(env, obj, io, pos, tot, &exceed);
535         if (result != 0)
536                 return result;
537         else if (exceed != 0)
538                 goto out;
539
540         LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu,
541                         "Read ino %lu, %lu bytes, offset %lld, size %llu\n",
542                         inode->i_ino, cnt, pos, i_size_read(inode));
543
544         /* turn off the kernel's read-ahead */
545         cio->cui_fd->fd_file->f_ra.ra_pages = 0;
546
547         /* initialize read-ahead window once per syscall */
548         if (!vio->cui_ra_window_set) {
549                 vio->cui_ra_window_set = 1;
550                 bead->lrr_start = cl_index(obj, pos);
551                 bead->lrr_count = cl_index(obj, tot + PAGE_CACHE_SIZE - 1);
552                 ll_ra_read_in(file, bead);
553         }
554
555         /* BUG: 5972 */
556         file_accessed(file);
557         switch (vio->cui_io_subtype) {
558         case IO_NORMAL:
559                 LASSERT(cio->cui_iocb->ki_pos == pos);
560                 result = generic_file_aio_read(cio->cui_iocb,
561                                                cio->cui_iov, cio->cui_nrsegs,
562                                                cio->cui_iocb->ki_pos);
563                 break;
564         case IO_SPLICE:
565                 result = generic_file_splice_read(file, &pos,
566                                 vio->u.splice.cui_pipe, cnt,
567                                 vio->u.splice.cui_flags);
568                 /* LU-1109: do splice read stripe by stripe otherwise if it
569                  * may make nfsd stuck if this read occupied all internal pipe
570                  * buffers. */
571                 io->ci_continue = 0;
572                 break;
573         default:
574                 CERROR("Wrong IO type %u\n", vio->cui_io_subtype);
575                 LBUG();
576         }
577
578 out:
579         if (result >= 0) {
580                 if (result < cnt)
581                         io->ci_continue = 0;
582                 io->ci_nob += result;
583                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid, cio->cui_fd,
584                                   pos, result, READ);
585                 result = 0;
586         }
587
588         return result;
589 }
590
591 static void vvp_io_read_fini(const struct lu_env *env, const struct cl_io_slice *ios)
592 {
593         struct vvp_io *vio = cl2vvp_io(env, ios);
594         struct ccc_io *cio = cl2ccc_io(env, ios);
595
596         if (vio->cui_ra_window_set)
597                 ll_ra_read_ex(cio->cui_fd->fd_file, &vio->cui_bead);
598
599         vvp_io_fini(env, ios);
600 }
601
602 static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io,
603                               struct cl_page_list *plist, int from, int to)
604 {
605         struct cl_2queue *queue = &io->ci_queue;
606         struct cl_page *page;
607         unsigned int bytes = 0;
608         int rc = 0;
609         ENTRY;
610
611         if (plist->pl_nr == 0)
612                 RETURN(0);
613
614         if (from > 0 || to != PAGE_SIZE) {
615                 page = cl_page_list_first(plist);
616                 if (plist->pl_nr == 1) {
617                         cl_page_clip(env, page, from, to);
618                 } else if (from > 0) {
619                         cl_page_clip(env, page, from, PAGE_SIZE);
620                 } else {
621                         page = cl_page_list_last(plist);
622                         cl_page_clip(env, page, 0, to);
623                 }
624         }
625
626         cl_2queue_init(queue);
627         cl_page_list_splice(plist, &queue->c2_qin);
628         rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0);
629
630         /* plist is not sorted any more */
631         cl_page_list_splice(&queue->c2_qin, plist);
632         cl_page_list_splice(&queue->c2_qout, plist);
633         cl_2queue_fini(env, queue);
634
635         if (rc == 0) {
636                 /* calculate bytes */
637                 bytes = plist->pl_nr << PAGE_SHIFT;
638                 bytes -= from + PAGE_SIZE - to;
639
640                 while (plist->pl_nr > 0) {
641                         page = cl_page_list_first(plist);
642                         cl_page_list_del(env, plist, page);
643
644                         cl_page_clip(env, page, 0, PAGE_SIZE);
645
646                         SetPageUptodate(cl_page_vmpage(page));
647                         cl_page_disown(env, io, page);
648
649                         /* held in ll_cl_init() */
650                         lu_ref_del(&page->cp_reference, "cl_io", io);
651                         cl_page_put(env, page);
652                 }
653         }
654
655         RETURN(bytes > 0 ? bytes : rc);
656 }
657
658 static void write_commit_callback(const struct lu_env *env, struct cl_io *io,
659                                 struct cl_page *page)
660 {
661         struct ccc_page *cp;
662         struct page *vmpage = page->cp_vmpage;
663         struct cl_object *clob = cl_io_top(io)->ci_obj;
664
665         SetPageUptodate(vmpage);
666         set_page_dirty(vmpage);
667
668         cp = cl2ccc_page(cl_object_page_slice(clob, page));
669         vvp_write_pending(cl2ccc(clob), cp);
670
671         cl_page_disown(env, io, page);
672
673         /* held in ll_cl_init() */
674         lu_ref_del(&page->cp_reference, "cl_io", io);
675         cl_page_put(env, page);
676 }
677
678 /* make sure the page list is contiguous */
679 static bool page_list_sanity_check(struct cl_object *obj,
680                                    struct cl_page_list *plist)
681 {
682         struct cl_page *page;
683         pgoff_t index = CL_PAGE_EOF;
684
685         cl_page_list_for_each(page, plist) {
686                 struct ccc_page *cp = cl_object_page_slice(obj, page);
687
688                 if (index == CL_PAGE_EOF) {
689                         index = ccc_index(cp);
690                         continue;
691                 }
692
693                 ++index;
694                 if (index == ccc_index(cp))
695                         continue;
696
697                 return false;
698         }
699         return true;
700 }
701
702 /* Return how many bytes have queued or written */
703 int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io)
704 {
705         struct cl_object *obj = io->ci_obj;
706         struct inode *inode = ccc_object_inode(obj);
707         struct ccc_io *cio = ccc_env_io(env);
708         struct cl_page_list *queue = &cio->u.write.cui_queue;
709         struct cl_page *page;
710         int rc = 0;
711         int bytes = 0;
712         unsigned int npages = cio->u.write.cui_queue.pl_nr;
713         ENTRY;
714
715         if (npages == 0)
716                 RETURN(0);
717
718         CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n",
719                 npages, cio->u.write.cui_from, cio->u.write.cui_to);
720
721         LASSERT(page_list_sanity_check(obj, queue));
722
723         /* submit IO with async write */
724         rc = cl_io_commit_async(env, io, queue,
725                                 cio->u.write.cui_from, cio->u.write.cui_to,
726                                 write_commit_callback);
727         npages -= queue->pl_nr; /* already committed pages */
728         if (npages > 0) {
729                 /* calculate how many bytes were written */
730                 bytes = npages << PAGE_SHIFT;
731
732                 /* first page */
733                 bytes -= cio->u.write.cui_from;
734                 if (queue->pl_nr == 0) /* last page */
735                         bytes -= PAGE_SIZE - cio->u.write.cui_to;
736                 LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages);
737
738                 cio->u.write.cui_written += bytes;
739
740                 CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n",
741                         npages, bytes, cio->u.write.cui_written);
742
743                 /* the first page must have been written. */
744                 cio->u.write.cui_from = 0;
745         }
746         LASSERT(page_list_sanity_check(obj, queue));
747         LASSERT(ergo(rc == 0, queue->pl_nr == 0));
748
749         /* out of quota, try sync write */
750         if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) {
751                 rc = vvp_io_commit_sync(env, io, queue,
752                                         cio->u.write.cui_from,
753                                         cio->u.write.cui_to);
754                 if (rc > 0) {
755                         cio->u.write.cui_written += rc;
756                         rc = 0;
757                 }
758         }
759
760         /* update inode size */
761         ll_merge_lvb(env, inode);
762
763         /* Now the pages in queue were failed to commit, discard them
764          * unless they were dirtied before. */
765         while (queue->pl_nr > 0) {
766                 page = cl_page_list_first(queue);
767                 cl_page_list_del(env, queue, page);
768
769                 if (!PageDirty(cl_page_vmpage(page)))
770                         cl_page_discard(env, io, page);
771
772                 cl_page_disown(env, io, page);
773
774                 /* held in ll_cl_init() */
775                 lu_ref_del(&page->cp_reference, "cl_io", io);
776                 cl_page_put(env, page);
777         }
778         cl_page_list_fini(env, queue);
779
780         RETURN(rc);
781 }
782
783 static int vvp_io_write_start(const struct lu_env *env,
784                               const struct cl_io_slice *ios)
785 {
786         struct ccc_io      *cio   = cl2ccc_io(env, ios);
787         struct cl_io       *io    = ios->cis_io;
788         struct cl_object   *obj   = io->ci_obj;
789         struct inode       *inode = ccc_object_inode(obj);
790         ssize_t result = 0;
791         loff_t pos = io->u.ci_wr.wr.crw_pos;
792         size_t cnt = io->u.ci_wr.wr.crw_count;
793
794         ENTRY;
795
796         if (!can_populate_pages(env, io, inode))
797                 RETURN(0);
798
799         if (cl_io_is_append(io)) {
800                 /*
801                  * PARALLEL IO This has to be changed for parallel IO doing
802                  * out-of-order writes.
803                  */
804                 pos = io->u.ci_wr.wr.crw_pos = i_size_read(inode);
805                 cio->cui_iocb->ki_pos = pos;
806         } else {
807                 LASSERT(cio->cui_iocb->ki_pos == pos);
808         }
809
810         CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt);
811
812         if (cio->cui_iov == NULL) /* from a temp io in ll_cl_init(). */
813                 result = 0;
814         else
815                 result = generic_file_aio_write(cio->cui_iocb,
816                                                 cio->cui_iov, cio->cui_nrsegs,
817                                                 cio->cui_iocb->ki_pos);
818         if (result > 0) {
819                 result = vvp_io_write_commit(env, io);
820                 if (cio->u.write.cui_written > 0) {
821                         result = cio->u.write.cui_written;
822                         io->ci_nob += result;
823
824                         CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n",
825                                 io->ci_nob, result);
826                 }
827         }
828         if (result > 0) {
829                 struct ll_inode_info *lli = ll_i2info(inode);
830
831                 spin_lock(&lli->lli_lock);
832                 lli->lli_flags |= LLIF_DATA_MODIFIED;
833                 spin_unlock(&lli->lli_lock);
834
835                 if (result < cnt)
836                         io->ci_continue = 0;
837                 ll_rw_stats_tally(ll_i2sbi(inode), current->pid,
838                                   cio->cui_fd, pos, result, WRITE);
839                 result = 0;
840         }
841
842         RETURN(result);
843 }
844
845 static int vvp_io_kernel_fault(struct vvp_fault_io *cfio)
846 {
847         struct vm_fault *vmf = cfio->fault.ft_vmf;
848
849         cfio->fault.ft_flags = filemap_fault(cfio->ft_vma, vmf);
850
851         if (vmf->page) {
852                 LL_CDEBUG_PAGE(D_PAGE, vmf->page, "got addr %p type NOPAGE\n",
853                                vmf->virtual_address);
854                 if (unlikely(!(cfio->fault.ft_flags & VM_FAULT_LOCKED))) {
855                         lock_page(vmf->page);
856                         cfio->fault.ft_flags &= VM_FAULT_LOCKED;
857                 }
858
859                 cfio->ft_vmpage = vmf->page;
860                 return 0;
861         }
862
863         if (cfio->fault.ft_flags & VM_FAULT_SIGBUS) {
864                 CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address);
865                 return -EFAULT;
866         }
867
868         if (cfio->fault.ft_flags & VM_FAULT_OOM) {
869                 CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address);
870                 return -ENOMEM;
871         }
872
873         if (cfio->fault.ft_flags & VM_FAULT_RETRY)
874                 return -EAGAIN;
875
876         CERROR("unknow error in page fault %d!\n", cfio->fault.ft_flags);
877         return -EINVAL;
878 }
879
880 static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io,
881                                     struct cl_page *page)
882 {
883         struct ccc_page *cp;
884         struct cl_object *clob = cl_io_top(io)->ci_obj;
885
886         set_page_dirty(page->cp_vmpage);
887
888         cp = cl2ccc_page(cl_object_page_slice(clob, page));
889         vvp_write_pending(cl2ccc(clob), cp);
890 }
891
892 static int vvp_io_fault_start(const struct lu_env *env,
893                               const struct cl_io_slice *ios)
894 {
895         struct vvp_io       *vio     = cl2vvp_io(env, ios);
896         struct cl_io        *io      = ios->cis_io;
897         struct cl_object    *obj     = io->ci_obj;
898         struct inode        *inode   = ccc_object_inode(obj);
899         struct cl_fault_io  *fio     = &io->u.ci_fault;
900         struct vvp_fault_io *cfio    = &vio->u.fault;
901         loff_t               offset;
902         int                  result  = 0;
903         struct page          *vmpage  = NULL;
904         struct cl_page      *page;
905         loff_t               size;
906         pgoff_t              last_index;
907         ENTRY;
908
909         if (fio->ft_executable &&
910             LTIME_S(inode->i_mtime) != vio->u.fault.ft_mtime)
911                 CWARN("binary "DFID
912                       " changed while waiting for the page fault lock\n",
913                       PFID(lu_object_fid(&obj->co_lu)));
914
915         /* offset of the last byte on the page */
916         offset = cl_offset(obj, fio->ft_index + 1) - 1;
917         LASSERT(cl_index(obj, offset) == fio->ft_index);
918         result = ccc_prep_size(env, obj, io, 0, offset + 1, NULL);
919         if (result != 0)
920                 RETURN(result);
921
922         /* must return locked page */
923         if (fio->ft_mkwrite) {
924                 LASSERT(cfio->ft_vmpage != NULL);
925                 lock_page(cfio->ft_vmpage);
926         } else {
927                 result = vvp_io_kernel_fault(cfio);
928                 if (result != 0)
929                         RETURN(result);
930         }
931
932         vmpage = cfio->ft_vmpage;
933         LASSERT(PageLocked(vmpage));
934
935         if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE))
936                 ll_invalidate_page(vmpage);
937
938         size = i_size_read(inode);
939         /* Though we have already held a cl_lock upon this page, but
940          * it still can be truncated locally. */
941         if (unlikely((vmpage->mapping != inode->i_mapping) ||
942                      (page_offset(vmpage) > size))) {
943                 CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n");
944
945                 /* return +1 to stop cl_io_loop() and ll_fault() will catch
946                  * and retry. */
947                 GOTO(out, result = +1);
948         }
949
950         last_index = cl_index(obj, size - 1);
951
952         if (fio->ft_mkwrite ) {
953                 /*
954                  * Capture the size while holding the lli_trunc_sem from above
955                  * we want to make sure that we complete the mkwrite action
956                  * while holding this lock. We need to make sure that we are
957                  * not past the end of the file.
958                  */
959                 if (last_index < fio->ft_index) {
960                         CDEBUG(D_PAGE,
961                                 "llite: mkwrite and truncate race happened: "
962                                 "%p: 0x%lx 0x%lx\n",
963                                 vmpage->mapping,fio->ft_index,last_index);
964                         /*
965                          * We need to return if we are
966                          * passed the end of the file. This will propagate
967                          * up the call stack to ll_page_mkwrite where
968                          * we will return VM_FAULT_NOPAGE. Any non-negative
969                          * value returned here will be silently
970                          * converted to 0. If the vmpage->mapping is null
971                          * the error code would be converted back to ENODATA
972                          * in ll_page_mkwrite0. Thus we return -ENODATA
973                          * to handle both cases
974                          */
975                         GOTO(out, result = -ENODATA);
976                 }
977         }
978
979         page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE);
980         if (IS_ERR(page))
981                 GOTO(out, result = PTR_ERR(page));
982
983         /* if page is going to be written, we should add this page into cache
984          * earlier. */
985         if (fio->ft_mkwrite) {
986                 wait_on_page_writeback(vmpage);
987                 if (!PageDirty(vmpage)) {
988                         struct cl_page_list *plist = &io->ci_queue.c2_qin;
989                         struct ccc_page *cp = cl_object_page_slice(obj, page);
990                         int to = PAGE_SIZE;
991
992                         /* vvp_page_assume() calls wait_on_page_writeback(). */
993                         cl_page_assume(env, io, page);
994
995                         cl_page_list_init(plist);
996                         cl_page_list_add(plist, page);
997
998                         /* size fixup */
999                         if (last_index == ccc_index(cp))
1000                                 to = size & ~CFS_PAGE_MASK;
1001
1002                         /* Do not set Dirty bit here so that in case IO is
1003                          * started before the page is really made dirty, we
1004                          * still have chance to detect it. */
1005                         result = cl_io_commit_async(env, io, plist, 0, to,
1006                                                     mkwrite_commit_callback);
1007                         LASSERT(cl_page_is_owned(page, io));
1008                         cl_page_list_fini(env, plist);
1009
1010                         vmpage = NULL;
1011                         if (result < 0) {
1012                                 cl_page_discard(env, io, page);
1013                                 cl_page_disown(env, io, page);
1014
1015                                 cl_page_put(env, page);
1016
1017                                 /* we're in big trouble, what can we do now? */
1018                                 if (result == -EDQUOT)
1019                                         result = -ENOSPC;
1020                                 GOTO(out, result);
1021                         } else
1022                                 cl_page_disown(env, io, page);
1023                 }
1024         }
1025
1026         /*
1027          * The ft_index is only used in the case of
1028          * a mkwrite action. We need to check
1029          * our assertions are correct, since
1030          * we should have caught this above
1031          */
1032         LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index);
1033         if (fio->ft_index == last_index)
1034                 /*
1035                  * Last page is mapped partially.
1036                  */
1037                 fio->ft_nob = size - cl_offset(obj, fio->ft_index);
1038         else
1039                 fio->ft_nob = cl_page_size(obj);
1040
1041         lu_ref_add(&page->cp_reference, "fault", io);
1042         fio->ft_page = page;
1043         EXIT;
1044
1045 out:
1046         /* return unlocked vmpage to avoid deadlocking */
1047         if (vmpage != NULL)
1048                 unlock_page(vmpage);
1049         cfio->fault.ft_flags &= ~VM_FAULT_LOCKED;
1050         return result;
1051 }
1052
1053 static int vvp_io_fsync_start(const struct lu_env *env,
1054                               const struct cl_io_slice *ios)
1055 {
1056         /* we should mark TOWRITE bit to each dirty page in radix tree to
1057          * verify pages have been written, but this is difficult because of
1058          * race. */
1059         return 0;
1060 }
1061
1062 static int vvp_io_read_page(const struct lu_env *env,
1063                             const struct cl_io_slice *ios,
1064                             const struct cl_page_slice *slice)
1065 {
1066         struct cl_io              *io     = ios->cis_io;
1067         struct ccc_page           *cp     = cl2ccc_page(slice);
1068         struct cl_page            *page   = slice->cpl_page;
1069         struct inode              *inode  = ccc_object_inode(slice->cpl_obj);
1070         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1071         struct ll_file_data       *fd     = cl2ccc_io(env, ios)->cui_fd;
1072         struct ll_readahead_state *ras    = &fd->fd_ras;
1073         struct cl_2queue          *queue  = &io->ci_queue;
1074
1075         ENTRY;
1076
1077         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1078             sbi->ll_ra_info.ra_max_pages > 0)
1079                 ras_update(sbi, inode, ras, ccc_index(cp),
1080                            cp->cpg_defer_uptodate);
1081
1082         if (cp->cpg_defer_uptodate) {
1083                 cp->cpg_ra_used = 1;
1084                 cl_page_export(env, page, 1);
1085         }
1086
1087         /*
1088          * Add page into the queue even when it is marked uptodate above.
1089          * this will unlock it automatically as part of cl_page_list_disown().
1090          */
1091         cl_2queue_add(queue, page);
1092         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1093             sbi->ll_ra_info.ra_max_pages > 0)
1094                 ll_readahead(env, io, &queue->c2_qin, ras,
1095                              cp->cpg_defer_uptodate);
1096
1097         RETURN(0);
1098 }
1099
1100 static const struct cl_io_operations vvp_io_ops = {
1101         .op = {
1102                 [CIT_READ] = {
1103                         .cio_fini      = vvp_io_read_fini,
1104                         .cio_lock      = vvp_io_read_lock,
1105                         .cio_start     = vvp_io_read_start,
1106                         .cio_advance   = ccc_io_advance
1107                 },
1108                 [CIT_WRITE] = {
1109                         .cio_fini      = vvp_io_fini,
1110                         .cio_iter_init = vvp_io_write_iter_init,
1111                         .cio_iter_fini = vvp_io_write_iter_fini,
1112                         .cio_lock      = vvp_io_write_lock,
1113                         .cio_start     = vvp_io_write_start,
1114                         .cio_advance   = ccc_io_advance
1115                 },
1116                 [CIT_SETATTR] = {
1117                         .cio_fini       = vvp_io_setattr_fini,
1118                         .cio_iter_init  = vvp_io_setattr_iter_init,
1119                         .cio_lock       = vvp_io_setattr_lock,
1120                         .cio_start      = vvp_io_setattr_start,
1121                         .cio_end        = vvp_io_setattr_end
1122                 },
1123                 [CIT_FAULT] = {
1124                         .cio_fini      = vvp_io_fault_fini,
1125                         .cio_iter_init = vvp_io_fault_iter_init,
1126                         .cio_lock      = vvp_io_fault_lock,
1127                         .cio_start     = vvp_io_fault_start,
1128                         .cio_end       = ccc_io_end
1129                 },
1130                 [CIT_FSYNC] = {
1131                         .cio_start  = vvp_io_fsync_start,
1132                         .cio_fini   = vvp_io_fini
1133                 },
1134                 [CIT_MISC] = {
1135                         .cio_fini   = vvp_io_fini
1136                 }
1137         },
1138         .cio_read_page     = vvp_io_read_page,
1139 };
1140
1141 int vvp_io_init(const struct lu_env *env, struct cl_object *obj,
1142                 struct cl_io *io)
1143 {
1144         struct vvp_io      *vio   = vvp_env_io(env);
1145         struct ccc_io      *cio   = ccc_env_io(env);
1146         struct inode       *inode = ccc_object_inode(obj);
1147         int                 result;
1148
1149         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
1150         ENTRY;
1151
1152         CDEBUG(D_VFSTRACE, DFID" ignore/verify layout %d/%d, layout version %d "
1153                            "restore needed %d\n",
1154                PFID(lu_object_fid(&obj->co_lu)),
1155                io->ci_ignore_layout, io->ci_verify_layout,
1156                cio->cui_layout_gen, io->ci_restore_needed);
1157
1158         CL_IO_SLICE_CLEAN(cio, cui_cl);
1159         cl_io_slice_add(io, &cio->cui_cl, obj, &vvp_io_ops);
1160         vio->cui_ra_window_set = 0;
1161         result = 0;
1162         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
1163                 size_t count;
1164                 struct ll_inode_info *lli = ll_i2info(inode);
1165
1166                 count = io->u.ci_rw.crw_count;
1167                 /* "If nbyte is 0, read() will return 0 and have no other
1168                  *  results."  -- Single Unix Spec */
1169                 if (count == 0)
1170                         result = 1;
1171                 else {
1172                         cio->cui_tot_count = count;
1173                         cio->cui_tot_nrsegs = 0;
1174                 }
1175
1176                 /* for read/write, we store the jobid in the inode, and
1177                  * it'll be fetched by osc when building RPC.
1178                  *
1179                  * it's not accurate if the file is shared by different
1180                  * jobs.
1181                  */
1182                 lustre_get_jobid(lli->lli_jobid);
1183         } else if (io->ci_type == CIT_SETATTR) {
1184                 if (!cl_io_is_trunc(io))
1185                         io->ci_lockreq = CILR_MANDATORY;
1186         }
1187
1188         /* ignore layout change for generic CIT_MISC but not for glimpse.
1189          * io context for glimpse must set ci_verify_layout to true,
1190          * see cl_glimpse_size0() for details. */
1191         if (io->ci_type == CIT_MISC && !io->ci_verify_layout)
1192                 io->ci_ignore_layout = 1;
1193
1194         /* Enqueue layout lock and get layout version. We need to do this
1195          * even for operations requiring to open file, such as read and write,
1196          * because it might not grant layout lock in IT_OPEN. */
1197         if (result == 0 && !io->ci_ignore_layout) {
1198                 result = ll_layout_refresh(inode, &cio->cui_layout_gen);
1199                 if (result == -ENOENT)
1200                         /* If the inode on MDS has been removed, but the objects
1201                          * on OSTs haven't been destroyed (async unlink), layout
1202                          * fetch will return -ENOENT, we'd ingore this error
1203                          * and continue with dirty flush. LU-3230. */
1204                         result = 0;
1205                 if (result < 0)
1206                         CERROR("%s: refresh file layout " DFID " error %d.\n",
1207                                 ll_get_fsname(inode->i_sb, NULL, 0),
1208                                 PFID(lu_object_fid(&obj->co_lu)), result);
1209         }
1210
1211         RETURN(result);
1212 }
1213
1214 static struct vvp_io *cl2vvp_io(const struct lu_env *env,
1215                                 const struct cl_io_slice *slice)
1216 {
1217         /* Caling just for assertion */
1218         cl2ccc_io(env, slice);
1219         return vvp_env_io(env);
1220 }