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