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