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