Whamcloud - gitweb
21080e30d9304039e87dc44024d20809b59e394f
[fs/lustre-release.git] / lustre / osc / osc_io.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * Implementation of cl_io for OSC layer.
40  *
41  *   Author: Nikita Danilov <nikita.danilov@sun.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_OSC
45
46 #include "osc_cl_internal.h"
47
48 /** \addtogroup osc 
49  *  @{ 
50  */
51
52 /*****************************************************************************
53  *
54  * Type conversions.
55  *
56  */
57
58 static struct osc_req *cl2osc_req(const struct cl_req_slice *slice)
59 {
60         LINVRNT(slice->crs_dev->cd_lu_dev.ld_type == &osc_device_type);
61         return container_of0(slice, struct osc_req, or_cl);
62 }
63
64 static struct osc_io *cl2osc_io(const struct lu_env *env,
65                                 const struct cl_io_slice *slice)
66 {
67         struct osc_io *oio = container_of0(slice, struct osc_io, oi_cl);
68         LINVRNT(oio == osc_env_io(env));
69         return oio;
70 }
71
72 static struct osc_page *osc_cl_page_osc(struct cl_page *page)
73 {
74         const struct cl_page_slice *slice;
75
76         slice = cl_page_at(page, &osc_device_type);
77         LASSERT(slice != NULL);
78
79         return cl2osc_page(slice);
80 }
81
82
83 /*****************************************************************************
84  *
85  * io operations.
86  *
87  */
88
89 static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
90 {
91 }
92
93 struct cl_page *osc_oap2cl_page(struct osc_async_page *oap)
94 {
95         return container_of(oap, struct osc_page, ops_oap)->ops_cl.cpl_page;
96 }
97
98 static void osc_io_unplug(const struct lu_env *env, struct osc_object *osc,
99                           struct client_obd *cli)
100 {
101         loi_list_maint(cli, osc->oo_oinfo);
102         osc_check_rpcs(env, cli);
103         client_obd_list_unlock(&cli->cl_loi_list_lock);
104 }
105
106 /**
107  * An implementation of cl_io_operations::cio_io_submit() method for osc
108  * layer. Iterates over pages in the in-queue, prepares each for io by calling
109  * cl_page_prep() and then either submits them through osc_io_submit_page()
110  * or, if page is already submitted, changes osc flags through
111  * osc_set_async_flags_base().
112  */
113 static int osc_io_submit(const struct lu_env *env,
114                          const struct cl_io_slice *ios,
115                          enum cl_req_type crt, struct cl_2queue *queue,
116                          enum cl_req_priority priority)
117 {
118         struct cl_page    *page;
119         struct cl_page    *tmp;
120         struct osc_object *osc0 = NULL;
121         struct client_obd *cli  = NULL;
122         struct osc_object *osc  = NULL; /* to keep gcc happy */
123         struct osc_page   *opg;
124         struct cl_io      *io;
125
126         struct cl_page_list *qin      = &queue->c2_qin;
127         struct cl_page_list *qout     = &queue->c2_qout;
128         int queued = 0;
129         int result = 0;
130
131         LASSERT(qin->pl_nr > 0);
132
133         CDEBUG(D_INFO, "%d %d\n", qin->pl_nr, crt);
134         /*
135          * NOTE: here @page is a top-level page. This is done to avoid
136          *       creation of sub-page-list.
137          */
138         cl_page_list_for_each_safe(page, tmp, qin) {
139                 struct osc_async_page *oap;
140                 struct obd_export     *exp;
141
142                 /* Top level IO. */
143                 io = page->cp_owner;
144                 LASSERT(io != NULL);
145
146                 opg = osc_cl_page_osc(page);
147                 oap = &opg->ops_oap;
148                 osc = cl2osc(opg->ops_cl.cpl_obj);
149                 exp = osc_export(osc);
150
151                 if (priority > CRP_NORMAL) {
152                         cfs_spin_lock(&oap->oap_lock);
153                         oap->oap_async_flags |= ASYNC_HP;
154                         cfs_spin_unlock(&oap->oap_lock);
155                 }
156                 /*
157                  * This can be checked without cli->cl_loi_list_lock, because
158                  * ->oap_*_item are always manipulated when the page is owned.
159                  */
160                 if (!cfs_list_empty(&oap->oap_urgent_item) ||
161                     !cfs_list_empty(&oap->oap_rpc_item)) {
162                         result = -EBUSY;
163                         break;
164                 }
165
166                 if (osc0 == NULL) { /* first iteration */
167                         cli = &exp->exp_obd->u.cli;
168                         osc0 = osc;
169                 } else /* check that all pages are against the same object
170                         * (for now) */
171                         LASSERT(osc == osc0);
172                 if (queued++ == 0)
173                         client_obd_list_lock(&cli->cl_loi_list_lock);
174                 result = cl_page_prep(env, io, page, crt);
175                 if (result == 0) {
176                         cl_page_list_move(qout, qin, page);
177                         if (cfs_list_empty(&oap->oap_pending_item)) {
178                                 osc_io_submit_page(env, cl2osc_io(env, ios),
179                                                    opg, crt);
180                         } else {
181                                 result = osc_set_async_flags_base(cli,
182                                                                   osc->oo_oinfo,
183                                                                   oap,
184                                                                   OSC_FLAGS);
185                                 /*
186                                  * bug 18881: we can't just break out here when
187                                  * error occurs after cl_page_prep has been
188                                  * called against the page. The correct
189                                  * way is to call page's completion routine,
190                                  * as in osc_oap_interrupted.  For simplicity,
191                                  * we just force osc_set_async_flags_base() to
192                                  * not return error.
193                                  */
194                                 LASSERT(result == 0);
195                         }
196                         opg->ops_submit_time = cfs_time_current();
197                 } else {
198                         LASSERT(result < 0);
199                         if (result != -EALREADY)
200                                 break;
201                         /*
202                          * Handle -EALREADY error: for read case, the page is
203                          * already in UPTODATE state; for write, the page
204                          * is not dirty.
205                          */
206                         result = 0;
207                 }
208
209                 /*
210                  * We might hold client_obd_list_lock() for too long and cause
211                  * soft-lockups (see bug 16651). But on the other hand, pages
212                  * are queued here with ASYNC_URGENT flag, thus will be sent
213                  * out immediately once osc_io_unplug() be called, possibly
214                  * resulting sub-optimal RPCs.
215                  *
216                  * We think creating optimal-sized RPCs is more important than
217                  * avoiding the transient soft-lockups, plus I believe the
218                  * soft-locks only happen in full debug testing.
219                  */
220         }
221
222         LASSERT(ergo(result == 0, cli != NULL));
223         LASSERT(ergo(result == 0, osc == osc0));
224
225         if (queued > 0)
226                 osc_io_unplug(env, osc, cli);
227         CDEBUG(D_INFO, "%d/%d %d\n", qin->pl_nr, qout->pl_nr, result);
228         return qout->pl_nr > 0 ? 0 : result;
229 }
230
231 static void osc_page_touch_at(const struct lu_env *env,
232                               struct cl_object *obj, pgoff_t idx, unsigned to)
233 {
234         struct lov_oinfo  *loi  = cl2osc(obj)->oo_oinfo;
235         struct cl_attr    *attr = &osc_env_info(env)->oti_attr;
236         int valid;
237         __u64 kms;
238
239         /* offset within stripe */
240         kms = cl_offset(obj, idx) + to;
241
242         cl_object_attr_lock(obj);
243         /*
244          * XXX old code used
245          *
246          *         ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
247          *
248          * here
249          */
250         CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
251                kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
252                loi->loi_lvb.lvb_size);
253
254         valid = 0;
255         if (kms > loi->loi_kms) {
256                 attr->cat_kms = kms;
257                 valid |= CAT_KMS;
258         }
259         if (kms > loi->loi_lvb.lvb_size) {
260                 attr->cat_size = kms;
261                 valid |= CAT_SIZE;
262         }
263         cl_object_attr_set(env, obj, attr, valid);
264         cl_object_attr_unlock(obj);
265 }
266
267 /**
268  * This is called when a page is accessed within file in a way that creates
269  * new page, if one were missing (i.e., if there were a hole at that place in
270  * the file, or accessed page is beyond the current file size). Examples:
271  * ->commit_write() and ->nopage() methods.
272  *
273  * Expand stripe KMS if necessary.
274  */
275 static void osc_page_touch(const struct lu_env *env,
276                            struct osc_page *opage, unsigned to)
277 {
278         struct cl_page    *page = opage->ops_cl.cpl_page;
279         struct cl_object  *obj  = opage->ops_cl.cpl_obj;
280
281         osc_page_touch_at(env, obj, page->cp_index, to);
282 }
283
284 /**
285  * Implements cl_io_operations::cio_prepare_write() method for osc layer.
286  *
287  * \retval -EIO transfer initiated against this osc will most likely fail
288  * \retval 0    transfer initiated against this osc will most likely succeed.
289  *
290  * The reason for this check is to immediately return an error to the caller
291  * in the case of a deactivated import. Note, that import can be deactivated
292  * later, while pages, dirtied by this IO, are still in the cache, but this is
293  * irrelevant, because that would still return an error to the application (if
294  * it does fsync), but many applications don't do fsync because of performance
295  * issues, and we wanted to return an -EIO at write time to notify the
296  * application.
297  */
298 static int osc_io_prepare_write(const struct lu_env *env,
299                                 const struct cl_io_slice *ios,
300                                 const struct cl_page_slice *slice,
301                                 unsigned from, unsigned to)
302 {
303         struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
304         struct obd_import *imp = class_exp2cliimp(dev->od_exp);
305         struct osc_io     *oio = cl2osc_io(env, ios);
306         int result = 0;
307         ENTRY;
308
309         /*
310          * This implements OBD_BRW_CHECK logic from old client.
311          */
312
313         if (imp == NULL || imp->imp_invalid)
314                 result = -EIO;
315         if (result == 0 && oio->oi_lockless)
316                 /* this page contains `invalid' data, but who cares?
317                  * nobody can access the invalid data.
318                  * in osc_io_commit_write(), we're going to write exact
319                  * [from, to) bytes of this page to OST. -jay */
320                 cl_page_export(env, slice->cpl_page, 1);
321
322         RETURN(result);
323 }
324
325 static int osc_io_commit_write(const struct lu_env *env,
326                                const struct cl_io_slice *ios,
327                                const struct cl_page_slice *slice,
328                                unsigned from, unsigned to)
329 {
330         struct osc_io         *oio = cl2osc_io(env, ios);
331         struct osc_page       *opg = cl2osc_page(slice);
332         struct osc_object     *obj = cl2osc(opg->ops_cl.cpl_obj);
333         struct osc_async_page *oap = &opg->ops_oap;
334         ENTRY;
335
336         LASSERT(to > 0);
337         /*
338          * XXX instead of calling osc_page_touch() here and in
339          * osc_io_fault_start() it might be more logical to introduce
340          * cl_page_touch() method, that generic cl_io_commit_write() and page
341          * fault code calls.
342          */
343         osc_page_touch(env, cl2osc_page(slice), to);
344         if (!client_is_remote(osc_export(obj)) &&
345             cfs_capable(CFS_CAP_SYS_RESOURCE))
346                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
347
348         if (oio->oi_lockless)
349                 /* see osc_io_prepare_write() for lockless io handling. */
350                 cl_page_clip(env, slice->cpl_page, from, to);
351
352         RETURN(0);
353 }
354
355 static int osc_io_fault_start(const struct lu_env *env,
356                               const struct cl_io_slice *ios)
357 {
358         struct cl_io       *io;
359         struct cl_fault_io *fio;
360
361         ENTRY;
362
363         io  = ios->cis_io;
364         fio = &io->u.ci_fault;
365         CDEBUG(D_INFO, "%lu %d %d\n",
366                fio->ft_index, fio->ft_writable, fio->ft_nob);
367         /*
368          * If mapping is writeable, adjust kms to cover this page,
369          * but do not extend kms beyond actual file size.
370          * See bug 10919.
371          */
372         if (fio->ft_writable)
373                 osc_page_touch_at(env, ios->cis_obj,
374                                   fio->ft_index, fio->ft_nob);
375         RETURN(0);
376 }
377
378 static int osc_setattr_upcall(void *a, int rc)
379 {
380         struct osc_setattr_cbargs *args = a;
381
382         args->opc_rc = rc;
383         cfs_complete(&args->opc_sync);
384         return 0;
385 }
386
387 /* Disable osc_trunc_check() because it is naturally race between read and
388  * truncate. See bug 20645 for details.
389  */
390 #if 0 && defined(__KERNEL__)
391 /**
392  * Checks that there are no pages being written in the extent being truncated.
393  */
394 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
395                             struct osc_io *oio, size_t size)
396 {
397         struct osc_page     *cp;
398         struct osc_object   *obj;
399         struct cl_object    *clob;
400         struct cl_page      *page;
401         struct cl_page_list *list;
402         int                  partial;
403         pgoff_t              start;
404
405         clob    = oio->oi_cl.cis_obj;
406         obj     = cl2osc(clob);
407         start   = cl_index(clob, size);
408         partial = cl_offset(clob, start) < size;
409         list    = &osc_env_info(env)->oti_plist;
410
411         /*
412          * Complain if there are pages in the truncated region.
413          *
414          * XXX this is quite expensive check.
415          */
416         cl_page_list_init(list);
417         cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF, list);
418
419         cl_page_list_for_each(page, list)
420                 CL_PAGE_DEBUG(D_ERROR, env, page, "exists %lu\n", start);
421
422         cl_page_list_disown(env, io, list);
423         cl_page_list_fini(env, list);
424
425         cfs_spin_lock(&obj->oo_seatbelt);
426         cfs_list_for_each_entry(cp, &obj->oo_inflight[CRT_WRITE],
427                                 ops_inflight) {
428                 page = cp->ops_cl.cpl_page;
429                 if (page->cp_index >= start + partial) {
430                         cfs_task_t *submitter;
431
432                         submitter = cp->ops_submitter;
433                         /*
434                          * XXX Linux specific debugging stuff.
435                          */
436                         CL_PAGE_DEBUG(D_ERROR, env, page, "%s/%d %lu\n",
437                                       submitter->comm, submitter->pid, start);
438                         libcfs_debug_dumpstack(submitter);
439                 }
440         }
441         cfs_spin_unlock(&obj->oo_seatbelt);
442 }
443 #else /* __KERNEL__ */
444 # define osc_trunc_check(env, io, oio, size) do {;} while (0)
445 #endif
446
447 static int osc_io_setattr_start(const struct lu_env *env,
448                                 const struct cl_io_slice *slice)
449 {
450         struct cl_io            *io     = slice->cis_io;
451         struct osc_io           *oio    = cl2osc_io(env, slice);
452         struct cl_object        *obj    = slice->cis_obj;
453         struct lov_oinfo        *loi    = cl2osc(obj)->oo_oinfo;
454         struct cl_attr          *attr   = &osc_env_info(env)->oti_attr;
455         struct obdo             *oa     = &oio->oi_oa;
456         struct osc_setattr_cbargs *cbargs = &oio->oi_setattr_cbarg;
457         loff_t                   size   = io->u.ci_setattr.sa_attr.lvb_size;
458         unsigned int             ia_valid = io->u.ci_setattr.sa_valid;
459         int                      result = 0;
460         struct obd_info          oinfo = { { { 0 } } };
461
462         if (ia_valid & ATTR_SIZE)
463                 osc_trunc_check(env, io, oio, size);
464
465         if (oio->oi_lockless == 0) {
466                 cl_object_attr_lock(obj);
467                 result = cl_object_attr_get(env, obj, attr);
468                 if (result == 0) {
469                         unsigned int cl_valid = 0;
470
471                         if (ia_valid & ATTR_SIZE) {
472                                 attr->cat_size = attr->cat_kms = size;
473                                 cl_valid = (CAT_SIZE | CAT_KMS);
474                         }
475                         if (ia_valid & ATTR_MTIME_SET) {
476                                 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
477                                 cl_valid |= CAT_MTIME;
478                         }
479                         if (ia_valid & ATTR_ATIME_SET) {
480                                 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
481                                 cl_valid |= CAT_ATIME;
482                         }
483                         if (ia_valid & ATTR_CTIME_SET) {
484                                 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
485                                 cl_valid |= CAT_CTIME;
486                         }
487                         result = cl_object_attr_set(env, obj, attr, cl_valid);
488                 }
489                 cl_object_attr_unlock(obj);
490         }
491         memset(oa, 0, sizeof(*oa));
492         if (result == 0) {
493                 oa->o_id = loi->loi_id;
494                 oa->o_seq = loi->loi_seq;
495                 oa->o_mtime = attr->cat_mtime;
496                 oa->o_atime = attr->cat_atime;
497                 oa->o_ctime = attr->cat_ctime;
498                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
499                         OBD_MD_FLCTIME | OBD_MD_FLMTIME;
500                 if (ia_valid & ATTR_SIZE) {
501                         oa->o_size = size;
502                         oa->o_blocks = OBD_OBJECT_EOF;
503                         oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
504
505                         if (oio->oi_lockless) {
506                                 oa->o_flags = OBD_FL_SRVLOCK;
507                                 oa->o_valid |= OBD_MD_FLFLAGS;
508                         }
509                 } else {
510                         LASSERT(oio->oi_lockless == 0);
511                 }
512
513                 oinfo.oi_oa = oa;
514                 oinfo.oi_capa = io->u.ci_setattr.sa_capa;
515                 cfs_init_completion(&cbargs->opc_sync);
516
517                 if (ia_valid & ATTR_SIZE)
518                         result = osc_punch_base(osc_export(cl2osc(obj)),
519                                                 &oinfo, osc_setattr_upcall,
520                                                 cbargs, PTLRPCD_SET);
521                 else
522                         result = osc_setattr_async_base(osc_export(cl2osc(obj)),
523                                                         &oinfo, NULL,
524                                                         osc_setattr_upcall,
525                                                         cbargs, PTLRPCD_SET);
526         }
527         return result;
528 }
529
530 static void osc_io_setattr_end(const struct lu_env *env,
531                                const struct cl_io_slice *slice)
532 {
533         struct cl_io            *io     = slice->cis_io;
534         struct osc_io           *oio    = cl2osc_io(env, slice);
535         struct osc_setattr_cbargs *cbargs = &oio->oi_setattr_cbarg;
536         int result;
537
538         cfs_wait_for_completion(&cbargs->opc_sync);
539
540         result = io->ci_result = cbargs->opc_rc;
541         if (result == 0) {
542                 struct cl_object *obj = slice->cis_obj;
543                 if (oio->oi_lockless) {
544                         /* lockless truncate */
545                         struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
546
547                         LASSERT(cl_io_is_trunc(io));
548                         /* XXX: Need a lock. */
549                         osd->od_stats.os_lockless_truncates++;
550                 }
551         }
552 }
553
554 static int osc_io_read_start(const struct lu_env *env,
555                              const struct cl_io_slice *slice)
556 {
557         struct osc_io    *oio   = cl2osc_io(env, slice);
558         struct cl_object *obj   = slice->cis_obj;
559         struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
560         int              result = 0;
561         ENTRY;
562
563         if (oio->oi_lockless == 0) {
564                 cl_object_attr_lock(obj);
565                 result = cl_object_attr_get(env, obj, attr);
566                 if (result == 0) {
567                         attr->cat_atime = LTIME_S(CFS_CURRENT_TIME);
568                         result = cl_object_attr_set(env, obj, attr,
569                                                     CAT_ATIME);
570                 }
571                 cl_object_attr_unlock(obj);
572         }
573         RETURN(result);
574 }
575
576 static int osc_io_write_start(const struct lu_env *env,
577                               const struct cl_io_slice *slice)
578 {
579         struct osc_io    *oio   = cl2osc_io(env, slice);
580         struct cl_object *obj   = slice->cis_obj;
581         struct cl_attr   *attr  = &osc_env_info(env)->oti_attr;
582         int              result = 0;
583         ENTRY;
584
585         if (oio->oi_lockless == 0) {
586                 cl_object_attr_lock(obj);
587                 result = cl_object_attr_get(env, obj, attr);
588                 if (result == 0) {
589                         attr->cat_mtime = attr->cat_ctime =
590                                 LTIME_S(CFS_CURRENT_TIME);
591                         result = cl_object_attr_set(env, obj, attr,
592                                                     CAT_MTIME | CAT_CTIME);
593                 }
594                 cl_object_attr_unlock(obj);
595         }
596         RETURN(result);
597 }
598
599 static const struct cl_io_operations osc_io_ops = {
600         .op = {
601                 [CIT_READ] = {
602                         .cio_start  = osc_io_read_start,
603                         .cio_fini   = osc_io_fini
604                 },
605                 [CIT_WRITE] = {
606                         .cio_start  = osc_io_write_start,
607                         .cio_fini   = osc_io_fini
608                 },
609                 [CIT_SETATTR] = {
610                         .cio_start  = osc_io_setattr_start,
611                         .cio_end    = osc_io_setattr_end
612                 },
613                 [CIT_FAULT] = {
614                         .cio_fini   = osc_io_fini,
615                         .cio_start  = osc_io_fault_start
616                 },
617                 [CIT_MISC] = {
618                         .cio_fini   = osc_io_fini
619                 }
620         },
621         .req_op = {
622                  [CRT_READ] = {
623                          .cio_submit    = osc_io_submit
624                  },
625                  [CRT_WRITE] = {
626                          .cio_submit    = osc_io_submit
627                  }
628          },
629         .cio_prepare_write = osc_io_prepare_write,
630         .cio_commit_write  = osc_io_commit_write
631 };
632
633 /*****************************************************************************
634  *
635  * Transfer operations.
636  *
637  */
638
639 static int osc_req_prep(const struct lu_env *env,
640                         const struct cl_req_slice *slice)
641 {
642         return 0;
643 }
644
645 static void osc_req_completion(const struct lu_env *env,
646                                const struct cl_req_slice *slice, int ioret)
647 {
648         struct osc_req *or;
649
650         or = cl2osc_req(slice);
651         OBD_SLAB_FREE_PTR(or, osc_req_kmem);
652 }
653
654 /**
655  * Implementation of struct cl_req_operations::cro_attr_set() for osc
656  * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
657  * fields.
658  */
659 static void osc_req_attr_set(const struct lu_env *env,
660                              const struct cl_req_slice *slice,
661                              const struct cl_object *obj,
662                              struct cl_req_attr *attr, obd_valid flags)
663 {
664         struct lov_oinfo *oinfo;
665         struct cl_req    *clerq;
666         struct cl_page   *apage; /* _some_ page in @clerq */
667         struct cl_lock   *lock;  /* _some_ lock protecting @apage */
668         struct osc_lock  *olck;
669         struct osc_page  *opg;
670         struct obdo      *oa;
671
672         oa = attr->cra_oa;
673         oinfo = cl2osc(obj)->oo_oinfo;
674         if (flags & OBD_MD_FLID) {
675                 oa->o_id = oinfo->loi_id;
676                 oa->o_valid |= OBD_MD_FLID;
677         }
678         if (flags & OBD_MD_FLGROUP) {
679                 oa->o_seq = oinfo->loi_seq;
680                 oa->o_valid |= OBD_MD_FLGROUP;
681         }
682         if (flags & OBD_MD_FLHANDLE) {
683                 clerq = slice->crs_req;
684                 LASSERT(!cfs_list_empty(&clerq->crq_pages));
685                 apage = container_of(clerq->crq_pages.next,
686                                      struct cl_page, cp_flight);
687                 opg = osc_cl_page_osc(apage);
688                 apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
689                 lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
690                 if (lock == NULL) {
691                         struct cl_object_header *head;
692                         struct cl_lock          *scan;
693
694                         head = cl_object_header(apage->cp_obj);
695                         cfs_list_for_each_entry(scan, &head->coh_locks,
696                                                 cll_linkage)
697                                 CL_LOCK_DEBUG(D_ERROR, env, scan,
698                                               "no cover page!\n");
699                         CL_PAGE_DEBUG(D_ERROR, env, apage,
700                                       "dump uncover page!\n");
701                         libcfs_debug_dumpstack(NULL);
702                         LBUG();
703                 }
704
705                 olck = osc_lock_at(lock);
706                 LASSERT(olck != NULL);
707                 LASSERT(ergo(opg->ops_srvlock, olck->ols_lock == NULL));
708                 /* check for lockless io. */
709                 if (olck->ols_lock != NULL) {
710                         oa->o_handle = olck->ols_lock->l_remote_handle;
711                         oa->o_valid |= OBD_MD_FLHANDLE;
712                 }
713                 cl_lock_put(env, lock);
714         }
715 }
716
717 static const struct cl_req_operations osc_req_ops = {
718         .cro_prep       = osc_req_prep,
719         .cro_attr_set   = osc_req_attr_set,
720         .cro_completion = osc_req_completion
721 };
722
723
724 int osc_io_init(const struct lu_env *env,
725                 struct cl_object *obj, struct cl_io *io)
726 {
727         struct osc_io *oio = osc_env_io(env);
728
729         CL_IO_SLICE_CLEAN(oio, oi_cl);
730         cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
731         return 0;
732 }
733
734 int osc_req_init(const struct lu_env *env, struct cl_device *dev,
735                  struct cl_req *req)
736 {
737         struct osc_req *or;
738         int result;
739
740         OBD_SLAB_ALLOC_PTR_GFP(or, osc_req_kmem, CFS_ALLOC_IO);
741         if (or != NULL) {
742                 cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);
743                 result = 0;
744         } else
745                 result = -ENOMEM;
746         return result;
747 }
748
749 /** @} osc */