Whamcloud - gitweb
059d0e2fc01258400d6c800f91db657a9799e2cd
[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
50 static int ofd_export_stats_init(struct ofd_device *ofd,
51                                  struct obd_export *exp, void *client_nid)
52 {
53         struct obd_device       *obd = ofd_obd(ofd);
54         struct nid_stat         *stats;
55         int                      num_stats;
56         int                      rc, newnid = 0;
57
58         ENTRY;
59
60         if (obd_uuid_equals(&exp->exp_client_uuid, &obd->obd_uuid))
61                 /* Self-export gets no proc entry */
62                 RETURN(0);
63
64         rc = lprocfs_exp_setup(exp, client_nid, &newnid);
65         if (rc) {
66                 /* Mask error for already created
67                  * /proc entries */
68                 if (rc == -EALREADY)
69                         rc = 0;
70                 RETURN(rc);
71         }
72
73         if (newnid == 0)
74                 RETURN(0);
75
76         stats = exp->exp_nid_stats;
77         LASSERT(stats != NULL);
78
79         num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
80                      LPROC_OFD_LAST - 1;
81
82         stats->nid_stats = lprocfs_alloc_stats(num_stats,
83                                                LPROCFS_STATS_FLAG_NOPERCPU);
84         if (stats->nid_stats == NULL)
85                 return -ENOMEM;
86
87         lprocfs_init_ops_stats(LPROC_OFD_LAST, stats->nid_stats);
88         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_READ_BYTES,
89                              LPROCFS_CNTR_AVGMINMAX, "read_bytes", "bytes");
90         lprocfs_counter_init(stats->nid_stats, LPROC_OFD_WRITE_BYTES,
91                              LPROCFS_CNTR_AVGMINMAX, "write_bytes", "bytes");
92
93         rc = lprocfs_register_stats(stats->nid_proc, "stats",
94                                     stats->nid_stats);
95         if (rc)
96                 GOTO(clean, rc);
97
98         rc = lprocfs_nid_ldlm_stats_init(stats);
99         if (rc) {
100                 lprocfs_free_stats(&stats->nid_stats);
101                 GOTO(clean, rc);
102         }
103
104         RETURN(0);
105 clean:
106         return rc;
107 }
108
109 static int ofd_parse_connect_data(const struct lu_env *env,
110                                   struct obd_export *exp,
111                                   struct obd_connect_data *data,
112                                   bool new_connection)
113 {
114         struct ofd_device                *ofd = ofd_exp(exp);
115         struct filter_export_data        *fed = &exp->exp_filter_data;
116
117         if (!data)
118                 RETURN(0);
119
120         CDEBUG(D_RPCTRACE, "%s: cli %s/%p ocd_connect_flags: "LPX64
121                " ocd_version: %x ocd_grant: %d ocd_index: %u"
122                " ocd_group %u\n",
123                exp->exp_obd->obd_name, exp->exp_client_uuid.uuid, exp,
124                data->ocd_connect_flags, data->ocd_version,
125                data->ocd_grant, data->ocd_index, data->ocd_group);
126
127         if (fed->fed_group != 0 && fed->fed_group != data->ocd_group) {
128                 CWARN("!!! This export (nid %s) used object group %d "
129                       "earlier; now it's trying to use group %d!  This could "
130                       "be a bug in the MDS. Please report to "
131                       "http://bugs.whamcloud.com/\n",
132                       obd_export_nid2str(exp), fed->fed_group,
133                       data->ocd_group);
134                 RETURN(-EPROTO);
135         }
136         fed->fed_group = data->ocd_group;
137
138         data->ocd_connect_flags &= OST_CONNECT_SUPPORTED;
139         exp->exp_connect_data = *data;
140         data->ocd_version = LUSTRE_VERSION_CODE;
141
142         /* Kindly make sure the SKIP_ORPHAN flag is from MDS. */
143         if (data->ocd_connect_flags & OBD_CONNECT_MDS)
144                 CDEBUG(D_HA, "%s: Received MDS connection for group %u\n",
145                        exp->exp_obd->obd_name, data->ocd_group);
146         else if (data->ocd_connect_flags & OBD_CONNECT_SKIP_ORPHAN)
147                 RETURN(-EPROTO);
148
149         if (ofd_grant_param_supp(exp)) {
150                 exp->exp_filter_data.fed_pagesize = data->ocd_blocksize;
151                 /* ocd_{blocksize,inodespace} are log2 values */
152                 data->ocd_blocksize  = ofd->ofd_blockbits;
153                 data->ocd_inodespace = ofd->ofd_dt_conf.ddp_inodespace;
154                 /* ocd_grant_extent is in 1K blocks */
155                 data->ocd_grant_extent = ofd->ofd_dt_conf.ddp_grant_frag >> 10;
156         }
157
158         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
159                 data->ocd_grant = ofd_grant_connect(env, exp, data->ocd_grant,
160                                                     new_connection);
161
162         if (data->ocd_connect_flags & OBD_CONNECT_INDEX) {
163                 struct lr_server_data *lsd = &ofd->ofd_lut.lut_lsd;
164                 int                    index = lsd->lsd_osd_index;
165
166                 if (index != data->ocd_index) {
167                         LCONSOLE_ERROR_MSG(0x136, "Connection from %s to index"
168                                            " %u doesn't match actual OST index"
169                                            " %u in last_rcvd file, bad "
170                                            "configuration?\n",
171                                            obd_export_nid2str(exp), index,
172                                            data->ocd_index);
173                         RETURN(-EBADF);
174                 }
175                 if (!(lsd->lsd_feature_compat & OBD_COMPAT_OST)) {
176                         /* this will only happen on the first connect */
177                         lsd->lsd_feature_compat |= OBD_COMPAT_OST;
178                         /* sync is not needed here as lut_client_add will
179                          * set exp_need_sync flag */
180                         tgt_server_data_update(env, &ofd->ofd_lut, 0);
181                 }
182         }
183         if (OBD_FAIL_CHECK(OBD_FAIL_OST_BRW_SIZE)) {
184                 data->ocd_brw_size = 65536;
185         } else if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
186                 data->ocd_brw_size = min(data->ocd_brw_size,
187                                          (__u32)DT_MAX_BRW_SIZE);
188                 if (data->ocd_brw_size == 0) {
189                         CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
190                                " ocd_version: %x ocd_grant: %d ocd_index: %u "
191                                "ocd_brw_size is unexpectedly zero, "
192                                "network data corruption?"
193                                "Refusing connection of this client\n",
194                                exp->exp_obd->obd_name,
195                                exp->exp_client_uuid.uuid,
196                                exp, data->ocd_connect_flags, data->ocd_version,
197                                data->ocd_grant, data->ocd_index);
198                         RETURN(-EPROTO);
199                 }
200         }
201
202         if (data->ocd_connect_flags & OBD_CONNECT_CKSUM) {
203                 __u32 cksum_types = data->ocd_cksum_types;
204
205                 /* The client set in ocd_cksum_types the checksum types it
206                  * supports. We have to mask off the algorithms that we don't
207                  * support */
208                 data->ocd_cksum_types &= cksum_types_supported_server();
209
210                 if (unlikely(data->ocd_cksum_types == 0)) {
211                         CERROR("%s: Connect with checksum support but no "
212                                "ocd_cksum_types is set\n",
213                                exp->exp_obd->obd_name);
214                         RETURN(-EPROTO);
215                 }
216
217                 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
218                        "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
219                        cksum_types, data->ocd_cksum_types);
220         } else {
221                 /* This client does not support OBD_CONNECT_CKSUM
222                  * fall back to CRC32 */
223                 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
224                        "OBD_CONNECT_CKSUM, CRC32 will be used\n",
225                        exp->exp_obd->obd_name, obd_export_nid2str(exp));
226         }
227
228         if (data->ocd_connect_flags & OBD_CONNECT_MAXBYTES)
229                 data->ocd_maxbytes = ofd->ofd_dt_conf.ddp_maxbytes;
230
231         if (OCD_HAS_FLAG(data, PINGLESS)) {
232                 if (ptlrpc_pinger_suppress_pings()) {
233                         spin_lock(&exp->exp_obd->obd_dev_lock);
234                         list_del_init(&exp->exp_obd_chain_timed);
235                         spin_unlock(&exp->exp_obd->obd_dev_lock);
236                 } else {
237                         data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
238                 }
239         }
240
241         RETURN(0);
242 }
243
244 static int ofd_obd_reconnect(const struct lu_env *env, struct obd_export *exp,
245                              struct obd_device *obd, struct obd_uuid *cluuid,
246                              struct obd_connect_data *data, void *localdata)
247 {
248         struct ofd_device       *ofd;
249         int                      rc;
250
251         ENTRY;
252
253         if (exp == NULL || obd == NULL || cluuid == NULL)
254                 RETURN(-EINVAL);
255
256         ofd = ofd_dev(obd->obd_lu_dev);
257
258         rc = lu_env_refill((struct lu_env *)env);
259         if (rc != 0) {
260                 CERROR("Failure to refill session: '%d'\n", rc);
261                 RETURN(rc);
262         }
263
264         ofd_info_init(env, exp);
265         rc = ofd_parse_connect_data(env, exp, data, false);
266         if (rc == 0)
267                 ofd_export_stats_init(ofd, exp, localdata);
268
269         RETURN(rc);
270 }
271
272 static int ofd_obd_connect(const struct lu_env *env, struct obd_export **_exp,
273                            struct obd_device *obd, struct obd_uuid *cluuid,
274                            struct obd_connect_data *data, void *localdata)
275 {
276         struct obd_export       *exp;
277         struct ofd_device       *ofd;
278         struct lustre_handle     conn = { 0 };
279         int                      rc;
280         ENTRY;
281
282         if (_exp == NULL || obd == NULL || cluuid == NULL)
283                 RETURN(-EINVAL);
284
285         ofd = ofd_dev(obd->obd_lu_dev);
286
287         rc = class_connect(&conn, obd, cluuid);
288         if (rc)
289                 RETURN(rc);
290
291         exp = class_conn2export(&conn);
292         LASSERT(exp != NULL);
293
294         rc = lu_env_refill((struct lu_env *)env);
295         if (rc != 0) {
296                 CERROR("Failure to refill session: '%d'\n", rc);
297                 GOTO(out, rc);
298         }
299
300         ofd_info_init(env, exp);
301
302         rc = ofd_parse_connect_data(env, exp, data, true);
303         if (rc)
304                 GOTO(out, rc);
305
306         if (obd->obd_replayable) {
307                 struct tg_export_data *ted = &exp->exp_target_data;
308
309                 memcpy(ted->ted_lcd->lcd_uuid, cluuid,
310                        sizeof(ted->ted_lcd->lcd_uuid));
311                 rc = tgt_client_new(env, exp);
312                 if (rc != 0)
313                         GOTO(out, rc);
314                 ofd_export_stats_init(ofd, exp, localdata);
315         }
316
317         CDEBUG(D_HA, "%s: get connection from MDS %d\n", obd->obd_name,
318                data->ocd_group);
319
320 out:
321         if (rc != 0) {
322                 class_disconnect(exp);
323                 *_exp = NULL;
324         } else {
325                 *_exp = exp;
326         }
327         RETURN(rc);
328 }
329
330 static int ofd_obd_disconnect(struct obd_export *exp)
331 {
332         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
333         struct lu_env            env;
334         int                      rc;
335
336         ENTRY;
337
338         LASSERT(exp);
339         class_export_get(exp);
340
341         if (!(exp->exp_flags & OBD_OPT_FORCE))
342                 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
343
344         rc = server_disconnect_export(exp);
345
346         ofd_grant_discard(exp);
347
348         rc = lu_env_init(&env, LCT_DT_THREAD);
349         if (rc)
350                 RETURN(rc);
351
352         /* Do not erase record for recoverable client. */
353         if (exp->exp_obd->obd_replayable &&
354             (!exp->exp_obd->obd_fail || exp->exp_failed))
355                 tgt_client_del(&env, exp);
356         lu_env_fini(&env);
357
358         class_export_put(exp);
359         RETURN(rc);
360 }
361
362 static int ofd_init_export(struct obd_export *exp)
363 {
364         int rc;
365
366         spin_lock_init(&exp->exp_filter_data.fed_lock);
367         CFS_INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
368         spin_lock(&exp->exp_lock);
369         exp->exp_connecting = 1;
370         spin_unlock(&exp->exp_lock);
371
372         /* self-export doesn't need client data and ldlm initialization */
373         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
374                                      &exp->exp_client_uuid)))
375                 return 0;
376
377         rc = tgt_client_alloc(exp);
378         if (rc == 0)
379                 ldlm_init_export(exp);
380         if (rc)
381                 CERROR("%s: Can't initialize export: rc %d\n",
382                        exp->exp_obd->obd_name, rc);
383         return rc;
384 }
385
386 static int ofd_destroy_export(struct obd_export *exp)
387 {
388         struct ofd_device *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
389
390         if (exp->exp_filter_data.fed_pending)
391                 CERROR("%s: cli %s/%p has %lu pending on destroyed export"
392                        "\n", exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
393                        exp, exp->exp_filter_data.fed_pending);
394
395         target_destroy_export(exp);
396
397         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
398                                      &exp->exp_client_uuid)))
399                 return 0;
400
401         ldlm_destroy_export(exp);
402         tgt_client_free(exp);
403
404         ofd_fmd_cleanup(exp);
405
406         /*
407          * discard grants once we're sure no more
408          * interaction with the client is possible
409          */
410         ofd_grant_discard(exp);
411         ofd_fmd_cleanup(exp);
412
413         if (exp_connect_flags(exp) & OBD_CONNECT_GRANT_SHRINK) {
414                 if (ofd->ofd_tot_granted_clients > 0)
415                         ofd->ofd_tot_granted_clients --;
416         }
417
418         if (!(exp->exp_flags & OBD_OPT_FORCE))
419                 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
420
421         LASSERT(cfs_list_empty(&exp->exp_filter_data.fed_mod_list));
422         return 0;
423 }
424
425 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
426 {
427         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
428
429         CDEBUG(D_HA, "%s: recovery is over\n", ofd_obd(ofd)->obd_name);
430         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
431 }
432
433 int ofd_obd_postrecov(struct obd_device *obd)
434 {
435         struct lu_env            env;
436         struct lu_device        *ldev = obd->obd_lu_dev;
437         int                      rc;
438
439         ENTRY;
440
441         rc = lu_env_init(&env, LCT_DT_THREAD);
442         if (rc)
443                 RETURN(rc);
444         ofd_info_init(&env, obd->obd_self_export);
445
446         rc = ofd_postrecov(&env, ofd_dev(ldev));
447
448         lu_env_fini(&env);
449         RETURN(rc);
450 }
451
452 static int ofd_adapt_sptlrpc_conf(const struct lu_env *env,
453                                   struct obd_device *obd, int initial)
454 {
455         struct filter_obd       *fo = &obd->u.filter;
456         struct sptlrpc_rule_set  tmp_rset;
457         int                      rc;
458
459         sptlrpc_rule_set_init(&tmp_rset);
460         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
461         if (rc) {
462                 CERROR("%s: failed get sptlrpc rules: rc = %d\n",
463                        obd->obd_name, rc);
464                 return rc;
465         }
466
467         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
468
469         write_lock(&fo->fo_sptlrpc_lock);
470         sptlrpc_rule_set_free(&fo->fo_sptlrpc_rset);
471         fo->fo_sptlrpc_rset = tmp_rset;
472         write_unlock(&fo->fo_sptlrpc_lock);
473
474         return 0;
475 }
476
477 static int ofd_set_mds_conn(struct obd_export *exp, void *val)
478 {
479         int rc = 0;
480
481         ENTRY;
482
483         LCONSOLE_WARN("%s: received MDS connection from %s\n",
484                       exp->exp_obd->obd_name, obd_export_nid2str(exp));
485         RETURN(rc);
486 }
487
488 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
489                               __u32 keylen, void *key, __u32 vallen, void *val,
490                               struct ptlrpc_request_set *set)
491 {
492         struct ofd_device       *ofd;
493         int                      rc = 0;
494
495         ENTRY;
496
497         if (exp->exp_obd == NULL) {
498                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
499                 RETURN(-EINVAL);
500         }
501
502         ofd = ofd_exp(exp);
503
504         if (KEY_IS(KEY_CAPA_KEY)) {
505                 rc = ofd_update_capa_key(ofd, val);
506                 if (rc)
507                         CERROR("%s: update capability key failed: rc = %d\n",
508                                exp->exp_obd->obd_name, rc);
509         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
510                 ofd_adapt_sptlrpc_conf(env, exp->exp_obd, 0);
511         } else if (KEY_IS(KEY_MDS_CONN)) {
512                 rc = ofd_set_mds_conn(exp, val);
513         } else if (KEY_IS(KEY_GRANT_SHRINK)) {
514                 struct ost_body *body = val;
515
516                 ofd_info_init(env, exp);
517                 /** handle grant shrink, similar to a read request */
518                 ofd_grant_prepare_read(env, exp, &body->oa);
519         } else {
520                 CERROR("%s: Unsupported key %s\n",
521                        exp->exp_obd->obd_name, (char*)key);
522                 rc = -EOPNOTSUPP;
523         }
524
525         RETURN(rc);
526 }
527
528 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
529                         __u32 keylen, void *key, __u32 *vallen, void *val,
530                         struct lov_stripe_md *lsm)
531 {
532         struct ofd_device       *ofd;
533         int                     rc = 0;
534
535         ENTRY;
536
537         if (exp->exp_obd == NULL) {
538                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
539                 RETURN(-EINVAL);
540         }
541
542         /* Because ofd_get_info might be called from
543          * handle_request_in as well(see LU-3239), where env might
544          * not be initilaized correctly, and le_ses might be in
545          * an un-initialized state, so only refill le_ctx here */
546         rc = lu_env_refill((struct lu_env *)env);
547         if (rc != 0)
548                 RETURN(rc);
549
550         ofd = ofd_exp(exp);
551
552         if (KEY_IS(KEY_BLOCKSIZE)) {
553                 __u32 *blocksize = val;
554                 if (blocksize) {
555                         if (*vallen < sizeof(*blocksize))
556                                 GOTO(out, rc = -EOVERFLOW);
557                         *blocksize = 1 << ofd->ofd_dt_conf.ddp_block_shift;
558                 }
559                 *vallen = sizeof(*blocksize);
560         } else if (KEY_IS(KEY_BLOCKSIZE_BITS)) {
561                 __u32 *blocksize_bits = val;
562                 if (blocksize_bits) {
563                         if (*vallen < sizeof(*blocksize_bits))
564                                 GOTO(out, rc = -EOVERFLOW);
565                         *blocksize_bits = ofd->ofd_dt_conf.ddp_block_shift;
566                 }
567                 *vallen = sizeof(*blocksize_bits);
568         } else if (KEY_IS(KEY_LAST_ID)) {
569                 obd_id *last_id = val;
570                 struct ofd_seq *oseq;
571
572                 if (val == NULL) {
573                         *vallen = sizeof(obd_id);
574                         GOTO(out, rc = 0);
575                 }
576                 ofd_info_init(env, exp);
577                 oseq = ofd_seq_load(env, ofd,
578                                     (obd_seq)exp->exp_filter_data.fed_group);
579                 LASSERT(!IS_ERR(oseq));
580                 if (last_id) {
581                         if (*vallen < sizeof(*last_id)) {
582                                 ofd_seq_put(env, oseq);
583                                 GOTO(out, rc = -EOVERFLOW);
584                         }
585                         *last_id = ofd_seq_last_oid(oseq);
586                 }
587                 ofd_seq_put(env, oseq);
588                 *vallen = sizeof(*last_id);
589         } else if (KEY_IS(KEY_FIEMAP)) {
590                 struct ofd_thread_info          *info;
591                 struct ofd_device               *ofd = ofd_exp(exp);
592                 struct ofd_object               *fo;
593                 struct ll_fiemap_info_key       *fm_key = key;
594
595                 if (val == NULL) {
596                         *vallen = fiemap_count_to_size(
597                                                fm_key->fiemap.fm_extent_count);
598                         GOTO(out, rc = 0);
599                 }
600
601                 info = ofd_info_init(env, exp);
602                 rc = ostid_to_fid(&info->fti_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(&info->fti_fid));
607
608                 fo = ofd_object_find(env, ofd, &info->fti_fid);
609                 if (IS_ERR(fo)) {
610                         CERROR("%s: error finding object "DFID"\n",
611                                exp->exp_obd->obd_name, PFID(&info->fti_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_AST_DISCARD_DATA, rc = 0;
1006         ldlm_policy_data_t       policy = {
1007                                         .l_extent = { 0, OBD_OBJECT_EOF }
1008                                  };
1009         struct ofd_object       *fo;
1010
1011         ENTRY;
1012
1013         fo = ofd_object_find(env, ofd, fid);
1014         if (IS_ERR(fo))
1015                 RETURN(PTR_ERR(fo));
1016
1017         /* Tell the clients that the object is gone now and that they should
1018          * throw away any cached pages. */
1019         ost_fid_build_resid(fid, &info->fti_resid);
1020         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
1021                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
1022                                     ldlm_blocking_ast, ldlm_completion_ast,
1023                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
1024
1025         /* We only care about the side-effects, just drop the lock. */
1026         if (rc == ELDLM_OK)
1027                 ldlm_lock_decref(&lockh, LCK_PW);
1028
1029         LASSERT(fo != NULL);
1030
1031         rc = ofd_object_destroy(env, fo, orphan);
1032
1033         ofd_object_put(env, fo);
1034         RETURN(rc);
1035 }
1036
1037 int ofd_destroy(const struct lu_env *env, struct obd_export *exp,
1038                 struct obdo *oa, struct lov_stripe_md *md,
1039                 struct obd_trans_info *oti, struct obd_export *md_exp,
1040                 void *capa)
1041 {
1042         struct ofd_device       *ofd = ofd_exp(exp);
1043         struct ofd_thread_info  *info;
1044         obd_count                count;
1045         int                      rc = 0;
1046
1047         ENTRY;
1048
1049         info = ofd_info_init(env, exp);
1050         ofd_oti2info(info, oti);
1051
1052         if (!(oa->o_valid & OBD_MD_FLGROUP))
1053                 ostid_set_seq_mdt0(&oa->o_oi);
1054
1055         /* check that o_misc makes sense */
1056         if (oa->o_valid & OBD_MD_FLOBJCOUNT)
1057                 count = oa->o_misc;
1058         else
1059                 count = 1; /* default case - single destroy */
1060
1061         /**
1062          * There can be sequence of objects to destroy. Therefore this request
1063          * may have multiple transaction involved in. It is OK, we need only
1064          * the highest used transno to be reported back in reply but not for
1065          * replays, they must report their transno
1066          */
1067         if (info->fti_transno == 0) /* not replay */
1068                 info->fti_mult_trans = 1;
1069
1070         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1071                POSTID(&oa->o_oi), count);
1072         while (count > 0) {
1073                 int lrc;
1074
1075                 lrc = ostid_to_fid(&info->fti_fid, &oa->o_oi, 0);
1076                 if (lrc != 0) {
1077                         if (rc == 0)
1078                                 rc = lrc;
1079                         GOTO(out, rc);
1080                 }
1081                 lrc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 0);
1082                 if (lrc == -ENOENT) {
1083                         CDEBUG(D_INODE,
1084                                "%s: destroying non-existent object "DFID"\n",
1085                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid));
1086                         /* rewrite rc with -ENOENT only if it is 0 */
1087                         if (rc == 0)
1088                                 rc = lrc;
1089                 } else if (lrc != 0) {
1090                         CERROR("%s: error destroying object "DFID": %d\n",
1091                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid),
1092                                rc);
1093                         rc = lrc;
1094                 }
1095                 count--;
1096                 ostid_inc_id(&oa->o_oi);
1097         }
1098
1099         /* if we have transaction then there were some deletions, we don't
1100          * need to return ENOENT in that case because it will not wait
1101          * for commit of these deletions. The ENOENT must be returned only
1102          * if there were no transations.
1103          */
1104         if (rc == -ENOENT) {
1105                 if (info->fti_transno != 0)
1106                         rc = 0;
1107         } else if (rc != 0) {
1108                 /*
1109                  * If we have at least one transaction then llog record
1110                  * on server will be removed upon commit, so for rc != 0
1111                  * we return no transno and llog record will be reprocessed.
1112                  */
1113                 info->fti_transno = 0;
1114         }
1115         ofd_info2oti(info, oti);
1116 out:
1117         RETURN(rc);
1118 }
1119
1120 static int ofd_orphans_destroy(const struct lu_env *env,
1121                                struct obd_export *exp, struct ofd_device *ofd,
1122                                struct obdo *oa)
1123 {
1124         struct ofd_thread_info  *info = ofd_info(env);
1125         obd_id                   last;
1126         int                      skip_orphan;
1127         int                      rc = 0;
1128         struct ost_id            oi = oa->o_oi;
1129         __u64                    end_id = ostid_id(&oa->o_oi);
1130         struct ofd_seq          *oseq;
1131
1132         ENTRY;
1133
1134         oseq = ofd_seq_get(ofd, ostid_seq(&oa->o_oi));
1135         if (oseq == NULL) {
1136                 CERROR("%s: Can not find seq for "DOSTID"\n",
1137                        ofd_name(ofd), POSTID(&oa->o_oi));
1138                 RETURN(-EINVAL);
1139         }
1140
1141         LASSERT(exp != NULL);
1142         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1143
1144         last = ofd_seq_last_oid(oseq);
1145         LCONSOLE_INFO("%s: deleting orphan objects from "LPX64":"LPU64
1146                       " to "LPU64"\n", ofd_name(ofd), ostid_seq(&oa->o_oi),
1147                       end_id + 1, last);
1148
1149         for (ostid_set_id(&oi, last); ostid_id(&oi) > end_id;
1150                           ostid_dec_id(&oi)) {
1151                 rc = ostid_to_fid(&info->fti_fid, &oi, 0);
1152                 if (rc != 0)
1153                         GOTO(out_put, rc);
1154                 rc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 1);
1155                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
1156                         CEMERG("error destroying precreated id "DOSTID": %d\n",
1157                                POSTID(&oi), rc);
1158                 if (!skip_orphan) {
1159                         ofd_seq_last_oid_set(oseq, ostid_id(&oi) - 1);
1160                         /* update last_id on disk periodically so that if we
1161                          * restart * we don't need to re-scan all of the just
1162                          * deleted objects. */
1163                         if ((ostid_id(&oi) & 511) == 0)
1164                                 ofd_seq_last_oid_write(env, ofd, oseq);
1165                 }
1166         }
1167         CDEBUG(D_HA, "%s: after destroy: set last_objids"DOSTID"\n",
1168                ofd_obd(ofd)->obd_name, POSTID(&oa->o_oi));
1169         if (!skip_orphan) {
1170                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1171         } else {
1172                 /* don't reuse orphan object, return last used objid */
1173                 ostid_set_id(&oa->o_oi, last);
1174                 rc = 0;
1175         }
1176 out_put:
1177         ofd_seq_put(env, oseq);
1178         RETURN(rc);
1179 }
1180
1181 int ofd_create(const struct lu_env *env, struct obd_export *exp,
1182                struct obdo *oa, struct lov_stripe_md **ea,
1183                struct obd_trans_info *oti)
1184 {
1185         struct ofd_device       *ofd = ofd_exp(exp);
1186         struct ofd_thread_info  *info;
1187         obd_seq                 seq = ostid_seq(&oa->o_oi);
1188         struct ofd_seq          *oseq;
1189         int                     rc = 0, diff;
1190         int                     sync_trans = 0;
1191
1192         ENTRY;
1193
1194         info = ofd_info_init(env, exp);
1195         ofd_oti2info(info, oti);
1196
1197         LASSERT(ostid_seq(&oa->o_oi) >= FID_SEQ_OST_MDT0);
1198         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1199
1200         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1201
1202         oseq = ofd_seq_load(env, ofd, seq);
1203         if (IS_ERR(oseq)) {
1204                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1205                        ofd_name(ofd), seq, PTR_ERR(oseq));
1206                 RETURN(-EINVAL);
1207         }
1208
1209         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1210             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1211                 if (!ofd_obd(ofd)->obd_recovering ||
1212                     ostid_id(&oa->o_oi) > ofd_seq_last_oid(oseq)) {
1213                         CERROR("recreate objid "DOSTID" > last id "LPU64"\n",
1214                                POSTID(&oa->o_oi), ofd_seq_last_oid(oseq));
1215                         GOTO(out_nolock, rc = -EINVAL);
1216                 }
1217                 /* do nothing because we create objects during first write */
1218                 GOTO(out_nolock, rc = 0);
1219         }
1220         /* former ofd_handle_precreate */
1221         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1222             (oa->o_flags & OBD_FL_DELORPHAN)) {
1223                 /* destroy orphans */
1224                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1225                         CERROR("%s: dropping old orphan cleanup request\n",
1226                                ofd_name(ofd));
1227                         GOTO(out_nolock, rc = 0);
1228                 }
1229                 /* This causes inflight precreates to abort and drop lock */
1230                 oseq->os_destroys_in_progress = 1;
1231                 mutex_lock(&oseq->os_create_lock);
1232                 if (!oseq->os_destroys_in_progress) {
1233                         CERROR("%s:["LPU64"] destroys_in_progress already"
1234                                " cleared\n", exp->exp_obd->obd_name,
1235                                ostid_seq(&oa->o_oi));
1236                         ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1237                         GOTO(out, rc = 0);
1238                 }
1239                 diff = ostid_id(&oa->o_oi) - ofd_seq_last_oid(oseq);
1240                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1241                         ofd_seq_last_oid(oseq), diff);
1242                 if (-diff > OST_MAX_PRECREATE) {
1243                         /* FIXME: should reset precreate_next_id on MDS */
1244                         rc = 0;
1245                 } else if (diff < 0) {
1246                         rc = ofd_orphans_destroy(env, exp, ofd, oa);
1247                         oseq->os_destroys_in_progress = 0;
1248                 } else {
1249                         /* XXX: Used by MDS for the first time! */
1250                         oseq->os_destroys_in_progress = 0;
1251                 }
1252         } else {
1253                 mutex_lock(&oseq->os_create_lock);
1254                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1255                         CERROR("%s: dropping old precreate request\n",
1256                                 ofd_obd(ofd)->obd_name);
1257                         GOTO(out, rc = 0);
1258                 }
1259                 /* only precreate if seq is 0, IDIF or normal and also o_id
1260                  * must be specfied */
1261                 if ((!fid_seq_is_mdt(ostid_seq(&oa->o_oi)) &&
1262                      !fid_seq_is_norm(ostid_seq(&oa->o_oi)) &&
1263                      !fid_seq_is_idif(ostid_seq(&oa->o_oi))) ||
1264                                 ostid_id(&oa->o_oi) == 0) {
1265                         diff = 1; /* shouldn't we create this right now? */
1266                 } else {
1267                         diff = ostid_id(&oa->o_oi) - ofd_seq_last_oid(oseq);
1268                         /* Do sync create if the seq is about to used up */
1269                         if (fid_seq_is_idif(ostid_seq(&oa->o_oi)) ||
1270                             fid_seq_is_mdt0(ostid_seq(&oa->o_oi))) {
1271                                 if (unlikely(ostid_id(&oa->o_oi) >= IDIF_MAX_OID - 1))
1272                                         sync_trans = 1;
1273                         } else if (fid_seq_is_norm(ostid_seq(&oa->o_oi))) {
1274                                 if (unlikely(ostid_id(&oa->o_oi) >=
1275                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1276                                         sync_trans = 1;
1277                         } else {
1278                                 CERROR("%s : invalid o_seq "DOSTID": rc = %d\n",
1279                                        ofd_name(ofd), POSTID(&oa->o_oi), -EINVAL);
1280                                 GOTO(out, rc = -EINVAL);
1281                         }
1282                 }
1283         }
1284         if (diff > 0) {
1285                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1286                 obd_id           next_id;
1287                 int              created = 0;
1288                 int              count;
1289
1290                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1291                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1292                         /* don't enforce grant during orphan recovery */
1293                         rc = ofd_grant_create(env,
1294                                               ofd_obd(ofd)->obd_self_export,
1295                                               &diff);
1296                         if (rc) {
1297                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1298                                        "space for precreate (%d): rc = %d\n",
1299                                        ofd_name(ofd), diff, rc);
1300                                 diff = 0;
1301                         }
1302                 }
1303
1304                 while (diff > 0) {
1305                         next_id = ofd_seq_last_oid(oseq) + 1;
1306                         count = ofd_precreate_batch(ofd, diff);
1307
1308                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1309                                " at "LPU64"\n", ofd_obd(ofd)->obd_name,
1310                                count, ostid_seq(&oa->o_oi), next_id);
1311
1312                         if (cfs_time_after(jiffies, enough_time)) {
1313                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1314                                               " created at a rate of %d/s\n",
1315                                               ofd_obd(ofd)->obd_name,
1316                                               created, diff + created,
1317                                               created / DISK_TIMEOUT);
1318                                 break;
1319                         }
1320
1321                         rc = ofd_precreate_objects(env, ofd, next_id,
1322                                                    oseq, count, sync_trans);
1323                         if (rc > 0) {
1324                                 created += rc;
1325                                 diff -= rc;
1326                         } else if (rc < 0) {
1327                                 break;
1328                         }
1329                 }
1330                 if (created > 0)
1331                         /* some objects got created, we can return
1332                          * them, even if last creation failed */
1333                         rc = 0;
1334                 else
1335                         CERROR("%s: unable to precreate: rc = %d\n",
1336                                ofd_name(ofd), rc);
1337
1338                 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1339                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1340
1341                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1342                     !(oa->o_flags & OBD_FL_DELORPHAN))
1343                         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export,
1344                                          rc);
1345         }
1346
1347         ofd_info2oti(info, oti);
1348 out:
1349         mutex_unlock(&oseq->os_create_lock);
1350 out_nolock:
1351         if (rc == 0 && ea != NULL) {
1352                 struct lov_stripe_md *lsm = *ea;
1353
1354                 lsm->lsm_oi = oa->o_oi;
1355         }
1356         ofd_seq_put(env, oseq);
1357         RETURN(rc);
1358 }
1359
1360 int ofd_getattr(const struct lu_env *env, struct obd_export *exp,
1361                 struct obd_info *oinfo)
1362 {
1363         struct ofd_device       *ofd = ofd_exp(exp);
1364         struct ofd_thread_info  *info;
1365         struct ofd_object       *fo;
1366         __u64                    curr_version;
1367         int                      rc = 0;
1368
1369         ENTRY;
1370
1371         info = ofd_info_init(env, exp);
1372
1373         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1374         if (rc != 0)
1375                 GOTO(out, rc);
1376         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oinfo->oi_oa->o_oi),
1377                            oinfo_capa(oinfo), CAPA_OPC_META_READ);
1378         if (rc)
1379                 GOTO(out, rc);
1380
1381         fo = ofd_object_find(env, ofd, &info->fti_fid);
1382         if (IS_ERR(fo))
1383                 GOTO(out, rc = PTR_ERR(fo));
1384         LASSERT(fo != NULL);
1385         rc = ofd_attr_get(env, fo, &info->fti_attr);
1386         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1387         if (rc == 0)
1388                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
1389                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1390
1391         /* Store object version in reply */
1392         curr_version = dt_version_get(env, ofd_object_child(fo));
1393         if ((__s64)curr_version != -EOPNOTSUPP) {
1394                 oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
1395                 oinfo->oi_oa->o_data_version = curr_version;
1396         }
1397         ofd_object_put(env, fo);
1398 out:
1399         RETURN(rc);
1400 }
1401
1402 static int ofd_sync(const struct lu_env *env, struct obd_export *exp,
1403                     struct obd_info *oinfo, obd_size start, obd_size end,
1404                     struct ptlrpc_request_set *set)
1405 {
1406         struct ofd_device       *ofd = ofd_exp(exp);
1407         struct ofd_thread_info  *info;
1408         struct ofd_object       *fo;
1409         int                      rc = 0;
1410
1411         ENTRY;
1412
1413         /* if no objid is specified, it means "sync whole filesystem" */
1414         if (oinfo->oi_oa == NULL || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
1415                 rc = dt_sync(env, ofd->ofd_osd);
1416                 GOTO(out, rc);
1417         }
1418
1419         info = ofd_info_init(env, exp);
1420         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1421         if (rc != 0)
1422                 GOTO(out, rc);
1423
1424         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oinfo->oi_oa->o_oi),
1425                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
1426         if (rc)
1427                 GOTO(out, rc);
1428
1429         fo = ofd_object_find(env, ofd, &info->fti_fid);
1430         if (IS_ERR(fo)) {
1431                 CERROR("%s: error finding object "DFID": rc = %ld\n",
1432                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
1433                        PTR_ERR(fo));
1434                 GOTO(out, rc = PTR_ERR(fo));
1435         }
1436
1437         if (!ofd_object_exists(fo))
1438                 GOTO(put, rc = -ENOENT);
1439
1440         if (dt_version_get(env, ofd_object_child(fo)) >
1441             ofd_obd(ofd)->obd_last_committed) {
1442                 rc = dt_object_sync(env, ofd_object_child(fo));
1443                 if (rc)
1444                         GOTO(put, rc);
1445         }
1446
1447         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1448         rc = ofd_attr_get(env, fo, &info->fti_attr);
1449         obdo_from_la(oinfo->oi_oa, &info->fti_attr, OFD_VALID_FLAGS);
1450
1451         ofd_counter_incr(exp, LPROC_OFD_STATS_SYNC, oinfo->oi_jobid, 1);
1452         EXIT;
1453 put:
1454         ofd_object_put(env, fo);
1455 out:
1456         return rc;
1457 }
1458
1459 static int ofd_ioc_get_obj_version(const struct lu_env *env,
1460                                    struct ofd_device *ofd, void *karg)
1461 {
1462         struct obd_ioctl_data *data = karg;
1463         struct lu_fid          fid;
1464         struct ofd_object     *fo;
1465         dt_obj_version_t       version;
1466         int                    rc = 0;
1467
1468         ENTRY;
1469
1470         if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
1471                 GOTO(out, rc = -EINVAL);
1472
1473         if (data->ioc_inlbuf1 != NULL && data->ioc_inllen1 == sizeof(fid)) {
1474                 fid = *(struct lu_fid *)data->ioc_inlbuf1;
1475         } else if (data->ioc_inlbuf3 != NULL &&
1476                    data->ioc_inllen3 == sizeof(__u64) &&
1477                    data->ioc_inlbuf4 != NULL &&
1478                    data->ioc_inllen4 == sizeof(__u64)) {
1479                 struct ost_id ostid;
1480
1481                 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
1482                 ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
1483                 rc = ostid_to_fid(&fid, &ostid, 0);
1484                 if (rc != 0)
1485                         GOTO(out, rc);
1486         } else {
1487                 GOTO(out, rc = -EINVAL);
1488         }
1489
1490         if (!fid_is_sane(&fid))
1491                 GOTO(out, rc = -EINVAL);
1492
1493         fo = ofd_object_find(env, ofd, &fid);
1494         if (IS_ERR(fo))
1495                 GOTO(out, rc = PTR_ERR(fo));
1496
1497         if (!ofd_object_exists(fo))
1498                 GOTO(out_fo, rc = -ENOENT);
1499
1500         if (lu_object_remote(&fo->ofo_obj.do_lu))
1501                 GOTO(out_fo, rc = -EREMOTE);
1502
1503         version = dt_version_get(env, ofd_object_child(fo));
1504         if (version == 0)
1505                 GOTO(out_fo, rc = -EIO);
1506
1507         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
1508
1509         EXIT;
1510 out_fo:
1511         ofd_object_put(env, fo);
1512 out:
1513         return rc;
1514 }
1515
1516 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1517                   void *karg, void *uarg)
1518 {
1519         struct lu_env            env;
1520         struct ofd_device       *ofd = ofd_exp(exp);
1521         struct obd_device       *obd = ofd_obd(ofd);
1522         int                      rc;
1523
1524         ENTRY;
1525
1526         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1527         rc = lu_env_init(&env, LCT_DT_THREAD);
1528         if (rc)
1529                 RETURN(rc);
1530
1531         switch (cmd) {
1532         case OBD_IOC_ABORT_RECOVERY:
1533                 CERROR("%s: aborting recovery\n", obd->obd_name);
1534                 target_stop_recovery_thread(obd);
1535                 break;
1536         case OBD_IOC_SYNC:
1537                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1538                 rc = dt_sync(&env, ofd->ofd_osd);
1539                 break;
1540         case OBD_IOC_SET_READONLY:
1541                 rc = dt_sync(&env, ofd->ofd_osd);
1542                 if (rc == 0)
1543                         rc = dt_ro(&env, ofd->ofd_osd);
1544                 break;
1545         case OBD_IOC_GET_OBJ_VERSION:
1546                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1547                 break;
1548         default:
1549                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1550                 rc = -ENOTTY;
1551         }
1552
1553         lu_env_fini(&env);
1554         RETURN(rc);
1555 }
1556
1557 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1558 {
1559         int rc = 0;
1560
1561         ENTRY;
1562
1563         switch(stage) {
1564         case OBD_CLEANUP_EARLY:
1565                 break;
1566         case OBD_CLEANUP_EXPORTS:
1567                 target_cleanup_recovery(obd);
1568                 break;
1569         }
1570         RETURN(rc);
1571 }
1572
1573 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1574 {
1575         ofd_fmd_expire(exp);
1576         return 0;
1577 }
1578
1579 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1580 {
1581         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1582         struct ofd_thread_info  *info;
1583         struct lu_env            env;
1584 #ifdef USE_HEALTH_CHECK_WRITE
1585         struct thandle          *th;
1586 #endif
1587         int                      rc = 0;
1588
1589         /* obd_proc_read_health pass NULL env, we need real one */
1590         rc = lu_env_init(&env, LCT_DT_THREAD);
1591         if (rc)
1592                 RETURN(rc);
1593
1594         info = ofd_info_init(&env, NULL);
1595         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1596         if (unlikely(rc))
1597                 GOTO(out, rc);
1598
1599         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1600                 GOTO(out, rc = -EROFS);
1601
1602 #ifdef USE_HEALTH_CHECK_WRITE
1603         OBD_ALLOC(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1604         if (info->fti_buf.lb_buf == NULL)
1605                 GOTO(out, rc = -ENOMEM);
1606
1607         info->fti_buf.lb_len = CFS_PAGE_SIZE;
1608         info->fti_off = 0;
1609
1610         th = dt_trans_create(&env, ofd->ofd_osd);
1611         if (IS_ERR(th))
1612                 GOTO(out, rc = PTR_ERR(th));
1613
1614         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1615                                      info->fti_buf.lb_len, info->fti_off, th);
1616         if (rc == 0) {
1617                 th->th_sync = 1; /* sync IO is needed */
1618                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1619                 if (rc == 0)
1620                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1621                                              &info->fti_buf, &info->fti_off,
1622                                              th);
1623         }
1624         dt_trans_stop(&env, ofd->ofd_osd, th);
1625
1626         OBD_FREE(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1627
1628         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1629 #endif
1630 out:
1631         lu_env_fini(&env);
1632         return !!rc;
1633 }
1634
1635 /*
1636  * Handle quota control requests to consult current usage/limit.
1637  *
1638  * \param obd - is the obd device associated with the ofd
1639  * \param exp - is the client's export
1640  * \param oqctl - is the obd_quotactl request to be processed
1641  */
1642 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1643                         struct obd_quotactl *oqctl)
1644 {
1645         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1646         struct lu_env       env;
1647         int                 rc;
1648         ENTRY;
1649
1650         /* report success for quota on/off for interoperability with current MDT
1651          * stack */
1652         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1653                 RETURN(0);
1654
1655         rc = lu_env_init(&env, LCT_DT_THREAD);
1656         if (rc)
1657                 RETURN(rc);
1658
1659         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1660         lu_env_fini(&env);
1661
1662         RETURN(rc);
1663 }
1664
1665 struct obd_ops ofd_obd_ops = {
1666         .o_owner                = THIS_MODULE,
1667         .o_connect              = ofd_obd_connect,
1668         .o_reconnect            = ofd_obd_reconnect,
1669         .o_disconnect           = ofd_obd_disconnect,
1670         .o_set_info_async       = ofd_set_info_async,
1671         .o_get_info             = ofd_get_info,
1672         .o_create               = ofd_create,
1673         .o_statfs               = ofd_statfs,
1674         .o_setattr              = ofd_setattr,
1675         .o_preprw               = ofd_preprw,
1676         .o_commitrw             = ofd_commitrw,
1677         .o_destroy              = ofd_destroy,
1678         .o_init_export          = ofd_init_export,
1679         .o_destroy_export       = ofd_destroy_export,
1680         .o_postrecov            = ofd_obd_postrecov,
1681         .o_punch                = ofd_punch,
1682         .o_getattr              = ofd_getattr,
1683         .o_sync                 = ofd_sync,
1684         .o_iocontrol            = ofd_iocontrol,
1685         .o_precleanup           = ofd_precleanup,
1686         .o_ping                 = ofd_ping,
1687         .o_health_check         = ofd_health_check,
1688         .o_quotactl             = ofd_quotactl,
1689 };