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