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