Whamcloud - gitweb
LU-5502 ofd: add a high level description of the OFD layer
[fs/lustre-release.git] / lustre / ofd / ofd_dev.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, 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  * lustre/ofd/ofd.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42 /*
43  * The OBD Filter Device (OFD) module belongs to the Object Storage
44  * Server stack and connects the RPC oriented Unified Target (TGT)
45  * layer (see lustre/include/lu_target.h) to the storage oriented OSD
46  * layer (see lustre/doc/osd-api.txt).
47  *
48  *     TGT
49  *      |      DT and OBD APIs
50  *     OFD
51  *      |      DT API
52  *     OSD
53  *
54  * OFD implements the LU and OBD device APIs and is responsible for:
55  *
56  * - Handling client requests (create, destroy, bulk IO, setattr,
57  *   get_info, set_info, statfs) for the objects belonging to the OST
58  *   (together with TGT).
59  *
60  * - Providing grant space management which allows clients to reserve
61  *   disk space for data writeback. OFD tracks grants on global and
62  *   per client levels.
63  *
64  * - Handling object precreation requests from MDTs.
65  *
66  * - Operating the LDLM service that allows clients to maintain object
67  *   data cache coherence.
68  */
69
70 #define DEBUG_SUBSYSTEM S_FILTER
71
72 #include <obd_class.h>
73 #include <lustre_param.h>
74 #include <lustre_fid.h>
75 #include <lustre_lfsck.h>
76 #include <lustre/lustre_idl.h>
77 #include <lustre_dlm.h>
78 #include <lustre_quota.h>
79
80 #include "ofd_internal.h"
81
82 /* Slab for OFD object allocation */
83 static struct kmem_cache *ofd_object_kmem;
84
85 static struct lu_kmem_descr ofd_caches[] = {
86         {
87                 .ckd_cache = &ofd_object_kmem,
88                 .ckd_name  = "ofd_obj",
89                 .ckd_size  = sizeof(struct ofd_object)
90         },
91         {
92                 .ckd_cache = NULL
93         }
94 };
95
96 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
97                                const char *next, struct obd_export **exp)
98 {
99         struct obd_connect_data *data = NULL;
100         struct obd_device       *obd;
101         int                      rc;
102         ENTRY;
103
104         OBD_ALLOC_PTR(data);
105         if (data == NULL)
106                 GOTO(out, rc = -ENOMEM);
107
108         obd = class_name2obd(next);
109         if (obd == NULL) {
110                 CERROR("%s: can't locate next device: %s\n",
111                        ofd_name(m), next);
112                 GOTO(out, rc = -ENOTCONN);
113         }
114
115         data->ocd_connect_flags = OBD_CONNECT_VERSION;
116         data->ocd_version = LUSTRE_VERSION_CODE;
117
118         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
119         if (rc) {
120                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
121                        ofd_name(m), next, rc);
122                 GOTO(out, rc);
123         }
124
125         m->ofd_dt_dev.dd_lu_dev.ld_site =
126                 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
127         LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
128         m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
129         m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
130
131 out:
132         if (data)
133                 OBD_FREE_PTR(data);
134         RETURN(rc);
135 }
136
137 static int ofd_stack_init(const struct lu_env *env,
138                           struct ofd_device *m, struct lustre_cfg *cfg)
139 {
140         const char              *dev = lustre_cfg_string(cfg, 0);
141         struct lu_device        *d;
142         struct ofd_thread_info  *info = ofd_info(env);
143         struct lustre_mount_info *lmi;
144         int                      rc;
145         char                    *osdname;
146
147         ENTRY;
148
149         lmi = server_get_mount(dev);
150         if (lmi == NULL) {
151                 CERROR("Cannot get mount info for %s!\n", dev);
152                 RETURN(-ENODEV);
153         }
154
155         /* find bottom osd */
156         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
157         if (osdname == NULL)
158                 RETURN(-ENOMEM);
159
160         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
161         rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
162         OBD_FREE(osdname, MTI_NAME_MAXLEN);
163         if (rc)
164                 RETURN(rc);
165
166         d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
167         LASSERT(d);
168         m->ofd_osd = lu2dt_dev(d);
169
170         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
171                  "%s-osd", lustre_cfg_string(cfg, 0));
172
173         RETURN(rc);
174 }
175
176 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
177                            struct lu_device *top)
178 {
179         struct obd_device       *obd = ofd_obd(m);
180         struct lustre_cfg_bufs   bufs;
181         struct lustre_cfg       *lcfg;
182         char                     flags[3] = "";
183
184         ENTRY;
185
186         lu_site_purge(env, top->ld_site, ~0);
187         /* process cleanup, pass mdt obd name to get obd umount flags */
188         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
189         if (obd->obd_force)
190                 strcat(flags, "F");
191         if (obd->obd_fail)
192                 strcat(flags, "A");
193         lustre_cfg_bufs_set_string(&bufs, 1, flags);
194         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
195         if (!lcfg) {
196                 CERROR("Cannot alloc lcfg!\n");
197                 RETURN_EXIT;
198         }
199
200         LASSERT(top);
201         top->ld_ops->ldo_process_config(env, top, lcfg);
202         lustre_cfg_free(lcfg);
203
204         lu_site_purge(env, top->ld_site, ~0);
205         if (!cfs_hash_is_empty(top->ld_site->ls_obj_hash)) {
206                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
207                 lu_site_print(env, top->ld_site, &msgdata, lu_cdebug_printer);
208         }
209
210         LASSERT(m->ofd_osd_exp);
211         obd_disconnect(m->ofd_osd_exp);
212
213         EXIT;
214 }
215
216 /* For interoperability, see mdt_interop_param[]. */
217 static struct cfg_interop_param ofd_interop_param[] = {
218         { "ost.quota_type",     NULL },
219         { NULL }
220 };
221
222 /* Some parameters were moved from ofd to osd and only their
223  * symlinks were kept in ofd by LU-3106. They are:
224  * -writehthrough_cache_enable
225  * -readcache_max_filese
226  * -read_cache_enable
227  * -brw_stats
228  * Since they are not included by the static lprocfs var list,
229  * a pre-check is added for them to avoid "unknown param" error
230  * message confuses the customer. If they are matched in this
231  * check, they will be passed to the osd directly.
232  */
233 static bool match_symlink_param(char *param)
234 {
235         char *sval;
236         int paramlen;
237
238         if (class_match_param(param, PARAM_OST, &param) == 0) {
239                 sval = strchr(param, '=');
240                 if (sval != NULL) {
241                         paramlen = sval - param;
242                         if (strncmp(param, "writethrough_cache_enable",
243                                     paramlen) == 0 ||
244                             strncmp(param, "readcache_max_filesize",
245                                     paramlen) == 0 ||
246                             strncmp(param, "read_cache_enable",
247                                     paramlen) == 0 ||
248                             strncmp(param, "brw_stats", paramlen) == 0)
249                                 return true;
250                 }
251         }
252
253         return false;
254 }
255
256 /* used by MGS to process specific configurations */
257 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
258                               struct lustre_cfg *cfg)
259 {
260         struct ofd_device       *m = ofd_dev(d);
261         struct dt_device        *dt_next = m->ofd_osd;
262         struct lu_device        *next = &dt_next->dd_lu_dev;
263         int                      rc;
264
265         ENTRY;
266
267         switch (cfg->lcfg_command) {
268         case LCFG_PARAM: {
269                 struct obd_device       *obd = ofd_obd(m);
270                 /* For interoperability */
271                 struct cfg_interop_param   *ptr = NULL;
272                 struct lustre_cfg          *old_cfg = NULL;
273                 char                       *param = NULL;
274
275                 param = lustre_cfg_string(cfg, 1);
276                 if (param == NULL) {
277                         CERROR("param is empty\n");
278                         rc = -EINVAL;
279                         break;
280                 }
281
282                 ptr = class_find_old_param(param, ofd_interop_param);
283                 if (ptr != NULL) {
284                         if (ptr->new_param == NULL) {
285                                 rc = 0;
286                                 CWARN("For interoperability, skip this %s."
287                                       " It is obsolete.\n", ptr->old_param);
288                                 break;
289                         }
290
291                         CWARN("Found old param %s, changed it to %s.\n",
292                               ptr->old_param, ptr->new_param);
293
294                         old_cfg = cfg;
295                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
296                         if (IS_ERR(cfg)) {
297                                 rc = PTR_ERR(cfg);
298                                 break;
299                         }
300                 }
301
302                 if (match_symlink_param(param)) {
303                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
304                         break;
305                 }
306
307                 rc = class_process_proc_seq_param(PARAM_OST, obd->obd_vars, cfg,
308                                               d->ld_obd);
309                 if (rc > 0 || rc == -ENOSYS) {
310                         CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
311                                param);
312                         /* we don't understand; pass it on */
313                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
314                 }
315                 break;
316         }
317         case LCFG_SPTLRPC_CONF: {
318                 rc = -ENOTSUPP;
319                 break;
320         }
321         default:
322                 /* others are passed further */
323                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
324                 break;
325         }
326         RETURN(rc);
327 }
328
329 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
330                            const struct lu_object_conf *conf)
331 {
332         struct ofd_device       *d = ofd_dev(o->lo_dev);
333         struct lu_device        *under;
334         struct lu_object        *below;
335         int                      rc = 0;
336
337         ENTRY;
338
339         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
340                PFID(lu_object_fid(o)));
341
342         under = &d->ofd_osd->dd_lu_dev;
343         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
344         if (below != NULL)
345                 lu_object_add(o, below);
346         else
347                 rc = -ENOMEM;
348
349         RETURN(rc);
350 }
351
352 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
353 {
354         struct ofd_object       *of = ofd_obj(o);
355         struct lu_object_header *h;
356
357         ENTRY;
358
359         h = o->lo_header;
360         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
361                PFID(lu_object_fid(o)));
362
363         lu_object_fini(o);
364         lu_object_header_fini(h);
365         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
366         EXIT;
367 }
368
369 static int ofd_object_print(const struct lu_env *env, void *cookie,
370                             lu_printer_t p, const struct lu_object *o)
371 {
372         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
373 }
374
375 struct lu_object_operations ofd_obj_ops = {
376         .loo_object_init        = ofd_object_init,
377         .loo_object_free        = ofd_object_free,
378         .loo_object_print       = ofd_object_print
379 };
380
381 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
382                                           const struct lu_object_header *hdr,
383                                           struct lu_device *d)
384 {
385         struct ofd_object *of;
386
387         ENTRY;
388
389         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, GFP_NOFS);
390         if (of != NULL) {
391                 struct lu_object        *o;
392                 struct lu_object_header *h;
393
394                 o = &of->ofo_obj.do_lu;
395                 h = &of->ofo_header;
396                 lu_object_header_init(h);
397                 lu_object_init(o, h, d);
398                 lu_object_add_top(h, o);
399                 o->lo_ops = &ofd_obj_ops;
400                 RETURN(o);
401         } else {
402                 RETURN(NULL);
403         }
404 }
405
406 extern int ost_handle(struct ptlrpc_request *req);
407
408 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
409                                 enum lfsck_events event)
410 {
411         struct ofd_device *ofd = data;
412         struct obd_device *obd = ofd_obd(ofd);
413
414         switch (event) {
415         case LE_LASTID_REBUILDING:
416                 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
417                       "on the device until the LAST_ID rebuilt successfully.\n",
418                       obd->obd_name);
419                 down_write(&ofd->ofd_lastid_rwsem);
420                 ofd->ofd_lastid_rebuilding = 1;
421                 up_write(&ofd->ofd_lastid_rwsem);
422                 break;
423         case LE_LASTID_REBUILT: {
424                 down_write(&ofd->ofd_lastid_rwsem);
425                 ofd_seqs_free(env, ofd);
426                 ofd->ofd_lastid_rebuilding = 0;
427                 ofd->ofd_lastid_gen++;
428                 up_write(&ofd->ofd_lastid_rwsem);
429                 break;
430         }
431         default:
432                 CERROR("%s: unknown lfsck event: rc = %d\n",
433                        ofd_name(ofd), event);
434                 return -EINVAL;
435         }
436
437         return 0;
438 }
439
440 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
441                        struct lu_device *dev)
442 {
443         struct ofd_thread_info          *info;
444         struct ofd_device               *ofd = ofd_dev(dev);
445         struct obd_device               *obd = ofd_obd(ofd);
446         struct lu_device                *next = &ofd->ofd_osd->dd_lu_dev;
447         int                              rc;
448
449         ENTRY;
450
451         info = ofd_info_init(env, NULL);
452         if (info == NULL)
453                 RETURN(-EFAULT);
454
455         /* initialize lower device */
456         rc = next->ld_ops->ldo_prepare(env, dev, next);
457         if (rc != 0)
458                 RETURN(rc);
459
460         rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
461                             ofd_lfsck_out_notify, ofd, false);
462         if (rc != 0) {
463                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
464                        obd->obd_name, rc);
465                 RETURN(rc);
466         }
467
468         rc = lfsck_register_namespace(env, ofd->ofd_osd, ofd->ofd_namespace);
469         /* The LFSCK instance is registered just now, so it must be there when
470          * register the namespace to such instance. */
471         LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
472
473         target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
474         LASSERT(obd->obd_no_conn);
475         spin_lock(&obd->obd_dev_lock);
476         obd->obd_no_conn = 0;
477         spin_unlock(&obd->obd_dev_lock);
478
479         if (obd->obd_recovering == 0)
480                 ofd_postrecov(env, ofd);
481
482         RETURN(rc);
483 }
484
485 static int ofd_recovery_complete(const struct lu_env *env,
486                                  struct lu_device *dev)
487 {
488         struct ofd_device       *ofd = ofd_dev(dev);
489         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
490         int                      rc = 0, max_precreate;
491
492         ENTRY;
493
494         /* Grant space for object precreation on the self export.
495          * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
496          * is enough to create 10k objects. More space is then acquired for
497          * precreation in ofd_grant_create().
498          */
499         max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
500         ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
501                           false);
502         rc = next->ld_ops->ldo_recovery_complete(env, next);
503         RETURN(rc);
504 }
505
506 static struct lu_device_operations ofd_lu_ops = {
507         .ldo_object_alloc       = ofd_object_alloc,
508         .ldo_process_config     = ofd_process_config,
509         .ldo_recovery_complete  = ofd_recovery_complete,
510         .ldo_prepare            = ofd_prepare,
511 };
512
513 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
514
515 static int ofd_procfs_init(struct ofd_device *ofd)
516 {
517         struct obd_device               *obd = ofd_obd(ofd);
518         cfs_proc_dir_entry_t            *entry;
519         int                              rc = 0;
520
521         ENTRY;
522
523         /* lprocfs must be setup before the ofd so state can be safely added
524          * to /proc incrementally as the ofd is setup */
525         obd->obd_vars = lprocfs_ofd_obd_vars;
526         rc = lprocfs_seq_obd_setup(obd);
527         if (rc) {
528                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
529                        obd->obd_name, rc);
530                 RETURN(rc);
531         }
532
533         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
534         if (rc) {
535                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
536                        obd->obd_name, rc);
537                 GOTO(obd_cleanup, rc);
538         }
539
540         obd->obd_uses_nid_stats = 1;
541
542         entry = lprocfs_seq_register("exports", obd->obd_proc_entry, NULL,
543                                      NULL);
544         if (IS_ERR(entry)) {
545                 rc = PTR_ERR(entry);
546                 CERROR("%s: error %d setting up lprocfs for %s\n",
547                        obd->obd_name, rc, "exports");
548                 GOTO(obd_cleanup, rc);
549         }
550         obd->obd_proc_exports_entry = entry;
551
552         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
553 #ifndef HAVE_ONLY_PROCFS_SEQ
554                                    NULL, NULL,
555 #endif
556                                    obd, &lprocfs_nid_stats_clear_fops);
557         if (IS_ERR(entry)) {
558                 rc = PTR_ERR(entry);
559                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
560                        obd->obd_name, rc);
561                 GOTO(obd_cleanup, rc);
562         }
563
564         ofd_stats_counter_init(obd->obd_stats);
565
566         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
567                                     ofd_stats_counter_init);
568         if (rc)
569                 GOTO(obd_cleanup, rc);
570         RETURN(0);
571 obd_cleanup:
572         lprocfs_obd_cleanup(obd);
573         lprocfs_free_obd_stats(obd);
574
575         return rc;
576 }
577
578 /**
579  * ofd_procfs_add_brw_stats_symlink - expose osd stats to ofd layer
580  *
581  * The osd interfaces to the backend file system exposes useful data
582  * such as brw_stats and read or write cache states. This same data
583  * needs to be exposed into the obdfilter (ofd) layer to maintain
584  * backwards compatibility. This function creates the symlinks in the
585  * proc layer to enable this.
586  */
587 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
588 {
589         struct obd_device       *obd = ofd_obd(ofd);
590         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
591
592         if (obd->obd_proc_entry == NULL)
593                 return;
594
595         lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
596                             "../../%s/%s/brw_stats",
597                             osd_obd->obd_type->typ_name, obd->obd_name);
598
599         lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
600                             "../../%s/%s/read_cache_enable",
601                             osd_obd->obd_type->typ_name, obd->obd_name);
602
603         lprocfs_add_symlink("readcache_max_filesize",
604                             obd->obd_proc_entry,
605                             "../../%s/%s/readcache_max_filesize",
606                             osd_obd->obd_type->typ_name, obd->obd_name);
607
608         lprocfs_add_symlink("writethrough_cache_enable",
609                             obd->obd_proc_entry,
610                             "../../%s/%s/writethrough_cache_enable",
611                             osd_obd->obd_type->typ_name, obd->obd_name);
612 }
613
614 static void ofd_procfs_fini(struct ofd_device *ofd)
615 {
616         struct obd_device *obd = ofd_obd(ofd);
617
618         lprocfs_free_per_client_stats(obd);
619         lprocfs_obd_cleanup(obd);
620         lprocfs_free_obd_stats(obd);
621         lprocfs_job_stats_fini(obd);
622 }
623
624 extern int ost_handle(struct ptlrpc_request *req);
625
626 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
627 {
628         return seq_site_fini(env, &ofd->ofd_seq_site);
629 }
630
631 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
632 {
633         struct seq_server_site  *ss = &ofd->ofd_seq_site;
634         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
635         char                    *obd_name = ofd_name(ofd);
636         char                    *name = NULL;
637         int                     rc = 0;
638
639         ss = &ofd->ofd_seq_site;
640         lu->ld_site->ld_seq_site = ss;
641         ss->ss_lu = lu->ld_site;
642         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
643
644         OBD_ALLOC_PTR(ss->ss_server_seq);
645         if (ss->ss_server_seq == NULL)
646                 GOTO(out_free, rc = -ENOMEM);
647
648         OBD_ALLOC(name, strlen(obd_name) + 10);
649         if (!name) {
650                 OBD_FREE_PTR(ss->ss_server_seq);
651                 ss->ss_server_seq = NULL;
652                 GOTO(out_free, rc = -ENOMEM);
653         }
654
655         rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
656                              LUSTRE_SEQ_SERVER, ss);
657         if (rc) {
658                 CERROR("%s : seq server init error %d\n", obd_name, rc);
659                 GOTO(out_free, rc);
660         }
661         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
662
663         OBD_ALLOC_PTR(ss->ss_client_seq);
664         if (ss->ss_client_seq == NULL)
665                 GOTO(out_free, rc = -ENOMEM);
666
667         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
668         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
669                              name, NULL);
670         if (rc) {
671                 CERROR("%s : seq client init error %d\n", obd_name, rc);
672                 GOTO(out_free, rc);
673         }
674         OBD_FREE(name, strlen(obd_name) + 10);
675         name = NULL;
676
677         rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
678
679 out_free:
680         if (rc) {
681                 if (ss->ss_server_seq) {
682                         seq_server_fini(ss->ss_server_seq, env);
683                         OBD_FREE_PTR(ss->ss_server_seq);
684                         ss->ss_server_seq = NULL;
685                 }
686
687                 if (ss->ss_client_seq) {
688                         seq_client_fini(ss->ss_client_seq);
689                         OBD_FREE_PTR(ss->ss_client_seq);
690                         ss->ss_client_seq = NULL;
691                 }
692
693                 if (name) {
694                         OBD_FREE(name, strlen(obd_name) + 10);
695                         name = NULL;
696                 }
697         }
698
699         return rc;
700 }
701
702 int ofd_set_info_hdl(struct tgt_session_info *tsi)
703 {
704         struct ptlrpc_request   *req = tgt_ses_req(tsi);
705         struct ost_body         *body = NULL, *repbody;
706         void                    *key, *val = NULL;
707         int                      keylen, vallen, rc = 0;
708         bool                     is_grant_shrink;
709         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
710
711         ENTRY;
712
713         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
714         if (key == NULL) {
715                 DEBUG_REQ(D_HA, req, "no set_info key");
716                 RETURN(err_serious(-EFAULT));
717         }
718         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
719                                       RCL_CLIENT);
720
721         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
722         if (val == NULL) {
723                 DEBUG_REQ(D_HA, req, "no set_info val");
724                 RETURN(err_serious(-EFAULT));
725         }
726         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
727                                       RCL_CLIENT);
728
729         is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
730         if (is_grant_shrink)
731                 /* In this case the value is actually an RMF_OST_BODY, so we
732                  * transmutate the type of this PTLRPC */
733                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
734
735         rc = req_capsule_server_pack(tsi->tsi_pill);
736         if (rc < 0)
737                 RETURN(rc);
738
739         if (is_grant_shrink) {
740                 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
741
742                 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
743                 *repbody = *body;
744
745                 /** handle grant shrink, similar to a read request */
746                 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
747                                        &repbody->oa);
748         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
749                 if (vallen > 0)
750                         obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
751                 rc = 0;
752         } else if (KEY_IS(KEY_CAPA_KEY)) {
753                 rc = ofd_update_capa_key(ofd, val);
754         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
755                 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
756         } else {
757                 CERROR("%s: Unsupported key %s\n",
758                        tgt_name(tsi->tsi_tgt), (char *)key);
759                 rc = -EOPNOTSUPP;
760         }
761         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
762                          tsi->tsi_jobid, 1);
763
764         RETURN(rc);
765 }
766
767 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
768                    struct lu_fid *fid, struct ll_user_fiemap *fiemap)
769 {
770         struct ofd_object       *fo;
771         int                      rc;
772
773         fo = ofd_object_find(env, ofd, fid);
774         if (IS_ERR(fo)) {
775                 CERROR("%s: error finding object "DFID"\n",
776                        ofd_name(ofd), PFID(fid));
777                 return PTR_ERR(fo);
778         }
779
780         ofd_read_lock(env, fo);
781         if (ofd_object_exists(fo))
782                 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
783         else
784                 rc = -ENOENT;
785         ofd_read_unlock(env, fo);
786         ofd_object_put(env, fo);
787         return rc;
788 }
789
790 struct locked_region {
791         struct list_head        list;
792         struct lustre_handle    lh;
793 };
794
795 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
796                        unsigned long long begin, unsigned long long end,
797                        struct list_head *locked)
798 {
799         struct locked_region    *region = NULL;
800         __u64                    flags = 0;
801         int                      rc;
802
803         LASSERT(begin <= end);
804         OBD_ALLOC_PTR(region);
805         if (region == NULL)
806                 return -ENOMEM;
807
808         rc = tgt_extent_lock(ns, res_id, begin, end, &region->lh,
809                              LCK_PR, &flags);
810         if (rc != 0)
811                 return rc;
812
813         CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
814                &region->lh);
815         list_add(&region->list, locked);
816
817         return 0;
818 }
819
820 static int lock_zero_regions(struct ldlm_namespace *ns,
821                              struct ldlm_res_id *res_id,
822                              struct ll_user_fiemap *fiemap,
823                              struct list_head *locked)
824 {
825         __u64 begin = fiemap->fm_start;
826         unsigned int i;
827         int rc = 0;
828         struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
829
830         ENTRY;
831
832         CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
833         for (i = 0; i < fiemap->fm_mapped_extents; i++) {
834                 if (fiemap_start[i].fe_logical > begin) {
835                         CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
836                                begin, fiemap_start[i].fe_logical);
837                         rc = lock_region(ns, res_id, begin,
838                                          fiemap_start[i].fe_logical, locked);
839                         if (rc)
840                                 RETURN(rc);
841                 }
842
843                 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
844         }
845
846         if (begin < (fiemap->fm_start + fiemap->fm_length)) {
847                 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
848                        begin, fiemap->fm_start + fiemap->fm_length);
849                 rc = lock_region(ns, res_id, begin,
850                                  fiemap->fm_start + fiemap->fm_length, locked);
851         }
852
853         RETURN(rc);
854 }
855
856 static void
857 unlock_zero_regions(struct ldlm_namespace *ns, struct list_head *locked)
858 {
859         struct locked_region *entry, *temp;
860
861         list_for_each_entry_safe(entry, temp, locked, list) {
862                 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
863                 tgt_extent_unlock(&entry->lh, LCK_PR);
864                 list_del(&entry->list);
865                 OBD_FREE_PTR(entry);
866         }
867 }
868
869 int ofd_get_info_hdl(struct tgt_session_info *tsi)
870 {
871         struct obd_export               *exp = tsi->tsi_exp;
872         struct ofd_device               *ofd = ofd_exp(exp);
873         struct ofd_thread_info          *fti = tsi2ofd_info(tsi);
874         void                            *key;
875         int                              keylen;
876         int                              replylen, rc = 0;
877
878         ENTRY;
879
880         /* this common part for get_info rpc */
881         key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
882         if (key == NULL) {
883                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
884                 RETURN(err_serious(-EPROTO));
885         }
886         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
887                                       RCL_CLIENT);
888
889         if (KEY_IS(KEY_LAST_ID)) {
890                 obd_id          *last_id;
891                 struct ofd_seq  *oseq;
892
893                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
894                 rc = req_capsule_server_pack(tsi->tsi_pill);
895                 if (rc)
896                         RETURN(err_serious(rc));
897
898                 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
899
900                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
901                                     (obd_seq)exp->exp_filter_data.fed_group);
902                 if (IS_ERR(oseq))
903                         rc = -EFAULT;
904                 else
905                         *last_id = ofd_seq_last_oid(oseq);
906                 ofd_seq_put(tsi->tsi_env, oseq);
907         } else if (KEY_IS(KEY_FIEMAP)) {
908                 struct ll_fiemap_info_key       *fm_key;
909                 struct ll_user_fiemap           *fiemap;
910                 struct lu_fid                   *fid;
911
912                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
913
914                 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
915                 rc = tgt_validate_obdo(tsi, &fm_key->oa);
916                 if (rc)
917                         RETURN(err_serious(rc));
918
919                 fid = &fm_key->oa.o_oi.oi_fid;
920
921                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
922
923                 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
924                 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
925                                      RCL_SERVER, replylen);
926
927                 rc = req_capsule_server_pack(tsi->tsi_pill);
928                 if (rc)
929                         RETURN(err_serious(rc));
930
931                 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
932                 if (fiemap == NULL)
933                         RETURN(-ENOMEM);
934
935                 *fiemap = fm_key->fiemap;
936                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
937
938                 /* LU-3219: Lock the sparse areas to make sure dirty
939                  * flushed back from client, then call fiemap again. */
940                 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
941                     fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
942                         struct list_head locked;
943
944                         INIT_LIST_HEAD(&locked);
945                         ost_fid_build_resid(fid, &fti->fti_resid);
946                         rc = lock_zero_regions(ofd->ofd_namespace,
947                                                &fti->fti_resid, fiemap,
948                                                &locked);
949                         if (rc == 0 && !list_empty(&locked)) {
950                                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
951                                                     fiemap);
952                                 unlock_zero_regions(ofd->ofd_namespace,
953                                                     &locked);
954                         }
955                 }
956         } else if (KEY_IS(KEY_LAST_FID)) {
957                 struct ofd_device       *ofd = ofd_exp(exp);
958                 struct ofd_seq          *oseq;
959                 struct lu_fid           *fid;
960                 int                      rc;
961
962                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
963                 rc = req_capsule_server_pack(tsi->tsi_pill);
964                 if (rc)
965                         RETURN(err_serious(rc));
966
967                 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
968                 if (fid == NULL)
969                         RETURN(err_serious(-EPROTO));
970
971                 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
972
973                 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
974                 if (fid == NULL)
975                         RETURN(-ENOMEM);
976
977                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
978                                     ostid_seq(&fti->fti_ostid));
979                 if (IS_ERR(oseq))
980                         RETURN(PTR_ERR(oseq));
981
982                 rc = ostid_to_fid(fid, &oseq->os_oi,
983                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
984                 if (rc != 0)
985                         GOTO(out_put, rc);
986
987                 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
988                        PFID(fid));
989 out_put:
990                 ofd_seq_put(tsi->tsi_env, oseq);
991         } else {
992                 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
993                        (char *)key);
994                 rc = -EOPNOTSUPP;
995         }
996         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
997                          tsi->tsi_jobid, 1);
998
999         RETURN(rc);
1000 }
1001
1002 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
1003 {
1004         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1005         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1006         struct ost_body         *repbody;
1007         struct lustre_handle     lh = { 0 };
1008         struct ofd_object       *fo;
1009         __u64                    flags = 0;
1010         ldlm_mode_t              lock_mode = LCK_PR;
1011         bool                     srvlock;
1012         int                      rc;
1013         ENTRY;
1014
1015         LASSERT(tsi->tsi_ost_body != NULL);
1016
1017         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1018         if (repbody == NULL)
1019                 RETURN(-ENOMEM);
1020
1021         repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
1022         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1023
1024         srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
1025                   tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
1026
1027         if (srvlock) {
1028                 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1029                         lock_mode = LCK_PW;
1030
1031                 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1032                                      &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1033                                      lock_mode, &flags);
1034                 if (rc != 0)
1035                         RETURN(rc);
1036         }
1037
1038         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1039         if (IS_ERR(fo))
1040                 GOTO(out, rc = PTR_ERR(fo));
1041
1042         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1043         if (rc == 0) {
1044                 __u64    curr_version;
1045
1046                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1047                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1048                 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1049
1050                 /* Store object version in reply */
1051                 curr_version = dt_version_get(tsi->tsi_env,
1052                                               ofd_object_child(fo));
1053                 if ((__s64)curr_version != -EOPNOTSUPP) {
1054                         repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1055                         repbody->oa.o_data_version = curr_version;
1056                 }
1057         }
1058
1059         ofd_object_put(tsi->tsi_env, fo);
1060 out:
1061         if (srvlock)
1062                 tgt_extent_unlock(&lh, lock_mode);
1063
1064         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1065                          tsi->tsi_jobid, 1);
1066
1067         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1068         repbody->oa.o_flags = OBD_FL_FLUSH;
1069
1070         RETURN(rc);
1071 }
1072
1073 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1074 {
1075         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1076         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1077         struct ost_body         *body = tsi->tsi_ost_body;
1078         struct ost_body         *repbody;
1079         struct ldlm_resource    *res;
1080         struct ofd_object       *fo;
1081         struct filter_fid       *ff = NULL;
1082         int                      rc = 0;
1083
1084         ENTRY;
1085
1086         LASSERT(body != NULL);
1087
1088         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1089         if (repbody == NULL)
1090                 RETURN(-ENOMEM);
1091
1092         repbody->oa.o_oi = body->oa.o_oi;
1093         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1094
1095         /* This would be very bad - accidentally truncating a file when
1096          * changing the time or similar - bug 12203. */
1097         if (body->oa.o_valid & OBD_MD_FLSIZE &&
1098             body->oa.o_size != OBD_OBJECT_EOF) {
1099                 static char mdsinum[48];
1100
1101                 if (body->oa.o_valid & OBD_MD_FLFID)
1102                         snprintf(mdsinum, sizeof(mdsinum) - 1,
1103                                  "of parent "DFID, body->oa.o_parent_seq,
1104                                  body->oa.o_parent_oid, 0);
1105                 else
1106                         mdsinum[0] = '\0';
1107
1108                 CERROR("%s: setattr from %s is trying to truncate object "DFID
1109                        " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1110                        PFID(&tsi->tsi_fid), mdsinum);
1111                 RETURN(-EPERM);
1112         }
1113
1114         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1115         if (IS_ERR(fo))
1116                 GOTO(out, rc = PTR_ERR(fo));
1117
1118         la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1119         fti->fti_attr.la_valid &= ~LA_TYPE;
1120
1121         if (body->oa.o_valid & OBD_MD_FLFID) {
1122                 ff = &fti->fti_mds_fid;
1123                 ofd_prepare_fidea(ff, &body->oa);
1124         }
1125
1126         /* setting objects attributes (including owner/group) */
1127         rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1128         if (rc != 0)
1129                 GOTO(out_put, rc);
1130
1131         obdo_from_la(&repbody->oa, &fti->fti_attr,
1132                      OFD_VALID_FLAGS | LA_UID | LA_GID);
1133         tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1134
1135         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1136                          tsi->tsi_jobid, 1);
1137         EXIT;
1138 out_put:
1139         ofd_object_put(tsi->tsi_env, fo);
1140 out:
1141         if (rc == 0) {
1142                 /* we do not call this before to avoid lu_object_find() in
1143                  *  ->lvbo_update() holding another reference on the object.
1144                  * otherwise concurrent destroy can make the object unavailable
1145                  * for 2nd lu_object_find() waiting for the first reference
1146                  * to go... deadlock! */
1147                 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1148                                         &tsi->tsi_resid, LDLM_EXTENT, 0);
1149                 if (!IS_ERR(res)) {
1150                         ldlm_res_lvbo_update(res, NULL, 0);
1151                         ldlm_resource_putref(res);
1152                 }
1153         }
1154         return rc;
1155 }
1156
1157 static int ofd_orphans_destroy(const struct lu_env *env,
1158                                struct obd_export *exp,
1159                                struct ofd_device *ofd, struct obdo *oa)
1160 {
1161         struct ofd_thread_info  *info   = ofd_info(env);
1162         struct lu_fid           *fid    = &info->fti_fid;
1163         struct ost_id           *oi     = &oa->o_oi;
1164         struct ofd_seq          *oseq;
1165         obd_seq                  seq    = ostid_seq(oi);
1166         obd_id                   end_id = ostid_id(oi);
1167         obd_id                   last;
1168         obd_id                   oid;
1169         int                      skip_orphan;
1170         int                      rc     = 0;
1171
1172         ENTRY;
1173
1174         oseq = ofd_seq_get(ofd, seq);
1175         if (oseq == NULL) {
1176                 CERROR("%s: Can not find seq for "DOSTID"\n",
1177                        ofd_name(ofd), POSTID(oi));
1178                 RETURN(-EINVAL);
1179         }
1180
1181         *fid = oi->oi_fid;
1182         last = ofd_seq_last_oid(oseq);
1183         oid = last;
1184
1185         LASSERT(exp != NULL);
1186         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1187
1188         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NODESTROY))
1189                 goto done;
1190
1191         LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1192                  " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1193
1194         while (oid > end_id) {
1195                 rc = fid_set_id(fid, oid);
1196                 if (unlikely(rc != 0))
1197                         GOTO(out_put, rc);
1198
1199                 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1200                 if (rc != 0 && rc != -ENOENT && rc != -ESTALE &&
1201                     likely(rc != -EREMCHG && rc != -EINPROGRESS))
1202                         /* this is pretty fatal... */
1203                         CEMERG("%s: error destroying precreated id "
1204                                DFID": rc = %d\n",
1205                                ofd_name(ofd), PFID(fid), rc);
1206
1207                 oid--;
1208                 if (!skip_orphan) {
1209                         ofd_seq_last_oid_set(oseq, oid);
1210                         /* update last_id on disk periodically so that if we
1211                          * restart * we don't need to re-scan all of the just
1212                          * deleted objects. */
1213                         if ((oid & 511) == 0)
1214                                 ofd_seq_last_oid_write(env, ofd, oseq);
1215                 }
1216         }
1217
1218         CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1219                ofd_name(ofd), seq, oid);
1220
1221 done:
1222         if (!skip_orphan) {
1223                 ofd_seq_last_oid_set(oseq, oid);
1224                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1225         } else {
1226                 /* don't reuse orphan object, return last used objid */
1227                 ostid_set_id(oi, last);
1228                 rc = 0;
1229         }
1230
1231         GOTO(out_put, rc);
1232
1233 out_put:
1234         ofd_seq_put(env, oseq);
1235         return rc;
1236 }
1237
1238 static int ofd_create_hdl(struct tgt_session_info *tsi)
1239 {
1240         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1241         struct ost_body         *repbody;
1242         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1243         struct obdo             *rep_oa;
1244         struct obd_export       *exp = tsi->tsi_exp;
1245         struct ofd_device       *ofd = ofd_exp(exp);
1246         obd_seq                  seq = ostid_seq(&oa->o_oi);
1247         obd_id                   oid = ostid_id(&oa->o_oi);
1248         struct ofd_seq          *oseq;
1249         int                      rc = 0, diff;
1250         int                      sync_trans = 0;
1251
1252         ENTRY;
1253
1254         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1255                 RETURN(-EROFS);
1256
1257         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1258         if (repbody == NULL)
1259                 RETURN(-ENOMEM);
1260
1261         down_read(&ofd->ofd_lastid_rwsem);
1262         /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1263          * we may do that in the future.
1264          * Return -ENOSPC until the LAST_ID rebuilt. */
1265         if (unlikely(ofd->ofd_lastid_rebuilding))
1266                 GOTO(out_sem, rc = -ENOSPC);
1267
1268         rep_oa = &repbody->oa;
1269         rep_oa->o_oi = oa->o_oi;
1270
1271         LASSERT(seq >= FID_SEQ_OST_MDT0);
1272         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1273
1274         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1275
1276         oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1277         if (IS_ERR(oseq)) {
1278                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1279                        ofd_name(ofd), seq, PTR_ERR(oseq));
1280                 GOTO(out_sem, rc = -EINVAL);
1281         }
1282
1283         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1284             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1285                 if (!ofd_obd(ofd)->obd_recovering ||
1286                     oid > ofd_seq_last_oid(oseq)) {
1287                         CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1288                                "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1289                                ofd_seq_last_oid(oseq));
1290                         GOTO(out_nolock, rc = -EINVAL);
1291                 }
1292                 /* Do nothing here, we re-create objects during recovery
1293                  * upon write replay, see ofd_preprw_write() */
1294                 GOTO(out_nolock, rc = 0);
1295         }
1296         /* former ofd_handle_precreate */
1297         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1298             (oa->o_flags & OBD_FL_DELORPHAN)) {
1299                 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1300
1301                 /* destroy orphans */
1302                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1303                     exp->exp_conn_cnt) {
1304                         CERROR("%s: dropping old orphan cleanup request\n",
1305                                ofd_name(ofd));
1306                         GOTO(out_nolock, rc = 0);
1307                 }
1308                 /* This causes inflight precreates to abort and drop lock */
1309                 oseq->os_destroys_in_progress = 1;
1310                 mutex_lock(&oseq->os_create_lock);
1311                 if (!oseq->os_destroys_in_progress) {
1312                         CERROR("%s:["LPU64"] destroys_in_progress already"
1313                                " cleared\n", ofd_name(ofd), seq);
1314                         ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1315                         GOTO(out, rc = 0);
1316                 }
1317                 diff = oid - ofd_seq_last_oid(oseq);
1318                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1319                         ofd_seq_last_oid(oseq), diff);
1320                 if (-diff > OST_MAX_PRECREATE) {
1321                         /* FIXME: should reset precreate_next_id on MDS */
1322                         rc = 0;
1323                 } else if (diff < 0) {
1324                         rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1325                                                  ofd, rep_oa);
1326                         oseq->os_destroys_in_progress = 0;
1327                 } else {
1328                         /* XXX: Used by MDS for the first time! */
1329                         oseq->os_destroys_in_progress = 0;
1330                 }
1331         } else {
1332                 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1333                              ofd->ofd_lastid_gen)) {
1334                         ofd_obd_disconnect(exp);
1335                         GOTO(out_nolock, rc = -ENOTCONN);
1336                 }
1337
1338                 mutex_lock(&oseq->os_create_lock);
1339                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1340                     exp->exp_conn_cnt) {
1341                         CERROR("%s: dropping old precreate request\n",
1342                                ofd_name(ofd));
1343                         GOTO(out, rc = 0);
1344                 }
1345                 /* only precreate if seq is 0, IDIF or normal and also o_id
1346                  * must be specfied */
1347                 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1348                      !fid_seq_is_idif(seq)) || oid == 0) {
1349                         diff = 1; /* shouldn't we create this right now? */
1350                 } else {
1351                         diff = oid - ofd_seq_last_oid(oseq);
1352                         /* Do sync create if the seq is about to used up */
1353                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1354                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
1355                                         sync_trans = 1;
1356                         } else if (fid_seq_is_norm(seq)) {
1357                                 if (unlikely(oid >=
1358                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1359                                         sync_trans = 1;
1360                         } else {
1361                                 CERROR("%s : invalid o_seq "DOSTID"\n",
1362                                        ofd_name(ofd), POSTID(&oa->o_oi));
1363                                 GOTO(out, rc = -EINVAL);
1364                         }
1365                 }
1366         }
1367         if (diff > 0) {
1368                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1369                 obd_id           next_id;
1370                 int              created = 0;
1371                 int              count;
1372
1373                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1374                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1375                         /* don't enforce grant during orphan recovery */
1376                         rc = ofd_grant_create(tsi->tsi_env,
1377                                               ofd_obd(ofd)->obd_self_export,
1378                                               &diff);
1379                         if (rc) {
1380                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1381                                        "space for precreate (%d): rc = %d\n",
1382                                        ofd_name(ofd), diff, rc);
1383                                 diff = 0;
1384                         }
1385                 }
1386
1387                 /* This can happen if a new OST is formatted and installed
1388                  * in place of an old one at the same index.  Instead of
1389                  * precreating potentially millions of deleted old objects
1390                  * (possibly filling the OST), only precreate the last batch.
1391                  * LFSCK will eventually clean up any orphans. LU-14 */
1392                 if (diff > 5 * OST_MAX_PRECREATE) {
1393                         diff = OST_MAX_PRECREATE / 2;
1394                         LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1395                                       "larger than the LAST_ID "DOSTID", only "
1396                                       "precreating the last %u objects.\n",
1397                                       ofd_name(ofd), POSTID(&oa->o_oi),
1398                                       5 * OST_MAX_PRECREATE,
1399                                       POSTID(&oseq->os_oi), diff);
1400                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1401                 }
1402
1403                 while (diff > 0) {
1404                         next_id = ofd_seq_last_oid(oseq) + 1;
1405                         count = ofd_precreate_batch(ofd, diff);
1406
1407                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1408                                " at "LPU64"\n", ofd_name(ofd),
1409                                count, seq, next_id);
1410
1411                         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1412                             && cfs_time_after(jiffies, enough_time)) {
1413                                 CDEBUG(D_HA, "%s: Slow creates, %d/%d objects"
1414                                       " created at a rate of %d/s\n",
1415                                       ofd_name(ofd), created, diff + created,
1416                                       created / DISK_TIMEOUT);
1417                                 break;
1418                         }
1419
1420                         rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1421                                                    oseq, count, sync_trans);
1422                         if (rc > 0) {
1423                                 created += rc;
1424                                 diff -= rc;
1425                         } else if (rc < 0) {
1426                                 break;
1427                         }
1428                 }
1429
1430                 if (diff > 0 &&
1431                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1432                         LCONSOLE_WARN("%s: can't create the same count of"
1433                                       " objects when replaying the request"
1434                                       " (diff is %d). see LU-4621\n",
1435                                       ofd_name(ofd), diff);
1436
1437                 if (created > 0)
1438                         /* some objects got created, we can return
1439                          * them, even if last creation failed */
1440                         rc = 0;
1441                 else
1442                         CERROR("%s: unable to precreate: rc = %d\n",
1443                                ofd_name(ofd), rc);
1444
1445                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1446                     !(oa->o_flags & OBD_FL_DELORPHAN))
1447                         ofd_grant_commit(tsi->tsi_env,
1448                                          ofd_obd(ofd)->obd_self_export, rc);
1449
1450                 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1451         }
1452         EXIT;
1453         ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1454                          tsi->tsi_jobid, 1);
1455 out:
1456         mutex_unlock(&oseq->os_create_lock);
1457 out_nolock:
1458         if (rc == 0) {
1459 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
1460                 struct ofd_thread_info  *info = ofd_info(tsi->tsi_env);
1461                 struct lu_fid           *fid = &info->fti_fid;
1462
1463                 /* For compatible purpose, it needs to convert back to
1464                  * OST ID before put it on wire. */
1465                 *fid = rep_oa->o_oi.oi_fid;
1466                 fid_to_ostid(fid, &rep_oa->o_oi);
1467 #endif
1468                 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1469         }
1470         ofd_seq_put(tsi->tsi_env, oseq);
1471
1472 out_sem:
1473         up_read(&ofd->ofd_lastid_rwsem);
1474         return rc;
1475 }
1476
1477 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1478 {
1479         const struct ost_body   *body = tsi->tsi_ost_body;
1480         struct ost_body         *repbody;
1481         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1482         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1483         struct lu_fid           *fid = &fti->fti_fid;
1484         obd_id                   oid;
1485         obd_count                count;
1486         int                      rc = 0;
1487
1488         ENTRY;
1489
1490         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1491                 RETURN(-EROFS);
1492
1493         /* This is old case for clients before Lustre 2.4 */
1494         /* If there's a DLM request, cancel the locks mentioned in it */
1495         if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1496                                       RCL_CLIENT)) {
1497                 struct ldlm_request *dlm;
1498
1499                 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1500                 if (dlm == NULL)
1501                         RETURN(-EFAULT);
1502                 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1503         }
1504
1505         *fid = body->oa.o_oi.oi_fid;
1506         oid = ostid_id(&body->oa.o_oi);
1507         LASSERT(oid != 0);
1508
1509         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1510
1511         /* check that o_misc makes sense */
1512         if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1513                 count = body->oa.o_misc;
1514         else
1515                 count = 1; /* default case - single destroy */
1516
1517         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1518                POSTID(&body->oa.o_oi), count);
1519
1520         while (count > 0) {
1521                 int lrc;
1522
1523                 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1524                 if (lrc == -ENOENT) {
1525                         CDEBUG(D_INODE,
1526                                "%s: destroying non-existent object "DFID"\n",
1527                                ofd_name(ofd), PFID(fid));
1528                         /* rewrite rc with -ENOENT only if it is 0 */
1529                         if (rc == 0)
1530                                 rc = lrc;
1531                 } else if (lrc != 0) {
1532                         CERROR("%s: error destroying object "DFID": %d\n",
1533                                ofd_name(ofd), PFID(fid), lrc);
1534                         rc = lrc;
1535                 }
1536
1537                 count--;
1538                 oid++;
1539                 lrc = fid_set_id(fid, oid);
1540                 if (unlikely(lrc != 0 && count > 0))
1541                         GOTO(out, rc = lrc);
1542         }
1543
1544         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1545                          tsi->tsi_jobid, 1);
1546
1547         GOTO(out, rc);
1548
1549 out:
1550         fid_to_ostid(fid, &repbody->oa.o_oi);
1551         return rc;
1552 }
1553
1554 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1555 {
1556         struct obd_statfs       *osfs;
1557         int                      rc;
1558
1559         ENTRY;
1560
1561         osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1562
1563         rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1564                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1565         if (rc != 0)
1566                 CERROR("%s: statfs failed: rc = %d\n",
1567                        tgt_name(tsi->tsi_tgt), rc);
1568
1569         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1570                 rc = -EINPROGRESS;
1571
1572         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1573                          tsi->tsi_jobid, 1);
1574
1575         RETURN(rc);
1576 }
1577
1578 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1579 {
1580         struct ost_body         *body = tsi->tsi_ost_body;
1581         struct ost_body         *repbody;
1582         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1583         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1584         struct ofd_object       *fo = NULL;
1585         int                      rc = 0;
1586
1587         ENTRY;
1588
1589         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1590
1591         /* if no objid is specified, it means "sync whole filesystem" */
1592         if (!fid_is_zero(&tsi->tsi_fid)) {
1593                 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1594                 if (IS_ERR(fo))
1595                         RETURN(PTR_ERR(fo));
1596         }
1597
1598         rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1599                       fo != NULL ? ofd_object_child(fo) : NULL,
1600                       repbody->oa.o_size, repbody->oa.o_blocks);
1601         if (rc)
1602                 GOTO(put, rc);
1603
1604         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1605                          tsi->tsi_jobid, 1);
1606         if (fo == NULL)
1607                 RETURN(0);
1608
1609         repbody->oa.o_oi = body->oa.o_oi;
1610         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1611
1612         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1613         if (rc == 0)
1614                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1615                              OFD_VALID_FLAGS);
1616         else
1617                 /* don't return rc from getattr */
1618                 rc = 0;
1619         EXIT;
1620 put:
1621         if (fo != NULL)
1622                 ofd_object_put(tsi->tsi_env, fo);
1623         return rc;
1624 }
1625
1626 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1627 {
1628         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1629         struct ost_body         *repbody;
1630         struct ofd_thread_info  *info = tsi2ofd_info(tsi);
1631         struct ldlm_namespace   *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1632         struct ldlm_resource    *res;
1633         struct ofd_object       *fo;
1634         struct filter_fid       *ff = NULL;
1635         __u64                    flags = 0;
1636         struct lustre_handle     lh = { 0, };
1637         int                      rc;
1638         __u64                    start, end;
1639         bool                     srvlock;
1640
1641         ENTRY;
1642
1643         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1644         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1645
1646         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1647             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1648                 RETURN(err_serious(-EPROTO));
1649
1650         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1651         if (repbody == NULL)
1652                 RETURN(err_serious(-ENOMEM));
1653
1654         /* punch start,end are passed in o_size,o_blocks throught wire */
1655         start = oa->o_size;
1656         end = oa->o_blocks;
1657
1658         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1659                 RETURN(-EPROTO);
1660
1661         /* standard truncate optimization: if file body is completely
1662          * destroyed, don't send data back to the server. */
1663         if (start == 0)
1664                 flags |= LDLM_FL_AST_DISCARD_DATA;
1665
1666         repbody->oa.o_oi = oa->o_oi;
1667         repbody->oa.o_valid = OBD_MD_FLID;
1668
1669         srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1670                   oa->o_flags & OBD_FL_SRVLOCK;
1671
1672         if (srvlock) {
1673                 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1674                                      LCK_PW, &flags);
1675                 if (rc != 0)
1676                         RETURN(rc);
1677         }
1678
1679         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1680                ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1681                oa->o_valid, start, end);
1682
1683         fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1684                                     &tsi->tsi_fid);
1685         if (IS_ERR(fo))
1686                 GOTO(out, rc = PTR_ERR(fo));
1687
1688         la_from_obdo(&info->fti_attr, oa,
1689                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1690         info->fti_attr.la_size = start;
1691         info->fti_attr.la_valid |= LA_SIZE;
1692
1693         if (oa->o_valid & OBD_MD_FLFID) {
1694                 ff = &info->fti_mds_fid;
1695                 ofd_prepare_fidea(ff, oa);
1696         }
1697
1698         rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1699                               ff, (struct obdo *)oa);
1700         if (rc)
1701                 GOTO(out_put, rc);
1702
1703         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1704                          tsi->tsi_jobid, 1);
1705         EXIT;
1706 out_put:
1707         ofd_object_put(tsi->tsi_env, fo);
1708 out:
1709         if (srvlock)
1710                 tgt_extent_unlock(&lh, LCK_PW);
1711         if (rc == 0) {
1712                 /* we do not call this before to avoid lu_object_find() in
1713                  *  ->lvbo_update() holding another reference on the object.
1714                  * otherwise concurrent destroy can make the object unavailable
1715                  * for 2nd lu_object_find() waiting for the first reference
1716                  * to go... deadlock! */
1717                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1718                                         LDLM_EXTENT, 0);
1719                 if (!IS_ERR(res)) {
1720                         ldlm_res_lvbo_update(res, NULL, 0);
1721                         ldlm_resource_putref(res);
1722                 }
1723         }
1724         return rc;
1725 }
1726
1727 static int ofd_quotactl(struct tgt_session_info *tsi)
1728 {
1729         struct obd_quotactl     *oqctl, *repoqc;
1730         int                      rc;
1731
1732         ENTRY;
1733
1734         oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1735         if (oqctl == NULL)
1736                 RETURN(err_serious(-EPROTO));
1737
1738         repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1739         if (repoqc == NULL)
1740                 RETURN(err_serious(-ENOMEM));
1741
1742         /* report success for quota on/off for interoperability with current MDT
1743          * stack */
1744         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1745                 RETURN(0);
1746
1747         *repoqc = *oqctl;
1748         rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1749
1750         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1751                          tsi->tsi_jobid, 1);
1752
1753         RETURN(rc);
1754 }
1755
1756 /* High priority request handlers for OFD */
1757
1758 /* prolong locks for the current service time of the corresponding
1759  * portal (= OST_IO_PORTAL)
1760  */
1761 static inline int prolong_timeout(struct ptlrpc_request *req)
1762 {
1763         struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1764
1765         if (AT_OFF)
1766                 return obd_timeout / 2;
1767
1768         return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
1769                    ldlm_timeout);
1770 }
1771
1772 static int ofd_prolong_one_lock(struct tgt_session_info *tsi,
1773                                 struct ldlm_lock *lock,
1774                                 struct ldlm_extent *extent, int timeout)
1775 {
1776
1777         if (lock->l_flags & LDLM_FL_DESTROYED) /* lock already cancelled */
1778                 return 0;
1779
1780         /* XXX: never try to grab resource lock here because we're inside
1781          * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
1782          * res lock and then exp_bl_list_lock. */
1783
1784         if (!(lock->l_flags & LDLM_FL_AST_SENT))
1785                 /* ignore locks not being cancelled */
1786                 return 0;
1787
1788         LDLM_DEBUG(lock, "refreshed for req x"LPU64" ext("LPU64"->"LPU64") "
1789                          "to %ds.\n", tgt_ses_req(tsi)->rq_xid, extent->start,
1790                          extent->end, timeout);
1791
1792         /* OK. this is a possible lock the user holds doing I/O
1793          * let's refresh eviction timer for it */
1794         ldlm_refresh_waiting_lock(lock, timeout);
1795         return 1;
1796 }
1797
1798 static int ofd_prolong_extent_locks(struct tgt_session_info *tsi,
1799                                     __u64 start, __u64 end)
1800 {
1801         struct obd_export       *exp = tsi->tsi_exp;
1802         struct obdo             *oa  = &tsi->tsi_ost_body->oa;
1803         struct ldlm_extent       extent = {
1804                 .start = start,
1805                 .end = end
1806         };
1807         struct ldlm_lock        *lock;
1808         int                      timeout = prolong_timeout(tgt_ses_req(tsi));
1809         int                      lock_count = 0;
1810
1811         ENTRY;
1812
1813         if (oa->o_valid & OBD_MD_FLHANDLE) {
1814                 /* mostly a request should be covered by only one lock, try
1815                  * fast path. */
1816                 lock = ldlm_handle2lock(&oa->o_handle);
1817                 if (lock != NULL) {
1818                         /* Fast path to check if the lock covers the whole IO
1819                          * region exclusively. */
1820                         if (lock->l_granted_mode == LCK_PW &&
1821                             ldlm_extent_contain(&lock->l_policy_data.l_extent,
1822                                                 &extent)) {
1823                                 /* bingo */
1824                                 LASSERT(lock->l_export == exp);
1825                                 lock_count = ofd_prolong_one_lock(tsi, lock,
1826                                                              &extent, timeout);
1827                                 LDLM_LOCK_PUT(lock);
1828                                 RETURN(lock_count);
1829                         }
1830                         LDLM_LOCK_PUT(lock);
1831                 }
1832         }
1833
1834         spin_lock_bh(&exp->exp_bl_list_lock);
1835         list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
1836                 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1837                 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
1838
1839                 if (!ldlm_res_eq(&tsi->tsi_resid, &lock->l_resource->lr_name))
1840                         continue;
1841
1842                 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
1843                                          &extent))
1844                         continue;
1845
1846                 lock_count += ofd_prolong_one_lock(tsi, lock, &extent, timeout);
1847         }
1848         spin_unlock_bh(&exp->exp_bl_list_lock);
1849
1850         RETURN(lock_count);
1851 }
1852
1853 /**
1854  * Returns 1 if the given PTLRPC matches the given LDLM lock, or 0 if it does
1855  * not.
1856  */
1857 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
1858                                    struct ldlm_lock *lock)
1859 {
1860         struct niobuf_remote    *rnb;
1861         struct obd_ioobj        *ioo;
1862         ldlm_mode_t              mode;
1863         struct ldlm_extent       ext;
1864         __u32                    opc = lustre_msg_get_opc(req->rq_reqmsg);
1865
1866         ENTRY;
1867
1868         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1869         LASSERT(ioo != NULL);
1870
1871         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1872         LASSERT(rnb != NULL);
1873
1874         ext.start = rnb->rnb_offset;
1875         rnb += ioo->ioo_bufcnt - 1;
1876         ext.end = rnb->rnb_offset + rnb->rnb_len - 1;
1877
1878         LASSERT(lock->l_resource != NULL);
1879         if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
1880                 RETURN(0);
1881
1882         mode = LCK_PW;
1883         if (opc == OST_READ)
1884                 mode |= LCK_PR;
1885
1886         if (!(lock->l_granted_mode & mode))
1887                 RETURN(0);
1888
1889         RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
1890 }
1891
1892 /**
1893  * High-priority queue request check for whether the given PTLRPC request
1894  * (\a req) is blocking an LDLM lock cancel.
1895  *
1896  * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1897  * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1898  *
1899  * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue.  This
1900  * function looks only at OST_READs and OST_WRITEs.
1901  */
1902 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
1903 {
1904         struct tgt_session_info *tsi;
1905         struct obd_ioobj        *ioo;
1906         struct niobuf_remote    *rnb;
1907         __u64                    start, end;
1908         int                      lock_count;
1909
1910         ENTRY;
1911
1912         /* Don't use tgt_ses_info() to get session info, because lock_match()
1913          * can be called while request has no processing thread yet. */
1914         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1915         LASSERT(tsi != NULL);
1916
1917         /*
1918          * Use LASSERT below because malformed RPCs should have
1919          * been filtered out in tgt_hpreq_handler().
1920          */
1921         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1922         LASSERT(ioo != NULL);
1923
1924         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1925         LASSERT(rnb != NULL);
1926         LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
1927
1928         start = rnb->rnb_offset;
1929         rnb += ioo->ioo_bufcnt - 1;
1930         end = rnb->rnb_offset + rnb->rnb_len - 1;
1931
1932         DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
1933                                    " ("LPU64"->"LPU64")\n",
1934                   tgt_name(tsi->tsi_tgt), current->comm,
1935                   PFID(&tsi->tsi_fid), start, end);
1936
1937         lock_count = ofd_prolong_extent_locks(tsi, start, end);
1938
1939         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1940                tgt_name(tsi->tsi_tgt), lock_count, req);
1941
1942         RETURN(lock_count > 0);
1943 }
1944
1945 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
1946 {
1947         ofd_rw_hpreq_check(req);
1948 }
1949
1950 /**
1951  * Like tgt_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1952  */
1953 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
1954                                       struct ldlm_lock *lock)
1955 {
1956         struct tgt_session_info *tsi;
1957
1958         /* Don't use tgt_ses_info() to get session info, because lock_match()
1959          * can be called while request has no processing thread yet. */
1960         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1961         LASSERT(tsi != NULL);
1962
1963         LASSERT(tsi->tsi_ost_body != NULL);
1964         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
1965             tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1966                 return 1;
1967
1968         return 0;
1969 }
1970
1971 /**
1972  * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1973  */
1974 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
1975 {
1976         struct tgt_session_info *tsi;
1977         struct obdo             *oa;
1978         int                      lock_count;
1979
1980         ENTRY;
1981
1982         /* Don't use tgt_ses_info() to get session info, because lock_match()
1983          * can be called while request has no processing thread yet. */
1984         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1985         LASSERT(tsi != NULL);
1986         oa = &tsi->tsi_ost_body->oa;
1987
1988         LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
1989                   oa->o_flags & OBD_FL_SRVLOCK));
1990
1991         CDEBUG(D_DLMTRACE,
1992                "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1993                tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
1994                tsi->tsi_resid.name[1], oa->o_size, oa->o_blocks);
1995
1996         lock_count = ofd_prolong_extent_locks(tsi, oa->o_size, oa->o_blocks);
1997
1998         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1999                tgt_name(tsi->tsi_tgt), lock_count, req);
2000
2001         RETURN(lock_count > 0);
2002 }
2003
2004 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
2005 {
2006         ofd_punch_hpreq_check(req);
2007 }
2008
2009 struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
2010         .hpreq_lock_match       = ofd_rw_hpreq_lock_match,
2011         .hpreq_check            = ofd_rw_hpreq_check,
2012         .hpreq_fini             = ofd_rw_hpreq_fini
2013 };
2014
2015 struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
2016         .hpreq_lock_match       = ofd_punch_hpreq_lock_match,
2017         .hpreq_check            = ofd_punch_hpreq_check,
2018         .hpreq_fini             = ofd_punch_hpreq_fini
2019 };
2020
2021 /** Assign high priority operations to the IO requests */
2022 static void ofd_hp_brw(struct tgt_session_info *tsi)
2023 {
2024         struct niobuf_remote    *rnb;
2025         struct obd_ioobj        *ioo;
2026
2027         ENTRY;
2028
2029         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
2030         LASSERT(ioo != NULL); /* must exist after request preprocessing */
2031         if (ioo->ioo_bufcnt > 0) {
2032                 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
2033                 LASSERT(rnb != NULL); /* must exist after request preprocessing */
2034
2035                 /* no high priority if server lock is needed */
2036                 if (rnb->rnb_flags & OBD_BRW_SRVLOCK)
2037                         return;
2038         }
2039         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
2040 }
2041
2042 static void ofd_hp_punch(struct tgt_session_info *tsi)
2043 {
2044         LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2045         /* no high-priority if server lock is needed */
2046         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2047             tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK)
2048                 return;
2049         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2050 }
2051
2052 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
2053 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
2054 #define OST_BRW_READ    OST_READ
2055 #define OST_BRW_WRITE   OST_WRITE
2056
2057 static struct tgt_handler ofd_tgt_handlers[] = {
2058 TGT_RPC_HANDLER(OST_FIRST_OPC,
2059                 0,                      OST_CONNECT,    tgt_connect,
2060                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2061 TGT_RPC_HANDLER(OST_FIRST_OPC,
2062                 0,                      OST_DISCONNECT, tgt_disconnect,
2063                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2064 TGT_RPC_HANDLER(OST_FIRST_OPC,
2065                 0,                      OST_SET_INFO,   ofd_set_info_hdl,
2066                 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2067 TGT_OST_HDL(0,                          OST_GET_INFO,   ofd_get_info_hdl),
2068 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR,    ofd_getattr_hdl),
2069 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2070                                         OST_SETATTR,    ofd_setattr_hdl),
2071 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2072                                         OST_CREATE,     ofd_create_hdl),
2073 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2074                                         OST_DESTROY,    ofd_destroy_hdl),
2075 TGT_OST_HDL(0           | HABEO_REFERO, OST_STATFS,     ofd_statfs_hdl),
2076 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2077                                         OST_BRW_READ,   tgt_brw_read,
2078                                                         ofd_hp_brw),
2079 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2080 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR,   OST_BRW_WRITE,  tgt_brw_write,
2081                                                         ofd_hp_brw),
2082 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2083                                         OST_PUNCH,      ofd_punch_hdl,
2084                                                         ofd_hp_punch),
2085 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC,       ofd_sync_hdl),
2086 TGT_OST_HDL(0           | HABEO_REFERO, OST_QUOTACTL,   ofd_quotactl),
2087 };
2088
2089 static struct tgt_opc_slice ofd_common_slice[] = {
2090         {
2091                 .tos_opc_start  = OST_FIRST_OPC,
2092                 .tos_opc_end    = OST_LAST_OPC,
2093                 .tos_hs         = ofd_tgt_handlers
2094         },
2095         {
2096                 .tos_opc_start  = OBD_FIRST_OPC,
2097                 .tos_opc_end    = OBD_LAST_OPC,
2098                 .tos_hs         = tgt_obd_handlers
2099         },
2100         {
2101                 .tos_opc_start  = LDLM_FIRST_OPC,
2102                 .tos_opc_end    = LDLM_LAST_OPC,
2103                 .tos_hs         = tgt_dlm_handlers
2104         },
2105         {
2106                 .tos_opc_start  = OUT_UPDATE_FIRST_OPC,
2107                 .tos_opc_end    = OUT_UPDATE_LAST_OPC,
2108                 .tos_hs         = tgt_out_handlers
2109         },
2110         {
2111                 .tos_opc_start  = SEQ_FIRST_OPC,
2112                 .tos_opc_end    = SEQ_LAST_OPC,
2113                 .tos_hs         = seq_handlers
2114         },
2115         {
2116                 .tos_opc_start  = LFSCK_FIRST_OPC,
2117                 .tos_opc_end    = LFSCK_LAST_OPC,
2118                 .tos_hs         = tgt_lfsck_handlers
2119         },
2120         {
2121                 .tos_hs         = NULL
2122         }
2123 };
2124
2125 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2126                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
2127 {
2128         const char              *dev = lustre_cfg_string(cfg, 0);
2129         struct ofd_thread_info  *info = NULL;
2130         struct obd_device       *obd;
2131         struct obd_statfs       *osfs;
2132         int                      rc;
2133
2134         ENTRY;
2135
2136         obd = class_name2obd(dev);
2137         if (obd == NULL) {
2138                 CERROR("Cannot find obd with name %s\n", dev);
2139                 RETURN(-ENODEV);
2140         }
2141
2142         rc = lu_env_refill((struct lu_env *)env);
2143         if (rc != 0)
2144                 RETURN(rc);
2145
2146         obd->u.obt.obt_magic = OBT_MAGIC;
2147
2148         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2149         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2150
2151         spin_lock_init(&m->ofd_flags_lock);
2152         m->ofd_raid_degraded = 0;
2153         m->ofd_syncjournal = 0;
2154         ofd_slc_set(m);
2155         m->ofd_grant_compat_disable = 0;
2156         m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2157
2158         /* statfs data */
2159         spin_lock_init(&m->ofd_osfs_lock);
2160         m->ofd_osfs_age = cfs_time_shift_64(-1000);
2161         m->ofd_osfs_unstable = 0;
2162         m->ofd_statfs_inflight = 0;
2163         m->ofd_osfs_inflight = 0;
2164
2165         /* grant data */
2166         spin_lock_init(&m->ofd_grant_lock);
2167         m->ofd_tot_dirty = 0;
2168         m->ofd_tot_granted = 0;
2169         m->ofd_tot_pending = 0;
2170         m->ofd_seq_count = 0;
2171         init_waitqueue_head(&m->ofd_inconsistency_thread.t_ctl_waitq);
2172         INIT_LIST_HEAD(&m->ofd_inconsistency_list);
2173         spin_lock_init(&m->ofd_inconsistency_lock);
2174
2175         spin_lock_init(&m->ofd_batch_lock);
2176         init_rwsem(&m->ofd_lastid_rwsem);
2177
2178         obd->u.filter.fo_fl_oss_capa = 0;
2179         INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
2180         obd->u.filter.fo_capa_hash = init_capa_hash();
2181         if (obd->u.filter.fo_capa_hash == NULL)
2182                 RETURN(-ENOMEM);
2183
2184         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2185         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2186         /* set this lu_device to obd, because error handling need it */
2187         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2188
2189         rc = ofd_procfs_init(m);
2190         if (rc) {
2191                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2192                 RETURN(rc);
2193         }
2194
2195         /* No connection accepted until configurations will finish */
2196         spin_lock(&obd->obd_dev_lock);
2197         obd->obd_no_conn = 1;
2198         spin_unlock(&obd->obd_dev_lock);
2199         obd->obd_replayable = 1;
2200         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2201                 char *str = lustre_cfg_string(cfg, 4);
2202
2203                 if (strchr(str, 'n')) {
2204                         CWARN("%s: recovery disabled\n", obd->obd_name);
2205                         obd->obd_replayable = 0;
2206                 }
2207         }
2208
2209         info = ofd_info_init(env, NULL);
2210         if (info == NULL)
2211                 RETURN(-EFAULT);
2212
2213         rc = ofd_stack_init(env, m, cfg);
2214         if (rc) {
2215                 CERROR("Can't init device stack, rc %d\n", rc);
2216                 GOTO(err_fini_proc, rc);
2217         }
2218
2219         ofd_procfs_add_brw_stats_symlink(m);
2220
2221         /* populate cached statfs data */
2222         osfs = &ofd_info(env)->fti_u.osfs;
2223         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
2224         if (rc != 0) {
2225                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
2226                 GOTO(err_fini_stack, rc);
2227         }
2228         if (!IS_PO2(osfs->os_bsize)) {
2229                 CERROR("%s: blocksize (%d) is not a power of 2\n",
2230                                 obd->obd_name, osfs->os_bsize);
2231                 GOTO(err_fini_stack, rc = -EPROTO);
2232         }
2233         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
2234
2235         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
2236         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
2237                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
2238
2239         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
2240                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
2241         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
2242                                               LDLM_NAMESPACE_SERVER,
2243                                               LDLM_NAMESPACE_GREEDY,
2244                                               LDLM_NS_TYPE_OST);
2245         if (m->ofd_namespace == NULL)
2246                 GOTO(err_fini_stack, rc = -ENOMEM);
2247         /* set obd_namespace for compatibility with old code */
2248         obd->obd_namespace = m->ofd_namespace;
2249         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
2250         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
2251         m->ofd_namespace->ns_lvbp = m;
2252
2253         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2254                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
2255
2256         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
2257
2258         /* Allow at most ddp_grant_reserved% of the available filesystem space
2259          * to be granted to clients, so that any errors in the grant overhead
2260          * calculations do not allow granting more space to clients than can be
2261          * written. Assumes that in aggregate the grant overhead calculations do
2262          * not have more than ddp_grant_reserved% estimation error in them. */
2263         m->ofd_grant_ratio =
2264                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
2265
2266         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
2267                       OBD_FAIL_OST_ALL_REQUEST_NET,
2268                       OBD_FAIL_OST_ALL_REPLY_NET);
2269         if (rc)
2270                 GOTO(err_free_ns, rc);
2271
2272         rc = ofd_fs_setup(env, m, obd);
2273         if (rc)
2274                 GOTO(err_fini_lut, rc);
2275
2276         rc = ofd_start_inconsistency_verification_thread(m);
2277         if (rc != 0)
2278                 GOTO(err_fini_fs, rc);
2279
2280         RETURN(0);
2281
2282 err_fini_fs:
2283         ofd_fs_cleanup(env, m);
2284 err_fini_lut:
2285         tgt_fini(env, &m->ofd_lut);
2286 err_free_ns:
2287         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
2288         obd->obd_namespace = m->ofd_namespace = NULL;
2289 err_fini_stack:
2290         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
2291 err_fini_proc:
2292         ofd_procfs_fini(m);
2293         return rc;
2294 }
2295
2296 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
2297 {
2298         struct obd_device       *obd = ofd_obd(m);
2299         struct lu_device        *d   = &m->ofd_dt_dev.dd_lu_dev;
2300         struct lfsck_stop        stop;
2301
2302         stop.ls_status = LS_PAUSED;
2303         stop.ls_flags = 0;
2304         lfsck_stop(env, m->ofd_osd, &stop);
2305         target_recovery_fini(obd);
2306         obd_exports_barrier(obd);
2307         obd_zombie_barrier();
2308
2309         tgt_fini(env, &m->ofd_lut);
2310         ofd_stop_inconsistency_verification_thread(m);
2311         lfsck_degister(env, m->ofd_osd);
2312         ofd_fs_cleanup(env, m);
2313
2314         ofd_free_capa_keys(m);
2315         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
2316
2317         if (m->ofd_namespace != NULL) {
2318                 ldlm_namespace_free(m->ofd_namespace, NULL,
2319                                     d->ld_obd->obd_force);
2320                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
2321         }
2322
2323         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
2324         ofd_procfs_fini(m);
2325         LASSERT(atomic_read(&d->ld_ref) == 0);
2326         server_put_mount(obd->obd_name, true);
2327         EXIT;
2328 }
2329
2330 static struct lu_device *ofd_device_fini(const struct lu_env *env,
2331                                          struct lu_device *d)
2332 {
2333         ENTRY;
2334         ofd_fini(env, ofd_dev(d));
2335         RETURN(NULL);
2336 }
2337
2338 static struct lu_device *ofd_device_free(const struct lu_env *env,
2339                                          struct lu_device *d)
2340 {
2341         struct ofd_device *m = ofd_dev(d);
2342
2343         dt_device_fini(&m->ofd_dt_dev);
2344         OBD_FREE_PTR(m);
2345         RETURN(NULL);
2346 }
2347
2348 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
2349                                           struct lu_device_type *t,
2350                                           struct lustre_cfg *cfg)
2351 {
2352         struct ofd_device *m;
2353         struct lu_device  *l;
2354         int                rc;
2355
2356         OBD_ALLOC_PTR(m);
2357         if (m == NULL)
2358                 return ERR_PTR(-ENOMEM);
2359
2360         l = &m->ofd_dt_dev.dd_lu_dev;
2361         dt_device_init(&m->ofd_dt_dev, t);
2362         rc = ofd_init0(env, m, t, cfg);
2363         if (rc != 0) {
2364                 ofd_device_free(env, l);
2365                 l = ERR_PTR(rc);
2366         }
2367
2368         return l;
2369 }
2370
2371 /* thread context key constructor/destructor */
2372 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2373
2374 static void ofd_key_exit(const struct lu_context *ctx,
2375                          struct lu_context_key *key, void *data)
2376 {
2377         struct ofd_thread_info *info = data;
2378
2379         info->fti_env = NULL;
2380         info->fti_exp = NULL;
2381
2382         info->fti_xid = 0;
2383         info->fti_pre_version = 0;
2384         info->fti_used = 0;
2385
2386         memset(&info->fti_attr, 0, sizeof info->fti_attr);
2387 }
2388
2389 struct lu_context_key ofd_thread_key = {
2390         .lct_tags = LCT_DT_THREAD,
2391         .lct_init = ofd_key_init,
2392         .lct_fini = ofd_key_fini,
2393         .lct_exit = ofd_key_exit
2394 };
2395
2396 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
2397 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
2398
2399 static struct lu_device_type_operations ofd_device_type_ops = {
2400         .ldto_init              = ofd_type_init,
2401         .ldto_fini              = ofd_type_fini,
2402
2403         .ldto_start             = ofd_type_start,
2404         .ldto_stop              = ofd_type_stop,
2405
2406         .ldto_device_alloc      = ofd_device_alloc,
2407         .ldto_device_free       = ofd_device_free,
2408         .ldto_device_fini       = ofd_device_fini
2409 };
2410
2411 static struct lu_device_type ofd_device_type = {
2412         .ldt_tags       = LU_DEVICE_DT,
2413         .ldt_name       = LUSTRE_OST_NAME,
2414         .ldt_ops        = &ofd_device_type_ops,
2415         .ldt_ctx_tags   = LCT_DT_THREAD
2416 };
2417
2418 int __init ofd_init(void)
2419 {
2420         int                             rc;
2421
2422         rc = lu_kmem_init(ofd_caches);
2423         if (rc)
2424                 return rc;
2425
2426         rc = ofd_fmd_init();
2427         if (rc) {
2428                 lu_kmem_fini(ofd_caches);
2429                 return(rc);
2430         }
2431
2432         rc = class_register_type(&ofd_obd_ops, NULL, true, NULL,
2433 #ifndef HAVE_ONLY_PROCFS_SEQ
2434                                  NULL,
2435 #endif
2436                                  LUSTRE_OST_NAME, &ofd_device_type);
2437         return rc;
2438 }
2439
2440 void __exit ofd_exit(void)
2441 {
2442         ofd_fmd_exit();
2443         lu_kmem_fini(ofd_caches);
2444         class_unregister_type(LUSTRE_OST_NAME);
2445 }
2446
2447 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
2448 MODULE_DESCRIPTION("Lustre Object Filtering Device");
2449 MODULE_LICENSE("GPL");
2450
2451 module_init(ofd_init);
2452 module_exit(ofd_exit);