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