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