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