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