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