Whamcloud - gitweb
82f5492132da8024bce68e3cfc1b8b6a89f63bfd
[fs/lustre-release.git] / lustre / ofd / ofd_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ofd/ofd_obd.c
37  *
38  * Author: Andreas Dilger <adilger@whamcloud.com>
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include "ofd_internal.h"
46 #include <obd_cksum.h>
47 #include <lustre_quota.h>
48
49 static int ofd_export_stats_init(struct ofd_device *ofd,
50                                  struct obd_export *exp, void *client_nid)
51 {
52         struct obd_device       *obd = ofd_obd(ofd);
53         struct nid_stat         *stats;
54         int                      num_stats, i;
55         int                      rc, newnid = 0;
56
57         ENTRY;
58
59         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
60                 /* Self-export gets no proc entry */
61                 RETURN(0);
62
63         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
64         if (rc) {
65                 /* Mask error for already created
66                  * /proc entries */
67                 if (rc == -EALREADY)
68                         rc = 0;
69                 RETURN(rc);
70         }
71
72         if (newnid == 0)
73                 RETURN(0);
74
75         stats = exp->exp_nid_stats;
76         LASSERT(stats != NULL);
77
78         OBD_ALLOC(stats->nid_brw_stats, sizeof(struct brw_stats));
79         if (stats->nid_brw_stats == NULL)
80                 GOTO(clean, rc = -ENOMEM);
81
82         for (i = 0; i < BRW_LAST; i++)
83                 spin_lock_init(&stats->nid_brw_stats->hist[i].oh_lock);
84
85         rc = lprocfs_seq_create(stats->nid_proc, "brw_stats", 0644,
86                                 &ofd_per_nid_stats_fops, stats);
87         if (rc)
88                 CWARN("Error adding the brw_stats file\n");
89
90         num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
91                      LPROC_OFD_LAST - 1;
92
93         stats->nid_stats = lprocfs_alloc_stats(num_stats,
94                                                LPROCFS_STATS_FLAG_NOPERCPU);
95         if (stats->nid_stats == NULL)
96                 return -ENOMEM;
97
98         lprocfs_init_ops_stats(LPROC_OFD_LAST, stats->nid_stats);
99         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_READ_BYTES,
100                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
101         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_WRITE_BYTES,
102                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
103
104         rc = lprocfs_register_stats(stats->nid_proc, "stats",
105                                     stats->nid_stats);
106         if (rc)
107                 GOTO(clean, rc);
108
109         rc = lprocfs_nid_ldlm_stats_init(stats);
110         if (rc) {
111                 lprocfs_free_stats(&stats->nid_stats);
112                 GOTO(clean, rc);
113         }
114
115         RETURN(0);
116 clean:
117         return rc;
118 }
119
120 static int ofd_parse_connect_data(const struct lu_env *env,
121                                   struct obd_export *exp,
122                                   struct obd_connect_data *data)
123 {
124         struct ofd_device                *ofd = ofd_exp(exp);
125         struct filter_export_data        *fed = &exp->exp_filter_data;
126
127         if (!data)
128                 RETURN(0);
129
130         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
131                " ocd_version: %x ocd_grant: %d ocd_index: %u\n",
132                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
133                data->ocd_connect_flags, data->ocd_version,
134                data->ocd_grant, data->ocd_index);
135
136         if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
137                 CWARN("!!! This export (nid %s) used object group %d "
138                       "earlier; now it's trying to use group %d!  This could "
139                       "be a bug in the MDS. Please report to "
140                       "http://bugs.whamcloud.com/\n",
141                       obd_export_nid2str(exp), fed->fed_group,
142                       data->ocd_group);
143                 RETURN(-EPROTO);
144         }
145         fed->fed_group = data->ocd_group;
146
147         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
148         exp->exp_connect_flags = data->ocd_connect_flags;
149         data->ocd_version = LUSTRE_VERSION_CODE;
150
151         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
152         if (data->ocd_connect_flags & OBD_CONNECT_MDS)
153                 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
154                        exp->exp_obd->obd_name, data->ocd_group);
155         else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
156                 RETURN(-EPROTO);
157
158         if (ofd_grant_param_supp(exp)) {
159                 exp->exp_filter_data.fed_pagesize = data->ocd_blocksize;
160                 /* ocd_{blocksize,inodespace} are log2 values */
161                 data->ocd_blocksize  = ofd->ofd_blockbits;
162                 data->ocd_inodespace = ofd->ofd_dt_conf.ddp_inodespace;
163                 /* ocd_grant_extent is in 1K blocks */
164                 data->ocd_grant_extent = ofd->ofd_dt_conf.ddp_grant_frag >> 10;
165         }
166
167         if (exp->exp_connect_flags & OBD_CONNECT_GRANT)
168                 data->ocd_grant = ofd_grant_connect(env, exp, data->ocd_grant);
169
170         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
171                 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
172                 int                    index = lsd->lsd_ost_index;
173
174                 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
175                         /* this will only happen on the first connect */
176                         lsd->lsd_ost_index = data->ocd_index;
177                         lsd->lsd_feature_compat |= OBD_COMPAT_OST;
178                         /* sync is not needed here as lut_client_add will
179                          * set exp_need_sync flag */
180                         tgt_server_data_update(env, &ofd->ofd_lut, 0);
181                 } else if (index != data->ocd_index) {
182                         LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index"
183                                            " %u doesn't match actual OST index"
184                                            " %u in last_rcvd file, bad "
185                                            "configuration?\n",
186                                            obd_export_nid2str(exp), index,
187                                            data->ocd_index);
188                         RETURN(-EBADF);
189                 }
190         }
191
192         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
193                 data->ocd_brw_size = 65536;
194         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
195                 data->ocd_brw_size = min(data->ocd_brw_size,
196                               (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
197                 if (data->ocd_brw_size == 0) {
198                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
199                                " ocd_version: %x ocd_grant: %d ocd_index: %u "
200                                "ocd_brw_size is unexpectedly zero, "
201                                "network data corruption?"
202                                "Refusing connection of this client\n",
203                                exp->exp_obd->obd_name,
204                                exp->exp_client_uuid.uuid,
205                                exp, data->ocd_connect_flags, data->ocd_version,
206                                data->ocd_grant, data->ocd_index);
207                         RETURN(-EPROTO);
208                 }
209         }
210
211         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
212                 __u32 cksum_types = data->ocd_cksum_types;
213
214                 /* The client set in ocd_cksum_types the checksum types it
215                  * supports. We have to mask off the algorithms that we don't
216                  * support */
217                 data->ocd_cksum_types &= cksum_types_supported_server();
218
219                 if (unlikely(data->ocd_cksum_types == 0)) {
220                         CERROR("%s: Connect with checksum support but no "
221                                "ocd_cksum_types is set\n",
222                                exp->exp_obd->obd_name);
223                         RETURN(-EPROTO);
224                 }
225
226                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
227                        "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
228                        cksum_types, data->ocd_cksum_types);
229         } else {
230                 /* This client does not support OBD_CONNECT_CKSUM
231                  * fall back to CRC32 */
232                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
233                        "OBD_CONNECT_CKSUM, CRC32 will be used\n",
234                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
235         }
236
237         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
238                 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
239
240         RETURN(0);
241 }
242
243 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
244                              struct obd_device *obd, struct obd_uuid *cluuid,
245                              struct obd_connect_data *data, void *localdata)
246 {
247         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
248         int                      rc;
249
250         ENTRY;
251
252         if (exp == NULL || obd == NULL || cluuid == NULL)
253                 RETURN(-EINVAL);
254
255         rc = lu_env_refill((struct lu_env *)env);
256         if (rc != 0) {
257                 CERROR("Failure to refill session: '%d'\n", rc);
258                 RETURN(rc);
259         }
260
261         ofd_info_init(env, exp);
262         rc = ofd_parse_connect_data(env, exp, data);
263         if (rc == 0)
264                 ofd_export_stats_init(ofd, exp, localdata);
265
266         RETURN(rc);
267 }
268
269 static int ofd_obd_connect(const struct lu_env *env, struct obd_export **_exp,
270                            struct obd_device *obd, struct obd_uuid *cluuid,
271                            struct obd_connect_data *data, void *localdata)
272 {
273         struct obd_export       *exp;
274         struct ofd_device       *ofd;
275         struct lustre_handle     conn = { 0 };
276         int                      rc;
277         ENTRY;
278
279         if (_exp == NULL || obd == NULL || cluuid == NULL)
280                 RETURN(-EINVAL);
281
282         ofd = ofd_dev(obd->obd_lu_dev);
283
284         rc = class_connect(&conn, obd, cluuid);
285         if (rc)
286                 RETURN(rc);
287
288         exp = class_conn2export(&conn);
289         LASSERT(exp != NULL);
290
291         rc = lu_env_refill((struct lu_env *)env);
292         if (rc != 0) {
293                 CERROR("Failure to refill session: '%d'\n", rc);
294                 GOTO(out, rc);
295         }
296
297         ofd_info_init(env, exp);
298
299         rc = ofd_parse_connect_data(env, exp, data);
300         if (rc)
301                 GOTO(out, rc);
302
303         if (obd->obd_replayable) {
304                 struct tg_export_data *ted = &exp->exp_target_data;
305
306                 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
307                        sizeof(ted->ted_lcd->lcd_uuid));
308                 rc = tgt_client_new(env, exp);
309                 if (rc != 0)
310                         GOTO(out, rc);
311                 ofd_export_stats_init(ofd, exp, localdata);
312         }
313
314         CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
315                data->ocd_group);
316
317 out:
318         if (rc != 0) {
319                 class_disconnect(exp);
320                 *_exp = NULL;
321         } else {
322                 *_exp = exp;
323         }
324         RETURN(rc);
325 }
326
327 static int ofd_obd_disconnect(struct obd_export *exp)
328 {
329         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
330         struct lu_env            env;
331         int                      rc;
332
333         ENTRY;
334
335         LASSERT(exp);
336         class_export_get(exp);
337
338         if (!(exp->exp_flags & OBD_OPT_FORCE))
339                 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
340
341         rc = server_disconnect_export(exp);
342
343         ofd_grant_discard(exp);
344
345         rc = lu_env_init(&env, LCT_DT_THREAD);
346         if (rc)
347                 RETURN(rc);
348
349         /* Do not erase record for recoverable client. */
350         if (exp->exp_obd->obd_replayable &&
351             (!exp->exp_obd->obd_fail || exp->exp_failed))
352                 tgt_client_del(&env, exp);
353         lu_env_fini(&env);
354
355         class_export_put(exp);
356         RETURN(rc);
357 }
358
359 static int ofd_init_export(struct obd_export *exp)
360 {
361         int rc;
362
363         spin_lock_init(&exp->exp_filter_data.fed_lock);
364         CFS_INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
365         spin_lock(&exp->exp_lock);
366         exp->exp_connecting = 1;
367         spin_unlock(&exp->exp_lock);
368
369         /* self-export doesn't need client data and ldlm initialization */
370         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
371                                      &exp->exp_client_uuid)))
372                 return 0;
373
374         rc = tgt_client_alloc(exp);
375         if (rc == 0)
376                 ldlm_init_export(exp);
377         if (rc)
378                 CERROR("%s: Can't initialize export: rc %d\n",
379                        exp->exp_obd->obd_name, rc);
380         return rc;
381 }
382
383 static int ofd_destroy_export(struct obd_export *exp)
384 {
385         struct ofd_device *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
386
387         if (exp->exp_filter_data.fed_pending)
388                 CERROR("%s: cli %s/%p has %lu pending on destroyed export"
389                        "\n", exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
390                        exp, exp->exp_filter_data.fed_pending);
391
392         target_destroy_export(exp);
393
394         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
395                                      &exp->exp_client_uuid)))
396                 return 0;
397
398         ldlm_destroy_export(exp);
399         tgt_client_free(exp);
400
401         ofd_fmd_cleanup(exp);
402
403         /*
404          * discard grants once we're sure no more
405          * interaction with the client is possible
406          */
407         ofd_grant_discard(exp);
408         ofd_fmd_cleanup(exp);
409
410         if (exp->exp_connect_flags & OBD_CONNECT_GRANT_SHRINK) {
411                 if (ofd->ofd_tot_granted_clients > 0)
412                         ofd->ofd_tot_granted_clients --;
413         }
414
415         if (!(exp->exp_flags & OBD_OPT_FORCE))
416                 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
417
418         LASSERT(cfs_list_empty(&exp->exp_filter_data.fed_mod_list));
419         return 0;
420 }
421
422 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
423 {
424         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
425
426         CDEBUG(D_HA, "%s: recovery is over\n", ofd_obd(ofd)->obd_name);
427         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
428 }
429
430 int ofd_obd_postrecov(struct obd_device *obd)
431 {
432         struct lu_env            env;
433         struct lu_device        *ldev = obd->obd_lu_dev;
434         int                      rc;
435
436         ENTRY;
437
438         rc = lu_env_init(&env, LCT_DT_THREAD);
439         if (rc)
440                 RETURN(rc);
441         ofd_info_init(&env, obd->obd_self_export);
442
443         rc = ofd_postrecov(&env, ofd_dev(ldev));
444
445         lu_env_fini(&env);
446         RETURN(rc);
447 }
448
449 static int ofd_adapt_sptlrpc_conf(const struct lu_env *env,
450                                   struct obd_device *obd, int initial)
451 {
452         struct filter_obd       *fo = &obd->u.filter;
453         struct sptlrpc_rule_set  tmp_rset;
454         int                      rc;
455
456         sptlrpc_rule_set_init(&tmp_rset);
457         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
458         if (rc) {
459                 CERROR("%s: failed get sptlrpc rules: rc = %d\n",
460                        obd->obd_name, rc);
461                 return rc;
462         }
463
464         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
465
466         write_lock(&fo->fo_sptlrpc_lock);
467         sptlrpc_rule_set_free(&fo->fo_sptlrpc_rset);
468         fo->fo_sptlrpc_rset = tmp_rset;
469         write_unlock(&fo->fo_sptlrpc_lock);
470
471         return 0;
472 }
473
474 static int ofd_set_mds_conn(struct obd_export *exp, void *val)
475 {
476         int rc = 0;
477
478         ENTRY;
479
480         LCONSOLE_WARN("%s: received MDS connection from %s\n",
481                       exp->exp_obd->obd_name, obd_export_nid2str(exp));
482         RETURN(rc);
483 }
484
485 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
486                               __u32 keylen, void *key, __u32 vallen, void *val,
487                               struct ptlrpc_request_set *set)
488 {
489         struct ofd_device       *ofd = ofd_exp(exp);
490         int                      rc = 0;
491
492         ENTRY;
493
494         if (exp->exp_obd == NULL) {
495                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
496                 RETURN(-EINVAL);
497         }
498
499         if (KEY_IS(KEY_CAPA_KEY)) {
500                 rc = ofd_update_capa_key(ofd, val);
501                 if (rc)
502                         CERROR("%s: update capability key failed: rc = %d\n",
503                                exp->exp_obd->obd_name, rc);
504         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
505                 ofd_adapt_sptlrpc_conf(env, exp->exp_obd, 0);
506         } else if (KEY_IS(KEY_MDS_CONN)) {
507                 rc = ofd_set_mds_conn(exp, val);
508         } else if (KEY_IS(KEY_GRANT_SHRINK)) {
509                 struct ost_body *body = val;
510
511                 ofd_info_init(env, exp);
512                 /** handle grant shrink, similar to a read request */
513                 ofd_grant_prepare_read(env, exp, &body->oa);
514         } else {
515                 CERROR("%s: Unsupported key %s\n",
516                        exp->exp_obd->obd_name, (char*)key);
517                 rc = -EOPNOTSUPP;
518         }
519
520         RETURN(rc);
521 }
522
523 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
524                         __u32 keylen, void *key, __u32 *vallen, void *val,
525                         struct lov_stripe_md *lsm)
526 {
527         struct ofd_device       *ofd = ofd_exp(exp);
528         int                      rc = 0;
529
530         ENTRY;
531
532         if (exp->exp_obd == NULL) {
533                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
534                 RETURN(-EINVAL);
535         }
536
537         if (KEY_IS(KEY_BLOCKSIZE)) {
538                 __u32 *blocksize = val;
539                 if (blocksize) {
540                         if (*vallen < sizeof(*blocksize))
541                                 RETURN(-EOVERFLOW);
542                         *blocksize = 1 << ofd->ofd_dt_conf.ddp_block_shift;
543                 }
544                 *vallen = sizeof(*blocksize);
545         } else if (KEY_IS(KEY_BLOCKSIZE_BITS)) {
546                 __u32 *blocksize_bits = val;
547                 if (blocksize_bits) {
548                         if (*vallen < sizeof(*blocksize_bits))
549                                 RETURN(-EOVERFLOW);
550                         *blocksize_bits = ofd->ofd_dt_conf.ddp_block_shift;
551                 }
552                 *vallen = sizeof(*blocksize_bits);
553         } else if (KEY_IS(KEY_LAST_ID)) {
554                 obd_id *last_id = val;
555                 struct ofd_seq *oseq;
556
557                 if (val == NULL) {
558                         *vallen = sizeof(obd_id);
559                         RETURN(0);
560                 }
561                 ofd_info_init(env, exp);
562                 oseq = ofd_seq_load(env, ofd,
563                                     (obd_seq)exp->exp_filter_data.fed_group);
564                 LASSERT(oseq != NULL);
565                 if (last_id) {
566                         if (*vallen < sizeof(*last_id)) {
567                                 ofd_seq_put(env, oseq);
568                                 RETURN(-EOVERFLOW);
569                         }
570                         *last_id = ofd_seq_last_oid(oseq);
571                 }
572                 ofd_seq_put(env, oseq);
573                 *vallen = sizeof(*last_id);
574         } else if (KEY_IS(KEY_FIEMAP)) {
575                 struct ofd_thread_info          *info;
576                 struct ofd_device               *ofd = ofd_exp(exp);
577                 struct ofd_object               *fo;
578                 struct ll_fiemap_info_key       *fm_key = key;
579
580                 if (val == NULL) {
581                         *vallen = fiemap_count_to_size(
582                                                fm_key->fiemap.fm_extent_count);
583                         RETURN(0);
584                 }
585
586                 info = ofd_info_init(env, exp);
587
588                 fid_ostid_unpack(&info->fti_fid, &fm_key->oa.o_oi, 0);
589
590                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n",
591                        PFID(&info->fti_fid));
592
593                 fo = ofd_object_find(env, ofd, &info->fti_fid);
594                 if (IS_ERR(fo)) {
595                         CERROR("%s: error finding object "DFID"\n",
596                                exp->exp_obd->obd_name, PFID(&info->fti_fid));
597                         rc = PTR_ERR(fo);
598                 } else {
599                         struct ll_user_fiemap *fiemap = val;
600
601                         ofd_read_lock(env, fo);
602                         if (ofd_object_exists(fo)) {
603                                 *fiemap = fm_key->fiemap;
604                                 rc = dt_fiemap_get(env,
605                                                    ofd_object_child(fo),
606                                                    fiemap);
607                         } else {
608                                 rc = -ENOENT;
609                         }
610                         ofd_read_unlock(env, fo);
611                         ofd_object_put(env, fo);
612                 }
613         } else if (KEY_IS(KEY_SYNC_LOCK_CANCEL)) {
614                 *((__u32 *) val) = ofd->ofd_sync_lock_cancel;
615                 *vallen = sizeof(__u32);
616         } else {
617                 CERROR("Not supported key %s\n", (char*)key);
618                 rc = -EOPNOTSUPP;
619         }
620
621         RETURN(rc);
622 }
623
624 /** helper function for statfs, also used by grant code */
625 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
626                         struct obd_statfs *osfs, __u64 max_age, int *from_cache)
627 {
628         int rc;
629
630         spin_lock(&ofd->ofd_osfs_lock);
631         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
632                 obd_size unstable;
633
634                 /* statfs data are too old, get up-to-date one.
635                  * we must be cautious here since multiple threads might be
636                  * willing to update statfs data concurrently and we must
637                  * grant that cached statfs data are always consistent */
638
639                 if (ofd->ofd_statfs_inflight == 0)
640                         /* clear inflight counter if no users, although it would
641                          * take a while to overflow this 64-bit counter ... */
642                         ofd->ofd_osfs_inflight = 0;
643                 /* notify ofd_grant_commit() that we want to track writes
644                  * completed as of now */
645                 ofd->ofd_statfs_inflight++;
646                 /* record value of inflight counter before running statfs to
647                  * compute the diff once statfs is completed */
648                 unstable = ofd->ofd_osfs_inflight;
649                 spin_unlock(&ofd->ofd_osfs_lock);
650
651                 /* statfs can sleep ... hopefully not for too long since we can
652                  * call it fairly often as space fills up */
653                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
654                 if (unlikely(rc))
655                         return rc;
656
657                 spin_lock(&ofd->ofd_grant_lock);
658                 spin_lock(&ofd->ofd_osfs_lock);
659                 /* calculate how much space was written while we released the
660                  * ofd_osfs_lock */
661                 unstable = ofd->ofd_osfs_inflight - unstable;
662                 ofd->ofd_osfs_unstable = 0;
663                 if (unstable) {
664                         /* some writes completed while we were running statfs
665                          * w/o the ofd_osfs_lock. Those ones got added to
666                          * the cached statfs data that we are about to crunch.
667                          * Take them into account in the new statfs data */
668                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
669                                                unstable >> ofd->ofd_blockbits);
670                         /* However, we don't really know if those writes got
671                          * accounted in the statfs call, so tell
672                          * ofd_grant_space_left() there is some uncertainty
673                          * on the accounting of those writes.
674                          * The purpose is to prevent spurious error messages in
675                          * ofd_grant_space_left() since those writes might be
676                          * accounted twice. */
677                         ofd->ofd_osfs_unstable += unstable;
678                 }
679                 /* similarly, there is some uncertainty on write requests
680                  * between prepare & commit */
681                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
682                 spin_unlock(&ofd->ofd_grant_lock);
683
684                 /* finally udpate cached statfs data */
685                 ofd->ofd_osfs = *osfs;
686                 ofd->ofd_osfs_age = cfs_time_current_64();
687
688                 ofd->ofd_statfs_inflight--; /* stop tracking */
689                 if (ofd->ofd_statfs_inflight == 0)
690                         ofd->ofd_osfs_inflight = 0;
691                 spin_unlock(&ofd->ofd_osfs_lock);
692
693                 if (from_cache)
694                         *from_cache = 0;
695         } else {
696                 /* use cached statfs data */
697                 *osfs = ofd->ofd_osfs;
698                 spin_unlock(&ofd->ofd_osfs_lock);
699                 if (from_cache)
700                         *from_cache = 1;
701         }
702         return 0;
703 }
704
705 static int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
706                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
707 {
708         struct obd_device       *obd = class_exp2obd(exp);
709         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
710         int                      rc;
711
712         ENTRY;
713
714         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
715         if (unlikely(rc))
716                 GOTO(out, rc);
717
718         /* at least try to account for cached pages.  its still racy and
719          * might be under-reporting if clients haven't announced their
720          * caches with brw recently */
721
722         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
723                " pending "LPU64" free "LPU64" avail "LPU64"\n",
724                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
725                osfs->os_bfree << ofd->ofd_blockbits,
726                osfs->os_bavail << ofd->ofd_blockbits);
727
728         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
729                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
730                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
731
732         /* The QoS code on the MDS does not care about space reserved for
733          * precreate, so take it out. */
734         if (exp->exp_connect_flags & OBD_CONNECT_MDS) {
735                 struct filter_export_data *fed;
736
737                 fed = &obd->obd_self_export->exp_filter_data;
738                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
739                                          fed->fed_grant >> ofd->ofd_blockbits);
740         }
741
742         ofd_grant_sanity_check(obd, __FUNCTION__);
743         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
744                LPU64" objects: "LPU64" free; state %x\n",
745                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
746                osfs->os_files, osfs->os_ffree, osfs->os_state);
747
748         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
749                                  ofd->ofd_lut.lut_lsd.lsd_ost_index))
750                 osfs->os_ffree = 0;
751
752         /* OS_STATE_READONLY can be set by OSD already */
753         if (ofd->ofd_raid_degraded)
754                 osfs->os_state |= OS_STATE_DEGRADED;
755
756         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
757                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
758                  * should not see a block size > page size, otherwise
759                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
760                  * block size which is the biggest block size known to work
761                  * with all client's page size. */
762                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
763                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
764                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
765                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
766         }
767
768         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
769                                  ofd->ofd_lut.lut_lsd.lsd_ost_index))
770                 osfs->os_bfree = osfs->os_bavail = 2;
771
772         EXIT;
773 out:
774         return rc;
775 }
776
777 int ofd_setattr(const struct lu_env *env, struct obd_export *exp,
778                 struct obd_info *oinfo, struct obd_trans_info *oti)
779 {
780         struct ofd_thread_info  *info;
781         struct ofd_device       *ofd = ofd_exp(exp);
782         struct ldlm_namespace   *ns = ofd->ofd_namespace;
783         struct ldlm_resource    *res;
784         struct ofd_object       *fo;
785         struct obdo             *oa = oinfo->oi_oa;
786         struct filter_fid       *ff = NULL;
787         int                      rc = 0;
788
789         ENTRY;
790
791         info = ofd_info_init(env, exp);
792         ofd_oti2info(info, oti);
793
794         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
795         ofd_build_resid(&info->fti_fid, &info->fti_resid);
796
797         rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
798                            oinfo_capa(oinfo), CAPA_OPC_META_WRITE);
799         if (rc)
800                 GOTO(out, rc);
801
802         /* This would be very bad - accidentally truncating a file when
803          * changing the time or similar - bug 12203. */
804         if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE &&
805             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
806                 static char mdsinum[48];
807
808                 if (oinfo->oi_oa->o_valid & OBD_MD_FLFID)
809                         snprintf(mdsinum, sizeof(mdsinum) - 1,
810                                  "of parent "DFID, oinfo->oi_oa->o_parent_seq,
811                                  oinfo->oi_oa->o_parent_oid, 0);
812                 else
813                         mdsinum[0] = '\0';
814
815                 CERROR("%s: setattr from %s trying to truncate object "DFID
816                        " %s\n", exp->exp_obd->obd_name,
817                        obd_export_nid2str(exp), PFID(&info->fti_fid), mdsinum);
818                 GOTO(out, rc = -EPERM);
819         }
820
821         fo = ofd_object_find(env, ofd, &info->fti_fid);
822         if (IS_ERR(fo)) {
823                 CERROR("%s: can't find object "DFID"\n",
824                        exp->exp_obd->obd_name, PFID(&info->fti_fid));
825                 GOTO(out, rc = PTR_ERR(fo));
826         }
827
828         la_from_obdo(&info->fti_attr, oinfo->oi_oa, oinfo->oi_oa->o_valid);
829         info->fti_attr.la_valid &= ~LA_TYPE;
830
831         if (oa->o_valid & OBD_MD_FLFID) {
832                 ff = &info->fti_mds_fid;
833                 ofd_prepare_fidea(ff, oa);
834         }
835
836         /* setting objects attributes (including owner/group) */
837         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
838         if (rc)
839                 GOTO(out_unlock, rc);
840
841         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
842         if (res != NULL) {
843                 ldlm_res_lvbo_update(res, NULL, 0);
844                 ldlm_resource_putref(res);
845         }
846
847         oinfo->oi_oa->o_valid = OBD_MD_FLID;
848
849         /* Quota release needs uid/gid info */
850         rc = ofd_attr_get(env, fo, &info->fti_attr);
851         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
852                      OFD_VALID_FLAGS | LA_UID | LA_GID);
853         ofd_info2oti(info, oti);
854
855         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, oti->oti_jobid, 1);
856         EXIT;
857 out_unlock:
858         ofd_object_put(env, fo);
859 out:
860         return rc;
861 }
862
863 static int ofd_punch(const struct lu_env *env, struct obd_export *exp,
864                      struct obd_info *oinfo, struct obd_trans_info *oti,
865                      struct ptlrpc_request_set *rqset)
866 {
867         struct ofd_thread_info  *info;
868         struct ofd_device       *ofd = ofd_exp(exp);
869         struct ldlm_namespace   *ns = ofd->ofd_namespace;
870         struct ldlm_resource    *res;
871         struct ofd_object       *fo;
872         struct filter_fid       *ff = NULL;
873         int                      rc = 0;
874
875         ENTRY;
876
877         info = ofd_info_init(env, exp);
878         ofd_oti2info(info, oti);
879
880         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
881         ofd_build_resid(&info->fti_fid, &info->fti_resid);
882
883         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
884                ", start = "LPD64", end = "LPD64"\n", PFID(&info->fti_fid),
885                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start,
886                oinfo->oi_policy.l_extent.end);
887
888         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
889                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
890         if (rc)
891                 GOTO(out_env, rc);
892
893         fo = ofd_object_find(env, ofd, &info->fti_fid);
894         if (IS_ERR(fo)) {
895                 CERROR("%s: error finding object "DFID": rc = %ld\n",
896                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
897                        PTR_ERR(fo));
898                 GOTO(out_env, rc = PTR_ERR(fo));
899         }
900
901         LASSERT(oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF);
902         if (oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF) {
903                 /* Truncate case */
904                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
905         } else if (oinfo->oi_policy.l_extent.end >= oinfo->oi_oa->o_size) {
906                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.end;
907         }
908
909         la_from_obdo(&info->fti_attr, oinfo->oi_oa,
910                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
911         info->fti_attr.la_valid &= ~LA_TYPE;
912         info->fti_attr.la_size = oinfo->oi_policy.l_extent.start;
913         info->fti_attr.la_valid |= LA_SIZE;
914
915         if (oinfo->oi_oa->o_valid & OBD_MD_FLFID) {
916                 ff = &info->fti_mds_fid;
917                 ofd_prepare_fidea(ff, oinfo->oi_oa);
918         }
919
920         rc = ofd_object_punch(env, fo, oinfo->oi_policy.l_extent.start,
921                               oinfo->oi_policy.l_extent.end, &info->fti_attr,
922                               ff);
923         if (rc)
924                 GOTO(out, rc);
925
926         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
927         if (res != NULL) {
928                 ldlm_res_lvbo_update(res, NULL, 0);
929                 ldlm_resource_putref(res);
930         }
931
932         oinfo->oi_oa->o_valid = OBD_MD_FLID;
933         /* Quota release needs uid/gid info */
934         rc = ofd_attr_get(env, fo, &info->fti_attr);
935         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
936                      OFD_VALID_FLAGS | LA_UID | LA_GID);
937         ofd_info2oti(info, oti);
938
939         ofd_counter_incr(exp, LPROC_OFD_STATS_PUNCH, oti->oti_jobid, 1);
940         EXIT;
941 out:
942         ofd_object_put(env, fo);
943 out_env:
944         return rc;
945 }
946
947 static int ofd_destroy_by_fid(const struct lu_env *env,
948                               struct ofd_device *ofd,
949                               const struct lu_fid *fid, int orphan)
950 {
951         struct ofd_thread_info  *info = ofd_info(env);
952         struct lustre_handle     lockh;
953         __u64                    flags = LDLM_AST_DISCARD_DATA, rc = 0;
954         ldlm_policy_data_t       policy = {
955                                         .l_extent = { 0, OBD_OBJECT_EOF }
956                                  };
957         struct ofd_object       *fo;
958
959         ENTRY;
960
961         fo = ofd_object_find(env, ofd, fid);
962         if (IS_ERR(fo))
963                 RETURN(PTR_ERR(fo));
964
965         /* Tell the clients that the object is gone now and that they should
966          * throw away any cached pages. */
967         ofd_build_resid(fid, &info->fti_resid);
968         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
969                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
970                                     ldlm_blocking_ast, ldlm_completion_ast,
971                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
972
973         /* We only care about the side-effects, just drop the lock. */
974         if (rc == ELDLM_OK)
975                 ldlm_lock_decref(&lockh, LCK_PW);
976
977         LASSERT(fo != NULL);
978
979         rc = ofd_object_destroy(env, fo, orphan);
980
981         ofd_object_put(env, fo);
982         RETURN(rc);
983 }
984
985 int ofd_destroy(const struct lu_env *env, struct obd_export *exp,
986                 struct obdo *oa, struct lov_stripe_md *md,
987                 struct obd_trans_info *oti, struct obd_export *md_exp,
988                 void *capa)
989 {
990         struct ofd_device       *ofd = ofd_exp(exp);
991         struct ofd_thread_info  *info;
992         obd_count                count;
993         int                      rc = 0;
994
995         ENTRY;
996
997         info = ofd_info_init(env, exp);
998         ofd_oti2info(info, oti);
999
1000         if (!(oa->o_valid & OBD_MD_FLGROUP))
1001                 oa->o_seq = 0;
1002
1003         /* check that o_misc makes sense */
1004         if (oa->o_valid & OBD_MD_FLOBJCOUNT)
1005                 count = oa->o_misc;
1006         else
1007                 count = 1; /* default case - single destroy */
1008
1009         /**
1010          * There can be sequence of objects to destroy. Therefore this request
1011          * may have multiple transaction involved in. It is OK, we need only
1012          * the highest used transno to be reported back in reply but not for
1013          * replays, they must report their transno
1014          */
1015         if (info->fti_transno == 0) /* not replay */
1016                 info->fti_mult_trans = 1;
1017         while (count > 0) {
1018                 int lrc;
1019
1020                 fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
1021                 lrc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 0);
1022                 if (lrc == -ENOENT) {
1023                         CDEBUG(D_INODE,
1024                                "%s: destroying non-existent object "DFID"\n",
1025                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid));
1026                         /* rewrite rc with -ENOENT only if it is 0 */
1027                         if (rc == 0)
1028                                 rc = lrc;
1029                 } else if (lrc != 0) {
1030                         CERROR("%s: error destroying object "DFID": %d\n",
1031                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid),
1032                                rc);
1033                         rc = lrc;
1034                 }
1035                 count--;
1036                 oa->o_id++;
1037         }
1038
1039         /* if we have transaction then there were some deletions, we don't
1040          * need to return ENOENT in that case because it will not wait
1041          * for commit of these deletions. The ENOENT must be returned only
1042          * if there were no transations.
1043          */
1044         if (rc == -ENOENT) {
1045                 if (info->fti_transno != 0)
1046                         rc = 0;
1047         } else if (rc != 0) {
1048                 /*
1049                  * If we have at least one transaction then llog record
1050                  * on server will be removed upon commit, so for rc != 0
1051                  * we return no transno and llog record will be reprocessed.
1052                  */
1053                 info->fti_transno = 0;
1054         }
1055         ofd_info2oti(info, oti);
1056         RETURN(rc);
1057 }
1058
1059 static int ofd_orphans_destroy(const struct lu_env *env,
1060                                struct obd_export *exp, struct ofd_device *ofd,
1061                                struct obdo *oa)
1062 {
1063         struct ofd_thread_info  *info = ofd_info(env);
1064         obd_id                   last;
1065         int                      skip_orphan;
1066         int                      rc = 0;
1067         struct ost_id            oi = oa->o_oi;
1068         struct ofd_seq          *oseq;
1069
1070         ENTRY;
1071
1072         oseq = ofd_seq_get(ofd, oa->o_seq);
1073         if (oseq == NULL) {
1074                 CERROR("%s: Can not find seq for "LPU64":"LPU64"\n",
1075                        ofd_name(ofd), oa->o_seq, oa->o_id);
1076                 RETURN(-EINVAL);
1077         }
1078
1079         LASSERT(exp != NULL);
1080         skip_orphan = !!(exp->exp_connect_flags & OBD_CONNECT_SKIP_ORPHAN);
1081
1082         last = ofd_seq_last_oid(oseq);
1083         LCONSOLE_INFO("%s: deleting orphan objects from "LPX64":"LPU64
1084                       " to "LPU64"\n", ofd_name(ofd), oa->o_seq,
1085                       oa->o_id + 1, last);
1086
1087         for (oi.oi_id = last; oi.oi_id > oa->o_id; oi.oi_id--) {
1088                 fid_ostid_unpack(&info->fti_fid, &oi, 0);
1089                 rc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 1);
1090                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
1091                         CEMERG("error destroying precreated id "LPU64": %d\n",
1092                                oi.oi_id, rc);
1093                 if (!skip_orphan) {
1094                         ofd_seq_last_oid_set(oseq, oi.oi_id - 1);
1095                         /* update last_id on disk periodically so that if we
1096                          * restart * we don't need to re-scan all of the just
1097                          * deleted objects. */
1098                         if ((oi.oi_id & 511) == 0)
1099                                 ofd_seq_last_oid_write(env, ofd, oseq);
1100                 }
1101         }
1102         CDEBUG(D_HA, "%s: after destroy: set last_objids["LPU64"] = "LPU64"\n",
1103                ofd_obd(ofd)->obd_name, oa->o_seq, oa->o_id);
1104         if (!skip_orphan) {
1105                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1106         } else {
1107                 /* don't reuse orphan object, return last used objid */
1108                 oa->o_id = last;
1109                 rc = 0;
1110         }
1111         ofd_seq_put(env, oseq);
1112         RETURN(rc);
1113 }
1114
1115 int ofd_create(const struct lu_env *env, struct obd_export *exp,
1116                struct obdo *oa, struct lov_stripe_md **ea,
1117                struct obd_trans_info *oti)
1118 {
1119         struct ofd_device       *ofd = ofd_exp(exp);
1120         struct ofd_thread_info  *info;
1121         obd_seq                 seq = oa->o_seq;
1122         struct ofd_seq          *oseq;
1123         int                      rc = 0, diff;
1124
1125         ENTRY;
1126
1127         info = ofd_info_init(env, exp);
1128         ofd_oti2info(info, oti);
1129
1130         LASSERT(oa->o_seq >= FID_SEQ_OST_MDT0);
1131         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1132
1133         CDEBUG(D_INFO, "ofd_create(oa->o_seq="LPU64",oa->o_id="LPU64")\n",
1134                seq, oa->o_id);
1135
1136         oseq = ofd_seq_load(env, ofd, seq);
1137         if (oseq == NULL) {
1138                 CERROR("%s: Can't find oseq "LPX64"\n", ofd_name(ofd), seq);
1139                 RETURN(-EINVAL);
1140         }
1141
1142         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1143             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1144                 if (!ofd_obd(ofd)->obd_recovering ||
1145                     oa->o_id > ofd_seq_last_oid(oseq)) {
1146                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
1147                                         oa->o_id, ofd_seq_last_oid(oseq));
1148                         GOTO(out_nolock, rc = -EINVAL);
1149                 }
1150                 /* do nothing because we create objects during first write */
1151                 GOTO(out_nolock, rc = 0);
1152         }
1153         /* former ofd_handle_precreate */
1154         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1155             (oa->o_flags & OBD_FL_DELORPHAN)) {
1156                 /* destroy orphans */
1157                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1158                         CERROR("%s: dropping old orphan cleanup request\n",
1159                                ofd_name(ofd));
1160                         GOTO(out_nolock, rc = 0);
1161                 }
1162                 /* This causes inflight precreates to abort and drop lock */
1163                 oseq->os_destroys_in_progress = 1;
1164                 mutex_lock(&oseq->os_create_lock);
1165                 if (!oseq->os_destroys_in_progress) {
1166                         CERROR("%s:["LPU64"] destroys_in_progress already cleared\n",
1167                                exp->exp_obd->obd_name, oa->o_seq);
1168                         GOTO(out, rc = 0);
1169                 }
1170                 diff = oa->o_id - ofd_seq_last_oid(oseq);
1171                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1172                         ofd_seq_last_oid(oseq), diff);
1173                 if (-diff > OST_MAX_PRECREATE) {
1174                         /* FIXME: should reset precreate_next_id on MDS */
1175                         rc = 0;
1176                 } else if (diff < 0) {
1177                         rc = ofd_orphans_destroy(env, exp, ofd, oa);
1178                         oseq->os_destroys_in_progress = 0;
1179                 } else {
1180                         /* XXX: Used by MDS for the first time! */
1181                         oseq->os_destroys_in_progress = 0;
1182                 }
1183         } else {
1184                 mutex_lock(&oseq->os_create_lock);
1185                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1186                         CERROR("%s: dropping old precreate request\n",
1187                                 ofd_obd(ofd)->obd_name);
1188                         GOTO(out, rc = 0);
1189                 }
1190                 /* only precreate if seq == 0 and o_id is specfied */
1191                 if (!fid_seq_is_mdt(oa->o_seq) || oa->o_id == 0) {
1192                         diff = 1; /* shouldn't we create this right now? */
1193                 } else {
1194                         diff = oa->o_id - ofd_seq_last_oid(oseq);
1195                 }
1196         }
1197         if (diff > 0) {
1198                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1199                 obd_id           next_id;
1200                 int              created = 0;
1201                 int              count;
1202
1203                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1204                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1205                         /* don't enforce grant during orphan recovery */
1206                         rc = ofd_grant_create(env,
1207                                               ofd_obd(ofd)->obd_self_export,
1208                                               &diff);
1209                         if (rc) {
1210                                 CDEBUG(D_HA, "%s: failed to acquire grant space"
1211                                        "for precreate (%d)\n",
1212                                        ofd_name(ofd), diff);
1213                                 diff = 0;
1214                         }
1215                 }
1216
1217                 while (diff > 0) {
1218                         next_id = ofd_seq_last_oid(oseq) + 1;
1219                         count = ofd_precreate_batch(ofd, diff);
1220
1221                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPU64
1222                                " at "LPU64"\n", ofd_obd(ofd)->obd_name,
1223                                count, oa->o_seq, next_id);
1224
1225                         if (cfs_time_after(jiffies, enough_time)) {
1226                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1227                                               " created at a rate of %d/s\n",
1228                                               ofd_obd(ofd)->obd_name,
1229                                               created, diff + created,
1230                                               created / DISK_TIMEOUT);
1231                                 break;
1232                         }
1233
1234                         rc = ofd_precreate_objects(env, ofd, next_id,
1235                                                    oseq, count);
1236                         if (rc > 0) {
1237                                 created += rc;
1238                                 diff -= rc;
1239                         } else if (rc < 0) {
1240                                 break;
1241                         }
1242                 }
1243                 if (created > 0)
1244                         /* some objects got created, we can return
1245                          * them, even if last creation failed */
1246                         rc = 0;
1247                 else
1248                         CERROR("unable to precreate: %d\n", rc);
1249
1250                 oa->o_id = ofd_seq_last_oid(oseq);
1251                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1252
1253                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1254                     !(oa->o_flags & OBD_FL_DELORPHAN))
1255                         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export,
1256                                          rc);
1257         }
1258
1259         ofd_info2oti(info, oti);
1260 out:
1261         mutex_unlock(&oseq->os_create_lock);
1262 out_nolock:
1263         if (rc == 0 && ea != NULL) {
1264                 struct lov_stripe_md *lsm = *ea;
1265
1266                 lsm->lsm_object_id = oa->o_id;
1267         }
1268         ofd_seq_put(env, oseq);
1269         RETURN(rc);
1270 }
1271
1272 int ofd_getattr(const struct lu_env *env, struct obd_export *exp,
1273                 struct obd_info *oinfo)
1274 {
1275         struct ofd_device       *ofd = ofd_exp(exp);
1276         struct ofd_thread_info  *info;
1277         struct ofd_object       *fo;
1278         __u64                    curr_version;
1279         int                      rc = 0;
1280
1281         ENTRY;
1282
1283         info = ofd_info_init(env, exp);
1284
1285         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1286         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1287                            oinfo_capa(oinfo), CAPA_OPC_META_READ);
1288         if (rc)
1289                 GOTO(out, rc);
1290
1291         fo = ofd_object_find(env, ofd, &info->fti_fid);
1292         if (IS_ERR(fo))
1293                 GOTO(out, rc = PTR_ERR(fo));
1294         LASSERT(fo != NULL);
1295         rc = ofd_attr_get(env, fo, &info->fti_attr);
1296         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1297         if (rc == 0)
1298                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
1299                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1300
1301         /* Store object version in reply */
1302         curr_version = dt_version_get(env, ofd_object_child(fo));
1303         if ((__s64)curr_version != -EOPNOTSUPP) {
1304                 oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
1305                 oinfo->oi_oa->o_data_version = curr_version;
1306         }
1307         ofd_object_put(env, fo);
1308 out:
1309         RETURN(rc);
1310 }
1311
1312 static int ofd_sync(const struct lu_env *env, struct obd_export *exp,
1313                     struct obd_info *oinfo, obd_size start, obd_size end,
1314                     struct ptlrpc_request_set *set)
1315 {
1316         struct ofd_device       *ofd = ofd_exp(exp);
1317         struct ofd_thread_info  *info;
1318         struct ofd_object       *fo;
1319         int                      rc = 0;
1320
1321         ENTRY;
1322
1323         /* if no objid is specified, it means "sync whole filesystem" */
1324         if (oinfo->oi_oa == NULL || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
1325                 rc = dt_sync(env, ofd->ofd_osd);
1326                 GOTO(out, rc);
1327         }
1328
1329         info = ofd_info_init(env, exp);
1330         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1331
1332         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1333                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
1334         if (rc)
1335                 GOTO(out, rc);
1336
1337         fo = ofd_object_find(env, ofd, &info->fti_fid);
1338         if (IS_ERR(fo)) {
1339                 CERROR("%s: error finding object "DFID": rc = %ld\n",
1340                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
1341                        PTR_ERR(fo));
1342                 GOTO(out, rc = PTR_ERR(fo));
1343         }
1344
1345         if (!ofd_object_exists(fo))
1346                 GOTO(put, rc = -ENOENT);
1347
1348         if (dt_version_get(env, ofd_object_child(fo)) >
1349             ofd_obd(ofd)->obd_last_committed) {
1350                 rc = dt_object_sync(env, ofd_object_child(fo));
1351                 if (rc)
1352                         GOTO(put, rc);
1353         }
1354
1355         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1356         rc = ofd_attr_get(env, fo, &info->fti_attr);
1357         obdo_from_la(oinfo->oi_oa, &info->fti_attr, OFD_VALID_FLAGS);
1358
1359         ofd_counter_incr(exp, LPROC_OFD_STATS_SYNC, oinfo->oi_jobid, 1);
1360         EXIT;
1361 put:
1362         ofd_object_put(env, fo);
1363 out:
1364         return rc;
1365 }
1366
1367 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1368                   void *karg, void *uarg)
1369 {
1370         struct lu_env            env;
1371         struct ofd_device       *ofd = ofd_exp(exp);
1372         struct obd_device       *obd = ofd_obd(ofd);
1373         int                      rc;
1374
1375         ENTRY;
1376
1377         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1378         rc = lu_env_init(&env, LCT_LOCAL);
1379         if (rc)
1380                 RETURN(rc);
1381
1382         switch (cmd) {
1383         case OBD_IOC_ABORT_RECOVERY:
1384                 CERROR("%s: aborting recovery\n", obd->obd_name);
1385                 target_stop_recovery_thread(obd);
1386                 break;
1387         case OBD_IOC_SYNC:
1388                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1389                 rc = dt_sync(&env, ofd->ofd_osd);
1390                 break;
1391         case OBD_IOC_SET_READONLY:
1392                 rc = dt_sync(&env, ofd->ofd_osd);
1393                 if (rc == 0)
1394                         rc = dt_ro(&env, ofd->ofd_osd);
1395                 break;
1396         default:
1397                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1398                 rc = -ENOTTY;
1399         }
1400
1401         lu_env_fini(&env);
1402         RETURN(rc);
1403 }
1404
1405 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1406 {
1407         int rc = 0;
1408
1409         ENTRY;
1410
1411         switch(stage) {
1412         case OBD_CLEANUP_EARLY:
1413                 break;
1414         case OBD_CLEANUP_EXPORTS:
1415                 target_cleanup_recovery(obd);
1416                 break;
1417         }
1418         RETURN(rc);
1419 }
1420
1421 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1422 {
1423         ofd_fmd_expire(exp);
1424         return 0;
1425 }
1426
1427 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1428 {
1429         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1430         struct ofd_thread_info  *info;
1431         struct lu_env            env;
1432 #ifdef USE_HEALTH_CHECK_WRITE
1433         struct thandle          *th;
1434 #endif
1435         int                      rc = 0;
1436
1437         /* obd_proc_read_health pass NULL env, we need real one */
1438         rc = lu_env_init(&env, LCT_DT_THREAD);
1439         if (rc)
1440                 RETURN(rc);
1441
1442         info = ofd_info_init(&env, NULL);
1443         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1444         if (unlikely(rc))
1445                 GOTO(out, rc);
1446
1447         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1448                 GOTO(out, rc = -EROFS);
1449
1450 #ifdef USE_HEALTH_CHECK_WRITE
1451         OBD_ALLOC(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1452         if (info->fti_buf.lb_buf == NULL)
1453                 GOTO(out, rc = -ENOMEM);
1454
1455         info->fti_buf.lb_len = CFS_PAGE_SIZE;
1456         info->fti_off = 0;
1457
1458         th = dt_trans_create(&env, ofd->ofd_osd);
1459         if (IS_ERR(th))
1460                 GOTO(out, rc = PTR_ERR(th));
1461
1462         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1463                                      info->fti_buf.lb_len, info->fti_off, th);
1464         if (rc == 0) {
1465                 th->th_sync = 1; /* sync IO is needed */
1466                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1467                 if (rc == 0)
1468                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1469                                              &info->fti_buf, &info->fti_off,
1470                                              th);
1471         }
1472         dt_trans_stop(&env, ofd->ofd_osd, th);
1473
1474         OBD_FREE(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1475
1476         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1477 #endif
1478 out:
1479         lu_env_fini(&env);
1480         return !!rc;
1481 }
1482
1483 /*
1484  * Handle quota control requests to consult current usage/limit.
1485  *
1486  * \param obd - is the obd device associated with the ofd
1487  * \param exp - is the client's export
1488  * \param oqctl - is the obd_quotactl request to be processed
1489  */
1490 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1491                         struct obd_quotactl *oqctl)
1492 {
1493         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1494         struct lu_env       env;
1495         int                 rc;
1496         ENTRY;
1497
1498         /* report success for quota on/off for interoperability with current MDT
1499          * stack */
1500         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1501                 RETURN(0);
1502
1503         rc = lu_env_init(&env, LCT_DT_THREAD);
1504         if (rc)
1505                 RETURN(rc);
1506
1507         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1508         lu_env_fini(&env);
1509
1510         RETURN(rc);
1511 }
1512
1513 struct obd_ops ofd_obd_ops = {
1514         .o_owner                = THIS_MODULE,
1515         .o_connect              = ofd_obd_connect,
1516         .o_reconnect            = ofd_obd_reconnect,
1517         .o_disconnect           = ofd_obd_disconnect,
1518         .o_set_info_async       = ofd_set_info_async,
1519         .o_get_info             = ofd_get_info,
1520         .o_create               = ofd_create,
1521         .o_statfs               = ofd_statfs,
1522         .o_setattr              = ofd_setattr,
1523         .o_preprw               = ofd_preprw,
1524         .o_commitrw             = ofd_commitrw,
1525         .o_destroy              = ofd_destroy,
1526         .o_init_export          = ofd_init_export,
1527         .o_destroy_export       = ofd_destroy_export,
1528         .o_postrecov            = ofd_obd_postrecov,
1529         .o_punch                = ofd_punch,
1530         .o_getattr              = ofd_getattr,
1531         .o_sync                 = ofd_sync,
1532         .o_iocontrol            = ofd_iocontrol,
1533         .o_precleanup           = ofd_precleanup,
1534         .o_ping                 = ofd_ping,
1535         .o_health_check         = ofd_health_check,
1536         .o_quotactl             = ofd_quotactl,
1537 };