Whamcloud - gitweb
silence can_merge_pages() about OBD_BRW_SYNC
[fs/lustre-release.git] / lustre / osc / osc_page.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 2008 Sun Microsystems, Inc.  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_page for OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 /** \addtogroup osc osc @{ */
42
43 #define DEBUG_SUBSYSTEM S_OSC
44
45 #include "osc_cl_internal.h"
46
47 static int osc_page_is_dlocked(const struct lu_env *env,
48                                const struct osc_page *opg,
49                                enum cl_lock_mode mode, int pending, int unref)
50 {
51         struct cl_page         *page;
52         struct osc_object      *obj;
53         struct osc_thread_info *info;
54         struct ldlm_res_id     *resname;
55         struct lustre_handle   *lockh;
56         ldlm_policy_data_t     *policy;
57         ldlm_mode_t             dlmmode;
58         int                     flags;
59
60         info = osc_env_info(env);
61         resname = &info->oti_resname;
62         policy = &info->oti_policy;
63         lockh = &info->oti_handle;
64         page = opg->ops_cl.cpl_page;
65         obj = cl2osc(opg->ops_cl.cpl_obj);
66
67         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
68         if (pending)
69                 flags |= LDLM_FL_CBPENDING;
70
71         dlmmode = osc_cl_lock2ldlm(mode) | LCK_PW;
72         osc_lock_build_res(env, obj, resname);
73         osc_index2policy(policy, page->cp_obj, page->cp_index, page->cp_index);
74         return osc_match_base(osc_export(obj), resname, LDLM_EXTENT, policy,
75                               dlmmode, &flags, NULL, lockh, unref);
76 }
77
78 static int osc_page_protected(const struct lu_env *env,
79                               const struct osc_page *opg,
80                               enum cl_lock_mode mode, int unref)
81 {
82         struct cl_object_header *hdr;
83         struct cl_lock          *scan;
84         struct cl_page          *page;
85         struct cl_lock_descr    *descr;
86         int result;
87
88         LINVRNT(!opg->ops_temp);
89
90         result = osc_page_is_dlocked(env, opg, mode, 1, unref);
91         if (result == 0) {
92                 /* maybe this page is a part of a lockless io? */
93                 hdr = cl_object_header(opg->ops_cl.cpl_obj);
94                 page = opg->ops_cl.cpl_page;
95                 descr = &osc_env_info(env)->oti_descr;
96                 descr->cld_mode = mode;
97                 descr->cld_start = page->cp_index;
98                 descr->cld_end   = page->cp_index;
99                 spin_lock(&hdr->coh_lock_guard);
100                 list_for_each_entry(scan, &hdr->coh_locks, cll_linkage) {
101                         /*
102                          * Lock-less sub-lock has to be either in HELD state
103                          * (when io is actively going on), or in CACHED state,
104                          * when top-lock is being unlocked:
105                          * cl_io_unlock()->cl_unuse()->...->lov_lock_unuse().
106                          */
107                         if ((scan->cll_state == CLS_HELD ||
108                              scan->cll_state == CLS_CACHED) &&
109                             cl_lock_ext_match(&scan->cll_descr, descr)) {
110                                 struct osc_lock *olck;
111
112                                 olck = osc_lock_at(scan);
113                                 result = osc_lock_is_lockless(olck);
114                                 break;
115                         }
116                 }
117                 spin_unlock(&hdr->coh_lock_guard);
118         }
119         return result;
120 }
121
122 /*****************************************************************************
123  *
124  * Page operations.
125  *
126  */
127 static void osc_page_fini(const struct lu_env *env,
128                           struct cl_page_slice *slice)
129 {
130         struct osc_page *opg = cl2osc_page(slice);
131         CDEBUG(D_TRACE, "%p\n", opg);
132         OBD_SLAB_FREE_PTR(opg, osc_page_kmem);
133 }
134
135 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
136 {
137         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
138
139         LASSERT(!opg->ops_transfer_pinned);
140         cl_page_get(page);
141         lu_ref_add_atomic(&page->cp_reference, label, page);
142         opg->ops_transfer_pinned = 1;
143 }
144
145 static void osc_page_transfer_put(const struct lu_env *env,
146                                   struct osc_page *opg)
147 {
148         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
149
150         if (opg->ops_transfer_pinned) {
151                 lu_ref_del(&page->cp_reference, "transfer", page);
152                 opg->ops_transfer_pinned = 0;
153                 cl_page_put(env, page);
154         }
155 }
156
157 /**
158  * This is called once for every page when it is submitted for a transfer
159  * either opportunistic (osc_page_cache_add()), or immediate
160  * (osc_page_submit()).
161  */
162 static void osc_page_transfer_add(const struct lu_env *env,
163                                   struct osc_page *opg, enum cl_req_type crt)
164 {
165         struct osc_object *obj;
166
167         LINVRNT(cl_page_is_vmlocked(env, opg->ops_cl.cpl_page));
168
169         obj = cl2osc(opg->ops_cl.cpl_obj);
170         spin_lock(&obj->oo_seatbelt);
171         list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
172         opg->ops_submitter = cfs_current();
173         spin_unlock(&obj->oo_seatbelt);
174 }
175
176 static int osc_page_cache_add(const struct lu_env *env,
177                               const struct cl_page_slice *slice,
178                               struct cl_io *_)
179 {
180         struct osc_page   *opg = cl2osc_page(slice);
181         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
182         struct osc_io     *oio = osc_env_io(env);
183         int result;
184         int brw_flags;
185         int noquota = 0;
186
187         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 0));
188         ENTRY;
189
190         /* Set the OBD_BRW_SRVLOCK before the page is queued. */
191         brw_flags = oio->oi_lockless ? OBD_BRW_SRVLOCK : 0;
192         if (!client_is_remote(osc_export(obj)) &&
193             cfs_capable(CFS_CAP_SYS_RESOURCE)) {
194                 brw_flags |= OBD_BRW_NOQUOTA;
195                 noquota = OBD_BRW_NOQUOTA;
196         }
197
198         osc_page_transfer_get(opg, "transfer\0cache");
199         result = osc_queue_async_io(env, osc_export(obj), NULL, obj->oo_oinfo,
200                                     &opg->ops_oap, OBD_BRW_WRITE | noquota,
201                                     0, 0, brw_flags, 0);
202         if (result != 0)
203                 osc_page_transfer_put(env, opg);
204         else
205                 osc_page_transfer_add(env, opg, CRT_WRITE);
206         RETURN(result);
207 }
208
209 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
210                       pgoff_t start, pgoff_t end)
211 {
212         memset(policy, 0, sizeof *policy);
213         policy->l_extent.start = cl_offset(obj, start);
214         policy->l_extent.end   = cl_offset(obj, end + 1) - 1;
215 }
216
217 static int osc_page_is_under_lock(const struct lu_env *env,
218                                   const struct cl_page_slice *slice,
219                                   struct cl_io *_)
220 {
221         struct cl_lock *lock;
222         int             result;
223
224         ENTRY;
225         lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
226                                NULL, 1, 0);
227         if (lock != NULL) {
228                 cl_lock_put(env, lock);
229                 result = -EBUSY;
230         } else
231                 result = -ENODATA;
232         RETURN(result);
233 }
234
235 static int osc_page_fail(const struct lu_env *env,
236                          const struct cl_page_slice *slice, struct cl_io *_)
237 {
238         /*
239          * Cached read?
240          */
241         LBUG();
242         return 0;
243 }
244
245
246 static const char *osc_list(struct list_head *head)
247 {
248         return list_empty(head) ? "-" : "+";
249 }
250
251 static int osc_page_print(const struct lu_env *env,
252                           const struct cl_page_slice *slice,
253                           void *cookie, lu_printer_t printer)
254 {
255         struct osc_page       *opg = cl2osc_page(slice);
256         struct osc_async_page *oap = &opg->ops_oap;
257
258         return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p: "
259                           "%#x %d %u %s %s %s %llu %u %#x %p %p %p %p %p\n",
260                           opg, oap->oap_magic, oap->oap_cmd,
261                           oap->oap_interrupted,
262                           osc_list(&oap->oap_pending_item),
263                           osc_list(&oap->oap_urgent_item),
264                           osc_list(&oap->oap_rpc_item),
265                           oap->oap_obj_off, oap->oap_page_off,
266                           oap->oap_async_flags, oap->oap_request,
267                           oap->oap_cli, oap->oap_loi, oap->oap_caller_ops,
268                           oap->oap_caller_data);
269 }
270
271 static void osc_page_delete(const struct lu_env *env,
272                             const struct cl_page_slice *slice)
273 {
274         struct osc_page       *opg = cl2osc_page(slice);
275         struct osc_object     *obj = cl2osc(opg->ops_cl.cpl_obj);
276         struct osc_async_page *oap = &opg->ops_oap;
277         int rc;
278
279         LINVRNT(opg->ops_temp || osc_page_protected(env, opg, CLM_READ, 1));
280
281         ENTRY;
282         CDEBUG(D_TRACE, "%p\n", opg);
283         osc_page_transfer_put(env, opg);
284         rc = osc_teardown_async_page(osc_export(obj), NULL, obj->oo_oinfo, oap);
285         LASSERTF(rc == 0, "%i\n", rc);
286         spin_lock(&obj->oo_seatbelt);
287         list_del_init(&opg->ops_inflight);
288         spin_unlock(&obj->oo_seatbelt);
289         EXIT;
290 }
291
292 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
293                    int from, int to)
294 {
295         struct osc_page       *opg = cl2osc_page(slice);
296         struct osc_async_page *oap = &opg->ops_oap;
297
298         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
299
300         opg->ops_from = from;
301         opg->ops_to   = to;
302         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
303 }
304
305 static int osc_page_cancel(const struct lu_env *env,
306                            const struct cl_page_slice *slice)
307 {
308         struct osc_page *opg       = cl2osc_page(slice);
309         struct osc_async_page *oap = &opg->ops_oap;
310         int rc = 0;
311
312         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
313
314         client_obd_list_lock(&oap->oap_cli->cl_loi_list_lock);
315         /* Check if the transferring against this page
316          * is completed, or not even queued. */
317         if (opg->ops_transfer_pinned)
318                 /* FIXME: may not be interrupted.. */
319                 rc = osc_oap_interrupted(env, oap);
320         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
321         client_obd_list_unlock(&oap->oap_cli->cl_loi_list_lock);
322         return rc;
323 }
324
325 static const struct cl_page_operations osc_page_ops = {
326         .cpo_fini          = osc_page_fini,
327         .cpo_print         = osc_page_print,
328         .cpo_delete        = osc_page_delete,
329         .cpo_is_under_lock = osc_page_is_under_lock,
330         .io = {
331                 [CRT_READ] = {
332                         .cpo_cache_add = osc_page_fail
333                 },
334                 [CRT_WRITE] = {
335                         .cpo_cache_add = osc_page_cache_add
336                 }
337         },
338         .cpo_clip           = osc_page_clip,
339         .cpo_cancel         = osc_page_cancel
340 };
341
342 static int osc_make_ready(const struct lu_env *env, void *data, int cmd)
343 {
344         struct osc_page *opg  = data;
345         struct cl_page  *page = cl_page_top(opg->ops_cl.cpl_page);
346         int result;
347
348         LASSERT(cmd == OBD_BRW_WRITE); /* no cached reads */
349         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 1));
350
351         ENTRY;
352         result = cl_page_make_ready(env, page, CRT_WRITE);
353         RETURN(result);
354 }
355
356 static int osc_refresh_count(const struct lu_env *env, void *data, int cmd)
357 {
358         struct cl_page   *page;
359         struct osc_page  *osc = data;
360         struct cl_object *obj;
361         struct cl_attr   *attr = &osc_env_info(env)->oti_attr;
362
363         int result;
364         loff_t kms;
365
366         LINVRNT(osc_page_protected(env, osc, CLM_READ, 1));
367
368         /* readpage queues with _COUNT_STABLE, shouldn't get here. */
369         LASSERT(!(cmd & OBD_BRW_READ));
370         LASSERT(osc != NULL);
371         page = osc->ops_cl.cpl_page;
372         obj = osc->ops_cl.cpl_obj;
373
374         cl_object_attr_lock(obj);
375         result = cl_object_attr_get(env, obj, attr);
376         cl_object_attr_unlock(obj);
377         if (result < 0)
378                 return result;
379         kms = attr->cat_kms;
380         if (cl_offset(obj, page->cp_index) >= kms)
381                 /* catch race with truncate */
382                 return 0;
383         else if (cl_offset(obj, page->cp_index + 1) > kms)
384                 /* catch sub-page write at end of file */
385                 return kms % CFS_PAGE_SIZE;
386         else
387                 return CFS_PAGE_SIZE;
388 }
389
390 static int osc_completion(const struct lu_env *env,
391                           void *data, int cmd, struct obdo *oa, int rc)
392 {
393         struct osc_page       *opg  = data;
394         struct osc_async_page *oap  = &opg->ops_oap;
395         struct cl_page        *page = cl_page_top(opg->ops_cl.cpl_page);
396         struct osc_object     *obj  = cl2osc(opg->ops_cl.cpl_obj);
397         enum cl_req_type crt;
398
399         LINVRNT(osc_page_protected(env, opg, CLM_READ, 1));
400         LINVRNT(cl_page_is_vmlocked(env, page));
401
402         ENTRY;
403
404         cmd &= ~OBD_BRW_NOQUOTA;
405         LASSERT(equi(page->cp_state == CPS_PAGEIN,  cmd == OBD_BRW_READ));
406         LASSERT(equi(page->cp_state == CPS_PAGEOUT, cmd == OBD_BRW_WRITE));
407         LASSERT(opg->ops_transfer_pinned);
408
409         /*
410          * page->cp_req can be NULL if io submission failed before
411          * cl_req was allocated.
412          */
413         if (page->cp_req != NULL)
414                 cl_req_page_done(env, page);
415         LASSERT(page->cp_req == NULL);
416
417         /* As the transfer for this page is being done, clear the flags */
418         oap->oap_async_flags = 0;
419
420         crt = cmd == OBD_BRW_READ ? CRT_READ : CRT_WRITE;
421         /* Clear opg->ops_transfer_pinned before VM lock is released. */
422         opg->ops_transfer_pinned = 0;
423
424         spin_lock(&obj->oo_seatbelt);
425         LASSERT(opg->ops_submitter != NULL);
426         LASSERT(!list_empty(&opg->ops_inflight));
427         list_del_init(&opg->ops_inflight);
428         spin_unlock(&obj->oo_seatbelt);
429
430         cl_page_completion(env, page, crt, rc);
431
432         /* statistic */
433         if (rc == 0 && oap->oap_brw_flags & OBD_BRW_SRVLOCK) {
434                 struct lu_device *ld    = opg->ops_cl.cpl_obj->co_lu.lo_dev;
435                 struct osc_stats *stats = &lu2osc_dev(ld)->od_stats;
436                 int bytes = opg->ops_to - opg->ops_from;
437
438                 if (crt == CRT_READ)
439                         stats->os_lockless_reads += bytes;
440                 else
441                         stats->os_lockless_writes += bytes;
442         }
443
444         /*
445          * This has to be the last operation with the page, as locks are
446          * released in cl_page_completion() and nothing except for the
447          * reference counter protects page from concurrent reclaim.
448          */
449         lu_ref_del(&page->cp_reference, "transfer", page);
450         /*
451          * As page->cp_obj is pinned by a reference from page->cp_req, it is
452          * safe to call cl_page_put() without risking object destruction in a
453          * non-blocking context.
454          */
455         cl_page_put(env, page);
456         RETURN(0);
457 }
458
459 const static struct obd_async_page_ops osc_async_page_ops = {
460         .ap_make_ready    = osc_make_ready,
461         .ap_refresh_count = osc_refresh_count,
462         .ap_completion    = osc_completion
463 };
464
465 struct cl_page *osc_page_init(const struct lu_env *env,
466                               struct cl_object *obj,
467                               struct cl_page *page, cfs_page_t *vmpage)
468 {
469         struct osc_object *osc = cl2osc(obj);
470         struct osc_page   *opg;
471         int result;
472
473         OBD_SLAB_ALLOC_PTR(opg, osc_page_kmem);
474         if (opg != NULL) {
475                 void *oap = &opg->ops_oap;
476
477                 opg->ops_from = 0;
478                 opg->ops_to   = CFS_PAGE_SIZE;
479
480                 result = osc_prep_async_page(osc_export(osc),
481                                              NULL, osc->oo_oinfo, vmpage,
482                                              cl_offset(obj, page->cp_index),
483                                              &osc_async_page_ops,
484                                              opg, (void **)&oap, 1, NULL);
485                 if (result == 0)
486                         cl_page_slice_add(page, &opg->ops_cl, obj,
487                                           &osc_page_ops);
488                 /*
489                  * Cannot assert osc_page_protected() here as read-ahead
490                  * creates temporary pages outside of a lock.
491                  */
492 #ifdef INVARIANT_CHECK
493                 opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
494 #endif
495                 CFS_INIT_LIST_HEAD(&opg->ops_inflight);
496         } else
497                 result = -ENOMEM;
498         return ERR_PTR(result);
499 }
500
501 /**
502  * Helper function called by osc_io_submit() for every page in an immediate
503  * transfer (i.e., transferred synchronously).
504  */
505 void osc_io_submit_page(const struct lu_env *env,
506                         struct osc_io *oio, struct osc_page *opg,
507                         enum cl_req_type crt)
508 {
509         struct osc_async_page *oap = &opg->ops_oap;
510         struct client_obd     *cli = oap->oap_cli;
511
512         LINVRNT(osc_page_protected(env, opg,
513                                    crt == CRT_WRITE ? CLM_WRITE : CLM_READ, 1));
514
515         oap->oap_page_off   = opg->ops_from;
516         oap->oap_count      = opg->ops_to - opg->ops_from;
517         oap->oap_brw_flags |= OBD_BRW_SYNC;
518         if (oio->oi_lockless)
519                 oap->oap_brw_flags |= OBD_BRW_SRVLOCK;
520
521         oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
522         if (!client_is_remote(osc_export(cl2osc(opg->ops_cl.cpl_obj))) &&
523             cfs_capable(CFS_CAP_SYS_RESOURCE)) {
524                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
525                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
526         }
527
528         oap->oap_async_flags |= OSC_FLAGS;
529         if (oap->oap_cmd & OBD_BRW_READ)
530                 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
531         else if (!(oap->oap_brw_page.flag & OBD_BRW_FROM_GRANT))
532                 osc_enter_cache_try(env, cli, oap->oap_loi, oap, 1);
533
534         osc_oap_to_pending(oap);
535         osc_page_transfer_get(opg, "transfer\0imm");
536         osc_page_transfer_add(env, opg, crt);
537 }
538
539 /** @} osc */