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