Whamcloud - gitweb
LU-2237 tests: new test for re-recreating last_rcvd
[fs/lustre-release.git] / lustre / osc / osc_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, Whamcloud, Inc.
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 OSC 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_OSC
43
44 #include "osc_cl_internal.h"
45
46 /** \addtogroup osc 
47  *  @{ 
48  */
49
50 /*****************************************************************************
51  *
52  * Type conversions.
53  *
54  */
55
56 static struct osc_req *cl2osc_req(const struct cl_req_slice *slice)
57 {
58         LINVRNT(slice->crs_dev->cd_lu_dev.ld_type == &osc_device_type);
59         return container_of0(slice, struct osc_req, or_cl);
60 }
61
62 static struct osc_io *cl2osc_io(const struct lu_env *env,
63                                 const struct cl_io_slice *slice)
64 {
65         struct osc_io *oio = container_of0(slice, struct osc_io, oi_cl);
66         LINVRNT(oio == osc_env_io(env));
67         return oio;
68 }
69
70 static struct osc_page *osc_cl_page_osc(struct cl_page *page)
71 {
72         const struct cl_page_slice *slice;
73
74         slice = cl_page_at(page, &osc_device_type);
75         LASSERT(slice != NULL);
76
77         return cl2osc_page(slice);
78 }
79
80
81 /*****************************************************************************
82  *
83  * io operations.
84  *
85  */
86
87 static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
88 {
89 }
90
91 /**
92  * An implementation of cl_io_operations::cio_io_submit() method for osc
93  * layer. Iterates over pages in the in-queue, prepares each for io by calling
94  * cl_page_prep() and then either submits them through osc_io_submit_page()
95  * or, if page is already submitted, changes osc flags through
96  * osc_set_async_flags().
97  */
98 static int osc_io_submit(const struct lu_env *env,
99                          const struct cl_io_slice *ios,
100                          enum cl_req_type crt, struct cl_2queue *queue)
101 {
102         struct cl_page    *page;
103         struct cl_page    *tmp;
104         struct client_obd *cli  = NULL;
105         struct osc_object *osc  = NULL; /* to keep gcc happy */
106         struct osc_page   *opg;
107         struct cl_io      *io;
108         CFS_LIST_HEAD     (list);
109
110         struct cl_page_list *qin      = &queue->c2_qin;
111         struct cl_page_list *qout     = &queue->c2_qout;
112         int queued = 0;
113         int result = 0;
114         int cmd;
115         int brw_flags;
116         int max_pages;
117
118         LASSERT(qin->pl_nr > 0);
119
120         CDEBUG(D_CACHE, "%d %d\n", qin->pl_nr, crt);
121
122         osc = cl2osc(ios->cis_obj);
123         cli = osc_cli(osc);
124         max_pages = cli->cl_max_pages_per_rpc;
125
126         cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
127         brw_flags = osc_io_srvlock(cl2osc_io(env, ios)) ? OBD_BRW_SRVLOCK : 0;
128
129         /*
130          * NOTE: here @page is a top-level page. This is done to avoid
131          *       creation of sub-page-list.
132          */
133         cl_page_list_for_each_safe(page, tmp, qin) {
134                 struct osc_async_page *oap;
135
136                 /* Top level IO. */
137                 io = page->cp_owner;
138                 LASSERT(io != NULL);
139
140                 opg = osc_cl_page_osc(page);
141                 oap = &opg->ops_oap;
142                 LASSERT(osc == oap->oap_obj);
143
144                 if (!cfs_list_empty(&oap->oap_pending_item) ||
145                     !cfs_list_empty(&oap->oap_rpc_item)) {
146                         CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
147                                oap, opg);
148                         result = -EBUSY;
149                         break;
150                 }
151
152                 result = cl_page_prep(env, io, page, crt);
153                 if (result != 0) {
154                         LASSERT(result < 0);
155                         if (result != -EALREADY)
156                                 break;
157                         /*
158                          * Handle -EALREADY error: for read case, the page is
159                          * already in UPTODATE state; for write, the page
160                          * is not dirty.
161                          */
162                         result = 0;
163                         continue;
164                 }
165
166                 cl_page_list_move(qout, qin, page);
167                 oap->oap_async_flags = ASYNC_URGENT|ASYNC_READY;
168                 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
169
170                 osc_page_submit(env, opg, crt, brw_flags);
171                 cfs_list_add_tail(&oap->oap_pending_item, &list);
172                 if (++queued == max_pages) {
173                         queued = 0;
174                         result = osc_queue_sync_pages(env, osc, &list, cmd,
175                                                       brw_flags);
176                         if (result < 0)
177                                 break;
178                 }
179         }
180
181         if (queued > 0)
182                 result = osc_queue_sync_pages(env, osc, &list, cmd, brw_flags);
183
184         CDEBUG(D_INFO, "%d/%d %d\n", qin->pl_nr, qout->pl_nr, result);
185         return qout->pl_nr > 0 ? 0 : result;
186 }
187
188 static void osc_page_touch_at(const struct lu_env *env,
189                               struct cl_object *obj, pgoff_t idx, unsigned to)
190 {
191         struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
192         struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
193         int valid;
194         __u64 kms;
195
196         /* offset within stripe */
197         kms = cl_offset(obj, idx) + to;
198
199         cl_object_attr_lock(obj);
200         /*
201          * XXX old code used
202          *
203          *         ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
204          *
205          * here
206          */
207         CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
208                kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
209                loi->loi_lvb.lvb_size);
210
211         valid = 0;
212         if (kms > loi->loi_kms) {
213                 attr->cat_kms = kms;
214                 valid |= CAT_KMS;
215         }
216         if (kms > loi->loi_lvb.lvb_size) {
217                 attr->cat_size = kms;
218                 valid |= CAT_SIZE;
219         }
220         cl_object_attr_set(env, obj, attr, valid);
221         cl_object_attr_unlock(obj);
222 }
223
224 /**
225  * This is called when a page is accessed within file in a way that creates
226  * new page, if one were missing (i.e., if there were a hole at that place in
227  * the file, or accessed page is beyond the current file size). Examples:
228  * ->commit_write() and ->nopage() methods.
229  *
230  * Expand stripe KMS if necessary.
231  */
232 static void osc_page_touch(const struct lu_env *env,
233                            struct osc_page *opage, unsigned to)
234 {
235         struct cl_page    *page = opage->ops_cl.cpl_page;
236         struct cl_object  *obj  = opage->ops_cl.cpl_obj;
237
238         osc_page_touch_at(env, obj, page->cp_index, to);
239 }
240
241 /**
242  * Implements cl_io_operations::cio_prepare_write() method for osc layer.
243  *
244  * \retval -EIO transfer initiated against this osc will most likely fail
245  * \retval 0    transfer initiated against this osc will most likely succeed.
246  *
247  * The reason for this check is to immediately return an error to the caller
248  * in the case of a deactivated import. Note, that import can be deactivated
249  * later, while pages, dirtied by this IO, are still in the cache, but this is
250  * irrelevant, because that would still return an error to the application (if
251  * it does fsync), but many applications don't do fsync because of performance
252  * issues, and we wanted to return an -EIO at write time to notify the
253  * application.
254  */
255 static int osc_io_prepare_write(const struct lu_env *env,
256                                 const struct cl_io_slice *ios,
257                                 const struct cl_page_slice *slice,
258                                 unsigned from, unsigned to)
259 {
260         struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
261         struct obd_import *imp = class_exp2cliimp(dev->od_exp);
262         struct osc_io     *oio = cl2osc_io(env, ios);
263         int result = 0;
264         ENTRY;
265
266         /*
267          * This implements OBD_BRW_CHECK logic from old client.
268          */
269
270         if (imp == NULL || imp->imp_invalid)
271                 result = -EIO;
272         if (result == 0 && oio->oi_lockless)
273                 /* this page contains `invalid' data, but who cares?
274                  * nobody can access the invalid data.
275                  * in osc_io_commit_write(), we're going to write exact
276                  * [from, to) bytes of this page to OST. -jay */
277                 cl_page_export(env, slice->cpl_page, 1);
278
279         RETURN(result);
280 }
281
282 static int osc_io_commit_write(const struct lu_env *env,
283                                const struct cl_io_slice *ios,
284                                const struct cl_page_slice *slice,
285                                unsigned from, unsigned to)
286 {
287         struct osc_io         *oio = cl2osc_io(env, ios);
288         struct osc_page       *opg = cl2osc_page(slice);
289         struct osc_object     *obj = cl2osc(opg->ops_cl.cpl_obj);
290         struct osc_async_page *oap = &opg->ops_oap;
291         ENTRY;
292
293         LASSERT(to > 0);
294         /*
295          * XXX instead of calling osc_page_touch() here and in
296          * osc_io_fault_start() it might be more logical to introduce
297          * cl_page_touch() method, that generic cl_io_commit_write() and page
298          * fault code calls.
299          */
300         osc_page_touch(env, cl2osc_page(slice), to);
301         if (!client_is_remote(osc_export(obj)) &&
302             cfs_capable(CFS_CAP_SYS_RESOURCE))
303                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
304
305         if (oio->oi_lockless)
306                 /* see osc_io_prepare_write() for lockless io handling. */
307                 cl_page_clip(env, slice->cpl_page, from, to);
308
309         RETURN(0);
310 }
311
312 static int osc_io_fault_start(const struct lu_env *env,
313                               const struct cl_io_slice *ios)
314 {
315         struct cl_io       *io;
316         struct cl_fault_io *fio;
317
318         ENTRY;
319
320         io  = ios->cis_io;
321         fio = &io->u.ci_fault;
322         CDEBUG(D_INFO, "%lu %d %d\n",
323                fio->ft_index, fio->ft_writable, fio->ft_nob);
324         /*
325          * If mapping is writeable, adjust kms to cover this page,
326          * but do not extend kms beyond actual file size.
327          * See bug 10919.
328          */
329         if (fio->ft_writable)
330                 osc_page_touch_at(env, ios->cis_obj,
331                                   fio->ft_index, fio->ft_nob);
332         RETURN(0);
333 }
334
335 static int osc_async_upcall(void *a, int rc)
336 {
337         struct osc_async_cbargs *args = a;
338
339         args->opc_rc = rc;
340         complete(&args->opc_sync);
341         return 0;
342 }
343
344 #if defined(__KERNEL__)
345 /**
346  * Checks that there are no pages being written in the extent being truncated.
347  */
348 static int trunc_check_cb(const struct lu_env *env, struct cl_io *io,
349                           struct cl_page *page, void *cbdata)
350 {
351         const struct cl_page_slice *slice;
352         struct osc_page *ops;
353         struct osc_async_page *oap;
354         __u64 start = *(__u64 *)cbdata;
355
356         slice = cl_page_at(page, &osc_device_type);
357         LASSERT(slice != NULL);
358         ops = cl2osc_page(slice);
359         oap = &ops->ops_oap;
360
361         if (oap->oap_cmd & OBD_BRW_WRITE &&
362             !cfs_list_empty(&oap->oap_pending_item))
363                 CL_PAGE_DEBUG(D_ERROR, env, page, "exists " LPU64 "/%s.\n",
364                                 start, current->comm);
365
366 #ifdef __linux__
367         {
368                 cfs_page_t *vmpage = cl_page_vmpage(env, page);
369                 if (PageLocked(vmpage))
370                         CDEBUG(D_CACHE, "page %p index %lu locked for %d.\n",
371                                ops, page->cp_index,
372                                (oap->oap_cmd & OBD_BRW_RWMASK));
373         }
374 #endif
375
376         return CLP_GANG_OKAY;
377 }
378
379 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
380                             struct osc_io *oio, __u64 size)
381 {
382         struct cl_object *clob;
383         int     partial;
384         pgoff_t start;
385
386         clob    = oio->oi_cl.cis_obj;
387         start   = cl_index(clob, size);
388         partial = cl_offset(clob, start) < size;
389
390         /*
391          * Complain if there are pages in the truncated region.
392          */
393         cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF,
394                             trunc_check_cb, (void *)&size);
395 }
396 #else /* __KERNEL__ */
397 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
398                             struct osc_io *oio, __u64 size)
399 {
400         return;
401 }
402 #endif
403
404 static int osc_io_setattr_start(const struct lu_env *env,
405                                 const struct cl_io_slice *slice)
406 {
407         struct cl_io            *io     = slice->cis_io;
408         struct osc_io           *oio    = cl2osc_io(env, slice);
409         struct cl_object        *obj    = slice->cis_obj;
410         struct lov_oinfo        *loi    = cl2osc(obj)->oo_oinfo;
411         struct cl_attr          *attr   = &osc_env_info(env)->oti_attr;
412         struct obdo             *oa     = &oio->oi_oa;
413         struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
414         __u64                    size   = io->u.ci_setattr.sa_attr.lvb_size;
415         unsigned int             ia_valid = io->u.ci_setattr.sa_valid;
416         int                      result = 0;
417         struct obd_info          oinfo = { { { 0 } } };
418
419         /* truncate cache dirty pages first */
420         if (cl_io_is_trunc(io))
421                 result = osc_cache_truncate_start(env, oio, cl2osc(obj), size);
422
423         if (result == 0 && oio->oi_lockless == 0) {
424                 cl_object_attr_lock(obj);
425                 result = cl_object_attr_get(env, obj, attr);
426                 if (result == 0) {
427                         struct ost_lvb *lvb = &io->u.ci_setattr.sa_attr;
428                         unsigned int cl_valid = 0;
429
430                         if (ia_valid & ATTR_SIZE) {
431                                 attr->cat_size = attr->cat_kms = size;
432                                 cl_valid = (CAT_SIZE | CAT_KMS);
433                         }
434                         if (ia_valid & ATTR_MTIME_SET) {
435                                 attr->cat_mtime = lvb->lvb_mtime;
436                                 cl_valid |= CAT_MTIME;
437                         }
438                         if (ia_valid & ATTR_ATIME_SET) {
439                                 attr->cat_atime = lvb->lvb_atime;
440                                 cl_valid |= CAT_ATIME;
441                         }
442                         if (ia_valid & ATTR_CTIME_SET) {
443                                 attr->cat_ctime = lvb->lvb_ctime;
444                                 cl_valid |= CAT_CTIME;
445                         }
446                         result = cl_object_attr_set(env, obj, attr, cl_valid);
447                 }
448                 cl_object_attr_unlock(obj);
449         }
450         memset(oa, 0, sizeof(*oa));
451         if (result == 0) {
452                 oa->o_id = loi->loi_id;
453                 oa->o_seq = loi->loi_seq;
454                 oa->o_mtime = attr->cat_mtime;
455                 oa->o_atime = attr->cat_atime;
456                 oa->o_ctime = attr->cat_ctime;
457                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
458                         OBD_MD_FLCTIME | OBD_MD_FLMTIME;
459                 if (ia_valid & ATTR_SIZE) {
460                         oa->o_size = size;
461                         oa->o_blocks = OBD_OBJECT_EOF;
462                         oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
463
464                         if (oio->oi_lockless) {
465                                 oa->o_flags = OBD_FL_SRVLOCK;
466                                 oa->o_valid |= OBD_MD_FLFLAGS;
467                         }
468                 } else {
469                         LASSERT(oio->oi_lockless == 0);
470                 }
471
472                 oinfo.oi_oa = oa;
473                 oinfo.oi_capa = io->u.ci_setattr.sa_capa;
474                 init_completion(&cbargs->opc_sync);
475
476                 if (ia_valid & ATTR_SIZE)
477                         result = osc_punch_base(osc_export(cl2osc(obj)),
478                                                 &oinfo, osc_async_upcall,
479                                                 cbargs, PTLRPCD_SET);
480                 else
481                         result = osc_setattr_async_base(osc_export(cl2osc(obj)),
482                                                         &oinfo, NULL,
483                                                         osc_async_upcall,
484                                                         cbargs, PTLRPCD_SET);
485                 cbargs->opc_rpc_sent = result == 0;
486         }
487         return result;
488 }
489
490 static void osc_io_setattr_end(const struct lu_env *env,
491                                const struct cl_io_slice *slice)
492 {
493         struct cl_io     *io  = slice->cis_io;
494         struct osc_io    *oio = cl2osc_io(env, slice);
495         struct cl_object *obj = slice->cis_obj;
496         struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
497         int result = 0;
498
499         if (cbargs->opc_rpc_sent) {
500                 wait_for_completion(&cbargs->opc_sync);
501                 result = io->ci_result = cbargs->opc_rc;
502         }
503         if (result == 0) {
504                 if (oio->oi_lockless) {
505                         /* lockless truncate */
506                         struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
507
508                         LASSERT(cl_io_is_trunc(io));
509                         /* XXX: Need a lock. */
510                         osd->od_stats.os_lockless_truncates++;
511                 }
512         }
513
514         if (cl_io_is_trunc(io)) {
515                 __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
516                 osc_trunc_check(env, io, oio, size);
517                 if (oio->oi_trunc != NULL) {
518                         osc_cache_truncate_end(env, oio, cl2osc(obj));
519                         oio->oi_trunc = NULL;
520                 }
521         }
522 }
523
524 static int osc_io_read_start(const struct lu_env *env,
525                              const struct cl_io_slice *slice)
526 {
527         struct osc_io    *oio   = cl2osc_io(env, slice);
528         struct cl_object *obj   = slice->cis_obj;
529         struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
530         int              result = 0;
531         ENTRY;
532
533         if (oio->oi_lockless == 0) {
534                 cl_object_attr_lock(obj);
535                 result = cl_object_attr_get(env, obj, attr);
536                 if (result == 0) {
537                         attr->cat_atime = LTIME_S(CFS_CURRENT_TIME);
538                         result = cl_object_attr_set(env, obj, attr,
539                                                     CAT_ATIME);
540                 }
541                 cl_object_attr_unlock(obj);
542         }
543         RETURN(result);
544 }
545
546 static int osc_io_write_start(const struct lu_env *env,
547                               const struct cl_io_slice *slice)
548 {
549         struct osc_io    *oio   = cl2osc_io(env, slice);
550         struct cl_object *obj   = slice->cis_obj;
551         struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
552         int              result = 0;
553         ENTRY;
554
555         if (oio->oi_lockless == 0) {
556                 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_DELAY_SETTIME, 1);
557                 cl_object_attr_lock(obj);
558                 result = cl_object_attr_get(env, obj, attr);
559                 if (result == 0) {
560                         attr->cat_mtime = attr->cat_ctime =
561                                 LTIME_S(CFS_CURRENT_TIME);
562                         result = cl_object_attr_set(env, obj, attr,
563                                                     CAT_MTIME | CAT_CTIME);
564                 }
565                 cl_object_attr_unlock(obj);
566         }
567         RETURN(result);
568 }
569
570 static int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
571                          struct cl_fsync_io *fio)
572 {
573         struct osc_io    *oio   = osc_env_io(env);
574         struct obdo      *oa    = &oio->oi_oa;
575         struct obd_info  *oinfo = &oio->oi_info;
576         struct lov_oinfo *loi   = obj->oo_oinfo;
577         struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
578         int rc = 0;
579         ENTRY;
580
581         memset(oa, 0, sizeof(*oa));
582         oa->o_id = loi->loi_id;
583         oa->o_seq = loi->loi_seq;
584         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
585
586         /* reload size abd blocks for start and end of sync range */
587         oa->o_size = fio->fi_start;
588         oa->o_blocks = fio->fi_end;
589         oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
590
591         obdo_set_parent_fid(oa, fio->fi_fid);
592
593         memset(oinfo, 0, sizeof(*oinfo));
594         oinfo->oi_oa = oa;
595         oinfo->oi_capa = fio->fi_capa;
596         init_completion(&cbargs->opc_sync);
597
598         rc = osc_sync_base(osc_export(obj), oinfo, osc_async_upcall, cbargs,
599                            PTLRPCD_SET);
600         RETURN(rc);
601 }
602
603 static int osc_io_fsync_start(const struct lu_env *env,
604                               const struct cl_io_slice *slice)
605 {
606         struct cl_io       *io  = slice->cis_io;
607         struct cl_fsync_io *fio = &io->u.ci_fsync;
608         struct cl_object   *obj = slice->cis_obj;
609         struct osc_object  *osc = cl2osc(obj);
610         pgoff_t start  = cl_index(obj, fio->fi_start);
611         pgoff_t end    = cl_index(obj, fio->fi_end);
612         int     result = 0;
613         ENTRY;
614
615         if (fio->fi_end == OBD_OBJECT_EOF)
616                 end = CL_PAGE_EOF;
617
618         result = osc_cache_writeback_range(env, osc, start, end, 0,
619                                            fio->fi_mode == CL_FSYNC_DISCARD);
620         if (result > 0) {
621                 fio->fi_nr_written += result;
622                 result = 0;
623         }
624         if (fio->fi_mode == CL_FSYNC_ALL) {
625                 int rc;
626
627                 /* we have to wait for writeback to finish before we can
628                  * send OST_SYNC RPC. This is bad because it causes extents
629                  * to be written osc by osc. However, we usually start
630                  * writeback before CL_FSYNC_ALL so this won't have any real
631                  * problem. */
632                 rc = osc_cache_wait_range(env, osc, start, end);
633                 if (result == 0)
634                         result = rc;
635                 rc = osc_fsync_ost(env, osc, fio);
636                 if (result == 0)
637                         result = rc;
638         }
639
640         RETURN(result);
641 }
642
643 static void osc_io_fsync_end(const struct lu_env *env,
644                              const struct cl_io_slice *slice)
645 {
646         struct cl_fsync_io *fio = &slice->cis_io->u.ci_fsync;
647         struct cl_object   *obj = slice->cis_obj;
648         pgoff_t start = cl_index(obj, fio->fi_start);
649         pgoff_t end   = cl_index(obj, fio->fi_end);
650         int result = 0;
651
652         if (fio->fi_mode == CL_FSYNC_LOCAL) {
653                 result = osc_cache_wait_range(env, cl2osc(obj), start, end);
654         } else if (fio->fi_mode == CL_FSYNC_ALL) {
655                 struct osc_io           *oio    = cl2osc_io(env, slice);
656                 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
657
658                 wait_for_completion(&cbargs->opc_sync);
659                 if (result == 0)
660                         result = cbargs->opc_rc;
661         }
662         slice->cis_io->ci_result = result;
663 }
664
665 static void osc_io_end(const struct lu_env *env,
666                        const struct cl_io_slice *slice)
667 {
668         struct osc_io *oio = cl2osc_io(env, slice);
669
670         if (oio->oi_active) {
671                 osc_extent_release(env, oio->oi_active);
672                 oio->oi_active = NULL;
673         }
674 }
675
676 static const struct cl_io_operations osc_io_ops = {
677         .op = {
678                 [CIT_READ] = {
679                         .cio_start  = osc_io_read_start,
680                         .cio_fini   = osc_io_fini
681                 },
682                 [CIT_WRITE] = {
683                         .cio_start  = osc_io_write_start,
684                         .cio_end    = osc_io_end,
685                         .cio_fini   = osc_io_fini
686                 },
687                 [CIT_SETATTR] = {
688                         .cio_start  = osc_io_setattr_start,
689                         .cio_end    = osc_io_setattr_end
690                 },
691                 [CIT_FAULT] = {
692                         .cio_start  = osc_io_fault_start,
693                         .cio_end    = osc_io_end,
694                         .cio_fini   = osc_io_fini
695                 },
696                 [CIT_FSYNC] = {
697                         .cio_start  = osc_io_fsync_start,
698                         .cio_end    = osc_io_fsync_end,
699                         .cio_fini   = osc_io_fini
700                 },
701                 [CIT_MISC] = {
702                         .cio_fini   = osc_io_fini
703                 }
704         },
705         .req_op = {
706                  [CRT_READ] = {
707                          .cio_submit    = osc_io_submit
708                  },
709                  [CRT_WRITE] = {
710                          .cio_submit    = osc_io_submit
711                  }
712          },
713         .cio_prepare_write = osc_io_prepare_write,
714         .cio_commit_write  = osc_io_commit_write
715 };
716
717 /*****************************************************************************
718  *
719  * Transfer operations.
720  *
721  */
722
723 static int osc_req_prep(const struct lu_env *env,
724                         const struct cl_req_slice *slice)
725 {
726         return 0;
727 }
728
729 static void osc_req_completion(const struct lu_env *env,
730                                const struct cl_req_slice *slice, int ioret)
731 {
732         struct osc_req *or;
733
734         or = cl2osc_req(slice);
735         OBD_SLAB_FREE_PTR(or, osc_req_kmem);
736 }
737
738 /**
739  * Implementation of struct cl_req_operations::cro_attr_set() for osc
740  * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
741  * fields.
742  */
743 static void osc_req_attr_set(const struct lu_env *env,
744                              const struct cl_req_slice *slice,
745                              const struct cl_object *obj,
746                              struct cl_req_attr *attr, obd_valid flags)
747 {
748         struct lov_oinfo *oinfo;
749         struct cl_req    *clerq;
750         struct cl_page   *apage; /* _some_ page in @clerq */
751         struct cl_lock   *lock;  /* _some_ lock protecting @apage */
752         struct osc_lock  *olck;
753         struct osc_page  *opg;
754         struct obdo      *oa;
755         struct ost_lvb   *lvb;
756
757         oinfo   = cl2osc(obj)->oo_oinfo;
758         lvb     = &oinfo->loi_lvb;
759         oa      = attr->cra_oa;
760
761         if ((flags & OBD_MD_FLMTIME) != 0) {
762                 oa->o_mtime = lvb->lvb_mtime;
763                 oa->o_valid |= OBD_MD_FLMTIME;
764         }
765         if ((flags & OBD_MD_FLATIME) != 0) {
766                 oa->o_atime = lvb->lvb_atime;
767                 oa->o_valid |= OBD_MD_FLATIME;
768         }
769         if ((flags & OBD_MD_FLCTIME) != 0) {
770                 oa->o_ctime = lvb->lvb_ctime;
771                 oa->o_valid |= OBD_MD_FLCTIME;
772         }
773         if (flags & OBD_MD_FLID) {
774                 oa->o_id = oinfo->loi_id;
775                 oa->o_valid |= OBD_MD_FLID;
776         }
777         if (flags & OBD_MD_FLGROUP) {
778                 oa->o_seq = oinfo->loi_seq;
779                 oa->o_valid |= OBD_MD_FLGROUP;
780         }
781         if (flags & OBD_MD_FLHANDLE) {
782                 clerq = slice->crs_req;
783                 LASSERT(!cfs_list_empty(&clerq->crq_pages));
784                 apage = container_of(clerq->crq_pages.next,
785                                      struct cl_page, cp_flight);
786                 opg = osc_cl_page_osc(apage);
787                 apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
788                 lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
789                 if (lock == NULL) {
790                         struct cl_object_header *head;
791                         struct cl_lock          *scan;
792
793                         head = cl_object_header(apage->cp_obj);
794                         cfs_list_for_each_entry(scan, &head->coh_locks,
795                                                 cll_linkage)
796                                 CL_LOCK_DEBUG(D_ERROR, env, scan,
797                                               "no cover page!\n");
798                         CL_PAGE_DEBUG(D_ERROR, env, apage,
799                                       "dump uncover page!\n");
800                         libcfs_debug_dumpstack(NULL);
801                         LBUG();
802                 }
803
804                 olck = osc_lock_at(lock);
805                 LASSERT(olck != NULL);
806                 LASSERT(ergo(opg->ops_srvlock, olck->ols_lock == NULL));
807                 /* check for lockless io. */
808                 if (olck->ols_lock != NULL) {
809                         oa->o_handle = olck->ols_lock->l_remote_handle;
810                         oa->o_valid |= OBD_MD_FLHANDLE;
811                 }
812                 cl_lock_put(env, lock);
813         }
814 }
815
816 static const struct cl_req_operations osc_req_ops = {
817         .cro_prep       = osc_req_prep,
818         .cro_attr_set   = osc_req_attr_set,
819         .cro_completion = osc_req_completion
820 };
821
822
823 int osc_io_init(const struct lu_env *env,
824                 struct cl_object *obj, struct cl_io *io)
825 {
826         struct osc_io *oio = osc_env_io(env);
827
828         CL_IO_SLICE_CLEAN(oio, oi_cl);
829         cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
830         return 0;
831 }
832
833 int osc_req_init(const struct lu_env *env, struct cl_device *dev,
834                  struct cl_req *req)
835 {
836         struct osc_req *or;
837         int result;
838
839         OBD_SLAB_ALLOC_PTR_GFP(or, osc_req_kmem, CFS_ALLOC_IO);
840         if (or != NULL) {
841                 cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);
842                 result = 0;
843         } else
844                 result = -ENOMEM;
845         return result;
846 }
847
848 /** @} osc */