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