]>
Commit | Line | Data |
---|---|---|
d7e09d03 PT |
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 | |
6a5b99a4 | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
d7e09d03 | 19 | * |
d7e09d03 PT |
20 | * GPL HEADER END |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. | |
24 | * Use is subject to license terms. | |
25 | * | |
1dc563a6 | 26 | * Copyright (c) 2011, 2015, Intel Corporation. |
d7e09d03 PT |
27 | */ |
28 | /* | |
29 | * This file is part of Lustre, http://www.lustre.org/ | |
30 | * Lustre is a trademark of Sun Microsystems, Inc. | |
31 | * | |
32 | * Implementation of cl_io for VVP layer. | |
33 | * | |
34 | * Author: Nikita Danilov <nikita.danilov@sun.com> | |
35 | * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com> | |
36 | */ | |
37 | ||
38 | #define DEBUG_SUBSYSTEM S_LLITE | |
39 | ||
67a235f5 | 40 | #include "../include/obd.h" |
d7e09d03 | 41 | |
0d345656 | 42 | #include "llite_internal.h" |
d7e09d03 PT |
43 | #include "vvp_internal.h" |
44 | ||
e3c9078a TH |
45 | static struct vvp_io *cl2vvp_io(const struct lu_env *env, |
46 | const struct cl_io_slice *slice) | |
fee6eb50 JH |
47 | { |
48 | struct vvp_io *vio; | |
49 | ||
50 | vio = container_of(slice, struct vvp_io, vui_cl); | |
51 | LASSERT(vio == vvp_env_io(env)); | |
52 | ||
53 | return vio; | |
54 | } | |
55 | ||
d7e09d03 PT |
56 | /** |
57 | * For swapping layout. The file's layout may have changed. | |
58 | * To avoid populating pages to a wrong stripe, we have to verify the | |
59 | * correctness of layout. It works because swapping layout processes | |
60 | * have to acquire group lock. | |
61 | */ | |
62 | static bool can_populate_pages(const struct lu_env *env, struct cl_io *io, | |
e15ba45d | 63 | struct inode *inode) |
d7e09d03 PT |
64 | { |
65 | struct ll_inode_info *lli = ll_i2info(inode); | |
e0a8144b | 66 | struct vvp_io *vio = vvp_env_io(env); |
d7e09d03 PT |
67 | bool rc = true; |
68 | ||
69 | switch (io->ci_type) { | |
70 | case CIT_READ: | |
71 | case CIT_WRITE: | |
72 | /* don't need lock here to check lli_layout_gen as we have held | |
c0894c6c OD |
73 | * extent lock and GROUP lock has to hold to swap layout |
74 | */ | |
e0a8144b | 75 | if (ll_layout_version_get(lli) != vio->vui_layout_gen) { |
d7e09d03 PT |
76 | io->ci_need_restart = 1; |
77 | /* this will return application a short read/write */ | |
78 | io->ci_continue = 0; | |
79 | rc = false; | |
80 | } | |
81 | case CIT_FAULT: | |
82 | /* fault is okay because we've already had a page. */ | |
83 | default: | |
84 | break; | |
85 | } | |
86 | ||
87 | return rc; | |
88 | } | |
89 | ||
fee6eb50 JH |
90 | static void vvp_object_size_lock(struct cl_object *obj) |
91 | { | |
92 | struct inode *inode = vvp_object_inode(obj); | |
93 | ||
94 | ll_inode_size_lock(inode); | |
95 | cl_object_attr_lock(obj); | |
96 | } | |
97 | ||
98 | static void vvp_object_size_unlock(struct cl_object *obj) | |
99 | { | |
100 | struct inode *inode = vvp_object_inode(obj); | |
101 | ||
102 | cl_object_attr_unlock(obj); | |
103 | ll_inode_size_unlock(inode); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Helper function that if necessary adjusts file size (inode->i_size), when | |
108 | * position at the offset \a pos is accessed. File size can be arbitrary stale | |
109 | * on a Lustre client, but client at least knows KMS. If accessed area is | |
110 | * inside [0, KMS], set file size to KMS, otherwise glimpse file size. | |
111 | * | |
112 | * Locking: cl_isize_lock is used to serialize changes to inode size and to | |
113 | * protect consistency between inode size and cl_object | |
114 | * attributes. cl_object_size_lock() protects consistency between cl_attr's of | |
115 | * top-object and sub-objects. | |
116 | */ | |
117 | static int vvp_prep_size(const struct lu_env *env, struct cl_object *obj, | |
118 | struct cl_io *io, loff_t start, size_t count, | |
119 | int *exceed) | |
120 | { | |
9acc4500 | 121 | struct cl_attr *attr = vvp_env_thread_attr(env); |
fee6eb50 JH |
122 | struct inode *inode = vvp_object_inode(obj); |
123 | loff_t pos = start + count - 1; | |
124 | loff_t kms; | |
125 | int result; | |
126 | ||
127 | /* | |
128 | * Consistency guarantees: following possibilities exist for the | |
129 | * relation between region being accessed and real file size at this | |
130 | * moment: | |
131 | * | |
132 | * (A): the region is completely inside of the file; | |
133 | * | |
134 | * (B-x): x bytes of region are inside of the file, the rest is | |
135 | * outside; | |
136 | * | |
137 | * (C): the region is completely outside of the file. | |
138 | * | |
139 | * This classification is stable under DLM lock already acquired by | |
140 | * the caller, because to change the class, other client has to take | |
141 | * DLM lock conflicting with our lock. Also, any updates to ->i_size | |
142 | * by other threads on this client are serialized by | |
143 | * ll_inode_size_lock(). This guarantees that short reads are handled | |
144 | * correctly in the face of concurrent writes and truncates. | |
145 | */ | |
146 | vvp_object_size_lock(obj); | |
147 | result = cl_object_attr_get(env, obj, attr); | |
148 | if (result == 0) { | |
149 | kms = attr->cat_kms; | |
150 | if (pos > kms) { | |
151 | /* | |
152 | * A glimpse is necessary to determine whether we | |
153 | * return a short read (B) or some zeroes at the end | |
154 | * of the buffer (C) | |
155 | */ | |
156 | vvp_object_size_unlock(obj); | |
157 | result = cl_glimpse_lock(env, io, inode, obj, 0); | |
158 | if (result == 0 && exceed) { | |
159 | /* If objective page index exceed end-of-file | |
160 | * page index, return directly. Do not expect | |
161 | * kernel will check such case correctly. | |
162 | * linux-2.6.18-128.1.1 miss to do that. | |
163 | * --bug 17336 | |
164 | */ | |
165 | loff_t size = i_size_read(inode); | |
5f479924 GKH |
166 | loff_t cur_index = start >> PAGE_SHIFT; |
167 | loff_t size_index = (size - 1) >> PAGE_SHIFT; | |
fee6eb50 JH |
168 | |
169 | if ((size == 0 && cur_index != 0) || | |
170 | size_index < cur_index) | |
171 | *exceed = 1; | |
172 | } | |
173 | return result; | |
174 | } | |
175 | /* | |
176 | * region is within kms and, hence, within real file | |
177 | * size (A). We need to increase i_size to cover the | |
178 | * read region so that generic_file_read() will do its | |
179 | * job, but that doesn't mean the kms size is | |
180 | * _correct_, it is only the _minimum_ size. If | |
181 | * someone does a stat they will get the correct size | |
182 | * which will always be >= the kms value here. | |
183 | * b=11081 | |
184 | */ | |
185 | if (i_size_read(inode) < kms) { | |
186 | i_size_write(inode, kms); | |
187 | CDEBUG(D_VFSTRACE, DFID " updating i_size %llu\n", | |
188 | PFID(lu_object_fid(&obj->co_lu)), | |
189 | (__u64)i_size_read(inode)); | |
190 | } | |
191 | } | |
192 | ||
193 | vvp_object_size_unlock(obj); | |
194 | ||
195 | return result; | |
196 | } | |
197 | ||
d7e09d03 PT |
198 | /***************************************************************************** |
199 | * | |
200 | * io operations. | |
201 | * | |
202 | */ | |
203 | ||
fee6eb50 JH |
204 | static int vvp_io_one_lock_index(const struct lu_env *env, struct cl_io *io, |
205 | __u32 enqflags, enum cl_lock_mode mode, | |
206 | pgoff_t start, pgoff_t end) | |
207 | { | |
208 | struct vvp_io *vio = vvp_env_io(env); | |
209 | struct cl_lock_descr *descr = &vio->vui_link.cill_descr; | |
210 | struct cl_object *obj = io->ci_obj; | |
211 | ||
212 | CLOBINVRNT(env, obj, vvp_object_invariant(obj)); | |
213 | ||
214 | CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end); | |
215 | ||
216 | memset(&vio->vui_link, 0, sizeof(vio->vui_link)); | |
217 | ||
218 | if (vio->vui_fd && (vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) { | |
219 | descr->cld_mode = CLM_GROUP; | |
98eae5e7 | 220 | descr->cld_gid = vio->vui_fd->fd_grouplock.lg_gid; |
fee6eb50 JH |
221 | } else { |
222 | descr->cld_mode = mode; | |
223 | } | |
224 | descr->cld_obj = obj; | |
225 | descr->cld_start = start; | |
226 | descr->cld_end = end; | |
227 | descr->cld_enq_flags = enqflags; | |
228 | ||
229 | cl_io_lock_add(env, io, &vio->vui_link); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | static int vvp_io_one_lock(const struct lu_env *env, struct cl_io *io, | |
234 | __u32 enqflags, enum cl_lock_mode mode, | |
235 | loff_t start, loff_t end) | |
236 | { | |
237 | struct cl_object *obj = io->ci_obj; | |
238 | ||
239 | return vvp_io_one_lock_index(env, io, enqflags, mode, | |
240 | cl_index(obj, start), cl_index(obj, end)); | |
241 | } | |
242 | ||
77605e41 JX |
243 | static int vvp_io_write_iter_init(const struct lu_env *env, |
244 | const struct cl_io_slice *ios) | |
245 | { | |
e0a8144b | 246 | struct vvp_io *vio = cl2vvp_io(env, ios); |
77605e41 | 247 | |
e0a8144b JH |
248 | cl_page_list_init(&vio->u.write.vui_queue); |
249 | vio->u.write.vui_written = 0; | |
250 | vio->u.write.vui_from = 0; | |
251 | vio->u.write.vui_to = PAGE_SIZE; | |
77605e41 JX |
252 | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static void vvp_io_write_iter_fini(const struct lu_env *env, | |
257 | const struct cl_io_slice *ios) | |
258 | { | |
e0a8144b | 259 | struct vvp_io *vio = cl2vvp_io(env, ios); |
77605e41 | 260 | |
e0a8144b | 261 | LASSERT(vio->u.write.vui_queue.pl_nr == 0); |
77605e41 JX |
262 | } |
263 | ||
d7e09d03 PT |
264 | static int vvp_io_fault_iter_init(const struct lu_env *env, |
265 | const struct cl_io_slice *ios) | |
266 | { | |
267 | struct vvp_io *vio = cl2vvp_io(env, ios); | |
8c7b0e1a | 268 | struct inode *inode = vvp_object_inode(ios->cis_obj); |
d7e09d03 | 269 | |
e0a8144b | 270 | LASSERT(inode == file_inode(vio->vui_fd->fd_file)); |
46c360f9 | 271 | vio->u.fault.ft_mtime = inode->i_mtime.tv_sec; |
d7e09d03 PT |
272 | return 0; |
273 | } | |
274 | ||
275 | static void vvp_io_fini(const struct lu_env *env, const struct cl_io_slice *ios) | |
276 | { | |
277 | struct cl_io *io = ios->cis_io; | |
278 | struct cl_object *obj = io->ci_obj; | |
e0a8144b | 279 | struct vvp_io *vio = cl2vvp_io(env, ios); |
1b1594da | 280 | struct inode *inode = vvp_object_inode(obj); |
d7e09d03 | 281 | |
8c7b0e1a | 282 | CLOBINVRNT(env, obj, vvp_object_invariant(obj)); |
d7e09d03 | 283 | |
5ea17d6c JL |
284 | CDEBUG(D_VFSTRACE, DFID |
285 | " ignore/verify layout %d/%d, layout version %d restore needed %d\n", | |
286 | PFID(lu_object_fid(&obj->co_lu)), | |
287 | io->ci_ignore_layout, io->ci_verify_layout, | |
e0a8144b | 288 | vio->vui_layout_gen, io->ci_restore_needed); |
5ea17d6c JL |
289 | |
290 | if (io->ci_restore_needed == 1) { | |
291 | int rc; | |
292 | ||
293 | /* file was detected release, we need to restore it | |
294 | * before finishing the io | |
295 | */ | |
1b1594da | 296 | rc = ll_layout_restore(inode, 0, OBD_OBJECT_EOF); |
5ea17d6c | 297 | /* if restore registration failed, no restart, |
c0894c6c OD |
298 | * we will return -ENODATA |
299 | */ | |
5ea17d6c JL |
300 | /* The layout will change after restore, so we need to |
301 | * block on layout lock hold by the MDT | |
302 | * as MDT will not send new layout in lvb (see LU-3124) | |
303 | * we have to explicitly fetch it, all this will be done | |
304 | * by ll_layout_refresh() | |
305 | */ | |
306 | if (rc == 0) { | |
307 | io->ci_restore_needed = 0; | |
308 | io->ci_need_restart = 1; | |
309 | io->ci_verify_layout = 1; | |
310 | } else { | |
311 | io->ci_restore_needed = 1; | |
312 | io->ci_need_restart = 0; | |
313 | io->ci_verify_layout = 0; | |
314 | io->ci_result = rc; | |
315 | } | |
316 | } | |
d7e09d03 PT |
317 | |
318 | if (!io->ci_ignore_layout && io->ci_verify_layout) { | |
319 | __u32 gen = 0; | |
320 | ||
321 | /* check layout version */ | |
1b1594da | 322 | ll_layout_refresh(inode, &gen); |
e0a8144b | 323 | io->ci_need_restart = vio->vui_layout_gen != gen; |
5ea17d6c JL |
324 | if (io->ci_need_restart) { |
325 | CDEBUG(D_VFSTRACE, | |
326 | DFID" layout changed from %d to %d.\n", | |
327 | PFID(lu_object_fid(&obj->co_lu)), | |
e0a8144b | 328 | vio->vui_layout_gen, gen); |
c0894c6c | 329 | /* today successful restore is the only possible case */ |
5ea17d6c | 330 | /* restore was done, clear restoring state */ |
8c7b0e1a | 331 | ll_i2info(vvp_object_inode(obj))->lli_flags &= |
5ea17d6c JL |
332 | ~LLIF_FILE_RESTORING; |
333 | } | |
d7e09d03 PT |
334 | } |
335 | } | |
336 | ||
337 | static void vvp_io_fault_fini(const struct lu_env *env, | |
338 | const struct cl_io_slice *ios) | |
339 | { | |
340 | struct cl_io *io = ios->cis_io; | |
341 | struct cl_page *page = io->u.ci_fault.ft_page; | |
342 | ||
8c7b0e1a | 343 | CLOBINVRNT(env, io->ci_obj, vvp_object_invariant(io->ci_obj)); |
d7e09d03 | 344 | |
6e16818b | 345 | if (page) { |
d7e09d03 PT |
346 | lu_ref_del(&page->cp_reference, "fault", io); |
347 | cl_page_put(env, page); | |
348 | io->u.ci_fault.ft_page = NULL; | |
349 | } | |
350 | vvp_io_fini(env, ios); | |
351 | } | |
352 | ||
2d95f10e | 353 | static enum cl_lock_mode vvp_mode_from_vma(struct vm_area_struct *vma) |
d7e09d03 PT |
354 | { |
355 | /* | |
356 | * we only want to hold PW locks if the mmap() can generate | |
357 | * writes back to the file and that only happens in shared | |
358 | * writable vmas | |
359 | */ | |
360 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) | |
361 | return CLM_WRITE; | |
362 | return CLM_READ; | |
363 | } | |
364 | ||
365 | static int vvp_mmap_locks(const struct lu_env *env, | |
10cdef73 | 366 | struct vvp_io *vio, struct cl_io *io) |
d7e09d03 | 367 | { |
9acc4500 | 368 | struct vvp_thread_info *cti = vvp_env_info(env); |
d7e09d03 PT |
369 | struct mm_struct *mm = current->mm; |
370 | struct vm_area_struct *vma; | |
9acc4500 | 371 | struct cl_lock_descr *descr = &cti->vti_descr; |
d7e09d03 PT |
372 | ldlm_policy_data_t policy; |
373 | unsigned long addr; | |
d7e09d03 | 374 | ssize_t count; |
06563b56 | 375 | int result = 0; |
b42b15fd AV |
376 | struct iov_iter i; |
377 | struct iovec iov; | |
d7e09d03 PT |
378 | |
379 | LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE); | |
380 | ||
e0a8144b | 381 | if (!vio->vui_iter) /* nfs or loop back device write */ |
0a3bdb00 | 382 | return 0; |
d7e09d03 PT |
383 | |
384 | /* No MM (e.g. NFS)? No vmas too. */ | |
6e16818b | 385 | if (!mm) |
0a3bdb00 | 386 | return 0; |
d7e09d03 | 387 | |
e0a8144b | 388 | iov_for_each(iov, i, *vio->vui_iter) { |
b42b15fd AV |
389 | addr = (unsigned long)iov.iov_base; |
390 | count = iov.iov_len; | |
d7e09d03 PT |
391 | if (count == 0) |
392 | continue; | |
393 | ||
616387e8 OD |
394 | count += addr & (~PAGE_MASK); |
395 | addr &= PAGE_MASK; | |
d7e09d03 PT |
396 | |
397 | down_read(&mm->mmap_sem); | |
a58a38ac | 398 | while ((vma = our_vma(mm, addr, count)) != NULL) { |
2a8a3597 | 399 | struct inode *inode = file_inode(vma->vm_file); |
d7e09d03 PT |
400 | int flags = CEF_MUST; |
401 | ||
402 | if (ll_file_nolock(vma->vm_file)) { | |
403 | /* | |
06563b56 | 404 | * For no lock case is not allowed for mmap |
d7e09d03 | 405 | */ |
06563b56 JX |
406 | result = -EINVAL; |
407 | break; | |
d7e09d03 PT |
408 | } |
409 | ||
410 | /* | |
411 | * XXX: Required lock mode can be weakened: CIT_WRITE | |
412 | * io only ever reads user level buffer, and CIT_READ | |
413 | * only writes on it. | |
414 | */ | |
415 | policy_from_vma(&policy, vma, addr, count); | |
416 | descr->cld_mode = vvp_mode_from_vma(vma); | |
417 | descr->cld_obj = ll_i2info(inode)->lli_clob; | |
418 | descr->cld_start = cl_index(descr->cld_obj, | |
419 | policy.l_extent.start); | |
420 | descr->cld_end = cl_index(descr->cld_obj, | |
421 | policy.l_extent.end); | |
422 | descr->cld_enq_flags = flags; | |
423 | result = cl_io_lock_alloc_add(env, io, descr); | |
424 | ||
425 | CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n", | |
426 | descr->cld_mode, descr->cld_start, | |
427 | descr->cld_end); | |
428 | ||
06563b56 JX |
429 | if (result < 0) |
430 | break; | |
d7e09d03 PT |
431 | |
432 | if (vma->vm_end - addr >= count) | |
433 | break; | |
434 | ||
435 | count -= vma->vm_end - addr; | |
436 | addr = vma->vm_end; | |
437 | } | |
438 | up_read(&mm->mmap_sem); | |
06563b56 JX |
439 | if (result < 0) |
440 | break; | |
d7e09d03 | 441 | } |
06563b56 | 442 | return result; |
d7e09d03 PT |
443 | } |
444 | ||
fee6eb50 JH |
445 | static void vvp_io_advance(const struct lu_env *env, |
446 | const struct cl_io_slice *ios, | |
447 | size_t nob) | |
448 | { | |
fee6eb50 | 449 | struct cl_object *obj = ios->cis_io->ci_obj; |
82c156f8 | 450 | struct vvp_io *vio = cl2vvp_io(env, ios); |
fee6eb50 JH |
451 | CLOBINVRNT(env, obj, vvp_object_invariant(obj)); |
452 | ||
fee6eb50 JH |
453 | iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count -= nob); |
454 | } | |
455 | ||
456 | static void vvp_io_update_iov(const struct lu_env *env, | |
457 | struct vvp_io *vio, struct cl_io *io) | |
458 | { | |
459 | size_t size = io->u.ci_rw.crw_count; | |
460 | ||
82c156f8 | 461 | if (!vio->vui_iter) |
fee6eb50 JH |
462 | return; |
463 | ||
464 | iov_iter_truncate(vio->vui_iter, size); | |
465 | } | |
466 | ||
d7e09d03 PT |
467 | static int vvp_io_rw_lock(const struct lu_env *env, struct cl_io *io, |
468 | enum cl_lock_mode mode, loff_t start, loff_t end) | |
469 | { | |
e0a8144b | 470 | struct vvp_io *vio = vvp_env_io(env); |
d7e09d03 PT |
471 | int result; |
472 | int ast_flags = 0; | |
473 | ||
474 | LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE); | |
d7e09d03 | 475 | |
e0a8144b | 476 | vvp_io_update_iov(env, vio, io); |
d7e09d03 PT |
477 | |
478 | if (io->u.ci_rw.crw_nonblock) | |
479 | ast_flags |= CEF_NONBLOCK; | |
e0a8144b | 480 | result = vvp_mmap_locks(env, vio, io); |
d7e09d03 | 481 | if (result == 0) |
10cdef73 | 482 | result = vvp_io_one_lock(env, io, ast_flags, mode, start, end); |
0a3bdb00 | 483 | return result; |
d7e09d03 PT |
484 | } |
485 | ||
486 | static int vvp_io_read_lock(const struct lu_env *env, | |
487 | const struct cl_io_slice *ios) | |
488 | { | |
4c309612 JX |
489 | struct cl_io *io = ios->cis_io; |
490 | struct cl_io_rw_common *rd = &io->u.ci_rd.rd; | |
d7e09d03 PT |
491 | int result; |
492 | ||
4c309612 JX |
493 | result = vvp_io_rw_lock(env, io, CLM_READ, rd->crw_pos, |
494 | rd->crw_pos + rd->crw_count - 1); | |
495 | ||
0a3bdb00 | 496 | return result; |
d7e09d03 PT |
497 | } |
498 | ||
499 | static int vvp_io_fault_lock(const struct lu_env *env, | |
500 | const struct cl_io_slice *ios) | |
501 | { | |
502 | struct cl_io *io = ios->cis_io; | |
503 | struct vvp_io *vio = cl2vvp_io(env, ios); | |
504 | /* | |
505 | * XXX LDLM_FL_CBPENDING | |
506 | */ | |
10cdef73 JH |
507 | return vvp_io_one_lock_index(env, |
508 | io, 0, | |
509 | vvp_mode_from_vma(vio->u.fault.ft_vma), | |
510 | io->u.ci_fault.ft_index, | |
511 | io->u.ci_fault.ft_index); | |
d7e09d03 PT |
512 | } |
513 | ||
514 | static int vvp_io_write_lock(const struct lu_env *env, | |
515 | const struct cl_io_slice *ios) | |
516 | { | |
517 | struct cl_io *io = ios->cis_io; | |
518 | loff_t start; | |
519 | loff_t end; | |
520 | ||
521 | if (io->u.ci_wr.wr_append) { | |
522 | start = 0; | |
523 | end = OBD_OBJECT_EOF; | |
524 | } else { | |
525 | start = io->u.ci_wr.wr.crw_pos; | |
526 | end = start + io->u.ci_wr.wr.crw_count - 1; | |
527 | } | |
528 | return vvp_io_rw_lock(env, io, CLM_WRITE, start, end); | |
529 | } | |
530 | ||
531 | static int vvp_io_setattr_iter_init(const struct lu_env *env, | |
532 | const struct cl_io_slice *ios) | |
533 | { | |
534 | return 0; | |
535 | } | |
536 | ||
537 | /** | |
e0a8144b | 538 | * Implementation of cl_io_operations::vio_lock() method for CIT_SETATTR io. |
d7e09d03 PT |
539 | * |
540 | * Handles "lockless io" mode when extent locking is done by server. | |
541 | */ | |
542 | static int vvp_io_setattr_lock(const struct lu_env *env, | |
543 | const struct cl_io_slice *ios) | |
544 | { | |
d7e09d03 PT |
545 | struct cl_io *io = ios->cis_io; |
546 | __u64 new_size; | |
547 | __u32 enqflags = 0; | |
548 | ||
549 | if (cl_io_is_trunc(io)) { | |
550 | new_size = io->u.ci_setattr.sa_attr.lvb_size; | |
551 | if (new_size == 0) | |
552 | enqflags = CEF_DISCARD_DATA; | |
553 | } else { | |
933eb397 JH |
554 | unsigned int valid = io->u.ci_setattr.sa_valid; |
555 | ||
556 | if (!(valid & TIMES_SET_FLAGS)) | |
557 | return 0; | |
558 | ||
559 | if ((!(valid & ATTR_MTIME) || | |
560 | io->u.ci_setattr.sa_attr.lvb_mtime >= | |
561 | io->u.ci_setattr.sa_attr.lvb_ctime) && | |
562 | (!(valid & ATTR_ATIME) || | |
563 | io->u.ci_setattr.sa_attr.lvb_atime >= | |
d7e09d03 PT |
564 | io->u.ci_setattr.sa_attr.lvb_ctime)) |
565 | return 0; | |
566 | new_size = 0; | |
567 | } | |
e0a8144b | 568 | |
10cdef73 | 569 | return vvp_io_one_lock(env, io, enqflags, CLM_WRITE, |
d7e09d03 PT |
570 | new_size, OBD_OBJECT_EOF); |
571 | } | |
572 | ||
573 | static int vvp_do_vmtruncate(struct inode *inode, size_t size) | |
574 | { | |
575 | int result; | |
576 | /* | |
577 | * Only ll_inode_size_lock is taken at this level. | |
578 | */ | |
579 | ll_inode_size_lock(inode); | |
580 | result = inode_newsize_ok(inode, size); | |
581 | if (result < 0) { | |
582 | ll_inode_size_unlock(inode); | |
583 | return result; | |
584 | } | |
585 | truncate_setsize(inode, size); | |
586 | ll_inode_size_unlock(inode); | |
587 | return result; | |
588 | } | |
589 | ||
590 | static int vvp_io_setattr_trunc(const struct lu_env *env, | |
591 | const struct cl_io_slice *ios, | |
592 | struct inode *inode, loff_t size) | |
593 | { | |
594 | inode_dio_wait(inode); | |
595 | return 0; | |
596 | } | |
597 | ||
598 | static int vvp_io_setattr_time(const struct lu_env *env, | |
599 | const struct cl_io_slice *ios) | |
600 | { | |
601 | struct cl_io *io = ios->cis_io; | |
602 | struct cl_object *obj = io->ci_obj; | |
9acc4500 | 603 | struct cl_attr *attr = vvp_env_thread_attr(env); |
d7e09d03 PT |
604 | int result; |
605 | unsigned valid = CAT_CTIME; | |
606 | ||
607 | cl_object_attr_lock(obj); | |
608 | attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime; | |
609 | if (io->u.ci_setattr.sa_valid & ATTR_ATIME_SET) { | |
610 | attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime; | |
611 | valid |= CAT_ATIME; | |
612 | } | |
613 | if (io->u.ci_setattr.sa_valid & ATTR_MTIME_SET) { | |
614 | attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime; | |
615 | valid |= CAT_MTIME; | |
616 | } | |
96234ec5 | 617 | result = cl_object_attr_update(env, obj, attr, valid); |
d7e09d03 PT |
618 | cl_object_attr_unlock(obj); |
619 | ||
620 | return result; | |
621 | } | |
622 | ||
623 | static int vvp_io_setattr_start(const struct lu_env *env, | |
624 | const struct cl_io_slice *ios) | |
625 | { | |
626 | struct cl_io *io = ios->cis_io; | |
8c7b0e1a | 627 | struct inode *inode = vvp_object_inode(io->ci_obj); |
5dd16419 | 628 | int result = 0; |
d7e09d03 | 629 | |
5955102c | 630 | inode_lock(inode); |
d7e09d03 | 631 | if (cl_io_is_trunc(io)) |
5dd16419 JX |
632 | result = vvp_io_setattr_trunc(env, ios, inode, |
633 | io->u.ci_setattr.sa_attr.lvb_size); | |
933eb397 | 634 | if (!result && io->u.ci_setattr.sa_valid & TIMES_SET_FLAGS) |
5dd16419 JX |
635 | result = vvp_io_setattr_time(env, ios); |
636 | return result; | |
d7e09d03 PT |
637 | } |
638 | ||
639 | static void vvp_io_setattr_end(const struct lu_env *env, | |
640 | const struct cl_io_slice *ios) | |
641 | { | |
642 | struct cl_io *io = ios->cis_io; | |
8c7b0e1a | 643 | struct inode *inode = vvp_object_inode(io->ci_obj); |
d7e09d03 | 644 | |
81e053c7 | 645 | if (cl_io_is_trunc(io)) |
d7e09d03 | 646 | /* Truncate in memory pages - they must be clean pages |
c0894c6c OD |
647 | * because osc has already notified to destroy osc_extents. |
648 | */ | |
d7e09d03 | 649 | vvp_do_vmtruncate(inode, io->u.ci_setattr.sa_attr.lvb_size); |
81e053c7 | 650 | |
5955102c | 651 | inode_unlock(inode); |
d7e09d03 PT |
652 | } |
653 | ||
654 | static void vvp_io_setattr_fini(const struct lu_env *env, | |
655 | const struct cl_io_slice *ios) | |
656 | { | |
657 | vvp_io_fini(env, ios); | |
658 | } | |
659 | ||
d7e09d03 PT |
660 | static int vvp_io_read_start(const struct lu_env *env, |
661 | const struct cl_io_slice *ios) | |
662 | { | |
663 | struct vvp_io *vio = cl2vvp_io(env, ios); | |
d7e09d03 PT |
664 | struct cl_io *io = ios->cis_io; |
665 | struct cl_object *obj = io->ci_obj; | |
8c7b0e1a | 666 | struct inode *inode = vvp_object_inode(obj); |
e0a8144b | 667 | struct file *file = vio->vui_fd->fd_file; |
d7e09d03 PT |
668 | |
669 | int result; | |
670 | loff_t pos = io->u.ci_rd.rd.crw_pos; | |
671 | long cnt = io->u.ci_rd.rd.crw_count; | |
e0a8144b | 672 | long tot = vio->vui_tot_count; |
d7e09d03 PT |
673 | int exceed = 0; |
674 | ||
8c7b0e1a | 675 | CLOBINVRNT(env, obj, vvp_object_invariant(obj)); |
d7e09d03 PT |
676 | |
677 | CDEBUG(D_VFSTRACE, "read: -> [%lli, %lli)\n", pos, pos + cnt); | |
678 | ||
679 | if (!can_populate_pages(env, io, inode)) | |
680 | return 0; | |
681 | ||
fee6eb50 | 682 | result = vvp_prep_size(env, obj, io, pos, tot, &exceed); |
d7e09d03 PT |
683 | if (result != 0) |
684 | return result; | |
685 | else if (exceed != 0) | |
686 | goto out; | |
687 | ||
688 | LU_OBJECT_HEADER(D_INODE, env, &obj->co_lu, | |
e15ba45d OD |
689 | "Read ino %lu, %lu bytes, offset %lld, size %llu\n", |
690 | inode->i_ino, cnt, pos, i_size_read(inode)); | |
d7e09d03 PT |
691 | |
692 | /* turn off the kernel's read-ahead */ | |
e0a8144b | 693 | vio->vui_fd->fd_file->f_ra.ra_pages = 0; |
d7e09d03 PT |
694 | |
695 | /* initialize read-ahead window once per syscall */ | |
e0a8144b JH |
696 | if (!vio->vui_ra_valid) { |
697 | vio->vui_ra_valid = true; | |
698 | vio->vui_ra_start = cl_index(obj, pos); | |
5f479924 | 699 | vio->vui_ra_count = cl_index(obj, tot + PAGE_SIZE - 1); |
bc4320a9 | 700 | ll_ras_enter(file); |
d7e09d03 PT |
701 | } |
702 | ||
703 | /* BUG: 5972 */ | |
704 | file_accessed(file); | |
82c156f8 AV |
705 | LASSERT(vio->vui_iocb->ki_pos == pos); |
706 | result = generic_file_read_iter(vio->vui_iocb, vio->vui_iter); | |
d7e09d03 PT |
707 | |
708 | out: | |
709 | if (result >= 0) { | |
710 | if (result < cnt) | |
711 | io->ci_continue = 0; | |
712 | io->ci_nob += result; | |
713 | ll_rw_stats_tally(ll_i2sbi(inode), current->pid, | |
e0a8144b | 714 | vio->vui_fd, pos, result, READ); |
d7e09d03 PT |
715 | result = 0; |
716 | } | |
717 | return result; | |
718 | } | |
719 | ||
77605e41 JX |
720 | static int vvp_io_commit_sync(const struct lu_env *env, struct cl_io *io, |
721 | struct cl_page_list *plist, int from, int to) | |
722 | { | |
723 | struct cl_2queue *queue = &io->ci_queue; | |
724 | struct cl_page *page; | |
725 | unsigned int bytes = 0; | |
726 | int rc = 0; | |
727 | ||
728 | if (plist->pl_nr == 0) | |
729 | return 0; | |
730 | ||
c11599b8 | 731 | if (from > 0 || to != PAGE_SIZE) { |
77605e41 | 732 | page = cl_page_list_first(plist); |
c11599b8 JX |
733 | if (plist->pl_nr == 1) { |
734 | cl_page_clip(env, page, from, to); | |
c11599b8 | 735 | } else { |
902a34ad LD |
736 | if (from > 0) |
737 | cl_page_clip(env, page, from, PAGE_SIZE); | |
738 | if (to != PAGE_SIZE) { | |
739 | page = cl_page_list_last(plist); | |
740 | cl_page_clip(env, page, 0, to); | |
741 | } | |
742 | } | |
c11599b8 | 743 | } |
77605e41 JX |
744 | |
745 | cl_2queue_init(queue); | |
746 | cl_page_list_splice(plist, &queue->c2_qin); | |
747 | rc = cl_io_submit_sync(env, io, CRT_WRITE, queue, 0); | |
748 | ||
749 | /* plist is not sorted any more */ | |
750 | cl_page_list_splice(&queue->c2_qin, plist); | |
751 | cl_page_list_splice(&queue->c2_qout, plist); | |
752 | cl_2queue_fini(env, queue); | |
753 | ||
754 | if (rc == 0) { | |
755 | /* calculate bytes */ | |
756 | bytes = plist->pl_nr << PAGE_SHIFT; | |
757 | bytes -= from + PAGE_SIZE - to; | |
758 | ||
759 | while (plist->pl_nr > 0) { | |
760 | page = cl_page_list_first(plist); | |
761 | cl_page_list_del(env, plist, page); | |
762 | ||
763 | cl_page_clip(env, page, 0, PAGE_SIZE); | |
764 | ||
7addf402 | 765 | SetPageUptodate(cl_page_vmpage(page)); |
77605e41 JX |
766 | cl_page_disown(env, io, page); |
767 | ||
768 | /* held in ll_cl_init() */ | |
769 | lu_ref_del(&page->cp_reference, "cl_io", io); | |
770 | cl_page_put(env, page); | |
771 | } | |
772 | } | |
773 | ||
774 | return bytes > 0 ? bytes : rc; | |
775 | } | |
776 | ||
777 | static void write_commit_callback(const struct lu_env *env, struct cl_io *io, | |
778 | struct cl_page *page) | |
779 | { | |
7addf402 | 780 | struct page *vmpage = page->cp_vmpage; |
77605e41 JX |
781 | |
782 | SetPageUptodate(vmpage); | |
783 | set_page_dirty(vmpage); | |
7addf402 | 784 | |
77605e41 JX |
785 | cl_page_disown(env, io, page); |
786 | ||
787 | /* held in ll_cl_init() */ | |
058a6a42 | 788 | lu_ref_del(&page->cp_reference, "cl_io", cl_io_top(io)); |
77605e41 JX |
789 | cl_page_put(env, page); |
790 | } | |
791 | ||
792 | /* make sure the page list is contiguous */ | |
7addf402 JX |
793 | static bool page_list_sanity_check(struct cl_object *obj, |
794 | struct cl_page_list *plist) | |
77605e41 JX |
795 | { |
796 | struct cl_page *page; | |
797 | pgoff_t index = CL_PAGE_EOF; | |
798 | ||
799 | cl_page_list_for_each(page, plist) { | |
3a52f803 | 800 | struct vvp_page *vpg = cl_object_page_slice(obj, page); |
7addf402 | 801 | |
77605e41 | 802 | if (index == CL_PAGE_EOF) { |
3a52f803 | 803 | index = vvp_index(vpg); |
77605e41 JX |
804 | continue; |
805 | } | |
806 | ||
807 | ++index; | |
3a52f803 | 808 | if (index == vvp_index(vpg)) |
77605e41 JX |
809 | continue; |
810 | ||
811 | return false; | |
812 | } | |
813 | return true; | |
814 | } | |
815 | ||
816 | /* Return how many bytes have queued or written */ | |
817 | int vvp_io_write_commit(const struct lu_env *env, struct cl_io *io) | |
818 | { | |
819 | struct cl_object *obj = io->ci_obj; | |
8c7b0e1a | 820 | struct inode *inode = vvp_object_inode(obj); |
e0a8144b JH |
821 | struct vvp_io *vio = vvp_env_io(env); |
822 | struct cl_page_list *queue = &vio->u.write.vui_queue; | |
77605e41 JX |
823 | struct cl_page *page; |
824 | int rc = 0; | |
825 | int bytes = 0; | |
e0a8144b | 826 | unsigned int npages = vio->u.write.vui_queue.pl_nr; |
77605e41 JX |
827 | |
828 | if (npages == 0) | |
829 | return 0; | |
830 | ||
831 | CDEBUG(D_VFSTRACE, "commit async pages: %d, from %d, to %d\n", | |
e0a8144b | 832 | npages, vio->u.write.vui_from, vio->u.write.vui_to); |
77605e41 | 833 | |
7addf402 | 834 | LASSERT(page_list_sanity_check(obj, queue)); |
77605e41 JX |
835 | |
836 | /* submit IO with async write */ | |
837 | rc = cl_io_commit_async(env, io, queue, | |
e0a8144b | 838 | vio->u.write.vui_from, vio->u.write.vui_to, |
77605e41 JX |
839 | write_commit_callback); |
840 | npages -= queue->pl_nr; /* already committed pages */ | |
841 | if (npages > 0) { | |
842 | /* calculate how many bytes were written */ | |
843 | bytes = npages << PAGE_SHIFT; | |
844 | ||
845 | /* first page */ | |
e0a8144b | 846 | bytes -= vio->u.write.vui_from; |
77605e41 | 847 | if (queue->pl_nr == 0) /* last page */ |
e0a8144b | 848 | bytes -= PAGE_SIZE - vio->u.write.vui_to; |
77605e41 JX |
849 | LASSERTF(bytes > 0, "bytes = %d, pages = %d\n", bytes, npages); |
850 | ||
e0a8144b | 851 | vio->u.write.vui_written += bytes; |
77605e41 JX |
852 | |
853 | CDEBUG(D_VFSTRACE, "Committed %d pages %d bytes, tot: %ld\n", | |
e0a8144b | 854 | npages, bytes, vio->u.write.vui_written); |
77605e41 JX |
855 | |
856 | /* the first page must have been written. */ | |
e0a8144b | 857 | vio->u.write.vui_from = 0; |
77605e41 | 858 | } |
7addf402 | 859 | LASSERT(page_list_sanity_check(obj, queue)); |
77605e41 JX |
860 | LASSERT(ergo(rc == 0, queue->pl_nr == 0)); |
861 | ||
862 | /* out of quota, try sync write */ | |
863 | if (rc == -EDQUOT && !cl_io_is_mkwrite(io)) { | |
864 | rc = vvp_io_commit_sync(env, io, queue, | |
e0a8144b JH |
865 | vio->u.write.vui_from, |
866 | vio->u.write.vui_to); | |
77605e41 | 867 | if (rc > 0) { |
e0a8144b | 868 | vio->u.write.vui_written += rc; |
77605e41 JX |
869 | rc = 0; |
870 | } | |
871 | } | |
872 | ||
873 | /* update inode size */ | |
d2995737 | 874 | ll_merge_attr(env, inode); |
77605e41 JX |
875 | |
876 | /* Now the pages in queue were failed to commit, discard them | |
877 | * unless they were dirtied before. | |
878 | */ | |
879 | while (queue->pl_nr > 0) { | |
880 | page = cl_page_list_first(queue); | |
881 | cl_page_list_del(env, queue, page); | |
882 | ||
7addf402 | 883 | if (!PageDirty(cl_page_vmpage(page))) |
77605e41 JX |
884 | cl_page_discard(env, io, page); |
885 | ||
886 | cl_page_disown(env, io, page); | |
887 | ||
888 | /* held in ll_cl_init() */ | |
889 | lu_ref_del(&page->cp_reference, "cl_io", io); | |
890 | cl_page_put(env, page); | |
891 | } | |
892 | cl_page_list_fini(env, queue); | |
893 | ||
894 | return rc; | |
895 | } | |
896 | ||
d7e09d03 PT |
897 | static int vvp_io_write_start(const struct lu_env *env, |
898 | const struct cl_io_slice *ios) | |
899 | { | |
e0a8144b | 900 | struct vvp_io *vio = cl2vvp_io(env, ios); |
d7e09d03 PT |
901 | struct cl_io *io = ios->cis_io; |
902 | struct cl_object *obj = io->ci_obj; | |
8c7b0e1a | 903 | struct inode *inode = vvp_object_inode(obj); |
d7e09d03 PT |
904 | ssize_t result = 0; |
905 | loff_t pos = io->u.ci_wr.wr.crw_pos; | |
906 | size_t cnt = io->u.ci_wr.wr.crw_count; | |
907 | ||
d7e09d03 PT |
908 | if (!can_populate_pages(env, io, inode)) |
909 | return 0; | |
910 | ||
911 | if (cl_io_is_append(io)) { | |
912 | /* | |
913 | * PARALLEL IO This has to be changed for parallel IO doing | |
914 | * out-of-order writes. | |
915 | */ | |
06563b56 | 916 | ll_merge_attr(env, inode); |
7551b8b5 NC |
917 | pos = i_size_read(inode); |
918 | io->u.ci_wr.wr.crw_pos = pos; | |
e0a8144b | 919 | vio->vui_iocb->ki_pos = pos; |
74c0da19 | 920 | } else { |
e0a8144b | 921 | LASSERT(vio->vui_iocb->ki_pos == pos); |
d7e09d03 PT |
922 | } |
923 | ||
924 | CDEBUG(D_VFSTRACE, "write: [%lli, %lli)\n", pos, pos + (long long)cnt); | |
925 | ||
5d770fe8 PS |
926 | if (!vio->vui_iter) { |
927 | /* from a temp io in ll_cl_init(). */ | |
d7e09d03 | 928 | result = 0; |
5d770fe8 PS |
929 | } else { |
930 | /* | |
931 | * When using the locked AIO function (generic_file_aio_write()) | |
932 | * testing has shown the inode mutex to be a limiting factor | |
933 | * with multi-threaded single shared file performance. To get | |
934 | * around this, we now use the lockless version. To maintain | |
935 | * consistency, proper locking to protect against writes, | |
936 | * trucates, etc. is handled in the higher layers of lustre. | |
937 | */ | |
938 | bool lock_node = !IS_NOSEC(inode); | |
939 | ||
940 | if (lock_node) | |
941 | inode_lock(inode); | |
942 | result = __generic_file_write_iter(vio->vui_iocb, | |
943 | vio->vui_iter); | |
944 | if (lock_node) | |
945 | inode_unlock(inode); | |
946 | ||
947 | if (result > 0 || result == -EIOCBQUEUED) | |
948 | result = generic_write_sync(vio->vui_iocb, result); | |
949 | } | |
b42b15fd | 950 | |
d7e09d03 | 951 | if (result > 0) { |
77605e41 | 952 | result = vvp_io_write_commit(env, io); |
e0a8144b JH |
953 | if (vio->u.write.vui_written > 0) { |
954 | result = vio->u.write.vui_written; | |
77605e41 JX |
955 | io->ci_nob += result; |
956 | ||
957 | CDEBUG(D_VFSTRACE, "write: nob %zd, result: %zd\n", | |
958 | io->ci_nob, result); | |
959 | } | |
960 | } | |
961 | if (result > 0) { | |
962 | struct ll_inode_info *lli = ll_i2info(inode); | |
963 | ||
964 | spin_lock(&lli->lli_lock); | |
965 | lli->lli_flags |= LLIF_DATA_MODIFIED; | |
966 | spin_unlock(&lli->lli_lock); | |
967 | ||
d7e09d03 PT |
968 | if (result < cnt) |
969 | io->ci_continue = 0; | |
d7e09d03 | 970 | ll_rw_stats_tally(ll_i2sbi(inode), current->pid, |
e0a8144b | 971 | vio->vui_fd, pos, result, WRITE); |
d7e09d03 PT |
972 | result = 0; |
973 | } | |
0a3bdb00 | 974 | return result; |
d7e09d03 PT |
975 | } |
976 | ||
977 | static int vvp_io_kernel_fault(struct vvp_fault_io *cfio) | |
978 | { | |
10cdef73 | 979 | struct vm_fault *vmf = cfio->ft_vmf; |
d7e09d03 | 980 | |
10cdef73 JH |
981 | cfio->ft_flags = filemap_fault(cfio->ft_vma, vmf); |
982 | cfio->ft_flags_valid = 1; | |
d7e09d03 PT |
983 | |
984 | if (vmf->page) { | |
aa3bee0d GKH |
985 | CDEBUG(D_PAGE, |
986 | "page %p map %p index %lu flags %lx count %u priv %0lx: got addr %p type NOPAGE\n", | |
987 | vmf->page, vmf->page->mapping, vmf->page->index, | |
988 | (long)vmf->page->flags, page_count(vmf->page), | |
989 | page_private(vmf->page), vmf->virtual_address); | |
10cdef73 | 990 | if (unlikely(!(cfio->ft_flags & VM_FAULT_LOCKED))) { |
d7e09d03 | 991 | lock_page(vmf->page); |
10cdef73 | 992 | cfio->ft_flags |= VM_FAULT_LOCKED; |
d7e09d03 PT |
993 | } |
994 | ||
995 | cfio->ft_vmpage = vmf->page; | |
996 | return 0; | |
997 | } | |
998 | ||
10cdef73 | 999 | if (cfio->ft_flags & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) { |
d7e09d03 PT |
1000 | CDEBUG(D_PAGE, "got addr %p - SIGBUS\n", vmf->virtual_address); |
1001 | return -EFAULT; | |
1002 | } | |
1003 | ||
10cdef73 | 1004 | if (cfio->ft_flags & VM_FAULT_OOM) { |
d7e09d03 PT |
1005 | CDEBUG(D_PAGE, "got addr %p - OOM\n", vmf->virtual_address); |
1006 | return -ENOMEM; | |
1007 | } | |
1008 | ||
10cdef73 | 1009 | if (cfio->ft_flags & VM_FAULT_RETRY) |
d7e09d03 PT |
1010 | return -EAGAIN; |
1011 | ||
10cdef73 | 1012 | CERROR("Unknown error in page fault %d!\n", cfio->ft_flags); |
d7e09d03 PT |
1013 | return -EINVAL; |
1014 | } | |
1015 | ||
77605e41 JX |
1016 | static void mkwrite_commit_callback(const struct lu_env *env, struct cl_io *io, |
1017 | struct cl_page *page) | |
1018 | { | |
7addf402 | 1019 | set_page_dirty(page->cp_vmpage); |
77605e41 JX |
1020 | } |
1021 | ||
d7e09d03 PT |
1022 | static int vvp_io_fault_start(const struct lu_env *env, |
1023 | const struct cl_io_slice *ios) | |
1024 | { | |
1025 | struct vvp_io *vio = cl2vvp_io(env, ios); | |
1026 | struct cl_io *io = ios->cis_io; | |
1027 | struct cl_object *obj = io->ci_obj; | |
8c7b0e1a | 1028 | struct inode *inode = vvp_object_inode(obj); |
d7e09d03 PT |
1029 | struct cl_fault_io *fio = &io->u.ci_fault; |
1030 | struct vvp_fault_io *cfio = &vio->u.fault; | |
1031 | loff_t offset; | |
1032 | int result = 0; | |
1033 | struct page *vmpage = NULL; | |
1034 | struct cl_page *page; | |
1035 | loff_t size; | |
77605e41 | 1036 | pgoff_t last_index; |
d7e09d03 PT |
1037 | |
1038 | if (fio->ft_executable && | |
46c360f9 | 1039 | inode->i_mtime.tv_sec != vio->u.fault.ft_mtime) |
d7e09d03 PT |
1040 | CWARN("binary "DFID |
1041 | " changed while waiting for the page fault lock\n", | |
1042 | PFID(lu_object_fid(&obj->co_lu))); | |
1043 | ||
1044 | /* offset of the last byte on the page */ | |
1045 | offset = cl_offset(obj, fio->ft_index + 1) - 1; | |
1046 | LASSERT(cl_index(obj, offset) == fio->ft_index); | |
fee6eb50 | 1047 | result = vvp_prep_size(env, obj, io, 0, offset + 1, NULL); |
d7e09d03 PT |
1048 | if (result != 0) |
1049 | return result; | |
1050 | ||
1051 | /* must return locked page */ | |
1052 | if (fio->ft_mkwrite) { | |
6e16818b | 1053 | LASSERT(cfio->ft_vmpage); |
d7e09d03 PT |
1054 | lock_page(cfio->ft_vmpage); |
1055 | } else { | |
1056 | result = vvp_io_kernel_fault(cfio); | |
1057 | if (result != 0) | |
1058 | return result; | |
1059 | } | |
1060 | ||
1061 | vmpage = cfio->ft_vmpage; | |
1062 | LASSERT(PageLocked(vmpage)); | |
1063 | ||
1064 | if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_FAULT_TRUNC_RACE)) | |
1065 | ll_invalidate_page(vmpage); | |
1066 | ||
1067 | size = i_size_read(inode); | |
1068 | /* Though we have already held a cl_lock upon this page, but | |
c0894c6c OD |
1069 | * it still can be truncated locally. |
1070 | */ | |
d7e09d03 PT |
1071 | if (unlikely((vmpage->mapping != inode->i_mapping) || |
1072 | (page_offset(vmpage) > size))) { | |
1073 | CDEBUG(D_PAGE, "llite: fault and truncate race happened!\n"); | |
1074 | ||
1075 | /* return +1 to stop cl_io_loop() and ll_fault() will catch | |
c0894c6c OD |
1076 | * and retry. |
1077 | */ | |
b2952d62 | 1078 | result = 1; |
34e1f2bb | 1079 | goto out; |
d7e09d03 PT |
1080 | } |
1081 | ||
77605e41 JX |
1082 | last_index = cl_index(obj, size - 1); |
1083 | ||
557732ad | 1084 | if (fio->ft_mkwrite) { |
d7e09d03 PT |
1085 | /* |
1086 | * Capture the size while holding the lli_trunc_sem from above | |
1087 | * we want to make sure that we complete the mkwrite action | |
1088 | * while holding this lock. We need to make sure that we are | |
1089 | * not past the end of the file. | |
1090 | */ | |
d7e09d03 PT |
1091 | if (last_index < fio->ft_index) { |
1092 | CDEBUG(D_PAGE, | |
2d00bd17 JP |
1093 | "llite: mkwrite and truncate race happened: %p: 0x%lx 0x%lx\n", |
1094 | vmpage->mapping, fio->ft_index, last_index); | |
d7e09d03 PT |
1095 | /* |
1096 | * We need to return if we are | |
1097 | * passed the end of the file. This will propagate | |
1098 | * up the call stack to ll_page_mkwrite where | |
1099 | * we will return VM_FAULT_NOPAGE. Any non-negative | |
1100 | * value returned here will be silently | |
1101 | * converted to 0. If the vmpage->mapping is null | |
1102 | * the error code would be converted back to ENODATA | |
1103 | * in ll_page_mkwrite0. Thus we return -ENODATA | |
1104 | * to handle both cases | |
1105 | */ | |
34e1f2bb JL |
1106 | result = -ENODATA; |
1107 | goto out; | |
d7e09d03 PT |
1108 | } |
1109 | } | |
1110 | ||
1111 | page = cl_page_find(env, obj, fio->ft_index, vmpage, CPT_CACHEABLE); | |
34e1f2bb JL |
1112 | if (IS_ERR(page)) { |
1113 | result = PTR_ERR(page); | |
1114 | goto out; | |
1115 | } | |
d7e09d03 PT |
1116 | |
1117 | /* if page is going to be written, we should add this page into cache | |
c0894c6c OD |
1118 | * earlier. |
1119 | */ | |
d7e09d03 PT |
1120 | if (fio->ft_mkwrite) { |
1121 | wait_on_page_writeback(vmpage); | |
77605e41 JX |
1122 | if (!PageDirty(vmpage)) { |
1123 | struct cl_page_list *plist = &io->ci_queue.c2_qin; | |
3a52f803 | 1124 | struct vvp_page *vpg = cl_object_page_slice(obj, page); |
77605e41 | 1125 | int to = PAGE_SIZE; |
d7e09d03 PT |
1126 | |
1127 | /* vvp_page_assume() calls wait_on_page_writeback(). */ | |
1128 | cl_page_assume(env, io, page); | |
1129 | ||
77605e41 JX |
1130 | cl_page_list_init(plist); |
1131 | cl_page_list_add(plist, page); | |
1132 | ||
1133 | /* size fixup */ | |
3a52f803 | 1134 | if (last_index == vvp_index(vpg)) |
77605e41 | 1135 | to = size & ~PAGE_MASK; |
d7e09d03 PT |
1136 | |
1137 | /* Do not set Dirty bit here so that in case IO is | |
1138 | * started before the page is really made dirty, we | |
c0894c6c OD |
1139 | * still have chance to detect it. |
1140 | */ | |
77605e41 JX |
1141 | result = cl_io_commit_async(env, io, plist, 0, to, |
1142 | mkwrite_commit_callback); | |
d7e09d03 | 1143 | LASSERT(cl_page_is_owned(page, io)); |
77605e41 | 1144 | cl_page_list_fini(env, plist); |
d7e09d03 PT |
1145 | |
1146 | vmpage = NULL; | |
1147 | if (result < 0) { | |
d7e09d03 PT |
1148 | cl_page_discard(env, io, page); |
1149 | cl_page_disown(env, io, page); | |
1150 | ||
1151 | cl_page_put(env, page); | |
1152 | ||
1153 | /* we're in big trouble, what can we do now? */ | |
1154 | if (result == -EDQUOT) | |
1155 | result = -ENOSPC; | |
34e1f2bb | 1156 | goto out; |
da5ecb4d | 1157 | } else { |
d7e09d03 | 1158 | cl_page_disown(env, io, page); |
da5ecb4d | 1159 | } |
d7e09d03 PT |
1160 | } |
1161 | } | |
1162 | ||
d7e09d03 PT |
1163 | /* |
1164 | * The ft_index is only used in the case of | |
1165 | * a mkwrite action. We need to check | |
1166 | * our assertions are correct, since | |
1167 | * we should have caught this above | |
1168 | */ | |
77605e41 JX |
1169 | LASSERT(!fio->ft_mkwrite || fio->ft_index <= last_index); |
1170 | if (fio->ft_index == last_index) | |
d7e09d03 PT |
1171 | /* |
1172 | * Last page is mapped partially. | |
1173 | */ | |
1174 | fio->ft_nob = size - cl_offset(obj, fio->ft_index); | |
1175 | else | |
1176 | fio->ft_nob = cl_page_size(obj); | |
1177 | ||
1178 | lu_ref_add(&page->cp_reference, "fault", io); | |
1179 | fio->ft_page = page; | |
d7e09d03 PT |
1180 | |
1181 | out: | |
1182 | /* return unlocked vmpage to avoid deadlocking */ | |
6e16818b | 1183 | if (vmpage) |
d7e09d03 | 1184 | unlock_page(vmpage); |
10cdef73 JH |
1185 | |
1186 | cfio->ft_flags &= ~VM_FAULT_LOCKED; | |
1187 | ||
d7e09d03 PT |
1188 | return result; |
1189 | } | |
1190 | ||
1191 | static int vvp_io_fsync_start(const struct lu_env *env, | |
1192 | const struct cl_io_slice *ios) | |
1193 | { | |
1194 | /* we should mark TOWRITE bit to each dirty page in radix tree to | |
1195 | * verify pages have been written, but this is difficult because of | |
c0894c6c OD |
1196 | * race. |
1197 | */ | |
d7e09d03 PT |
1198 | return 0; |
1199 | } | |
1200 | ||
1e1db2a9 JX |
1201 | static int vvp_io_read_ahead(const struct lu_env *env, |
1202 | const struct cl_io_slice *ios, | |
1203 | pgoff_t start, struct cl_read_ahead *ra) | |
d7e09d03 | 1204 | { |
1e1db2a9 | 1205 | int result = 0; |
fd7444fe | 1206 | |
1e1db2a9 JX |
1207 | if (ios->cis_io->ci_type == CIT_READ || |
1208 | ios->cis_io->ci_type == CIT_FAULT) { | |
1209 | struct vvp_io *vio = cl2vvp_io(env, ios); | |
d7e09d03 | 1210 | |
1e1db2a9 JX |
1211 | if (unlikely(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) { |
1212 | ra->cra_end = CL_PAGE_EOF; | |
1213 | result = 1; /* no need to call down */ | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | return result; | |
d7e09d03 PT |
1218 | } |
1219 | ||
e3c9078a | 1220 | static void vvp_io_end(const struct lu_env *env, const struct cl_io_slice *ios) |
fee6eb50 JH |
1221 | { |
1222 | CLOBINVRNT(env, ios->cis_io->ci_obj, | |
1223 | vvp_object_invariant(ios->cis_io->ci_obj)); | |
1224 | } | |
1225 | ||
d7e09d03 PT |
1226 | static const struct cl_io_operations vvp_io_ops = { |
1227 | .op = { | |
1228 | [CIT_READ] = { | |
bc4320a9 | 1229 | .cio_fini = vvp_io_fini, |
d7e09d03 PT |
1230 | .cio_lock = vvp_io_read_lock, |
1231 | .cio_start = vvp_io_read_start, | |
10cdef73 | 1232 | .cio_advance = vvp_io_advance, |
d7e09d03 PT |
1233 | }, |
1234 | [CIT_WRITE] = { | |
1235 | .cio_fini = vvp_io_fini, | |
77605e41 JX |
1236 | .cio_iter_init = vvp_io_write_iter_init, |
1237 | .cio_iter_fini = vvp_io_write_iter_fini, | |
d7e09d03 PT |
1238 | .cio_lock = vvp_io_write_lock, |
1239 | .cio_start = vvp_io_write_start, | |
10cdef73 | 1240 | .cio_advance = vvp_io_advance, |
d7e09d03 PT |
1241 | }, |
1242 | [CIT_SETATTR] = { | |
1243 | .cio_fini = vvp_io_setattr_fini, | |
1244 | .cio_iter_init = vvp_io_setattr_iter_init, | |
1245 | .cio_lock = vvp_io_setattr_lock, | |
1246 | .cio_start = vvp_io_setattr_start, | |
1247 | .cio_end = vvp_io_setattr_end | |
1248 | }, | |
1249 | [CIT_FAULT] = { | |
1250 | .cio_fini = vvp_io_fault_fini, | |
1251 | .cio_iter_init = vvp_io_fault_iter_init, | |
1252 | .cio_lock = vvp_io_fault_lock, | |
1253 | .cio_start = vvp_io_fault_start, | |
10cdef73 | 1254 | .cio_end = vvp_io_end, |
d7e09d03 PT |
1255 | }, |
1256 | [CIT_FSYNC] = { | |
1257 | .cio_start = vvp_io_fsync_start, | |
1258 | .cio_fini = vvp_io_fini | |
1259 | }, | |
1260 | [CIT_MISC] = { | |
1261 | .cio_fini = vvp_io_fini | |
1262 | } | |
1263 | }, | |
1e1db2a9 | 1264 | .cio_read_ahead = vvp_io_read_ahead, |
d7e09d03 PT |
1265 | }; |
1266 | ||
1267 | int vvp_io_init(const struct lu_env *env, struct cl_object *obj, | |
1268 | struct cl_io *io) | |
1269 | { | |
1270 | struct vvp_io *vio = vvp_env_io(env); | |
8c7b0e1a | 1271 | struct inode *inode = vvp_object_inode(obj); |
d7e09d03 PT |
1272 | int result; |
1273 | ||
8c7b0e1a | 1274 | CLOBINVRNT(env, obj, vvp_object_invariant(obj)); |
d7e09d03 | 1275 | |
5ea17d6c JL |
1276 | CDEBUG(D_VFSTRACE, DFID |
1277 | " ignore/verify layout %d/%d, layout version %d restore needed %d\n", | |
1278 | PFID(lu_object_fid(&obj->co_lu)), | |
1279 | io->ci_ignore_layout, io->ci_verify_layout, | |
e0a8144b | 1280 | vio->vui_layout_gen, io->ci_restore_needed); |
5ea17d6c | 1281 | |
e0a8144b JH |
1282 | CL_IO_SLICE_CLEAN(vio, vui_cl); |
1283 | cl_io_slice_add(io, &vio->vui_cl, obj, &vvp_io_ops); | |
1284 | vio->vui_ra_valid = false; | |
d7e09d03 PT |
1285 | result = 0; |
1286 | if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) { | |
1287 | size_t count; | |
1288 | struct ll_inode_info *lli = ll_i2info(inode); | |
1289 | ||
1290 | count = io->u.ci_rw.crw_count; | |
1291 | /* "If nbyte is 0, read() will return 0 and have no other | |
c0894c6c OD |
1292 | * results." -- Single Unix Spec |
1293 | */ | |
d7e09d03 PT |
1294 | if (count == 0) |
1295 | result = 1; | |
b42b15fd | 1296 | else |
e0a8144b | 1297 | vio->vui_tot_count = count; |
b42b15fd | 1298 | |
d7e09d03 PT |
1299 | /* for read/write, we store the jobid in the inode, and |
1300 | * it'll be fetched by osc when building RPC. | |
1301 | * | |
1302 | * it's not accurate if the file is shared by different | |
1303 | * jobs. | |
1304 | */ | |
1305 | lustre_get_jobid(lli->lli_jobid); | |
1306 | } else if (io->ci_type == CIT_SETATTR) { | |
1307 | if (!cl_io_is_trunc(io)) | |
1308 | io->ci_lockreq = CILR_MANDATORY; | |
1309 | } | |
1310 | ||
1311 | /* ignore layout change for generic CIT_MISC but not for glimpse. | |
1312 | * io context for glimpse must set ci_verify_layout to true, | |
c0894c6c OD |
1313 | * see cl_glimpse_size0() for details. |
1314 | */ | |
d7e09d03 PT |
1315 | if (io->ci_type == CIT_MISC && !io->ci_verify_layout) |
1316 | io->ci_ignore_layout = 1; | |
1317 | ||
1318 | /* Enqueue layout lock and get layout version. We need to do this | |
1319 | * even for operations requiring to open file, such as read and write, | |
c0894c6c OD |
1320 | * because it might not grant layout lock in IT_OPEN. |
1321 | */ | |
65fb55d1 | 1322 | if (result == 0 && !io->ci_ignore_layout) { |
e0a8144b | 1323 | result = ll_layout_refresh(inode, &vio->vui_layout_gen); |
65fb55d1 NY |
1324 | if (result == -ENOENT) |
1325 | /* If the inode on MDS has been removed, but the objects | |
1326 | * on OSTs haven't been destroyed (async unlink), layout | |
d0a0acc3 | 1327 | * fetch will return -ENOENT, we'd ignore this error |
c0894c6c OD |
1328 | * and continue with dirty flush. LU-3230. |
1329 | */ | |
65fb55d1 NY |
1330 | result = 0; |
1331 | if (result < 0) | |
1332 | CERROR("%s: refresh file layout " DFID " error %d.\n", | |
e15ba45d OD |
1333 | ll_get_fsname(inode->i_sb, NULL, 0), |
1334 | PFID(lu_object_fid(&obj->co_lu)), result); | |
65fb55d1 | 1335 | } |
d7e09d03 | 1336 | |
0a3bdb00 | 1337 | return result; |
d7e09d03 | 1338 | } |