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