Whamcloud - gitweb
LU-1217 osc: to not check a cl_lock's state w/o protection
[fs/lustre-release.git] / lustre / osc / osc_page.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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_page 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  * Comment out osc_page_protected because it may sleep inside the
51  * the client_obd_list_lock.
52  * client_obd_list_lock -> osc_ap_completion -> osc_completion ->
53  *   -> osc_page_protected -> osc_page_is_dlocked -> osc_match_base
54  *   -> ldlm_lock_match -> sptlrpc_import_check_ctx -> sleep.
55  */
56 #if 0
57 static int osc_page_is_dlocked(const struct lu_env *env,
58                                const struct osc_page *opg,
59                                enum cl_lock_mode mode, int pending, int unref)
60 {
61         struct cl_page         *page;
62         struct osc_object      *obj;
63         struct osc_thread_info *info;
64         struct ldlm_res_id     *resname;
65         struct lustre_handle   *lockh;
66         ldlm_policy_data_t     *policy;
67         ldlm_mode_t             dlmmode;
68         int                     flags;
69
70         cfs_might_sleep();
71
72         info = osc_env_info(env);
73         resname = &info->oti_resname;
74         policy = &info->oti_policy;
75         lockh = &info->oti_handle;
76         page = opg->ops_cl.cpl_page;
77         obj = cl2osc(opg->ops_cl.cpl_obj);
78
79         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
80         if (pending)
81                 flags |= LDLM_FL_CBPENDING;
82
83         dlmmode = osc_cl_lock2ldlm(mode) | LCK_PW;
84         osc_lock_build_res(env, obj, resname);
85         osc_index2policy(policy, page->cp_obj, page->cp_index, page->cp_index);
86         return osc_match_base(osc_export(obj), resname, LDLM_EXTENT, policy,
87                               dlmmode, &flags, NULL, lockh, unref);
88 }
89
90 /**
91  * Checks an invariant that a page in the cache is covered by a lock, as
92  * needed.
93  */
94 static int osc_page_protected(const struct lu_env *env,
95                               const struct osc_page *opg,
96                               enum cl_lock_mode mode, int unref)
97 {
98         struct cl_object_header *hdr;
99         struct cl_lock          *scan;
100         struct cl_page          *page;
101         struct cl_lock_descr    *descr;
102         int result;
103
104         LINVRNT(!opg->ops_temp);
105
106         page = opg->ops_cl.cpl_page;
107         if (page->cp_owner != NULL &&
108             cl_io_top(page->cp_owner)->ci_lockreq == CILR_NEVER)
109                 /*
110                  * If IO is done without locks (liblustre, or lloop), lock is
111                  * not required.
112                  */
113                 result = 1;
114         else
115                 /* otherwise check for a DLM lock */
116         result = osc_page_is_dlocked(env, opg, mode, 1, unref);
117         if (result == 0) {
118                 /* maybe this page is a part of a lockless io? */
119                 hdr = cl_object_header(opg->ops_cl.cpl_obj);
120                 descr = &osc_env_info(env)->oti_descr;
121                 descr->cld_mode = mode;
122                 descr->cld_start = page->cp_index;
123                 descr->cld_end   = page->cp_index;
124                 cfs_spin_lock(&hdr->coh_lock_guard);
125                 cfs_list_for_each_entry(scan, &hdr->coh_locks, cll_linkage) {
126                         /*
127                          * Lock-less sub-lock has to be either in HELD state
128                          * (when io is actively going on), or in CACHED state,
129                          * when top-lock is being unlocked:
130                          * cl_io_unlock()->cl_unuse()->...->lov_lock_unuse().
131                          */
132                         if ((scan->cll_state == CLS_HELD ||
133                              scan->cll_state == CLS_CACHED) &&
134                             cl_lock_ext_match(&scan->cll_descr, descr)) {
135                                 struct osc_lock *olck;
136
137                                 olck = osc_lock_at(scan);
138                                 result = osc_lock_is_lockless(olck);
139                                 break;
140                         }
141                 }
142                 cfs_spin_unlock(&hdr->coh_lock_guard);
143         }
144         return result;
145 }
146 #else
147 static int osc_page_protected(const struct lu_env *env,
148                               const struct osc_page *opg,
149                               enum cl_lock_mode mode, int unref)
150 {
151         return 1;
152 }
153 #endif
154
155 /*****************************************************************************
156  *
157  * Page operations.
158  *
159  */
160 static void osc_page_fini(const struct lu_env *env,
161                           struct cl_page_slice *slice)
162 {
163         struct osc_page *opg = cl2osc_page(slice);
164         CDEBUG(D_TRACE, "%p\n", opg);
165         LASSERT(opg->ops_lock == NULL);
166         OBD_SLAB_FREE_PTR(opg, osc_page_kmem);
167 }
168
169 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
170 {
171         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
172
173         LASSERT(!opg->ops_transfer_pinned);
174         cl_page_get(page);
175         lu_ref_add_atomic(&page->cp_reference, label, page);
176         opg->ops_transfer_pinned = 1;
177 }
178
179 static void osc_page_transfer_put(const struct lu_env *env,
180                                   struct osc_page *opg)
181 {
182         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
183
184         if (opg->ops_transfer_pinned) {
185                 lu_ref_del(&page->cp_reference, "transfer", page);
186                 opg->ops_transfer_pinned = 0;
187                 cl_page_put(env, page);
188         }
189 }
190
191 /**
192  * This is called once for every page when it is submitted for a transfer
193  * either opportunistic (osc_page_cache_add()), or immediate
194  * (osc_page_submit()).
195  */
196 static void osc_page_transfer_add(const struct lu_env *env,
197                                   struct osc_page *opg, enum cl_req_type crt)
198 {
199         struct osc_object *obj;
200
201         obj = cl2osc(opg->ops_cl.cpl_obj);
202         cfs_spin_lock(&obj->oo_seatbelt);
203         cfs_list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
204         opg->ops_submitter = cfs_current();
205         cfs_spin_unlock(&obj->oo_seatbelt);
206 }
207
208 static int osc_page_cache_add(const struct lu_env *env,
209                               const struct cl_page_slice *slice,
210                               struct cl_io *unused)
211 {
212         struct osc_page   *opg = cl2osc_page(slice);
213         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
214         int result;
215         /* All cacheable IO is async-capable */
216         int brw_flags = OBD_BRW_ASYNC;
217         int noquota = 0;
218
219         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 0));
220         ENTRY;
221
222         /* Set the OBD_BRW_SRVLOCK before the page is queued. */
223         brw_flags |= opg->ops_srvlock ? OBD_BRW_SRVLOCK : 0;
224         if (!client_is_remote(osc_export(obj)) &&
225             cfs_capable(CFS_CAP_SYS_RESOURCE)) {
226                 brw_flags |= OBD_BRW_NOQUOTA;
227                 noquota = OBD_BRW_NOQUOTA;
228         }
229
230         osc_page_transfer_get(opg, "transfer\0cache");
231         result = osc_queue_async_io(env, osc_export(obj), NULL, obj->oo_oinfo,
232                                     &opg->ops_oap, OBD_BRW_WRITE | noquota,
233                                     opg->ops_from, opg->ops_to - opg->ops_from,
234                                     brw_flags, 0);
235         if (result != 0)
236                 osc_page_transfer_put(env, opg);
237         else
238                 osc_page_transfer_add(env, opg, CRT_WRITE);
239         RETURN(result);
240 }
241
242 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
243                       pgoff_t start, pgoff_t end)
244 {
245         memset(policy, 0, sizeof *policy);
246         policy->l_extent.start = cl_offset(obj, start);
247         policy->l_extent.end   = cl_offset(obj, end + 1) - 1;
248 }
249
250 static int osc_page_addref_lock(const struct lu_env *env,
251                                 struct osc_page *opg,
252                                 struct cl_lock *lock)
253 {
254         struct osc_lock *olock;
255         int              rc;
256
257         LASSERT(opg->ops_lock == NULL);
258
259         olock = osc_lock_at(lock);
260         if (cfs_atomic_inc_return(&olock->ols_pageref) <= 0) {
261                 cfs_atomic_dec(&olock->ols_pageref);
262                 cl_lock_put(env, lock);
263                 rc = 1;
264         } else {
265                 opg->ops_lock = lock;
266                 rc = 0;
267         }
268         return rc;
269 }
270
271 static void osc_page_putref_lock(const struct lu_env *env,
272                                  struct osc_page *opg)
273 {
274         struct cl_lock  *lock = opg->ops_lock;
275         struct osc_lock *olock;
276
277         LASSERT(lock != NULL);
278         olock = osc_lock_at(lock);
279
280         cfs_atomic_dec(&olock->ols_pageref);
281         opg->ops_lock = NULL;
282
283         cl_lock_put(env, lock);
284 }
285
286 static int osc_page_is_under_lock(const struct lu_env *env,
287                                   const struct cl_page_slice *slice,
288                                   struct cl_io *unused)
289 {
290         struct cl_lock *lock;
291         int             result;
292
293         ENTRY;
294         lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
295                                NULL, 1, 0);
296         if (lock != NULL &&
297             osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
298                 result = -EBUSY;
299         else
300                 result = -ENODATA;
301         RETURN(result);
302 }
303
304 static void osc_page_disown(const struct lu_env *env,
305                             const struct cl_page_slice *slice,
306                             struct cl_io *io)
307 {
308         struct osc_page *opg = cl2osc_page(slice);
309
310         if (unlikely(opg->ops_lock))
311                 osc_page_putref_lock(env, opg);
312 }
313
314 static void osc_page_completion_read(const struct lu_env *env,
315                                      const struct cl_page_slice *slice,
316                                      int ioret)
317 {
318         struct osc_page *opg = cl2osc_page(slice);
319
320         if (likely(opg->ops_lock))
321                 osc_page_putref_lock(env, opg);
322 }
323
324 static int osc_page_fail(const struct lu_env *env,
325                          const struct cl_page_slice *slice,
326                          struct cl_io *unused)
327 {
328         /*
329          * Cached read?
330          */
331         LBUG();
332         return 0;
333 }
334
335
336 static const char *osc_list(cfs_list_t *head)
337 {
338         return cfs_list_empty(head) ? "-" : "+";
339 }
340
341 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
342 {
343         if (opg->ops_submit_time == 0)
344                 return 0;
345
346         return (cfs_time_current() - opg->ops_submit_time);
347 }
348
349 static int osc_page_print(const struct lu_env *env,
350                           const struct cl_page_slice *slice,
351                           void *cookie, lu_printer_t printer)
352 {
353         struct osc_page       *opg = cl2osc_page(slice);
354         struct osc_async_page *oap = &opg->ops_oap;
355         struct osc_object     *obj = cl2osc(slice->cpl_obj);
356         struct client_obd     *cli = &osc_export(obj)->exp_obd->u.cli;
357         struct lov_oinfo      *loi = obj->oo_oinfo;
358
359         return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p: "
360                           "1< %#x %d %u %s %s %s > "
361                           "2< "LPU64" %u %u %#x %#x | %p %p %p %p %p > "
362                           "3< %s %p %d %lu %d > "
363                           "4< %d %d %d %lu %s | %s %s %s %s > "
364                           "5< %s %s %s %s | %d %s %s | %d %s %s>\n",
365                           opg,
366                           /* 1 */
367                           oap->oap_magic, oap->oap_cmd,
368                           oap->oap_interrupted,
369                           osc_list(&oap->oap_pending_item),
370                           osc_list(&oap->oap_urgent_item),
371                           osc_list(&oap->oap_rpc_item),
372                           /* 2 */
373                           oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
374                           oap->oap_async_flags, oap->oap_brw_flags,
375                           oap->oap_request,
376                           oap->oap_cli, oap->oap_loi, oap->oap_caller_ops,
377                           oap->oap_caller_data,
378                           /* 3 */
379                           osc_list(&opg->ops_inflight),
380                           opg->ops_submitter, opg->ops_transfer_pinned,
381                           osc_submit_duration(opg), opg->ops_srvlock,
382                           /* 4 */
383                           cli->cl_r_in_flight, cli->cl_w_in_flight,
384                           cli->cl_max_rpcs_in_flight,
385                           cli->cl_avail_grant,
386                           osc_list(&cli->cl_cache_waiters),
387                           osc_list(&cli->cl_loi_ready_list),
388                           osc_list(&cli->cl_loi_hp_ready_list),
389                           osc_list(&cli->cl_loi_write_list),
390                           osc_list(&cli->cl_loi_read_list),
391                           /* 5 */
392                           osc_list(&loi->loi_ready_item),
393                           osc_list(&loi->loi_hp_ready_item),
394                           osc_list(&loi->loi_write_item),
395                           osc_list(&loi->loi_read_item),
396                           loi->loi_read_lop.lop_num_pending,
397                           osc_list(&loi->loi_read_lop.lop_pending),
398                           osc_list(&loi->loi_read_lop.lop_urgent),
399                           loi->loi_write_lop.lop_num_pending,
400                           osc_list(&loi->loi_write_lop.lop_pending),
401                           osc_list(&loi->loi_write_lop.lop_urgent));
402 }
403
404 static void osc_page_delete(const struct lu_env *env,
405                             const struct cl_page_slice *slice)
406 {
407         struct osc_page       *opg = cl2osc_page(slice);
408         struct osc_object     *obj = cl2osc(opg->ops_cl.cpl_obj);
409         struct osc_async_page *oap = &opg->ops_oap;
410         int rc;
411
412         LINVRNT(opg->ops_temp || osc_page_protected(env, opg, CLM_READ, 1));
413
414         ENTRY;
415         CDEBUG(D_TRACE, "%p\n", opg);
416         osc_page_transfer_put(env, opg);
417         rc = osc_teardown_async_page(osc_export(obj), NULL, obj->oo_oinfo, oap);
418         if (rc) {
419                 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(slice->cpl_page),
420                               "Trying to teardown failed: %d\n", rc);
421                 LASSERT(0);
422         }
423         cfs_spin_lock(&obj->oo_seatbelt);
424         cfs_list_del_init(&opg->ops_inflight);
425         cfs_spin_unlock(&obj->oo_seatbelt);
426         EXIT;
427 }
428
429 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
430                    int from, int to)
431 {
432         struct osc_page       *opg = cl2osc_page(slice);
433         struct osc_async_page *oap = &opg->ops_oap;
434
435         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
436
437         opg->ops_from = from;
438         opg->ops_to   = to;
439         cfs_spin_lock(&oap->oap_lock);
440         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
441         cfs_spin_unlock(&oap->oap_lock);
442 }
443
444 static int osc_page_cancel(const struct lu_env *env,
445                            const struct cl_page_slice *slice)
446 {
447         struct osc_page *opg       = cl2osc_page(slice);
448         struct osc_async_page *oap = &opg->ops_oap;
449         int rc = 0;
450
451         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
452
453         client_obd_list_lock(&oap->oap_cli->cl_loi_list_lock);
454         /* Check if the transferring against this page
455          * is completed, or not even queued. */
456         if (opg->ops_transfer_pinned)
457                 /* FIXME: may not be interrupted.. */
458                 rc = osc_oap_interrupted(env, oap);
459         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
460         client_obd_list_unlock(&oap->oap_cli->cl_loi_list_lock);
461         return rc;
462 }
463
464 static const struct cl_page_operations osc_page_ops = {
465         .cpo_fini          = osc_page_fini,
466         .cpo_print         = osc_page_print,
467         .cpo_delete        = osc_page_delete,
468         .cpo_is_under_lock = osc_page_is_under_lock,
469         .cpo_disown        = osc_page_disown,
470         .io = {
471                 [CRT_READ] = {
472                         .cpo_cache_add  = osc_page_fail,
473                         .cpo_completion = osc_page_completion_read
474                 },
475                 [CRT_WRITE] = {
476                         .cpo_cache_add  = osc_page_cache_add
477                 }
478         },
479         .cpo_clip           = osc_page_clip,
480         .cpo_cancel         = osc_page_cancel
481 };
482
483 static int osc_make_ready(const struct lu_env *env, void *data, int cmd)
484 {
485         struct osc_page *opg  = data;
486         struct cl_page  *page = cl_page_top(opg->ops_cl.cpl_page);
487         int result;
488
489         LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
490         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 1));
491
492         ENTRY;
493         result = cl_page_make_ready(env, page, CRT_WRITE);
494         if (result == 0)
495                 opg->ops_submit_time = cfs_time_current();
496         RETURN(result);
497 }
498
499 static int osc_refresh_count(const struct lu_env *env, void *data, int cmd)
500 {
501         struct cl_page   *page;
502         struct osc_page  *osc = data;
503         struct cl_object *obj;
504         struct cl_attr   *attr = &osc_env_info(env)->oti_attr;
505
506         int result;
507         loff_t kms;
508
509         LINVRNT(osc_page_protected(env, osc, CLM_READ, 1));
510
511         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
512         LASSERT(!(cmd & OBD_BRW_READ));
513         LASSERT(osc != NULL);
514         page = osc->ops_cl.cpl_page;
515         obj = osc->ops_cl.cpl_obj;
516
517         cl_object_attr_lock(obj);
518         result = cl_object_attr_get(env, obj, attr);
519         cl_object_attr_unlock(obj);
520         if (result < 0)
521                 return result;
522         kms = attr->cat_kms;
523         if (cl_offset(obj, page->cp_index) >= kms)
524                 /* catch race with truncate */
525                 return 0;
526         else if (cl_offset(obj, page->cp_index + 1) > kms)
527                 /* catch sub-page write at end of file */
528                 return kms % CFS_PAGE_SIZE;
529         else
530                 return CFS_PAGE_SIZE;
531 }
532
533 static int osc_completion(const struct lu_env *env,
534                           void *data, int cmd, struct obdo *oa, int rc)
535 {
536         struct osc_page       *opg  = data;
537         struct osc_async_page *oap  = &opg->ops_oap;
538         struct cl_page        *page = cl_page_top(opg->ops_cl.cpl_page);
539         struct osc_object     *obj  = cl2osc(opg->ops_cl.cpl_obj);
540         enum cl_req_type crt;
541         int srvlock;
542
543         LINVRNT(osc_page_protected(env, opg, CLM_READ, 1));
544
545         ENTRY;
546
547         cmd &= ~OBD_BRW_NOQUOTA;
548         LASSERT(equi(page->cp_state == CPS_PAGEIN,  cmd == OBD_BRW_READ));
549         LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
550         LASSERT(opg->ops_transfer_pinned);
551
552         /*
553          * page->cp_req can be NULL if io submission failed before
554          * cl_req was allocated.
555          */
556         if (page->cp_req != NULL)
557                 cl_req_page_done(env, page);
558         LASSERT(page->cp_req == NULL);
559
560         /* As the transfer for this page is being done, clear the flags */
561         cfs_spin_lock(&oap->oap_lock);
562         oap->oap_async_flags = 0;
563         cfs_spin_unlock(&oap->oap_lock);
564
565         crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
566         /* Clear opg->ops_transfer_pinned before VM lock is released. */
567         opg->ops_transfer_pinned = 0;
568
569         cfs_spin_lock(&obj->oo_seatbelt);
570         LASSERT(opg->ops_submitter != NULL);
571         LASSERT(!cfs_list_empty(&opg->ops_inflight));
572         cfs_list_del_init(&opg->ops_inflight);
573         cfs_spin_unlock(&obj->oo_seatbelt);
574
575         opg->ops_submit_time = 0;
576         srvlock = oap->oap_brw_flags & OBD_BRW_SRVLOCK;
577
578         cl_page_completion(env, page, crt, rc);
579
580         /* statistic */
581         if (rc == 0 && srvlock) {
582                 struct lu_device *ld    = opg->ops_cl.cpl_obj->co_lu.lo_dev;
583                 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
584                 int bytes = oap->oap_count;
585
586                 if (crt == CRT_READ)
587                         stats->os_lockless_reads += bytes;
588                 else
589                         stats->os_lockless_writes += bytes;
590         }
591
592         /*
593          * This has to be the last operation with the page, as locks are
594          * released in cl_page_completion() and nothing except for the
595          * reference counter protects page from concurrent reclaim.
596          */
597         lu_ref_del(&page->cp_reference, "transfer", page);
598         /*
599          * As page->cp_obj is pinned by a reference from page->cp_req, it is
600          * safe to call cl_page_put() without risking object destruction in a
601          * non-blocking context.
602          */
603         cl_page_put(env, page);
604         RETURN(0);
605 }
606
607 const static struct obd_async_page_ops osc_async_page_ops = {
608         .ap_make_ready    = osc_make_ready,
609         .ap_refresh_count = osc_refresh_count,
610         .ap_completion    = osc_completion
611 };
612
613 struct cl_page *osc_page_init(const struct lu_env *env,
614                               struct cl_object *obj,
615                               struct cl_page *page, cfs_page_t *vmpage)
616 {
617         struct osc_object *osc = cl2osc(obj);
618         struct osc_page   *opg;
619         int result;
620
621         OBD_SLAB_ALLOC_PTR_GFP(opg, osc_page_kmem, CFS_ALLOC_IO);
622         if (opg != NULL) {
623                 void *oap = &opg->ops_oap;
624
625                 opg->ops_from = 0;
626                 opg->ops_to   = CFS_PAGE_SIZE;
627
628                 result = osc_prep_async_page(osc_export(osc),
629                                              NULL, osc->oo_oinfo, vmpage,
630                                              cl_offset(obj, page->cp_index),
631                                              &osc_async_page_ops,
632                                              opg, (void **)&oap, 1, NULL);
633                 if (result == 0) {
634                         struct osc_io *oio = osc_env_io(env);
635                         opg->ops_srvlock = osc_io_srvlock(oio);
636                         cl_page_slice_add(page, &opg->ops_cl, obj,
637                                           &osc_page_ops);
638                 }
639                 /*
640                  * Cannot assert osc_page_protected() here as read-ahead
641                  * creates temporary pages outside of a lock.
642                  */
643 #ifdef INVARIANT_CHECK
644                 opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
645 #endif
646                 CFS_INIT_LIST_HEAD(&opg->ops_inflight);
647         } else
648                 result = -ENOMEM;
649         return ERR_PTR(result);
650 }
651
652 /**
653  * Helper function called by osc_io_submit() for every page in an immediate
654  * transfer (i.e., transferred synchronously).
655  */
656 void osc_io_submit_page(const struct lu_env *env,
657                         struct osc_io *oio, struct osc_page *opg,
658                         enum cl_req_type crt)
659 {
660         struct osc_async_page *oap = &opg->ops_oap;
661         struct client_obd     *cli = oap->oap_cli;
662         int flags = 0;
663
664         LINVRNT(osc_page_protected(env, opg,
665                                    crt == CRT_WRITE ? CLM_WRITE : CLM_READ, 1));
666
667         oap->oap_page_off   = opg->ops_from;
668         oap->oap_count      = opg->ops_to - opg->ops_from;
669         /* Give a hint to OST that requests are coming from kswapd - bug19529 */
670         if (cfs_memory_pressure_get())
671                 oap->oap_brw_flags |= OBD_BRW_MEMALLOC;
672         oap->oap_brw_flags |= OBD_BRW_SYNC;
673         if (osc_io_srvlock(oio))
674                 oap->oap_brw_flags |= OBD_BRW_SRVLOCK;
675
676         oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
677         if (!client_is_remote(osc_export(cl2osc(opg->ops_cl.cpl_obj))) &&
678             cfs_capable(CFS_CAP_SYS_RESOURCE)) {
679                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
680                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
681         }
682
683         if (oap->oap_cmd & OBD_BRW_READ)
684                 flags = ASYNC_COUNT_STABLE;
685         else if (!(oap->oap_brw_page.flag & OBD_BRW_FROM_GRANT))
686                 osc_enter_cache_try(env, cli, oap->oap_loi, oap, 1);
687
688         cfs_spin_lock(&oap->oap_lock);
689         oap->oap_async_flags |= OSC_FLAGS | flags;
690         cfs_spin_unlock(&oap->oap_lock);
691
692         osc_oap_to_pending(oap);
693         osc_page_transfer_get(opg, "transfer\0imm");
694         osc_page_transfer_add(env, opg, crt);
695 }
696
697 /** @} osc */