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