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