Whamcloud - gitweb
LU-1030 osc: move io data from lov_oinfo into osc_object
[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         int result;
214         ENTRY;
215
216         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 0));
217
218         osc_page_transfer_get(opg, "transfer\0cache");
219         result = osc_queue_async_io(env, opg);
220         if (result != 0)
221                 osc_page_transfer_put(env, opg);
222         else
223                 osc_page_transfer_add(env, opg, CRT_WRITE);
224         RETURN(result);
225 }
226
227 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
228                       pgoff_t start, pgoff_t end)
229 {
230         memset(policy, 0, sizeof *policy);
231         policy->l_extent.start = cl_offset(obj, start);
232         policy->l_extent.end   = cl_offset(obj, end + 1) - 1;
233 }
234
235 static int osc_page_addref_lock(const struct lu_env *env,
236                                 struct osc_page *opg,
237                                 struct cl_lock *lock)
238 {
239         struct osc_lock *olock;
240         int              rc;
241
242         LASSERT(opg->ops_lock == NULL);
243
244         olock = osc_lock_at(lock);
245         if (cfs_atomic_inc_return(&olock->ols_pageref) <= 0) {
246                 cfs_atomic_dec(&olock->ols_pageref);
247                 cl_lock_put(env, lock);
248                 rc = 1;
249         } else {
250                 opg->ops_lock = lock;
251                 rc = 0;
252         }
253         return rc;
254 }
255
256 static void osc_page_putref_lock(const struct lu_env *env,
257                                  struct osc_page *opg)
258 {
259         struct cl_lock  *lock = opg->ops_lock;
260         struct osc_lock *olock;
261
262         LASSERT(lock != NULL);
263         olock = osc_lock_at(lock);
264
265         cfs_atomic_dec(&olock->ols_pageref);
266         opg->ops_lock = NULL;
267
268         cl_lock_put(env, lock);
269 }
270
271 static int osc_page_is_under_lock(const struct lu_env *env,
272                                   const struct cl_page_slice *slice,
273                                   struct cl_io *unused)
274 {
275         struct cl_lock *lock;
276         int             result;
277
278         ENTRY;
279         lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
280                                NULL, 1, 0);
281         if (lock != NULL &&
282             osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
283                 result = -EBUSY;
284         else
285                 result = -ENODATA;
286         RETURN(result);
287 }
288
289 static void osc_page_disown(const struct lu_env *env,
290                             const struct cl_page_slice *slice,
291                             struct cl_io *io)
292 {
293         struct osc_page *opg = cl2osc_page(slice);
294
295         if (unlikely(opg->ops_lock))
296                 osc_page_putref_lock(env, opg);
297 }
298
299 static void osc_page_completion_read(const struct lu_env *env,
300                                      const struct cl_page_slice *slice,
301                                      int ioret)
302 {
303         struct osc_page *opg = cl2osc_page(slice);
304
305         if (likely(opg->ops_lock))
306                 osc_page_putref_lock(env, opg);
307 }
308
309 static int osc_page_fail(const struct lu_env *env,
310                          const struct cl_page_slice *slice,
311                          struct cl_io *unused)
312 {
313         /*
314          * Cached read?
315          */
316         LBUG();
317         return 0;
318 }
319
320
321 static const char *osc_list(cfs_list_t *head)
322 {
323         return cfs_list_empty(head) ? "-" : "+";
324 }
325
326 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
327 {
328         if (opg->ops_submit_time == 0)
329                 return 0;
330
331         return (cfs_time_current() - opg->ops_submit_time);
332 }
333
334 static int osc_page_print(const struct lu_env *env,
335                           const struct cl_page_slice *slice,
336                           void *cookie, lu_printer_t printer)
337 {
338         struct osc_page       *opg = cl2osc_page(slice);
339         struct osc_async_page *oap = &opg->ops_oap;
340         struct osc_object     *obj = cl2osc(slice->cpl_obj);
341         struct client_obd     *cli = &osc_export(obj)->exp_obd->u.cli;
342
343         return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p: "
344                           "1< %#x %d %u %s %s %s > "
345                           "2< "LPU64" %u %u %#x %#x | %p %p %p > "
346                           "3< %s %p %d %lu %d > "
347                           "4< %d %d %d %lu %s | %s %s %s %s > "
348                           "5< %s %s %s %s | %d %s %s | %d %s %s>\n",
349                           opg,
350                           /* 1 */
351                           oap->oap_magic, oap->oap_cmd,
352                           oap->oap_interrupted,
353                           osc_list(&oap->oap_pending_item),
354                           osc_list(&oap->oap_urgent_item),
355                           osc_list(&oap->oap_rpc_item),
356                           /* 2 */
357                           oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
358                           oap->oap_async_flags, oap->oap_brw_flags,
359                           oap->oap_request, oap->oap_cli, obj,
360                           /* 3 */
361                           osc_list(&opg->ops_inflight),
362                           opg->ops_submitter, opg->ops_transfer_pinned,
363                           osc_submit_duration(opg), opg->ops_srvlock,
364                           /* 4 */
365                           cli->cl_r_in_flight, cli->cl_w_in_flight,
366                           cli->cl_max_rpcs_in_flight,
367                           cli->cl_avail_grant,
368                           osc_list(&cli->cl_cache_waiters),
369                           osc_list(&cli->cl_loi_ready_list),
370                           osc_list(&cli->cl_loi_hp_ready_list),
371                           osc_list(&cli->cl_loi_write_list),
372                           osc_list(&cli->cl_loi_read_list),
373                           /* 5 */
374                           osc_list(&obj->oo_ready_item),
375                           osc_list(&obj->oo_hp_ready_item),
376                           osc_list(&obj->oo_write_item),
377                           osc_list(&obj->oo_read_item),
378                           obj->oo_read_pages.oop_num_pending,
379                           osc_list(&obj->oo_read_pages.oop_pending),
380                           osc_list(&obj->oo_read_pages.oop_urgent),
381                           obj->oo_write_pages.oop_num_pending,
382                           osc_list(&obj->oo_write_pages.oop_pending),
383                           osc_list(&obj->oo_write_pages.oop_urgent));
384 }
385
386 static void osc_page_delete(const struct lu_env *env,
387                             const struct cl_page_slice *slice)
388 {
389         struct osc_page   *opg = cl2osc_page(slice);
390         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
391         int rc;
392
393         LINVRNT(opg->ops_temp || osc_page_protected(env, opg, CLM_READ, 1));
394
395         ENTRY;
396         CDEBUG(D_TRACE, "%p\n", opg);
397         osc_page_transfer_put(env, opg);
398         rc = osc_teardown_async_page(obj, opg);
399         if (rc) {
400                 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(slice->cpl_page),
401                               "Trying to teardown failed: %d\n", rc);
402                 LASSERT(0);
403         }
404         cfs_spin_lock(&obj->oo_seatbelt);
405         cfs_list_del_init(&opg->ops_inflight);
406         cfs_spin_unlock(&obj->oo_seatbelt);
407         EXIT;
408 }
409
410 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
411                    int from, int to)
412 {
413         struct osc_page       *opg = cl2osc_page(slice);
414         struct osc_async_page *oap = &opg->ops_oap;
415
416         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
417
418         opg->ops_from = from;
419         opg->ops_to   = to;
420         cfs_spin_lock(&oap->oap_lock);
421         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
422         cfs_spin_unlock(&oap->oap_lock);
423 }
424
425 static int osc_page_cancel(const struct lu_env *env,
426                            const struct cl_page_slice *slice)
427 {
428         struct osc_page *opg       = cl2osc_page(slice);
429         struct osc_async_page *oap = &opg->ops_oap;
430         int rc = 0;
431
432         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
433
434         client_obd_list_lock(&oap->oap_cli->cl_loi_list_lock);
435         /* Check if the transferring against this page
436          * is completed, or not even queued. */
437         if (opg->ops_transfer_pinned)
438                 /* FIXME: may not be interrupted.. */
439                 rc = osc_cancel_async_page(env, opg);
440         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
441         client_obd_list_unlock(&oap->oap_cli->cl_loi_list_lock);
442         return rc;
443 }
444
445 static const struct cl_page_operations osc_page_ops = {
446         .cpo_fini          = osc_page_fini,
447         .cpo_print         = osc_page_print,
448         .cpo_delete        = osc_page_delete,
449         .cpo_is_under_lock = osc_page_is_under_lock,
450         .cpo_disown        = osc_page_disown,
451         .io = {
452                 [CRT_READ] = {
453                         .cpo_cache_add  = osc_page_fail,
454                         .cpo_completion = osc_page_completion_read
455                 },
456                 [CRT_WRITE] = {
457                         .cpo_cache_add  = osc_page_cache_add
458                 }
459         },
460         .cpo_clip           = osc_page_clip,
461         .cpo_cancel         = osc_page_cancel
462 };
463
464 struct cl_page *osc_page_init(const struct lu_env *env,
465                               struct cl_object *obj,
466                               struct cl_page *page, cfs_page_t *vmpage)
467 {
468         struct osc_object *osc = cl2osc(obj);
469         struct osc_page   *opg;
470         int result;
471
472         OBD_SLAB_ALLOC_PTR_GFP(opg, osc_page_kmem, CFS_ALLOC_IO);
473         if (opg != NULL) {
474                 opg->ops_from = 0;
475                 opg->ops_to   = CFS_PAGE_SIZE;
476
477                 result = osc_prep_async_page(osc, opg, vmpage,
478                                              cl_offset(obj, page->cp_index));
479                 if (result == 0) {
480                         struct osc_io *oio = osc_env_io(env);
481                         opg->ops_srvlock = osc_io_srvlock(oio);
482                         cl_page_slice_add(page, &opg->ops_cl, obj,
483                                           &osc_page_ops);
484                 }
485                 /*
486                  * Cannot assert osc_page_protected() here as read-ahead
487                  * creates temporary pages outside of a lock.
488                  */
489 #ifdef INVARIANT_CHECK
490                 opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
491 #endif
492                 CFS_INIT_LIST_HEAD(&opg->ops_inflight);
493         } else
494                 result = -ENOMEM;
495         return ERR_PTR(result);
496 }
497
498 /**
499  * Helper function called by osc_io_submit() for every page in an immediate
500  * transfer (i.e., transferred synchronously).
501  */
502 void osc_io_submit_page(const struct lu_env *env,
503                         struct osc_io *oio, struct osc_page *opg,
504                         enum cl_req_type crt)
505 {
506         LINVRNT(osc_page_protected(env, opg,
507                                    crt == CRT_WRITE ? CLM_WRITE : CLM_READ, 1));
508
509         osc_queue_sync_page(env, opg,
510                             crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ,
511                             osc_io_srvlock(oio) ? OBD_BRW_SRVLOCK : 0);
512
513         osc_page_transfer_get(opg, "transfer\0imm");
514         osc_page_transfer_add(env, opg, crt);
515 }
516
517 /** @} osc */