Whamcloud - gitweb
LU-2955 tests: make replay-ost-single/8b SLOW for ZFS
[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;
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         num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
79                      LPROC_OFD_LAST - 1;
80
81         stats->nid_stats = lprocfs_alloc_stats(num_stats,
82                                                LPROCFS_STATS_FLAG_NOPERCPU);
83         if (stats->nid_stats == NULL)
84                 return -ENOMEM;
85
86         lprocfs_init_ops_stats(LPROC_OFD_LAST, stats->nid_stats);
87         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_READ_BYTES,
88                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
89         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_WRITE_BYTES,
90                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
91
92         rc = lprocfs_register_stats(stats->nid_proc, "stats",
93                                     stats->nid_stats);
94         if (rc)
95                 GOTO(clean, rc);
96
97         rc = lprocfs_nid_ldlm_stats_init(stats);
98         if (rc) {
99                 lprocfs_free_stats(&stats->nid_stats);
100                 GOTO(clean, rc);
101         }
102
103         RETURN(0);
104 clean:
105         return rc;
106 }
107
108 static int ofd_parse_connect_data(const struct lu_env *env,
109                                   struct obd_export *exp,
110                                   struct obd_connect_data *data)
111 {
112         struct ofd_device                *ofd = ofd_exp(exp);
113         struct filter_export_data        *fed = &exp->exp_filter_data;
114
115         if (!data)
116                 RETURN(0);
117
118         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
119                " ocd_version: %x ocd_grant: %d ocd_index: %u"
120                " ocd_group %u\n",
121                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
122                data->ocd_connect_flags, data->ocd_version,
123                data->ocd_grant, data->ocd_index, data->ocd_group);
124
125         if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
126                 CWARN("!!! This export (nid %s) used object group %d "
127                       "earlier; now it's trying to use group %d!  This could "
128                       "be a bug in the MDS. Please report to "
129                       "http://bugs.whamcloud.com/\n",
130                       obd_export_nid2str(exp), fed->fed_group,
131                       data->ocd_group);
132                 RETURN(-EPROTO);
133         }
134         fed->fed_group = data->ocd_group;
135
136         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
137         exp->exp_connect_data = *data;
138         data->ocd_version = LUSTRE_VERSION_CODE;
139
140         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
141         if (data->ocd_connect_flags & OBD_CONNECT_MDS)
142                 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
143                        exp->exp_obd->obd_name, data->ocd_group);
144         else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
145                 RETURN(-EPROTO);
146
147         if (ofd_grant_param_supp(exp)) {
148                 exp->exp_filter_data.fed_pagesize = data->ocd_blocksize;
149                 /* ocd_{blocksize,inodespace} are log2 values */
150                 data->ocd_blocksize  = ofd->ofd_blockbits;
151                 data->ocd_inodespace = ofd->ofd_dt_conf.ddp_inodespace;
152                 /* ocd_grant_extent is in 1K blocks */
153                 data->ocd_grant_extent = ofd->ofd_dt_conf.ddp_grant_frag >> 10;
154         }
155
156         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
157                 data->ocd_grant = ofd_grant_connect(env, exp, data->ocd_grant);
158
159         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
160                 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
161                 int                    index = lsd->lsd_osd_index;
162
163                 if (index != data->ocd_index) {
164                         LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index"
165                                            " %u doesn't match actual OST index"
166                                            " %u in last_rcvd file, bad "
167                                            "configuration?\n",
168                                            obd_export_nid2str(exp), index,
169                                            data->ocd_index);
170                         RETURN(-EBADF);
171                 }
172                 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
173                         /* this will only happen on the first connect */
174                         lsd->lsd_feature_compat |= OBD_COMPAT_OST;
175                         /* sync is not needed here as lut_client_add will
176                          * set exp_need_sync flag */
177                         tgt_server_data_update(env, &ofd->ofd_lut, 0);
178                 }
179         }
180         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
181                 data->ocd_brw_size = 65536;
182         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
183                 data->ocd_brw_size = min(data->ocd_brw_size,
184                                          (__u32)DT_MAX_BRW_SIZE);
185                 if (data->ocd_brw_size == 0) {
186                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
187                                " ocd_version: %x ocd_grant: %d ocd_index: %u "
188                                "ocd_brw_size is unexpectedly zero, "
189                                "network data corruption?"
190                                "Refusing connection of this client\n",
191                                exp->exp_obd->obd_name,
192                                exp->exp_client_uuid.uuid,
193                                exp, data->ocd_connect_flags, data->ocd_version,
194                                data->ocd_grant, data->ocd_index);
195                         RETURN(-EPROTO);
196                 }
197         }
198
199         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
200                 __u32 cksum_types = data->ocd_cksum_types;
201
202                 /* The client set in ocd_cksum_types the checksum types it
203                  * supports. We have to mask off the algorithms that we don't
204                  * support */
205                 data->ocd_cksum_types &= cksum_types_supported_server();
206
207                 if (unlikely(data->ocd_cksum_types == 0)) {
208                         CERROR("%s: Connect with checksum support but no "
209                                "ocd_cksum_types is set\n",
210                                exp->exp_obd->obd_name);
211                         RETURN(-EPROTO);
212                 }
213
214                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
215                        "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
216                        cksum_types, data->ocd_cksum_types);
217         } else {
218                 /* This client does not support OBD_CONNECT_CKSUM
219                  * fall back to CRC32 */
220                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
221                        "OBD_CONNECT_CKSUM, CRC32 will be used\n",
222                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
223         }
224
225         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
226                 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
227
228         if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
229                 if (suppress_pings) {
230                         spin_lock(&exp->exp_obd->obd_dev_lock);
231                         list_del_init(&exp->exp_obd_chain_timed);
232                         spin_unlock(&exp->exp_obd->obd_dev_lock);
233                 } else {
234                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
235                 }
236         }
237
238         RETURN(0);
239 }
240
241 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
242                              struct obd_device *obd, struct obd_uuid *cluuid,
243                              struct obd_connect_data *data, void *localdata)
244 {
245         struct ofd_device       *ofd;
246         int                      rc;
247
248         ENTRY;
249
250         if (exp == NULL || obd == NULL || cluuid == NULL)
251                 RETURN(-EINVAL);
252
253         ofd = ofd_dev(obd->obd_lu_dev);
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_connect_flags(exp) & 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;
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         ofd = ofd_exp(exp);
500
501         if (KEY_IS(KEY_CAPA_KEY)) {
502                 rc = ofd_update_capa_key(ofd, val);
503                 if (rc)
504                         CERROR("%s: update capability key failed: rc = %d\n",
505                                exp->exp_obd->obd_name, rc);
506         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
507                 ofd_adapt_sptlrpc_conf(env, exp->exp_obd, 0);
508         } else if (KEY_IS(KEY_MDS_CONN)) {
509                 rc = ofd_set_mds_conn(exp, val);
510         } else if (KEY_IS(KEY_GRANT_SHRINK)) {
511                 struct ost_body *body = val;
512
513                 ofd_info_init(env, exp);
514                 /** handle grant shrink, similar to a read request */
515                 ofd_grant_prepare_read(env, exp, &body->oa);
516         } else {
517                 CERROR("%s: Unsupported key %s\n",
518                        exp->exp_obd->obd_name, (char*)key);
519                 rc = -EOPNOTSUPP;
520         }
521
522         RETURN(rc);
523 }
524
525 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
526                         __u32 keylen, void *key, __u32 *vallen, void *val,
527                         struct lov_stripe_md *lsm)
528 {
529         struct ofd_device       *ofd;
530         int                      rc = 0;
531
532         ENTRY;
533
534         if (exp->exp_obd == NULL) {
535                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
536                 RETURN(-EINVAL);
537         }
538
539         ofd = ofd_exp(exp);
540
541         if (KEY_IS(KEY_BLOCKSIZE)) {
542                 __u32 *blocksize = val;
543                 if (blocksize) {
544                         if (*vallen < sizeof(*blocksize))
545                                 RETURN(-EOVERFLOW);
546                         *blocksize = 1 << ofd->ofd_dt_conf.ddp_block_shift;
547                 }
548                 *vallen = sizeof(*blocksize);
549         } else if (KEY_IS(KEY_BLOCKSIZE_BITS)) {
550                 __u32 *blocksize_bits = val;
551                 if (blocksize_bits) {
552                         if (*vallen < sizeof(*blocksize_bits))
553                                 RETURN(-EOVERFLOW);
554                         *blocksize_bits = ofd->ofd_dt_conf.ddp_block_shift;
555                 }
556                 *vallen = sizeof(*blocksize_bits);
557         } else if (KEY_IS(KEY_LAST_ID)) {
558                 obd_id *last_id = val;
559                 struct ofd_seq *oseq;
560
561                 if (val == NULL) {
562                         *vallen = sizeof(obd_id);
563                         RETURN(0);
564                 }
565                 ofd_info_init(env, exp);
566                 oseq = ofd_seq_load(env, ofd,
567                                     (obd_seq)exp->exp_filter_data.fed_group);
568                 LASSERT(!IS_ERR(oseq));
569                 if (last_id) {
570                         if (*vallen < sizeof(*last_id)) {
571                                 ofd_seq_put(env, oseq);
572                                 RETURN(-EOVERFLOW);
573                         }
574                         *last_id = ofd_seq_last_oid(oseq);
575                 }
576                 ofd_seq_put(env, oseq);
577                 *vallen = sizeof(*last_id);
578         } else if (KEY_IS(KEY_FIEMAP)) {
579                 struct ofd_thread_info          *info;
580                 struct ofd_device               *ofd = ofd_exp(exp);
581                 struct ofd_object               *fo;
582                 struct ll_fiemap_info_key       *fm_key = key;
583
584                 if (val == NULL) {
585                         *vallen = fiemap_count_to_size(
586                                                fm_key->fiemap.fm_extent_count);
587                         RETURN(0);
588                 }
589
590                 info = ofd_info_init(env, exp);
591
592                 fid_ostid_unpack(&info->fti_fid, &fm_key->oa.o_oi, 0);
593
594                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n",
595                        PFID(&info->fti_fid));
596
597                 fo = ofd_object_find(env, ofd, &info->fti_fid);
598                 if (IS_ERR(fo)) {
599                         CERROR("%s: error finding object "DFID"\n",
600                                exp->exp_obd->obd_name, PFID(&info->fti_fid));
601                         rc = PTR_ERR(fo);
602                 } else {
603                         struct ll_user_fiemap *fiemap = val;
604
605                         ofd_read_lock(env, fo);
606                         if (ofd_object_exists(fo)) {
607                                 *fiemap = fm_key->fiemap;
608                                 rc = dt_fiemap_get(env,
609                                                    ofd_object_child(fo),
610                                                    fiemap);
611                         } else {
612                                 rc = -ENOENT;
613                         }
614                         ofd_read_unlock(env, fo);
615                         ofd_object_put(env, fo);
616                 }
617         } else if (KEY_IS(KEY_SYNC_LOCK_CANCEL)) {
618                 *((__u32 *) val) = ofd->ofd_sync_lock_cancel;
619                 *vallen = sizeof(__u32);
620         } else if (KEY_IS(KEY_LAST_FID)) {
621                 struct lu_env           env;
622                 struct ofd_device       *ofd = ofd_exp(exp);
623                 struct ofd_seq          *oseq;
624                 struct ost_id           *oid = val;
625                 int                     rc;
626
627                 if (oid == NULL) {
628                         *vallen = sizeof(struct ost_id);
629                         RETURN(0);
630                 }
631
632                 if (*vallen < sizeof(*oid))
633                         RETURN(-EOVERFLOW);
634
635                 rc = lu_env_init(&env, LCT_DT_THREAD);
636                 if (rc != 0)
637                         RETURN(rc);
638                 ofd_info_init(&env, exp);
639
640                 ostid_le_to_cpu(oid, oid);
641                 CDEBUG(D_HA, "Get LAST FID for seq "LPX64"\n", oid->oi_seq);
642
643                 oseq = ofd_seq_load(&env, ofd, oid->oi_seq);
644                 if (IS_ERR(oseq))
645                         GOTO(out_fini, rc = PTR_ERR(oseq));
646
647                 CDEBUG(D_HA, "LAST FID is "POSTID"\n", oseq->os_last_oid,
648                        oseq->os_seq);
649
650                 *oid = oseq->os_oi;
651                 *vallen = sizeof(*oid);
652                 ofd_seq_put(&env, oseq);
653 out_fini:
654                 lu_env_fini(&env);
655         } else {
656                 CERROR("Not supported key %s\n", (char*)key);
657                 rc = -EOPNOTSUPP;
658         }
659
660         RETURN(rc);
661 }
662
663 /** helper function for statfs, also used by grant code */
664 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
665                         struct obd_statfs *osfs, __u64 max_age, int *from_cache)
666 {
667         int rc;
668
669         spin_lock(&ofd->ofd_osfs_lock);
670         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
671                 obd_size unstable;
672
673                 /* statfs data are too old, get up-to-date one.
674                  * we must be cautious here since multiple threads might be
675                  * willing to update statfs data concurrently and we must
676                  * grant that cached statfs data are always consistent */
677
678                 if (ofd->ofd_statfs_inflight == 0)
679                         /* clear inflight counter if no users, although it would
680                          * take a while to overflow this 64-bit counter ... */
681                         ofd->ofd_osfs_inflight = 0;
682                 /* notify ofd_grant_commit() that we want to track writes
683                  * completed as of now */
684                 ofd->ofd_statfs_inflight++;
685                 /* record value of inflight counter before running statfs to
686                  * compute the diff once statfs is completed */
687                 unstable = ofd->ofd_osfs_inflight;
688                 spin_unlock(&ofd->ofd_osfs_lock);
689
690                 /* statfs can sleep ... hopefully not for too long since we can
691                  * call it fairly often as space fills up */
692                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
693                 if (unlikely(rc))
694                         return rc;
695
696                 spin_lock(&ofd->ofd_grant_lock);
697                 spin_lock(&ofd->ofd_osfs_lock);
698                 /* calculate how much space was written while we released the
699                  * ofd_osfs_lock */
700                 unstable = ofd->ofd_osfs_inflight - unstable;
701                 ofd->ofd_osfs_unstable = 0;
702                 if (unstable) {
703                         /* some writes completed while we were running statfs
704                          * w/o the ofd_osfs_lock. Those ones got added to
705                          * the cached statfs data that we are about to crunch.
706                          * Take them into account in the new statfs data */
707                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
708                                                unstable >> ofd->ofd_blockbits);
709                         /* However, we don't really know if those writes got
710                          * accounted in the statfs call, so tell
711                          * ofd_grant_space_left() there is some uncertainty
712                          * on the accounting of those writes.
713                          * The purpose is to prevent spurious error messages in
714                          * ofd_grant_space_left() since those writes might be
715                          * accounted twice. */
716                         ofd->ofd_osfs_unstable += unstable;
717                 }
718                 /* similarly, there is some uncertainty on write requests
719                  * between prepare & commit */
720                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
721                 spin_unlock(&ofd->ofd_grant_lock);
722
723                 /* finally udpate cached statfs data */
724                 ofd->ofd_osfs = *osfs;
725                 ofd->ofd_osfs_age = cfs_time_current_64();
726
727                 ofd->ofd_statfs_inflight--; /* stop tracking */
728                 if (ofd->ofd_statfs_inflight == 0)
729                         ofd->ofd_osfs_inflight = 0;
730                 spin_unlock(&ofd->ofd_osfs_lock);
731
732                 if (from_cache)
733                         *from_cache = 0;
734         } else {
735                 /* use cached statfs data */
736                 *osfs = ofd->ofd_osfs;
737                 spin_unlock(&ofd->ofd_osfs_lock);
738                 if (from_cache)
739                         *from_cache = 1;
740         }
741         return 0;
742 }
743
744 static int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
745                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
746 {
747         struct obd_device       *obd = class_exp2obd(exp);
748         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
749         int                      rc;
750
751         ENTRY;
752
753         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
754         if (unlikely(rc))
755                 GOTO(out, rc);
756
757         /* at least try to account for cached pages.  its still racy and
758          * might be under-reporting if clients haven't announced their
759          * caches with brw recently */
760
761         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
762                " pending "LPU64" free "LPU64" avail "LPU64"\n",
763                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
764                osfs->os_bfree << ofd->ofd_blockbits,
765                osfs->os_bavail << ofd->ofd_blockbits);
766
767         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
768                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
769                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
770
771         /* The QoS code on the MDS does not care about space reserved for
772          * precreate, so take it out. */
773         if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
774                 struct filter_export_data *fed;
775
776                 fed = &obd->obd_self_export->exp_filter_data;
777                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
778                                          fed->fed_grant >> ofd->ofd_blockbits);
779         }
780
781         ofd_grant_sanity_check(obd, __FUNCTION__);
782         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
783                LPU64" objects: "LPU64" free; state %x\n",
784                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
785                osfs->os_files, osfs->os_ffree, osfs->os_state);
786
787         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
788                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
789                 osfs->os_ffree = 0;
790
791         /* OS_STATE_READONLY can be set by OSD already */
792         if (ofd->ofd_raid_degraded)
793                 osfs->os_state |= OS_STATE_DEGRADED;
794
795         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
796                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
797                  * should not see a block size > page size, otherwise
798                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
799                  * block size which is the biggest block size known to work
800                  * with all client's page size. */
801                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
802                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
803                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
804                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
805         }
806
807         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
808                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
809                 osfs->os_bfree = osfs->os_bavail = 2;
810
811         EXIT;
812 out:
813         return rc;
814 }
815
816 int ofd_setattr(const struct lu_env *env, struct obd_export *exp,
817                 struct obd_info *oinfo, struct obd_trans_info *oti)
818 {
819         struct ofd_thread_info  *info;
820         struct ofd_device       *ofd = ofd_exp(exp);
821         struct ldlm_namespace   *ns = ofd->ofd_namespace;
822         struct ldlm_resource    *res;
823         struct ofd_object       *fo;
824         struct obdo             *oa = oinfo->oi_oa;
825         struct filter_fid       *ff = NULL;
826         int                      rc = 0;
827
828         ENTRY;
829
830         info = ofd_info_init(env, exp);
831         ofd_oti2info(info, oti);
832
833         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
834         ofd_build_resid(&info->fti_fid, &info->fti_resid);
835
836         rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
837                            oinfo_capa(oinfo), CAPA_OPC_META_WRITE);
838         if (rc)
839                 GOTO(out, rc);
840
841         /* This would be very bad - accidentally truncating a file when
842          * changing the time or similar - bug 12203. */
843         if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE &&
844             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
845                 static char mdsinum[48];
846
847                 if (oinfo->oi_oa->o_valid & OBD_MD_FLFID)
848                         snprintf(mdsinum, sizeof(mdsinum) - 1,
849                                  "of parent "DFID, oinfo->oi_oa->o_parent_seq,
850                                  oinfo->oi_oa->o_parent_oid, 0);
851                 else
852                         mdsinum[0] = '\0';
853
854                 CERROR("%s: setattr from %s trying to truncate object "DFID
855                        " %s\n", exp->exp_obd->obd_name,
856                        obd_export_nid2str(exp), PFID(&info->fti_fid), mdsinum);
857                 GOTO(out, rc = -EPERM);
858         }
859
860         fo = ofd_object_find(env, ofd, &info->fti_fid);
861         if (IS_ERR(fo)) {
862                 CERROR("%s: can't find object "DFID"\n",
863                        exp->exp_obd->obd_name, PFID(&info->fti_fid));
864                 GOTO(out, rc = PTR_ERR(fo));
865         }
866
867         la_from_obdo(&info->fti_attr, oinfo->oi_oa, oinfo->oi_oa->o_valid);
868         info->fti_attr.la_valid &= ~LA_TYPE;
869
870         if (oa->o_valid & OBD_MD_FLFID) {
871                 ff = &info->fti_mds_fid;
872                 ofd_prepare_fidea(ff, oa);
873         }
874
875         /* setting objects attributes (including owner/group) */
876         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
877         if (rc)
878                 GOTO(out_unlock, rc);
879
880         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
881         if (res != NULL) {
882                 ldlm_res_lvbo_update(res, NULL, 0);
883                 ldlm_resource_putref(res);
884         }
885
886         oinfo->oi_oa->o_valid = OBD_MD_FLID;
887
888         /* Quota release needs uid/gid info */
889         rc = ofd_attr_get(env, fo, &info->fti_attr);
890         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
891                      OFD_VALID_FLAGS | LA_UID | LA_GID);
892         ofd_info2oti(info, oti);
893
894         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, oti->oti_jobid, 1);
895         EXIT;
896 out_unlock:
897         ofd_object_put(env, fo);
898 out:
899         return rc;
900 }
901
902 static int ofd_punch(const struct lu_env *env, struct obd_export *exp,
903                      struct obd_info *oinfo, struct obd_trans_info *oti,
904                      struct ptlrpc_request_set *rqset)
905 {
906         struct ofd_thread_info  *info;
907         struct ofd_device       *ofd = ofd_exp(exp);
908         struct ldlm_namespace   *ns = ofd->ofd_namespace;
909         struct ldlm_resource    *res;
910         struct ofd_object       *fo;
911         struct filter_fid       *ff = NULL;
912         int                      rc = 0;
913
914         ENTRY;
915
916         info = ofd_info_init(env, exp);
917         ofd_oti2info(info, oti);
918
919         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
920         ofd_build_resid(&info->fti_fid, &info->fti_resid);
921
922         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
923                ", start = "LPD64", end = "LPD64"\n", PFID(&info->fti_fid),
924                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start,
925                oinfo->oi_policy.l_extent.end);
926
927         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
928                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
929         if (rc)
930                 GOTO(out_env, rc);
931
932         fo = ofd_object_find(env, ofd, &info->fti_fid);
933         if (IS_ERR(fo)) {
934                 CERROR("%s: error finding object "DFID": rc = %ld\n",
935                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
936                        PTR_ERR(fo));
937                 GOTO(out_env, rc = PTR_ERR(fo));
938         }
939
940         LASSERT(oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF);
941         if (oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF) {
942                 /* Truncate case */
943                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
944         } else if (oinfo->oi_policy.l_extent.end >= oinfo->oi_oa->o_size) {
945                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.end;
946         }
947
948         la_from_obdo(&info->fti_attr, oinfo->oi_oa,
949                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
950         info->fti_attr.la_valid &= ~LA_TYPE;
951         info->fti_attr.la_size = oinfo->oi_policy.l_extent.start;
952         info->fti_attr.la_valid |= LA_SIZE;
953
954         if (oinfo->oi_oa->o_valid & OBD_MD_FLFID) {
955                 ff = &info->fti_mds_fid;
956                 ofd_prepare_fidea(ff, oinfo->oi_oa);
957         }
958
959         rc = ofd_object_punch(env, fo, oinfo->oi_policy.l_extent.start,
960                               oinfo->oi_policy.l_extent.end, &info->fti_attr,
961                               ff);
962         if (rc)
963                 GOTO(out, rc);
964
965         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
966         if (res != NULL) {
967                 ldlm_res_lvbo_update(res, NULL, 0);
968                 ldlm_resource_putref(res);
969         }
970
971         oinfo->oi_oa->o_valid = OBD_MD_FLID;
972         /* Quota release needs uid/gid info */
973         rc = ofd_attr_get(env, fo, &info->fti_attr);
974         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
975                      OFD_VALID_FLAGS | LA_UID | LA_GID);
976         ofd_info2oti(info, oti);
977
978         ofd_counter_incr(exp, LPROC_OFD_STATS_PUNCH, oti->oti_jobid, 1);
979         EXIT;
980 out:
981         ofd_object_put(env, fo);
982 out_env:
983         return rc;
984 }
985
986 static int ofd_destroy_by_fid(const struct lu_env *env,
987                               struct ofd_device *ofd,
988                               const struct lu_fid *fid, int orphan)
989 {
990         struct ofd_thread_info  *info = ofd_info(env);
991         struct lustre_handle     lockh;
992         __u64                    flags = LDLM_AST_DISCARD_DATA, rc = 0;
993         ldlm_policy_data_t       policy = {
994                                         .l_extent = { 0, OBD_OBJECT_EOF }
995                                  };
996         struct ofd_object       *fo;
997
998         ENTRY;
999
1000         fo = ofd_object_find(env, ofd, fid);
1001         if (IS_ERR(fo))
1002                 RETURN(PTR_ERR(fo));
1003
1004         /* Tell the clients that the object is gone now and that they should
1005          * throw away any cached pages. */
1006         ofd_build_resid(fid, &info->fti_resid);
1007         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
1008                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
1009                                     ldlm_blocking_ast, ldlm_completion_ast,
1010                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
1011
1012         /* We only care about the side-effects, just drop the lock. */
1013         if (rc == ELDLM_OK)
1014                 ldlm_lock_decref(&lockh, LCK_PW);
1015
1016         LASSERT(fo != NULL);
1017
1018         rc = ofd_object_destroy(env, fo, orphan);
1019
1020         ofd_object_put(env, fo);
1021         RETURN(rc);
1022 }
1023
1024 int ofd_destroy(const struct lu_env *env, struct obd_export *exp,
1025                 struct obdo *oa, struct lov_stripe_md *md,
1026                 struct obd_trans_info *oti, struct obd_export *md_exp,
1027                 void *capa)
1028 {
1029         struct ofd_device       *ofd = ofd_exp(exp);
1030         struct ofd_thread_info  *info;
1031         obd_count                count;
1032         int                      rc = 0;
1033
1034         ENTRY;
1035
1036         info = ofd_info_init(env, exp);
1037         ofd_oti2info(info, oti);
1038
1039         if (!(oa->o_valid & OBD_MD_FLGROUP))
1040                 oa->o_seq = 0;
1041
1042         /* check that o_misc makes sense */
1043         if (oa->o_valid & OBD_MD_FLOBJCOUNT)
1044                 count = oa->o_misc;
1045         else
1046                 count = 1; /* default case - single destroy */
1047
1048         /**
1049          * There can be sequence of objects to destroy. Therefore this request
1050          * may have multiple transaction involved in. It is OK, we need only
1051          * the highest used transno to be reported back in reply but not for
1052          * replays, they must report their transno
1053          */
1054         if (info->fti_transno == 0) /* not replay */
1055                 info->fti_mult_trans = 1;
1056         while (count > 0) {
1057                 int lrc;
1058
1059                 fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
1060                 lrc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 0);
1061                 if (lrc == -ENOENT) {
1062                         CDEBUG(D_INODE,
1063                                "%s: destroying non-existent object "DFID"\n",
1064                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid));
1065                         /* rewrite rc with -ENOENT only if it is 0 */
1066                         if (rc == 0)
1067                                 rc = lrc;
1068                 } else if (lrc != 0) {
1069                         CERROR("%s: error destroying object "DFID": %d\n",
1070                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid),
1071                                rc);
1072                         rc = lrc;
1073                 }
1074                 count--;
1075                 oa->o_id++;
1076         }
1077
1078         /* if we have transaction then there were some deletions, we don't
1079          * need to return ENOENT in that case because it will not wait
1080          * for commit of these deletions. The ENOENT must be returned only
1081          * if there were no transations.
1082          */
1083         if (rc == -ENOENT) {
1084                 if (info->fti_transno != 0)
1085                         rc = 0;
1086         } else if (rc != 0) {
1087                 /*
1088                  * If we have at least one transaction then llog record
1089                  * on server will be removed upon commit, so for rc != 0
1090                  * we return no transno and llog record will be reprocessed.
1091                  */
1092                 info->fti_transno = 0;
1093         }
1094         ofd_info2oti(info, oti);
1095         RETURN(rc);
1096 }
1097
1098 static int ofd_orphans_destroy(const struct lu_env *env,
1099                                struct obd_export *exp, struct ofd_device *ofd,
1100                                struct obdo *oa)
1101 {
1102         struct ofd_thread_info  *info = ofd_info(env);
1103         obd_id                   last;
1104         int                      skip_orphan;
1105         int                      rc = 0;
1106         struct ost_id            oi = oa->o_oi;
1107         struct ofd_seq          *oseq;
1108
1109         ENTRY;
1110
1111         oseq = ofd_seq_get(ofd, oa->o_seq);
1112         if (oseq == NULL) {
1113                 CERROR("%s: Can not find seq for "LPU64":"LPU64"\n",
1114                        ofd_name(ofd), oa->o_seq, oa->o_id);
1115                 RETURN(-EINVAL);
1116         }
1117
1118         LASSERT(exp != NULL);
1119         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1120
1121         last = ofd_seq_last_oid(oseq);
1122         LCONSOLE_INFO("%s: deleting orphan objects from "LPX64":"LPU64
1123                       " to "LPU64"\n", ofd_name(ofd), oa->o_seq,
1124                       oa->o_id + 1, last);
1125
1126         for (oi.oi_id = last; oi.oi_id > oa->o_id; oi.oi_id--) {
1127                 fid_ostid_unpack(&info->fti_fid, &oi, 0);
1128                 rc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 1);
1129                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
1130                         CEMERG("error destroying precreated id "LPU64": %d\n",
1131                                oi.oi_id, rc);
1132                 if (!skip_orphan) {
1133                         ofd_seq_last_oid_set(oseq, oi.oi_id - 1);
1134                         /* update last_id on disk periodically so that if we
1135                          * restart * we don't need to re-scan all of the just
1136                          * deleted objects. */
1137                         if ((oi.oi_id & 511) == 0)
1138                                 ofd_seq_last_oid_write(env, ofd, oseq);
1139                 }
1140         }
1141         CDEBUG(D_HA, "%s: after destroy: set last_objids["LPU64"] = "LPU64"\n",
1142                ofd_obd(ofd)->obd_name, oa->o_seq, oa->o_id);
1143         if (!skip_orphan) {
1144                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1145         } else {
1146                 /* don't reuse orphan object, return last used objid */
1147                 oa->o_id = last;
1148                 rc = 0;
1149         }
1150         ofd_seq_put(env, oseq);
1151         RETURN(rc);
1152 }
1153
1154 int ofd_create(const struct lu_env *env, struct obd_export *exp,
1155                struct obdo *oa, struct lov_stripe_md **ea,
1156                struct obd_trans_info *oti)
1157 {
1158         struct ofd_device       *ofd = ofd_exp(exp);
1159         struct ofd_thread_info  *info;
1160         obd_seq                 seq = oa->o_seq;
1161         struct ofd_seq          *oseq;
1162         int                     rc = 0, diff;
1163         int                     sync_trans = 0;
1164
1165         ENTRY;
1166
1167         info = ofd_info_init(env, exp);
1168         ofd_oti2info(info, oti);
1169
1170         LASSERT(oa->o_seq >= FID_SEQ_OST_MDT0);
1171         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1172
1173         CDEBUG(D_INFO, "ofd_create(oa->o_seq="LPU64",oa->o_id="LPU64")\n",
1174                seq, oa->o_id);
1175
1176         oseq = ofd_seq_load(env, ofd, seq);
1177         if (IS_ERR(oseq)) {
1178                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1179                        ofd_name(ofd), seq, PTR_ERR(oseq));
1180                 RETURN(-EINVAL);
1181         }
1182
1183         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1184             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1185                 if (!ofd_obd(ofd)->obd_recovering ||
1186                     oa->o_id > ofd_seq_last_oid(oseq)) {
1187                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
1188                                         oa->o_id, ofd_seq_last_oid(oseq));
1189                         GOTO(out_nolock, rc = -EINVAL);
1190                 }
1191                 /* do nothing because we create objects during first write */
1192                 GOTO(out_nolock, rc = 0);
1193         }
1194         /* former ofd_handle_precreate */
1195         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1196             (oa->o_flags & OBD_FL_DELORPHAN)) {
1197                 /* destroy orphans */
1198                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1199                         CERROR("%s: dropping old orphan cleanup request\n",
1200                                ofd_name(ofd));
1201                         GOTO(out_nolock, rc = 0);
1202                 }
1203                 /* This causes inflight precreates to abort and drop lock */
1204                 oseq->os_destroys_in_progress = 1;
1205                 mutex_lock(&oseq->os_create_lock);
1206                 if (!oseq->os_destroys_in_progress) {
1207                         CERROR("%s:["LPU64"] destroys_in_progress already cleared\n",
1208                                exp->exp_obd->obd_name, oa->o_seq);
1209                         GOTO(out, rc = 0);
1210                 }
1211                 diff = oa->o_id - ofd_seq_last_oid(oseq);
1212                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1213                         ofd_seq_last_oid(oseq), diff);
1214                 if (-diff > OST_MAX_PRECREATE) {
1215                         /* FIXME: should reset precreate_next_id on MDS */
1216                         rc = 0;
1217                 } else if (diff < 0) {
1218                         rc = ofd_orphans_destroy(env, exp, ofd, oa);
1219                         oseq->os_destroys_in_progress = 0;
1220                 } else {
1221                         /* XXX: Used by MDS for the first time! */
1222                         oseq->os_destroys_in_progress = 0;
1223                 }
1224         } else {
1225                 mutex_lock(&oseq->os_create_lock);
1226                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1227                         CERROR("%s: dropping old precreate request\n",
1228                                 ofd_obd(ofd)->obd_name);
1229                         GOTO(out, rc = 0);
1230                 }
1231                 /* only precreate if seq is 0, IDIF or normal and also o_id
1232                  * must be specfied */
1233                 if ((!fid_seq_is_mdt(oa->o_seq) &&
1234                      !fid_seq_is_norm(oa->o_seq) &&
1235                      !fid_seq_is_idif(oa->o_seq)) || oa->o_id == 0) {
1236                         diff = 1; /* shouldn't we create this right now? */
1237                 } else {
1238                         diff = oa->o_id - ofd_seq_last_oid(oseq);
1239                         /* Do sync create if the seq is about to used up */
1240                         if (fid_seq_is_idif(oa->o_seq) ||
1241                             fid_seq_is_mdt0(oa->o_seq)) {
1242                                 if (unlikely(oa->o_id >= IDIF_MAX_OID - 1))
1243                                         sync_trans = 1;
1244                         } else if (fid_seq_is_norm(oa->o_seq)) {
1245                                 if (unlikely(oa->o_id >=
1246                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1247                                         sync_trans = 1;
1248                         } else {
1249                                 CERROR("%s : invalid o_seq "LPX64": rc = %d\n",
1250                                        ofd_name(ofd), oa->o_seq, -EINVAL);
1251                                 GOTO(out, rc = -EINVAL);
1252                         }
1253                 }
1254         }
1255         if (diff > 0) {
1256                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1257                 obd_id           next_id;
1258                 int              created = 0;
1259                 int              count;
1260
1261                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1262                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1263                         /* don't enforce grant during orphan recovery */
1264                         rc = ofd_grant_create(env,
1265                                               ofd_obd(ofd)->obd_self_export,
1266                                               &diff);
1267                         if (rc) {
1268                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1269                                        "space for precreate (%d): rc = %d\n",
1270                                        ofd_name(ofd), diff, rc);
1271                                 diff = 0;
1272                         }
1273                 }
1274
1275                 while (diff > 0) {
1276                         next_id = ofd_seq_last_oid(oseq) + 1;
1277                         count = ofd_precreate_batch(ofd, diff);
1278
1279                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPU64
1280                                " at "LPU64"\n", ofd_obd(ofd)->obd_name,
1281                                count, oa->o_seq, next_id);
1282
1283                         if (cfs_time_after(jiffies, enough_time)) {
1284                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1285                                               " created at a rate of %d/s\n",
1286                                               ofd_obd(ofd)->obd_name,
1287                                               created, diff + created,
1288                                               created / DISK_TIMEOUT);
1289                                 break;
1290                         }
1291
1292                         rc = ofd_precreate_objects(env, ofd, next_id,
1293                                                    oseq, count, sync_trans);
1294                         if (rc > 0) {
1295                                 created += rc;
1296                                 diff -= rc;
1297                         } else if (rc < 0) {
1298                                 break;
1299                         }
1300                 }
1301                 if (created > 0)
1302                         /* some objects got created, we can return
1303                          * them, even if last creation failed */
1304                         rc = 0;
1305                 else
1306                         CERROR("%s: unable to precreate: rc = %d\n",
1307                                ofd_name(ofd), rc);
1308
1309                 oa->o_id = ofd_seq_last_oid(oseq);
1310                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1311
1312                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1313                     !(oa->o_flags & OBD_FL_DELORPHAN))
1314                         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export,
1315                                          rc);
1316         }
1317
1318         ofd_info2oti(info, oti);
1319 out:
1320         mutex_unlock(&oseq->os_create_lock);
1321 out_nolock:
1322         if (rc == 0 && ea != NULL) {
1323                 struct lov_stripe_md *lsm = *ea;
1324
1325                 lsm->lsm_object_id = oa->o_id;
1326         }
1327         ofd_seq_put(env, oseq);
1328         RETURN(rc);
1329 }
1330
1331 int ofd_getattr(const struct lu_env *env, struct obd_export *exp,
1332                 struct obd_info *oinfo)
1333 {
1334         struct ofd_device       *ofd = ofd_exp(exp);
1335         struct ofd_thread_info  *info;
1336         struct ofd_object       *fo;
1337         __u64                    curr_version;
1338         int                      rc = 0;
1339
1340         ENTRY;
1341
1342         info = ofd_info_init(env, exp);
1343
1344         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1345         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1346                            oinfo_capa(oinfo), CAPA_OPC_META_READ);
1347         if (rc)
1348                 GOTO(out, rc);
1349
1350         fo = ofd_object_find(env, ofd, &info->fti_fid);
1351         if (IS_ERR(fo))
1352                 GOTO(out, rc = PTR_ERR(fo));
1353         LASSERT(fo != NULL);
1354         rc = ofd_attr_get(env, fo, &info->fti_attr);
1355         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1356         if (rc == 0)
1357                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
1358                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1359
1360         /* Store object version in reply */
1361         curr_version = dt_version_get(env, ofd_object_child(fo));
1362         if ((__s64)curr_version != -EOPNOTSUPP) {
1363                 oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
1364                 oinfo->oi_oa->o_data_version = curr_version;
1365         }
1366         ofd_object_put(env, fo);
1367 out:
1368         RETURN(rc);
1369 }
1370
1371 static int ofd_sync(const struct lu_env *env, struct obd_export *exp,
1372                     struct obd_info *oinfo, obd_size start, obd_size end,
1373                     struct ptlrpc_request_set *set)
1374 {
1375         struct ofd_device       *ofd = ofd_exp(exp);
1376         struct ofd_thread_info  *info;
1377         struct ofd_object       *fo;
1378         int                      rc = 0;
1379
1380         ENTRY;
1381
1382         /* if no objid is specified, it means "sync whole filesystem" */
1383         if (oinfo->oi_oa == NULL || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
1384                 rc = dt_sync(env, ofd->ofd_osd);
1385                 GOTO(out, rc);
1386         }
1387
1388         info = ofd_info_init(env, exp);
1389         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1390
1391         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1392                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
1393         if (rc)
1394                 GOTO(out, rc);
1395
1396         fo = ofd_object_find(env, ofd, &info->fti_fid);
1397         if (IS_ERR(fo)) {
1398                 CERROR("%s: error finding object "DFID": rc = %ld\n",
1399                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
1400                        PTR_ERR(fo));
1401                 GOTO(out, rc = PTR_ERR(fo));
1402         }
1403
1404         if (!ofd_object_exists(fo))
1405                 GOTO(put, rc = -ENOENT);
1406
1407         if (dt_version_get(env, ofd_object_child(fo)) >
1408             ofd_obd(ofd)->obd_last_committed) {
1409                 rc = dt_object_sync(env, ofd_object_child(fo));
1410                 if (rc)
1411                         GOTO(put, rc);
1412         }
1413
1414         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1415         rc = ofd_attr_get(env, fo, &info->fti_attr);
1416         obdo_from_la(oinfo->oi_oa, &info->fti_attr, OFD_VALID_FLAGS);
1417
1418         ofd_counter_incr(exp, LPROC_OFD_STATS_SYNC, oinfo->oi_jobid, 1);
1419         EXIT;
1420 put:
1421         ofd_object_put(env, fo);
1422 out:
1423         return rc;
1424 }
1425
1426 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1427                   void *karg, void *uarg)
1428 {
1429         struct lu_env            env;
1430         struct ofd_device       *ofd = ofd_exp(exp);
1431         struct obd_device       *obd = ofd_obd(ofd);
1432         int                      rc;
1433
1434         ENTRY;
1435
1436         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1437         rc = lu_env_init(&env, LCT_LOCAL);
1438         if (rc)
1439                 RETURN(rc);
1440
1441         switch (cmd) {
1442         case OBD_IOC_ABORT_RECOVERY:
1443                 CERROR("%s: aborting recovery\n", obd->obd_name);
1444                 target_stop_recovery_thread(obd);
1445                 break;
1446         case OBD_IOC_SYNC:
1447                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1448                 rc = dt_sync(&env, ofd->ofd_osd);
1449                 break;
1450         case OBD_IOC_SET_READONLY:
1451                 rc = dt_sync(&env, ofd->ofd_osd);
1452                 if (rc == 0)
1453                         rc = dt_ro(&env, ofd->ofd_osd);
1454                 break;
1455         default:
1456                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1457                 rc = -ENOTTY;
1458         }
1459
1460         lu_env_fini(&env);
1461         RETURN(rc);
1462 }
1463
1464 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1465 {
1466         int rc = 0;
1467
1468         ENTRY;
1469
1470         switch(stage) {
1471         case OBD_CLEANUP_EARLY:
1472                 break;
1473         case OBD_CLEANUP_EXPORTS:
1474                 target_cleanup_recovery(obd);
1475                 break;
1476         }
1477         RETURN(rc);
1478 }
1479
1480 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1481 {
1482         ofd_fmd_expire(exp);
1483         return 0;
1484 }
1485
1486 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1487 {
1488         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1489         struct ofd_thread_info  *info;
1490         struct lu_env            env;
1491 #ifdef USE_HEALTH_CHECK_WRITE
1492         struct thandle          *th;
1493 #endif
1494         int                      rc = 0;
1495
1496         /* obd_proc_read_health pass NULL env, we need real one */
1497         rc = lu_env_init(&env, LCT_DT_THREAD);
1498         if (rc)
1499                 RETURN(rc);
1500
1501         info = ofd_info_init(&env, NULL);
1502         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1503         if (unlikely(rc))
1504                 GOTO(out, rc);
1505
1506         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1507                 GOTO(out, rc = -EROFS);
1508
1509 #ifdef USE_HEALTH_CHECK_WRITE
1510         OBD_ALLOC(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1511         if (info->fti_buf.lb_buf == NULL)
1512                 GOTO(out, rc = -ENOMEM);
1513
1514         info->fti_buf.lb_len = CFS_PAGE_SIZE;
1515         info->fti_off = 0;
1516
1517         th = dt_trans_create(&env, ofd->ofd_osd);
1518         if (IS_ERR(th))
1519                 GOTO(out, rc = PTR_ERR(th));
1520
1521         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1522                                      info->fti_buf.lb_len, info->fti_off, th);
1523         if (rc == 0) {
1524                 th->th_sync = 1; /* sync IO is needed */
1525                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1526                 if (rc == 0)
1527                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1528                                              &info->fti_buf, &info->fti_off,
1529                                              th);
1530         }
1531         dt_trans_stop(&env, ofd->ofd_osd, th);
1532
1533         OBD_FREE(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1534
1535         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1536 #endif
1537 out:
1538         lu_env_fini(&env);
1539         return !!rc;
1540 }
1541
1542 /*
1543  * Handle quota control requests to consult current usage/limit.
1544  *
1545  * \param obd - is the obd device associated with the ofd
1546  * \param exp - is the client's export
1547  * \param oqctl - is the obd_quotactl request to be processed
1548  */
1549 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1550                         struct obd_quotactl *oqctl)
1551 {
1552         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1553         struct lu_env       env;
1554         int                 rc;
1555         ENTRY;
1556
1557         /* report success for quota on/off for interoperability with current MDT
1558          * stack */
1559         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1560                 RETURN(0);
1561
1562         rc = lu_env_init(&env, LCT_DT_THREAD);
1563         if (rc)
1564                 RETURN(rc);
1565
1566         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1567         lu_env_fini(&env);
1568
1569         RETURN(rc);
1570 }
1571
1572 struct obd_ops ofd_obd_ops = {
1573         .o_owner                = THIS_MODULE,
1574         .o_connect              = ofd_obd_connect,
1575         .o_reconnect            = ofd_obd_reconnect,
1576         .o_disconnect           = ofd_obd_disconnect,
1577         .o_set_info_async       = ofd_set_info_async,
1578         .o_get_info             = ofd_get_info,
1579         .o_create               = ofd_create,
1580         .o_statfs               = ofd_statfs,
1581         .o_setattr              = ofd_setattr,
1582         .o_preprw               = ofd_preprw,
1583         .o_commitrw             = ofd_commitrw,
1584         .o_destroy              = ofd_destroy,
1585         .o_init_export          = ofd_init_export,
1586         .o_destroy_export       = ofd_destroy_export,
1587         .o_postrecov            = ofd_obd_postrecov,
1588         .o_punch                = ofd_punch,
1589         .o_getattr              = ofd_getattr,
1590         .o_sync                 = ofd_sync,
1591         .o_iocontrol            = ofd_iocontrol,
1592         .o_precleanup           = ofd_precleanup,
1593         .o_ping                 = ofd_ping,
1594         .o_health_check         = ofd_health_check,
1595         .o_quotactl             = ofd_quotactl,
1596 };