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