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