Whamcloud - gitweb
LU-14 utils: allow formatting OST without VIRGIN flag
[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         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 (data->ocd_connect_flags & 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 ? data->ocd_group : -1);
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_device               *ofd = ofd_exp(exp);
591                 struct ofd_object               *fo;
592                 struct ll_fiemap_info_key       *fm_key = key;
593                 struct lu_fid                    fid;
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                 rc = ostid_to_fid(&fid, &fm_key->oa.o_oi, 0);
602                 if (rc != 0)
603                         GOTO(out, rc);
604                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n",
605                        PFID(&fid));
606
607                 fo = ofd_object_find(env, ofd, &fid);
608                 if (IS_ERR(fo)) {
609                         CERROR("%s: error finding object "DFID"\n",
610                                exp->exp_obd->obd_name, PFID(&fid));
611                         rc = PTR_ERR(fo);
612                 } else {
613                         struct ll_user_fiemap *fiemap = val;
614
615                         ofd_read_lock(env, fo);
616                         if (ofd_object_exists(fo)) {
617                                 *fiemap = fm_key->fiemap;
618                                 rc = dt_fiemap_get(env,
619                                                    ofd_object_child(fo),
620                                                    fiemap);
621                         } else {
622                                 rc = -ENOENT;
623                         }
624                         ofd_read_unlock(env, fo);
625                         ofd_object_put(env, fo);
626                 }
627         } else if (KEY_IS(KEY_SYNC_LOCK_CANCEL)) {
628                 *((__u32 *) val) = ofd->ofd_sync_lock_cancel;
629                 *vallen = sizeof(__u32);
630         } else if (KEY_IS(KEY_LAST_FID)) {
631                 struct ofd_device       *ofd = ofd_exp(exp);
632                 struct ofd_seq          *oseq;
633                 struct lu_fid           *fid = val;
634                 int                     rc;
635
636                 if (fid == NULL) {
637                         *vallen = sizeof(struct lu_fid);
638                         GOTO(out, rc = 0);
639                 }
640
641                 if (*vallen < sizeof(*fid))
642                         GOTO(out, rc = -EOVERFLOW);
643
644                 ofd_info_init(env, exp);
645
646                 fid_le_to_cpu(fid, fid);
647
648                 oseq = ofd_seq_load(env, ofd,
649                                     ostid_seq((struct ost_id *)fid));
650                 if (IS_ERR(oseq))
651                         GOTO(out, rc = PTR_ERR(oseq));
652
653                 rc = ostid_to_fid(fid, &oseq->os_oi,
654                              ofd->ofd_lut.lut_lsd.lsd_osd_index);
655                 if (rc != 0)
656                         GOTO(out_put, rc);
657
658                 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
659                        PFID(fid));
660                 *vallen = sizeof(*fid);
661 out_put:
662                 ofd_seq_put(env, oseq);
663         } else {
664                 CERROR("Not supported key %s\n", (char*)key);
665                 rc = -EOPNOTSUPP;
666         }
667 out:
668         RETURN(rc);
669 }
670
671 /** helper function for statfs, also used by grant code */
672 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
673                         struct obd_statfs *osfs, __u64 max_age, int *from_cache)
674 {
675         int rc;
676
677         spin_lock(&ofd->ofd_osfs_lock);
678         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
679                 obd_size unstable;
680
681                 /* statfs data are too old, get up-to-date one.
682                  * we must be cautious here since multiple threads might be
683                  * willing to update statfs data concurrently and we must
684                  * grant that cached statfs data are always consistent */
685
686                 if (ofd->ofd_statfs_inflight == 0)
687                         /* clear inflight counter if no users, although it would
688                          * take a while to overflow this 64-bit counter ... */
689                         ofd->ofd_osfs_inflight = 0;
690                 /* notify ofd_grant_commit() that we want to track writes
691                  * completed as of now */
692                 ofd->ofd_statfs_inflight++;
693                 /* record value of inflight counter before running statfs to
694                  * compute the diff once statfs is completed */
695                 unstable = ofd->ofd_osfs_inflight;
696                 spin_unlock(&ofd->ofd_osfs_lock);
697
698                 /* statfs can sleep ... hopefully not for too long since we can
699                  * call it fairly often as space fills up */
700                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
701                 if (unlikely(rc))
702                         return rc;
703
704                 spin_lock(&ofd->ofd_grant_lock);
705                 spin_lock(&ofd->ofd_osfs_lock);
706                 /* calculate how much space was written while we released the
707                  * ofd_osfs_lock */
708                 unstable = ofd->ofd_osfs_inflight - unstable;
709                 ofd->ofd_osfs_unstable = 0;
710                 if (unstable) {
711                         /* some writes completed while we were running statfs
712                          * w/o the ofd_osfs_lock. Those ones got added to
713                          * the cached statfs data that we are about to crunch.
714                          * Take them into account in the new statfs data */
715                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
716                                                unstable >> ofd->ofd_blockbits);
717                         /* However, we don't really know if those writes got
718                          * accounted in the statfs call, so tell
719                          * ofd_grant_space_left() there is some uncertainty
720                          * on the accounting of those writes.
721                          * The purpose is to prevent spurious error messages in
722                          * ofd_grant_space_left() since those writes might be
723                          * accounted twice. */
724                         ofd->ofd_osfs_unstable += unstable;
725                 }
726                 /* similarly, there is some uncertainty on write requests
727                  * between prepare & commit */
728                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
729                 spin_unlock(&ofd->ofd_grant_lock);
730
731                 /* finally udpate cached statfs data */
732                 ofd->ofd_osfs = *osfs;
733                 ofd->ofd_osfs_age = cfs_time_current_64();
734
735                 ofd->ofd_statfs_inflight--; /* stop tracking */
736                 if (ofd->ofd_statfs_inflight == 0)
737                         ofd->ofd_osfs_inflight = 0;
738                 spin_unlock(&ofd->ofd_osfs_lock);
739
740                 if (from_cache)
741                         *from_cache = 0;
742         } else {
743                 /* use cached statfs data */
744                 *osfs = ofd->ofd_osfs;
745                 spin_unlock(&ofd->ofd_osfs_lock);
746                 if (from_cache)
747                         *from_cache = 1;
748         }
749         return 0;
750 }
751
752 static int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
753                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
754 {
755         struct obd_device       *obd = class_exp2obd(exp);
756         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
757         int                      rc;
758
759         ENTRY;
760
761         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
762         if (unlikely(rc))
763                 GOTO(out, rc);
764
765         /* at least try to account for cached pages.  its still racy and
766          * might be under-reporting if clients haven't announced their
767          * caches with brw recently */
768
769         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
770                " pending "LPU64" free "LPU64" avail "LPU64"\n",
771                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
772                osfs->os_bfree << ofd->ofd_blockbits,
773                osfs->os_bavail << ofd->ofd_blockbits);
774
775         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
776                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
777                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
778
779         /* The QoS code on the MDS does not care about space reserved for
780          * precreate, so take it out. */
781         if (exp_connect_flags(exp) & OBD_CONNECT_MDS) {
782                 struct filter_export_data *fed;
783
784                 fed = &obd->obd_self_export->exp_filter_data;
785                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
786                                          fed->fed_grant >> ofd->ofd_blockbits);
787         }
788
789         ofd_grant_sanity_check(obd, __FUNCTION__);
790         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
791                LPU64" objects: "LPU64" free; state %x\n",
792                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
793                osfs->os_files, osfs->os_ffree, osfs->os_state);
794
795         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
796                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
797                 osfs->os_ffree = 0;
798
799         /* OS_STATE_READONLY can be set by OSD already */
800         if (ofd->ofd_raid_degraded)
801                 osfs->os_state |= OS_STATE_DEGRADED;
802
803         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
804                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
805                  * should not see a block size > page size, otherwise
806                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
807                  * block size which is the biggest block size known to work
808                  * with all client's page size. */
809                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
810                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
811                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
812                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
813         }
814
815         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
816                                  ofd->ofd_lut.lut_lsd.lsd_osd_index))
817                 osfs->os_bfree = osfs->os_bavail = 2;
818
819         EXIT;
820 out:
821         return rc;
822 }
823
824 int ofd_setattr(const struct lu_env *env, struct obd_export *exp,
825                 struct obd_info *oinfo, struct obd_trans_info *oti)
826 {
827         struct ofd_thread_info  *info;
828         struct ofd_device       *ofd = ofd_exp(exp);
829         struct ldlm_namespace   *ns = ofd->ofd_namespace;
830         struct ldlm_resource    *res;
831         struct ofd_object       *fo;
832         struct obdo             *oa = oinfo->oi_oa;
833         struct filter_fid       *ff = NULL;
834         int                      rc = 0;
835
836         ENTRY;
837
838         info = ofd_info_init(env, exp);
839         ofd_oti2info(info, oti);
840
841         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
842         if (rc != 0)
843                 RETURN(rc);
844
845         ost_fid_build_resid(&info->fti_fid, &info->fti_resid);
846         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oa->o_oi),
847                            oinfo_capa(oinfo), CAPA_OPC_META_WRITE);
848         if (rc)
849                 GOTO(out, rc);
850
851         /* This would be very bad - accidentally truncating a file when
852          * changing the time or similar - bug 12203. */
853         if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE &&
854             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
855                 static char mdsinum[48];
856
857                 if (oinfo->oi_oa->o_valid & OBD_MD_FLFID)
858                         snprintf(mdsinum, sizeof(mdsinum) - 1,
859                                  "of parent "DFID, oinfo->oi_oa->o_parent_seq,
860                                  oinfo->oi_oa->o_parent_oid, 0);
861                 else
862                         mdsinum[0] = '\0';
863
864                 CERROR("%s: setattr from %s trying to truncate object "DFID
865                        " %s\n", exp->exp_obd->obd_name,
866                        obd_export_nid2str(exp), PFID(&info->fti_fid), mdsinum);
867                 GOTO(out, rc = -EPERM);
868         }
869
870         fo = ofd_object_find(env, ofd, &info->fti_fid);
871         if (IS_ERR(fo)) {
872                 CERROR("%s: can't find object "DFID"\n",
873                        exp->exp_obd->obd_name, PFID(&info->fti_fid));
874                 GOTO(out, rc = PTR_ERR(fo));
875         }
876
877         la_from_obdo(&info->fti_attr, oinfo->oi_oa, oinfo->oi_oa->o_valid);
878         info->fti_attr.la_valid &= ~LA_TYPE;
879
880         if (oa->o_valid & OBD_MD_FLFID) {
881                 ff = &info->fti_mds_fid;
882                 ofd_prepare_fidea(ff, oa);
883         }
884
885         /* setting objects attributes (including owner/group) */
886         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
887         if (rc)
888                 GOTO(out_unlock, rc);
889
890         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
891         if (res != NULL) {
892                 ldlm_res_lvbo_update(res, NULL, 0);
893                 ldlm_resource_putref(res);
894         }
895
896         ofd_info2oti(info, oti);
897
898         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, oti->oti_jobid, 1);
899         EXIT;
900 out_unlock:
901         ofd_object_put(env, fo);
902 out:
903         return rc;
904 }
905
906 static int ofd_punch(const struct lu_env *env, struct obd_export *exp,
907                      struct obd_info *oinfo, struct obd_trans_info *oti,
908                      struct ptlrpc_request_set *rqset)
909 {
910         struct ofd_thread_info  *info;
911         struct ofd_device       *ofd = ofd_exp(exp);
912         struct ldlm_namespace   *ns = ofd->ofd_namespace;
913         struct ldlm_resource    *res;
914         struct ofd_object       *fo;
915         struct filter_fid       *ff = NULL;
916         int                      rc = 0;
917
918         ENTRY;
919
920         info = ofd_info_init(env, exp);
921         ofd_oti2info(info, oti);
922
923         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
924         if (rc != 0)
925                 RETURN(rc);
926         ost_fid_build_resid(&info->fti_fid, &info->fti_resid);
927
928         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
929                ", start = "LPD64", end = "LPD64"\n", PFID(&info->fti_fid),
930                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start,
931                oinfo->oi_policy.l_extent.end);
932
933         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oinfo->oi_oa->o_oi),
934                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
935         if (rc)
936                 GOTO(out_env, rc);
937
938         fo = ofd_object_find(env, ofd, &info->fti_fid);
939         if (IS_ERR(fo)) {
940                 CERROR("%s: error finding object "DFID": rc = %ld\n",
941                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
942                        PTR_ERR(fo));
943                 GOTO(out_env, rc = PTR_ERR(fo));
944         }
945
946         LASSERT(oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF);
947         if (oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF) {
948                 /* Truncate case */
949                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
950         } else if (oinfo->oi_policy.l_extent.end >= oinfo->oi_oa->o_size) {
951                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.end;
952         }
953
954         la_from_obdo(&info->fti_attr, oinfo->oi_oa,
955                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
956         info->fti_attr.la_valid &= ~LA_TYPE;
957         info->fti_attr.la_size = oinfo->oi_policy.l_extent.start;
958         info->fti_attr.la_valid |= LA_SIZE;
959
960         if (oinfo->oi_oa->o_valid & OBD_MD_FLFID) {
961                 ff = &info->fti_mds_fid;
962                 ofd_prepare_fidea(ff, oinfo->oi_oa);
963         }
964
965         rc = ofd_object_punch(env, fo, oinfo->oi_policy.l_extent.start,
966                               oinfo->oi_policy.l_extent.end, &info->fti_attr,
967                               ff);
968         if (rc)
969                 GOTO(out, rc);
970
971         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
972         if (res != NULL) {
973                 ldlm_res_lvbo_update(res, NULL, 0);
974                 ldlm_resource_putref(res);
975         }
976
977         oinfo->oi_oa->o_valid = OBD_MD_FLID;
978         /* Quota release needs uid/gid info */
979         rc = ofd_attr_get(env, fo, &info->fti_attr);
980         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
981                      OFD_VALID_FLAGS | LA_UID | LA_GID);
982         ofd_info2oti(info, oti);
983
984         ofd_counter_incr(exp, LPROC_OFD_STATS_PUNCH, oti->oti_jobid, 1);
985         EXIT;
986 out:
987         ofd_object_put(env, fo);
988 out_env:
989         return rc;
990 }
991
992 static int ofd_destroy_by_fid(const struct lu_env *env,
993                               struct ofd_device *ofd,
994                               const struct lu_fid *fid, int orphan)
995 {
996         struct ofd_thread_info  *info = ofd_info(env);
997         struct lustre_handle     lockh;
998         __u64                    flags = LDLM_FL_AST_DISCARD_DATA;
999         __u64                    rc = 0;
1000         ldlm_policy_data_t       policy = {
1001                                         .l_extent = { 0, OBD_OBJECT_EOF }
1002                                  };
1003         struct ofd_object       *fo;
1004
1005         ENTRY;
1006
1007         fo = ofd_object_find(env, ofd, fid);
1008         if (IS_ERR(fo))
1009                 RETURN(PTR_ERR(fo));
1010         if (!ofd_object_exists(fo))
1011                 GOTO(out, rc = -ENOENT);
1012
1013         /* Tell the clients that the object is gone now and that they should
1014          * throw away any cached pages. */
1015         ost_fid_build_resid(fid, &info->fti_resid);
1016         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
1017                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
1018                                     ldlm_blocking_ast, ldlm_completion_ast,
1019                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lockh);
1020
1021         /* We only care about the side-effects, just drop the lock. */
1022         if (rc == ELDLM_OK)
1023                 ldlm_lock_decref(&lockh, LCK_PW);
1024
1025         LASSERT(fo != NULL);
1026
1027         rc = ofd_object_destroy(env, fo, orphan);
1028         EXIT;
1029 out:
1030         ofd_object_put(env, fo);
1031         RETURN(rc);
1032 }
1033
1034 int ofd_destroy(const struct lu_env *env, struct obd_export *exp,
1035                 struct obdo *oa, struct lov_stripe_md *md,
1036                 struct obd_trans_info *oti, struct obd_export *md_exp,
1037                 void *capa)
1038 {
1039         struct ofd_device       *ofd = ofd_exp(exp);
1040         struct ofd_thread_info  *info;
1041         obd_count                count;
1042         int                      rc = 0;
1043
1044         ENTRY;
1045
1046         info = ofd_info_init(env, exp);
1047         ofd_oti2info(info, oti);
1048
1049         if (!(oa->o_valid & OBD_MD_FLGROUP))
1050                 ostid_set_seq_mdt0(&oa->o_oi);
1051
1052         /* check that o_misc makes sense */
1053         if (oa->o_valid & OBD_MD_FLOBJCOUNT)
1054                 count = oa->o_misc;
1055         else
1056                 count = 1; /* default case - single destroy */
1057
1058         /**
1059          * There can be sequence of objects to destroy. Therefore this request
1060          * may have multiple transaction involved in. It is OK, we need only
1061          * the highest used transno to be reported back in reply but not for
1062          * replays, they must report their transno
1063          */
1064         if (info->fti_transno == 0) /* not replay */
1065                 info->fti_mult_trans = 1;
1066
1067         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1068                POSTID(&oa->o_oi), count);
1069         while (count > 0) {
1070                 int lrc;
1071
1072                 lrc = ostid_to_fid(&info->fti_fid, &oa->o_oi, 0);
1073                 if (lrc != 0) {
1074                         if (rc == 0)
1075                                 rc = lrc;
1076                         GOTO(out, rc);
1077                 }
1078                 lrc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 0);
1079                 if (lrc == -ENOENT) {
1080                         CDEBUG(D_INODE,
1081                                "%s: destroying non-existent object "DFID"\n",
1082                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid));
1083                         /* rewrite rc with -ENOENT only if it is 0 */
1084                         if (rc == 0)
1085                                 rc = lrc;
1086                 } else if (lrc != 0) {
1087                         CERROR("%s: error destroying object "DFID": %d\n",
1088                                ofd_obd(ofd)->obd_name, PFID(&info->fti_fid),
1089                                rc);
1090                         rc = lrc;
1091                 }
1092                 count--;
1093                 ostid_inc_id(&oa->o_oi);
1094         }
1095
1096         /* if we have transaction then there were some deletions, we don't
1097          * need to return ENOENT in that case because it will not wait
1098          * for commit of these deletions. The ENOENT must be returned only
1099          * if there were no transations.
1100          */
1101         if (rc == -ENOENT) {
1102                 if (info->fti_transno != 0)
1103                         rc = 0;
1104         } else if (rc != 0) {
1105                 /*
1106                  * If we have at least one transaction then llog record
1107                  * on server will be removed upon commit, so for rc != 0
1108                  * we return no transno and llog record will be reprocessed.
1109                  */
1110                 info->fti_transno = 0;
1111         }
1112         ofd_info2oti(info, oti);
1113 out:
1114         RETURN(rc);
1115 }
1116
1117 static int ofd_orphans_destroy(const struct lu_env *env,
1118                                struct obd_export *exp, struct ofd_device *ofd,
1119                                struct obdo *oa)
1120 {
1121         struct ofd_thread_info  *info = ofd_info(env);
1122         obd_id                   last;
1123         int                      skip_orphan;
1124         int                      rc = 0;
1125         struct ost_id            oi = oa->o_oi;
1126         __u64                    end_id = ostid_id(&oa->o_oi);
1127         struct ofd_seq          *oseq;
1128
1129         ENTRY;
1130
1131         oseq = ofd_seq_get(ofd, ostid_seq(&oa->o_oi));
1132         if (oseq == NULL) {
1133                 CERROR("%s: Can not find seq for "DOSTID"\n",
1134                        ofd_name(ofd), POSTID(&oa->o_oi));
1135                 RETURN(-EINVAL);
1136         }
1137
1138         LASSERT(exp != NULL);
1139         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1140
1141         last = ofd_seq_last_oid(oseq);
1142         LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1143                  " to "DOSTID"\n", ofd_name(ofd), ostid_seq(&oa->o_oi),
1144                  end_id + 1, ostid_seq(&oa->o_oi), last);
1145
1146         for (ostid_set_id(&oi, last); ostid_id(&oi) > end_id;
1147                           ostid_dec_id(&oi)) {
1148                 rc = ostid_to_fid(&info->fti_fid, &oi, 0);
1149                 if (rc != 0)
1150                         GOTO(out_put, rc);
1151                 rc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 1);
1152                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
1153                         CEMERG("%s: error destroying precreated id "DOSTID
1154                                ": rc = %d\n", ofd_name(ofd), POSTID(&oi), rc);
1155                 if (!skip_orphan) {
1156                         ofd_seq_last_oid_set(oseq, ostid_id(&oi) - 1);
1157                         /* update last_id on disk periodically so that if we
1158                          * restart * we don't need to re-scan all of the just
1159                          * deleted objects. */
1160                         if ((ostid_id(&oi) & 511) == 0)
1161                                 ofd_seq_last_oid_write(env, ofd, oseq);
1162                 }
1163         }
1164         CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1165                ofd_obd(ofd)->obd_name, POSTID(&oa->o_oi));
1166         if (!skip_orphan) {
1167                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1168         } else {
1169                 /* don't reuse orphan object, return last used objid */
1170                 ostid_set_id(&oa->o_oi, last);
1171                 rc = 0;
1172         }
1173 out_put:
1174         ofd_seq_put(env, oseq);
1175         RETURN(rc);
1176 }
1177
1178 int ofd_create(const struct lu_env *env, struct obd_export *exp,
1179                struct obdo *oa, struct lov_stripe_md **ea,
1180                struct obd_trans_info *oti)
1181 {
1182         struct ofd_device       *ofd = ofd_exp(exp);
1183         struct ofd_thread_info  *info;
1184         obd_seq                 seq = ostid_seq(&oa->o_oi);
1185         struct ofd_seq          *oseq;
1186         int                     rc = 0, diff;
1187         int                     sync_trans = 0;
1188
1189         ENTRY;
1190
1191         info = ofd_info_init(env, exp);
1192         ofd_oti2info(info, oti);
1193
1194         LASSERT(ostid_seq(&oa->o_oi) >= FID_SEQ_OST_MDT0);
1195         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1196
1197         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1198
1199         oseq = ofd_seq_load(env, ofd, seq);
1200         if (IS_ERR(oseq)) {
1201                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1202                        ofd_name(ofd), seq, PTR_ERR(oseq));
1203                 RETURN(-EINVAL);
1204         }
1205
1206         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1207             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1208                 if (!ofd_obd(ofd)->obd_recovering ||
1209                     ostid_id(&oa->o_oi) > ofd_seq_last_oid(oseq)) {
1210                         CERROR("recreate objid "DOSTID" > last id "LPU64"\n",
1211                                POSTID(&oa->o_oi), ofd_seq_last_oid(oseq));
1212                         GOTO(out_nolock, rc = -EINVAL);
1213                 }
1214                 /* do nothing because we create objects during first write */
1215                 GOTO(out_nolock, rc = 0);
1216         }
1217         /* former ofd_handle_precreate */
1218         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1219             (oa->o_flags & OBD_FL_DELORPHAN)) {
1220                 /* destroy orphans */
1221                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1222                         CERROR("%s: dropping old orphan cleanup request\n",
1223                                ofd_name(ofd));
1224                         GOTO(out_nolock, rc = 0);
1225                 }
1226                 /* This causes inflight precreates to abort and drop lock */
1227                 oseq->os_destroys_in_progress = 1;
1228                 mutex_lock(&oseq->os_create_lock);
1229                 if (!oseq->os_destroys_in_progress) {
1230                         CERROR("%s:["LPU64"] destroys_in_progress already"
1231                                " cleared\n", exp->exp_obd->obd_name,
1232                                ostid_seq(&oa->o_oi));
1233                         ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1234                         GOTO(out, rc = 0);
1235                 }
1236                 diff = ostid_id(&oa->o_oi) - ofd_seq_last_oid(oseq);
1237                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1238                         ofd_seq_last_oid(oseq), diff);
1239                 if (-diff > OST_MAX_PRECREATE) {
1240                         /* FIXME: should reset precreate_next_id on MDS */
1241                         rc = 0;
1242                 } else if (diff < 0) {
1243                         rc = ofd_orphans_destroy(env, exp, ofd, oa);
1244                         oseq->os_destroys_in_progress = 0;
1245                 } else {
1246                         /* XXX: Used by MDS for the first time! */
1247                         oseq->os_destroys_in_progress = 0;
1248                 }
1249         } else {
1250                 mutex_lock(&oseq->os_create_lock);
1251                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1252                         CERROR("%s: dropping old precreate request\n",
1253                                 ofd_obd(ofd)->obd_name);
1254                         GOTO(out, rc = 0);
1255                 }
1256                 /* only precreate if seq is 0, IDIF or normal and also o_id
1257                  * must be specfied */
1258                 if ((!fid_seq_is_mdt(ostid_seq(&oa->o_oi)) &&
1259                      !fid_seq_is_norm(ostid_seq(&oa->o_oi)) &&
1260                      !fid_seq_is_idif(ostid_seq(&oa->o_oi))) ||
1261                                 ostid_id(&oa->o_oi) == 0) {
1262                         diff = 1; /* shouldn't we create this right now? */
1263                 } else {
1264                         diff = ostid_id(&oa->o_oi) - ofd_seq_last_oid(oseq);
1265                         /* Do sync create if the seq is about to used up */
1266                         if (fid_seq_is_idif(ostid_seq(&oa->o_oi)) ||
1267                             fid_seq_is_mdt0(ostid_seq(&oa->o_oi))) {
1268                                 if (unlikely(ostid_id(&oa->o_oi) >= IDIF_MAX_OID - 1))
1269                                         sync_trans = 1;
1270                         } else if (fid_seq_is_norm(ostid_seq(&oa->o_oi))) {
1271                                 if (unlikely(ostid_id(&oa->o_oi) >=
1272                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1273                                         sync_trans = 1;
1274                         } else {
1275                                 CERROR("%s : invalid o_seq "DOSTID": rc = %d\n",
1276                                        ofd_name(ofd), POSTID(&oa->o_oi), -EINVAL);
1277                                 GOTO(out, rc = -EINVAL);
1278                         }
1279                 }
1280         }
1281         if (diff > 0) {
1282                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1283                 obd_id           next_id;
1284                 int              created = 0;
1285                 int              count;
1286
1287                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1288                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1289                         /* don't enforce grant during orphan recovery */
1290                         rc = ofd_grant_create(env,
1291                                               ofd_obd(ofd)->obd_self_export,
1292                                               &diff);
1293                         if (rc) {
1294                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1295                                        "space for precreate (%d): rc = %d\n",
1296                                        ofd_name(ofd), diff, rc);
1297                                 diff = 0;
1298                         }
1299                 }
1300
1301                 /* This can happen if a new OST is formatted and installed
1302                  * in place of an old one at the same index.  Instead of
1303                  * precreating potentially millions of deleted old objects
1304                  * (possibly filling the OST), only precreate the last batch.
1305                  * LFSCK will eventually clean up any orphans. LU-14 */
1306                 if (diff > 5 * OST_MAX_PRECREATE) {
1307                         diff = OST_MAX_PRECREATE / 2;
1308                         LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1309                                       "larger than the LAST_ID "DOSTID", only "
1310                                       "precreating the last %u objects.\n",
1311                                       ofd_name(ofd), POSTID(&oa->o_oi),
1312                                       5 * OST_MAX_PRECREATE,
1313                                       POSTID(&oseq->os_oi), diff);
1314                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1315                 }
1316
1317                 while (diff > 0) {
1318                         next_id = ofd_seq_last_oid(oseq) + 1;
1319                         count = ofd_precreate_batch(ofd, diff);
1320
1321                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1322                                " at "LPU64"\n", ofd_obd(ofd)->obd_name,
1323                                count, ostid_seq(&oa->o_oi), next_id);
1324
1325                         if (cfs_time_after(jiffies, enough_time)) {
1326                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1327                                               " created at a rate of %d/s\n",
1328                                               ofd_obd(ofd)->obd_name,
1329                                               created, diff + created,
1330                                               created / DISK_TIMEOUT);
1331                                 break;
1332                         }
1333
1334                         rc = ofd_precreate_objects(env, ofd, next_id,
1335                                                    oseq, count, sync_trans);
1336                         if (rc > 0) {
1337                                 created += rc;
1338                                 diff -= rc;
1339                         } else if (rc < 0) {
1340                                 break;
1341                         }
1342                 }
1343                 if (created > 0)
1344                         /* some objects got created, we can return
1345                          * them, even if last creation failed */
1346                         rc = 0;
1347                 else
1348                         CERROR("%s: unable to precreate: rc = %d\n",
1349                                ofd_name(ofd), rc);
1350
1351                 ostid_set_id(&oa->o_oi, ofd_seq_last_oid(oseq));
1352                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1353
1354                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1355                     !(oa->o_flags & OBD_FL_DELORPHAN))
1356                         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export,
1357                                          rc);
1358         }
1359
1360         ofd_info2oti(info, oti);
1361 out:
1362         mutex_unlock(&oseq->os_create_lock);
1363 out_nolock:
1364         if (rc == 0 && ea != NULL) {
1365                 struct lov_stripe_md *lsm = *ea;
1366
1367                 lsm->lsm_oi = oa->o_oi;
1368         }
1369         ofd_seq_put(env, oseq);
1370         RETURN(rc);
1371 }
1372
1373 int ofd_getattr(const struct lu_env *env, struct obd_export *exp,
1374                 struct obd_info *oinfo)
1375 {
1376         struct ofd_device       *ofd = ofd_exp(exp);
1377         struct ofd_thread_info  *info;
1378         struct ofd_object       *fo;
1379         int                      rc = 0;
1380
1381         ENTRY;
1382
1383         info = ofd_info_init(env, exp);
1384
1385         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1386         if (rc != 0)
1387                 GOTO(out, rc);
1388         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oinfo->oi_oa->o_oi),
1389                            oinfo_capa(oinfo), CAPA_OPC_META_READ);
1390         if (rc)
1391                 GOTO(out, rc);
1392
1393         fo = ofd_object_find(env, ofd, &info->fti_fid);
1394         if (IS_ERR(fo))
1395                 GOTO(out, rc = PTR_ERR(fo));
1396         if (!ofd_object_exists(fo))
1397                 GOTO(out_put, rc = -ENOENT);
1398
1399         LASSERT(fo != NULL);
1400         rc = ofd_attr_get(env, fo, &info->fti_attr);
1401         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1402         if (rc == 0) {
1403                 __u64 curr_version;
1404
1405                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
1406                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1407
1408                 /* Store object version in reply */
1409                 curr_version = dt_version_get(env, ofd_object_child(fo));
1410                 if ((__s64)curr_version != -EOPNOTSUPP) {
1411                         oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
1412                         oinfo->oi_oa->o_data_version = curr_version;
1413                 }
1414         }
1415
1416 out_put:
1417         ofd_object_put(env, fo);
1418 out:
1419         RETURN(rc);
1420 }
1421
1422 static int ofd_sync(const struct lu_env *env, struct obd_export *exp,
1423                     struct obd_info *oinfo, obd_size start, obd_size end,
1424                     struct ptlrpc_request_set *set)
1425 {
1426         struct ofd_device       *ofd = ofd_exp(exp);
1427         struct ofd_thread_info  *info;
1428         struct ofd_object       *fo;
1429         int                      rc = 0;
1430
1431         ENTRY;
1432
1433         /* if no objid is specified, it means "sync whole filesystem" */
1434         if (oinfo->oi_oa == NULL || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
1435                 rc = dt_sync(env, ofd->ofd_osd);
1436                 GOTO(out, rc);
1437         }
1438
1439         info = ofd_info_init(env, exp);
1440         rc = ostid_to_fid(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1441         if (rc != 0)
1442                 GOTO(out, rc);
1443
1444         rc = ofd_auth_capa(exp, &info->fti_fid, ostid_seq(&oinfo->oi_oa->o_oi),
1445                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
1446         if (rc)
1447                 GOTO(out, rc);
1448
1449         fo = ofd_object_find(env, ofd, &info->fti_fid);
1450         if (IS_ERR(fo)) {
1451                 CERROR("%s: error finding object "DFID": rc = %ld\n",
1452                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
1453                        PTR_ERR(fo));
1454                 GOTO(out, rc = PTR_ERR(fo));
1455         }
1456
1457         if (!ofd_object_exists(fo))
1458                 GOTO(put, rc = -ENOENT);
1459
1460         if (dt_version_get(env, ofd_object_child(fo)) >
1461             ofd_obd(ofd)->obd_last_committed) {
1462                 rc = dt_object_sync(env, ofd_object_child(fo));
1463                 if (rc)
1464                         GOTO(put, rc);
1465         }
1466
1467         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1468         rc = ofd_attr_get(env, fo, &info->fti_attr);
1469         obdo_from_la(oinfo->oi_oa, &info->fti_attr, OFD_VALID_FLAGS);
1470
1471         ofd_counter_incr(exp, LPROC_OFD_STATS_SYNC, oinfo->oi_jobid, 1);
1472         EXIT;
1473 put:
1474         ofd_object_put(env, fo);
1475 out:
1476         return rc;
1477 }
1478
1479 static int ofd_ioc_get_obj_version(const struct lu_env *env,
1480                                    struct ofd_device *ofd, void *karg)
1481 {
1482         struct obd_ioctl_data *data = karg;
1483         struct lu_fid          fid;
1484         struct ofd_object     *fo;
1485         dt_obj_version_t       version;
1486         int                    rc = 0;
1487
1488         ENTRY;
1489
1490         if (data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
1491                 GOTO(out, rc = -EINVAL);
1492
1493         if (data->ioc_inlbuf1 != NULL && data->ioc_inllen1 == sizeof(fid)) {
1494                 fid = *(struct lu_fid *)data->ioc_inlbuf1;
1495         } else if (data->ioc_inlbuf3 != NULL &&
1496                    data->ioc_inllen3 == sizeof(__u64) &&
1497                    data->ioc_inlbuf4 != NULL &&
1498                    data->ioc_inllen4 == sizeof(__u64)) {
1499                 struct ost_id ostid;
1500
1501                 ostid_set_seq(&ostid, *(__u64 *)data->ioc_inlbuf4);
1502                 ostid_set_id(&ostid, *(__u64 *)data->ioc_inlbuf3);
1503                 rc = ostid_to_fid(&fid, &ostid, 0);
1504                 if (rc != 0)
1505                         GOTO(out, rc);
1506         } else {
1507                 GOTO(out, rc = -EINVAL);
1508         }
1509
1510         if (!fid_is_sane(&fid))
1511                 GOTO(out, rc = -EINVAL);
1512
1513         fo = ofd_object_find(env, ofd, &fid);
1514         if (IS_ERR(fo))
1515                 GOTO(out, rc = PTR_ERR(fo));
1516
1517         if (!ofd_object_exists(fo))
1518                 GOTO(out_fo, rc = -ENOENT);
1519
1520         if (lu_object_remote(&fo->ofo_obj.do_lu))
1521                 GOTO(out_fo, rc = -EREMOTE);
1522
1523         version = dt_version_get(env, ofd_object_child(fo));
1524         if (version == 0)
1525                 GOTO(out_fo, rc = -EIO);
1526
1527         *(dt_obj_version_t *)data->ioc_inlbuf2 = version;
1528
1529         EXIT;
1530 out_fo:
1531         ofd_object_put(env, fo);
1532 out:
1533         return rc;
1534 }
1535
1536 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1537                   void *karg, void *uarg)
1538 {
1539         struct lu_env            env;
1540         struct ofd_device       *ofd = ofd_exp(exp);
1541         struct obd_device       *obd = ofd_obd(ofd);
1542         int                      rc;
1543
1544         ENTRY;
1545
1546         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1547         rc = lu_env_init(&env, LCT_DT_THREAD);
1548         if (rc)
1549                 RETURN(rc);
1550
1551         switch (cmd) {
1552         case OBD_IOC_ABORT_RECOVERY:
1553                 CERROR("%s: aborting recovery\n", obd->obd_name);
1554                 target_stop_recovery_thread(obd);
1555                 break;
1556         case OBD_IOC_SYNC:
1557                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1558                 rc = dt_sync(&env, ofd->ofd_osd);
1559                 break;
1560         case OBD_IOC_SET_READONLY:
1561                 rc = dt_sync(&env, ofd->ofd_osd);
1562                 if (rc == 0)
1563                         rc = dt_ro(&env, ofd->ofd_osd);
1564                 break;
1565         case OBD_IOC_START_LFSCK: {
1566                 struct obd_ioctl_data *data = karg;
1567                 struct lfsck_start_param lsp;
1568
1569                 if (unlikely(data == NULL)) {
1570                         rc = -EINVAL;
1571                         break;
1572                 }
1573
1574                 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
1575                 lsp.lsp_namespace = ofd->ofd_namespace;
1576                 rc = lfsck_start(&env, ofd->ofd_osd, &lsp);
1577                 break;
1578         }
1579         case OBD_IOC_STOP_LFSCK: {
1580                 rc = lfsck_stop(&env, ofd->ofd_osd, false);
1581                 break;
1582         }
1583         case OBD_IOC_GET_OBJ_VERSION:
1584                 rc = ofd_ioc_get_obj_version(&env, ofd, karg);
1585                 break;
1586         default:
1587                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1588                 rc = -ENOTTY;
1589         }
1590
1591         lu_env_fini(&env);
1592         RETURN(rc);
1593 }
1594
1595 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1596 {
1597         int rc = 0;
1598
1599         ENTRY;
1600
1601         switch(stage) {
1602         case OBD_CLEANUP_EARLY:
1603                 break;
1604         case OBD_CLEANUP_EXPORTS:
1605                 target_cleanup_recovery(obd);
1606                 break;
1607         }
1608         RETURN(rc);
1609 }
1610
1611 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1612 {
1613         ofd_fmd_expire(exp);
1614         return 0;
1615 }
1616
1617 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1618 {
1619         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1620         struct ofd_thread_info  *info;
1621         struct lu_env            env;
1622 #ifdef USE_HEALTH_CHECK_WRITE
1623         struct thandle          *th;
1624 #endif
1625         int                      rc = 0;
1626
1627         /* obd_proc_read_health pass NULL env, we need real one */
1628         rc = lu_env_init(&env, LCT_DT_THREAD);
1629         if (rc)
1630                 RETURN(rc);
1631
1632         info = ofd_info_init(&env, NULL);
1633         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1634         if (unlikely(rc))
1635                 GOTO(out, rc);
1636
1637         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1638                 GOTO(out, rc = -EROFS);
1639
1640 #ifdef USE_HEALTH_CHECK_WRITE
1641         OBD_ALLOC(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1642         if (info->fti_buf.lb_buf == NULL)
1643                 GOTO(out, rc = -ENOMEM);
1644
1645         info->fti_buf.lb_len = PAGE_CACHE_SIZE;
1646         info->fti_off = 0;
1647
1648         th = dt_trans_create(&env, ofd->ofd_osd);
1649         if (IS_ERR(th))
1650                 GOTO(out, rc = PTR_ERR(th));
1651
1652         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1653                                      info->fti_buf.lb_len, info->fti_off, th);
1654         if (rc == 0) {
1655                 th->th_sync = 1; /* sync IO is needed */
1656                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1657                 if (rc == 0)
1658                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1659                                              &info->fti_buf, &info->fti_off,
1660                                              th);
1661         }
1662         dt_trans_stop(&env, ofd->ofd_osd, th);
1663
1664         OBD_FREE(info->fti_buf.lb_buf, PAGE_CACHE_SIZE);
1665
1666         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1667 #endif
1668 out:
1669         lu_env_fini(&env);
1670         return !!rc;
1671 }
1672
1673 /*
1674  * Handle quota control requests to consult current usage/limit.
1675  *
1676  * \param obd - is the obd device associated with the ofd
1677  * \param exp - is the client's export
1678  * \param oqctl - is the obd_quotactl request to be processed
1679  */
1680 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1681                         struct obd_quotactl *oqctl)
1682 {
1683         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1684         struct lu_env       env;
1685         int                 rc;
1686         ENTRY;
1687
1688         /* report success for quota on/off for interoperability with current MDT
1689          * stack */
1690         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1691                 RETURN(0);
1692
1693         rc = lu_env_init(&env, LCT_DT_THREAD);
1694         if (rc)
1695                 RETURN(rc);
1696
1697         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1698         lu_env_fini(&env);
1699
1700         RETURN(rc);
1701 }
1702
1703 struct obd_ops ofd_obd_ops = {
1704         .o_owner                = THIS_MODULE,
1705         .o_connect              = ofd_obd_connect,
1706         .o_reconnect            = ofd_obd_reconnect,
1707         .o_disconnect           = ofd_obd_disconnect,
1708         .o_set_info_async       = ofd_set_info_async,
1709         .o_get_info             = ofd_get_info,
1710         .o_create               = ofd_create,
1711         .o_statfs               = ofd_statfs,
1712         .o_setattr              = ofd_setattr,
1713         .o_preprw               = ofd_preprw,
1714         .o_commitrw             = ofd_commitrw,
1715         .o_destroy              = ofd_destroy,
1716         .o_init_export          = ofd_init_export,
1717         .o_destroy_export       = ofd_destroy_export,
1718         .o_postrecov            = ofd_obd_postrecov,
1719         .o_punch                = ofd_punch,
1720         .o_getattr              = ofd_getattr,
1721         .o_sync                 = ofd_sync,
1722         .o_iocontrol            = ofd_iocontrol,
1723         .o_precleanup           = ofd_precleanup,
1724         .o_ping                 = ofd_ping,
1725         .o_health_check         = ofd_health_check,
1726         .o_quotactl             = ofd_quotactl,
1727 };