Whamcloud - gitweb
LU-8560 libcfs: handle PAGE_CACHE_* removal in newer kernels
[fs/lustre-release.git] / lustre / osc / osc_object.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, 2015, Intel Corporation.
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_object for OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_OSC
43
44 #include "osc_cl_internal.h"
45
46 /** \addtogroup osc
47  *  @{
48  */
49
50 /*****************************************************************************
51  *
52  * Type conversions.
53  *
54  */
55
56 static struct lu_object *osc2lu(struct osc_object *osc)
57 {
58         return &osc->oo_cl.co_lu;
59 }
60
61 static struct osc_object *lu2osc(const struct lu_object *obj)
62 {
63         LINVRNT(osc_is_object(obj));
64         return container_of0(obj, struct osc_object, oo_cl.co_lu);
65 }
66
67 /*****************************************************************************
68  *
69  * Object operations.
70  *
71  */
72
73 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
74                            const struct lu_object_conf *conf)
75 {
76         struct osc_object           *osc   = lu2osc(obj);
77         const struct cl_object_conf *cconf = lu2cl_conf(conf);
78
79         osc->oo_oinfo = cconf->u.coc_oinfo;
80 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
81         mutex_init(&osc->oo_debug_mutex);
82 #endif
83         INIT_LIST_HEAD(&osc->oo_ready_item);
84         INIT_LIST_HEAD(&osc->oo_hp_ready_item);
85         INIT_LIST_HEAD(&osc->oo_write_item);
86         INIT_LIST_HEAD(&osc->oo_read_item);
87
88         osc->oo_root.rb_node = NULL;
89         INIT_LIST_HEAD(&osc->oo_hp_exts);
90         INIT_LIST_HEAD(&osc->oo_urgent_exts);
91         INIT_LIST_HEAD(&osc->oo_rpc_exts);
92         INIT_LIST_HEAD(&osc->oo_reading_exts);
93         atomic_set(&osc->oo_nr_reads, 0);
94         atomic_set(&osc->oo_nr_writes, 0);
95         spin_lock_init(&osc->oo_lock);
96         spin_lock_init(&osc->oo_tree_lock);
97         spin_lock_init(&osc->oo_ol_spin);
98         INIT_LIST_HEAD(&osc->oo_ol_list);
99
100         atomic_set(&osc->oo_nr_ios, 0);
101         init_waitqueue_head(&osc->oo_io_waitq);
102
103         cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
104
105         return 0;
106 }
107
108 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
109 {
110         struct osc_object *osc = lu2osc(obj);
111
112         LASSERT(list_empty(&osc->oo_ready_item));
113         LASSERT(list_empty(&osc->oo_hp_ready_item));
114         LASSERT(list_empty(&osc->oo_write_item));
115         LASSERT(list_empty(&osc->oo_read_item));
116
117         LASSERT(osc->oo_root.rb_node == NULL);
118         LASSERT(list_empty(&osc->oo_hp_exts));
119         LASSERT(list_empty(&osc->oo_urgent_exts));
120         LASSERT(list_empty(&osc->oo_rpc_exts));
121         LASSERT(list_empty(&osc->oo_reading_exts));
122         LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
123         LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
124         LASSERT(list_empty(&osc->oo_ol_list));
125         LASSERT(atomic_read(&osc->oo_nr_ios) == 0);
126
127         lu_object_fini(obj);
128         OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
129 }
130
131 int osc_lvb_print(const struct lu_env *env, void *cookie,
132                   lu_printer_t p, const struct ost_lvb *lvb)
133 {
134         return (*p)(env, cookie, "size: %llu mtime: %llu atime: %llu "
135                     "ctime: %llu blocks: %llu",
136                     lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
137                     lvb->lvb_ctime, lvb->lvb_blocks);
138 }
139
140 static int osc_object_print(const struct lu_env *env, void *cookie,
141                             lu_printer_t p, const struct lu_object *obj)
142 {
143         struct osc_object   *osc   = lu2osc(obj);
144         struct lov_oinfo    *oinfo = osc->oo_oinfo;
145         struct osc_async_rc *ar    = &oinfo->loi_ar;
146
147         (*p)(env, cookie, "id: "DOSTID" "
148              "idx: %d gen: %d kms_valid: %u kms %llu "
149              "rc: %d force_sync: %d min_xid: %llu ",
150              POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
151              oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
152              ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
153         osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
154         return 0;
155 }
156
157
158 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
159                         struct cl_attr *attr)
160 {
161         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
162
163         cl_lvb2attr(attr, &oinfo->loi_lvb);
164         attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
165         return 0;
166 }
167
168 static int osc_attr_update(const struct lu_env *env, struct cl_object *obj,
169                            const struct cl_attr *attr, unsigned valid)
170 {
171         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
172         struct ost_lvb   *lvb   = &oinfo->loi_lvb;
173
174         if (valid & CAT_SIZE)
175                 lvb->lvb_size = attr->cat_size;
176         if (valid & CAT_MTIME)
177                 lvb->lvb_mtime = attr->cat_mtime;
178         if (valid & CAT_ATIME)
179                 lvb->lvb_atime = attr->cat_atime;
180         if (valid & CAT_CTIME)
181                 lvb->lvb_ctime = attr->cat_ctime;
182         if (valid & CAT_BLOCKS)
183                 lvb->lvb_blocks = attr->cat_blocks;
184         if (valid & CAT_KMS) {
185                 CDEBUG(D_CACHE, "set kms from %lluto %llu\n",
186                        oinfo->loi_kms, (__u64)attr->cat_kms);
187                 loi_kms_set(oinfo, attr->cat_kms);
188         }
189         return 0;
190 }
191
192 static int osc_object_glimpse(const struct lu_env *env,
193                               const struct cl_object *obj, struct ost_lvb *lvb)
194 {
195         struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
196
197         ENTRY;
198         lvb->lvb_size   = oinfo->loi_kms;
199         lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
200         RETURN(0);
201 }
202
203 static int osc_object_ast_clear(struct ldlm_lock *lock, void *data)
204 {
205         ENTRY;
206
207         if (lock->l_ast_data == data)
208                 lock->l_ast_data = NULL;
209         RETURN(LDLM_ITER_CONTINUE);
210 }
211
212 static int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
213 {
214         struct osc_object       *osc = cl2osc(obj);
215         struct ldlm_res_id      *resname = &osc_env_info(env)->oti_resname;
216
217         /* DLM locks don't hold a reference of osc_object so we have to
218          * clear it before the object is being destroyed. */
219         ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
220         ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
221                               osc_object_ast_clear, osc);
222         return 0;
223 }
224
225 static int osc_object_fiemap(const struct lu_env *env, struct cl_object *obj,
226                              struct ll_fiemap_info_key *fmkey,
227                              struct fiemap *fiemap, size_t *buflen)
228 {
229         struct obd_export *exp = osc_export(cl2osc(obj));
230         struct ldlm_res_id resid;
231         union ldlm_policy_data policy;
232         struct lustre_handle lockh;
233         enum ldlm_mode mode = LCK_MINMODE;
234         struct ptlrpc_request *req;
235         struct fiemap *reply;
236         char *tmp;
237         int rc;
238         ENTRY;
239
240         fmkey->lfik_oa.o_oi = cl2osc(obj)->oo_oinfo->loi_oi;
241         if (!(fmkey->lfik_fiemap.fm_flags & FIEMAP_FLAG_SYNC))
242                 goto skip_locking;
243
244         policy.l_extent.start = fmkey->lfik_fiemap.fm_start & PAGE_MASK;
245
246         if (OBD_OBJECT_EOF - fmkey->lfik_fiemap.fm_length <=
247             fmkey->lfik_fiemap.fm_start + PAGE_SIZE - 1)
248                 policy.l_extent.end = OBD_OBJECT_EOF;
249         else
250                 policy.l_extent.end = (fmkey->lfik_fiemap.fm_start +
251                                        fmkey->lfik_fiemap.fm_length +
252                                        PAGE_SIZE - 1) & PAGE_MASK;
253
254         ostid_build_res_name(&fmkey->lfik_oa.o_oi, &resid);
255         mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
256                                LDLM_FL_BLOCK_GRANTED | LDLM_FL_LVB_READY,
257                                &resid, LDLM_EXTENT, &policy,
258                                LCK_PR | LCK_PW, &lockh, 0);
259         if (mode) { /* lock is cached on client */
260                 if (mode != LCK_PR) {
261                         ldlm_lock_addref(&lockh, LCK_PR);
262                         ldlm_lock_decref(&lockh, LCK_PW);
263                 }
264         } else { /* no cached lock, needs acquire lock on server side */
265                 fmkey->lfik_oa.o_valid |= OBD_MD_FLFLAGS;
266                 fmkey->lfik_oa.o_flags |= OBD_FL_SRVLOCK;
267         }
268
269 skip_locking:
270         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
271                                    &RQF_OST_GET_INFO_FIEMAP);
272         if (req == NULL)
273                 GOTO(drop_lock, rc = -ENOMEM);
274
275         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY, RCL_CLIENT,
276                              sizeof(*fmkey));
277         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_CLIENT,
278                              *buflen);
279         req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_SERVER,
280                              *buflen);
281
282         rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
283         if (rc != 0) {
284                 ptlrpc_request_free(req);
285                 GOTO(drop_lock, rc);
286         }
287         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
288         memcpy(tmp, fmkey, sizeof(*fmkey));
289         tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_VAL);
290         memcpy(tmp, fiemap, *buflen);
291         ptlrpc_request_set_replen(req);
292
293         rc = ptlrpc_queue_wait(req);
294         if (rc != 0)
295                 GOTO(fini_req, rc);
296
297         reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
298         if (reply == NULL)
299                 GOTO(fini_req, rc = -EPROTO);
300
301         memcpy(fiemap, reply, *buflen);
302 fini_req:
303         ptlrpc_req_finished(req);
304 drop_lock:
305         if (mode)
306                 ldlm_lock_decref(&lockh, LCK_PR);
307         RETURN(rc);
308 }
309
310 void osc_object_set_contended(struct osc_object *obj)
311 {
312         obj->oo_contention_time = cfs_time_current();
313         /* mb(); */
314         obj->oo_contended = 1;
315 }
316
317 void osc_object_clear_contended(struct osc_object *obj)
318 {
319         obj->oo_contended = 0;
320 }
321
322 int osc_object_is_contended(struct osc_object *obj)
323 {
324         struct osc_device *dev  = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
325         int osc_contention_time = dev->od_contention_time;
326         cfs_time_t cur_time     = cfs_time_current();
327         cfs_time_t retry_time;
328
329         if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
330                 return 1;
331
332         if (!obj->oo_contended)
333                 return 0;
334
335         /*
336          * I like copy-paste. the code is copied from
337          * ll_file_is_contended.
338          */
339         retry_time = cfs_time_add(obj->oo_contention_time,
340                                   cfs_time_seconds(osc_contention_time));
341         if (cfs_time_after(cur_time, retry_time)) {
342                 osc_object_clear_contended(obj);
343                 return 0;
344         }
345         return 1;
346 }
347
348 /**
349  * Implementation of struct cl_object_operations::coo_req_attr_set() for osc
350  * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
351  * fields.
352  */
353 static void osc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
354                              struct cl_req_attr *attr)
355 {
356         struct lov_oinfo *oinfo;
357         struct obdo      *oa;
358         struct ost_lvb   *lvb;
359         u64               flags = attr->cra_flags;
360
361         oinfo   = cl2osc(obj)->oo_oinfo;
362         lvb     = &oinfo->loi_lvb;
363         oa      = attr->cra_oa;
364
365         if ((flags & OBD_MD_FLMTIME) != 0) {
366                 oa->o_mtime = lvb->lvb_mtime;
367                 oa->o_valid |= OBD_MD_FLMTIME;
368         }
369         if ((flags & OBD_MD_FLATIME) != 0) {
370                 oa->o_atime = lvb->lvb_atime;
371                 oa->o_valid |= OBD_MD_FLATIME;
372         }
373         if ((flags & OBD_MD_FLCTIME) != 0) {
374                 oa->o_ctime = lvb->lvb_ctime;
375                 oa->o_valid |= OBD_MD_FLCTIME;
376         }
377         if (flags & OBD_MD_FLGROUP) {
378                 ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
379                 oa->o_valid |= OBD_MD_FLGROUP;
380         }
381         if (flags & OBD_MD_FLID) {
382                 ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
383                 oa->o_valid |= OBD_MD_FLID;
384         }
385         if (flags & OBD_MD_FLHANDLE) {
386                 struct ldlm_lock *lock;
387                 struct osc_page *opg;
388
389                 opg = osc_cl_page_osc(attr->cra_page, cl2osc(obj));
390                 lock = osc_dlmlock_at_pgoff(env, cl2osc(obj), osc_index(opg),
391                                 OSC_DAP_FL_TEST_LOCK | OSC_DAP_FL_CANCELING);
392                 if (lock == NULL && !opg->ops_srvlock) {
393                         struct ldlm_resource *res;
394                         struct ldlm_res_id *resname;
395
396                         CL_PAGE_DEBUG(D_ERROR, env, attr->cra_page,
397                                       "uncovered page!\n");
398
399                         resname = &osc_env_info(env)->oti_resname;
400                         ostid_build_res_name(&oinfo->loi_oi, resname);
401                         res = ldlm_resource_get(
402                                 osc_export(cl2osc(obj))->exp_obd->obd_namespace,
403                                 NULL, resname, LDLM_EXTENT, 0);
404                         ldlm_resource_dump(D_ERROR, res);
405
406                         libcfs_debug_dumpstack(NULL);
407                         LBUG();
408                 }
409
410                 /* check for lockless io. */
411                 if (lock != NULL) {
412                         oa->o_handle = lock->l_remote_handle;
413                         oa->o_valid |= OBD_MD_FLHANDLE;
414                         LDLM_LOCK_PUT(lock);
415                 }
416         }
417 }
418
419 static const struct cl_object_operations osc_ops = {
420         .coo_page_init    = osc_page_init,
421         .coo_lock_init    = osc_lock_init,
422         .coo_io_init      = osc_io_init,
423         .coo_attr_get     = osc_attr_get,
424         .coo_attr_update  = osc_attr_update,
425         .coo_glimpse      = osc_object_glimpse,
426         .coo_prune        = osc_object_prune,
427         .coo_fiemap       = osc_object_fiemap,
428         .coo_req_attr_set = osc_req_attr_set
429 };
430
431 static const struct lu_object_operations osc_lu_obj_ops = {
432         .loo_object_init      = osc_object_init,
433         .loo_object_release   = NULL,
434         .loo_object_free      = osc_object_free,
435         .loo_object_print     = osc_object_print,
436         .loo_object_invariant = NULL
437 };
438
439 struct lu_object *osc_object_alloc(const struct lu_env *env,
440                                    const struct lu_object_header *unused,
441                                    struct lu_device *dev)
442 {
443         struct osc_object *osc;
444         struct lu_object  *obj;
445
446         OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
447         if (osc != NULL) {
448                 obj = osc2lu(osc);
449                 lu_object_init(obj, NULL, dev);
450                 osc->oo_cl.co_ops = &osc_ops;
451                 obj->lo_ops = &osc_lu_obj_ops;
452         } else
453                 obj = NULL;
454         return obj;
455 }
456
457 int osc_object_invalidate(const struct lu_env *env, struct osc_object *osc)
458 {
459         struct l_wait_info lwi = { 0 };
460         ENTRY;
461
462         CDEBUG(D_INODE, "Invalidate osc object: %p, # of active IOs: %d\n",
463                osc, atomic_read(&osc->oo_nr_ios));
464
465         l_wait_event(osc->oo_io_waitq, atomic_read(&osc->oo_nr_ios) == 0, &lwi);
466
467         /* Discard all dirty pages of this object. */
468         osc_cache_truncate_start(env, osc, 0, NULL);
469
470         /* Discard all caching pages */
471         osc_lock_discard_pages(env, osc, 0, CL_PAGE_EOF, CLM_WRITE);
472
473         /* Clear ast data of dlm lock. Do this after discarding all pages */
474         osc_object_prune(env, osc2cl(osc));
475
476         RETURN(0);
477 }
478
479 /** @} osc */