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