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