Whamcloud - gitweb
c44524d8d378ff66e01b29a54b8d64ebec65d3df
[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) 2011, 2012, Whamcloud, Inc.
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 <lquota.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                 cfs_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                         lut_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, group;
277
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         group = 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 = lut_client_new(env, exp);
311                 if (rc != 0)
312                         GOTO(out, rc);
313                 ofd_export_stats_init(ofd, exp, localdata);
314         }
315         if (group == 0)
316                 GOTO(out, rc = 0);
317
318         /* init new group */
319         if (group > ofd->ofd_max_group) {
320                 ofd->ofd_max_group = group;
321                 rc = ofd_group_load(env, ofd, group);
322         }
323 out:
324         if (rc != 0) {
325                 class_disconnect(exp);
326                 *_exp = NULL;
327         } else {
328                 *_exp = exp;
329         }
330         RETURN(rc);
331 }
332
333 static int ofd_obd_disconnect(struct obd_export *exp)
334 {
335         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
336         struct lu_env            env;
337         int                      rc;
338
339         ENTRY;
340
341         LASSERT(exp);
342         class_export_get(exp);
343
344         if (!(exp->exp_flags & OBD_OPT_FORCE))
345                 ofd_grant_sanity_check(ofd_obd(ofd), __FUNCTION__);
346
347         rc = server_disconnect_export(exp);
348
349         ofd_grant_discard(exp);
350
351         rc = lu_env_init(&env, LCT_DT_THREAD);
352         if (rc)
353                 RETURN(rc);
354
355         /* Do not erase record for recoverable client. */
356         if (exp->exp_obd->obd_replayable &&
357             (!exp->exp_obd->obd_fail || exp->exp_failed))
358                 lut_client_del(&env, exp);
359         lu_env_fini(&env);
360
361         class_export_put(exp);
362         RETURN(rc);
363 }
364
365 static int ofd_init_export(struct obd_export *exp)
366 {
367         int rc;
368
369         cfs_spin_lock_init(&exp->exp_filter_data.fed_lock);
370         CFS_INIT_LIST_HEAD(&exp->exp_filter_data.fed_mod_list);
371         cfs_spin_lock(&exp->exp_lock);
372         exp->exp_connecting = 1;
373         cfs_spin_unlock(&exp->exp_lock);
374
375         /* self-export doesn't need client data and ldlm initialization */
376         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
377                                      &exp->exp_client_uuid)))
378                 return 0;
379
380         rc = lut_client_alloc(exp);
381         if (rc == 0)
382                 ldlm_init_export(exp);
383         if (rc)
384                 CERROR("%s: Can't initialize export: rc %d\n",
385                        exp->exp_obd->obd_name, rc);
386         return rc;
387 }
388
389 static int ofd_destroy_export(struct obd_export *exp)
390 {
391         struct ofd_device *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
392
393         if (exp->exp_filter_data.fed_pending)
394                 CERROR("%s: cli %s/%p has %lu pending on destroyed export"
395                        "\n", exp->exp_obd->obd_name, exp->exp_client_uuid.uuid,
396                        exp, exp->exp_filter_data.fed_pending);
397
398         target_destroy_export(exp);
399
400         if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
401                                      &exp->exp_client_uuid)))
402                 return 0;
403
404         ldlm_destroy_export(exp);
405         lut_client_free(exp);
406
407         ofd_fmd_cleanup(exp);
408
409         /*
410          * discard grants once we're sure no more
411          * interaction with the client is possible
412          */
413         ofd_grant_discard(exp);
414         ofd_fmd_cleanup(exp);
415
416         if (exp->exp_connect_flags & OBD_CONNECT_GRANT_SHRINK) {
417                 if (ofd->ofd_tot_granted_clients > 0)
418                         ofd->ofd_tot_granted_clients --;
419         }
420
421         if (!(exp->exp_flags & OBD_OPT_FORCE))
422                 ofd_grant_sanity_check(exp->exp_obd, __FUNCTION__);
423
424         LASSERT(cfs_list_empty(&exp->exp_filter_data.fed_mod_list));
425         return 0;
426 }
427
428 int ofd_postrecov(const struct lu_env *env, struct ofd_device *ofd)
429 {
430         struct lu_device *ldev = &ofd->ofd_dt_dev.dd_lu_dev;
431
432         CDEBUG(D_HA, "%s: recovery is over\n", ofd_obd(ofd)->obd_name);
433         return ldev->ld_ops->ldo_recovery_complete(env, ldev);
434 }
435
436 int ofd_obd_postrecov(struct obd_device *obd)
437 {
438         struct lu_env            env;
439         struct lu_device        *ldev = obd->obd_lu_dev;
440         int                      rc;
441
442         ENTRY;
443
444         rc = lu_env_init(&env, LCT_DT_THREAD);
445         if (rc)
446                 RETURN(rc);
447         ofd_info_init(&env, obd->obd_self_export);
448
449         rc = ofd_postrecov(&env, ofd_dev(ldev));
450
451         lu_env_fini(&env);
452         RETURN(rc);
453 }
454
455 static int ofd_adapt_sptlrpc_conf(const struct lu_env *env,
456                                   struct obd_device *obd, int initial)
457 {
458         struct filter_obd       *fo = &obd->u.filter;
459         struct sptlrpc_rule_set  tmp_rset;
460         int                      rc;
461
462         sptlrpc_rule_set_init(&tmp_rset);
463         rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
464         if (rc) {
465                 CERROR("%s: failed get sptlrpc rules: rc = %d\n",
466                        obd->obd_name, rc);
467                 return rc;
468         }
469
470         sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
471
472         cfs_write_lock(&fo->fo_sptlrpc_lock);
473         sptlrpc_rule_set_free(&fo->fo_sptlrpc_rset);
474         fo->fo_sptlrpc_rset = tmp_rset;
475         cfs_write_unlock(&fo->fo_sptlrpc_lock);
476
477         return 0;
478 }
479
480 static int ofd_set_mds_conn(struct obd_export *exp, void *val)
481 {
482         int rc = 0;
483
484         ENTRY;
485
486         LCONSOLE_WARN("%s: received MDS connection from %s\n",
487                       exp->exp_obd->obd_name, obd_export_nid2str(exp));
488         RETURN(rc);
489 }
490
491 static int ofd_set_info_async(const struct lu_env *env, struct obd_export *exp,
492                               __u32 keylen, void *key, __u32 vallen, void *val,
493                               struct ptlrpc_request_set *set)
494 {
495         struct ofd_device       *ofd = ofd_exp(exp);
496         int                      rc = 0;
497
498         ENTRY;
499
500         if (exp->exp_obd == NULL) {
501                 CDEBUG(D_IOCTL, "invalid export %p\n", exp);
502                 RETURN(-EINVAL);
503         }
504
505         if (KEY_IS(KEY_CAPA_KEY)) {
506                 rc = ofd_update_capa_key(ofd, val);
507                 if (rc)
508                         CERROR("%s: update capability key failed: rc = %d\n",
509                                exp->exp_obd->obd_name, rc);
510         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
511                 ofd_adapt_sptlrpc_conf(env, exp->exp_obd, 0);
512         } else if (KEY_IS(KEY_MDS_CONN)) {
513                 rc = ofd_set_mds_conn(exp, val);
514         } else if (KEY_IS(KEY_GRANT_SHRINK)) {
515                 struct ost_body *body = val;
516
517                 /** handle grant shrink, similar to a read request */
518                 ofd_grant_prepare_read(env, exp, &body->oa);
519         } else {
520                 CERROR("%s: Unsupported key %s\n",
521                        exp->exp_obd->obd_name, (char*)key);
522                 rc = -EOPNOTSUPP;
523         }
524
525         RETURN(rc);
526 }
527
528 static int ofd_get_info(const struct lu_env *env, struct obd_export *exp,
529                         __u32 keylen, void *key, __u32 *vallen, void *val,
530                         struct lov_stripe_md *lsm)
531 {
532         struct ofd_device       *ofd = ofd_exp(exp);
533         int                      rc = 0;
534
535         ENTRY;
536
537         if (exp->exp_obd == NULL) {
538                 CDEBUG(D_IOCTL, "invalid client export %p\n", exp);
539                 RETURN(-EINVAL);
540         }
541
542         if (KEY_IS(KEY_BLOCKSIZE)) {
543                 __u32 *blocksize = val;
544                 if (blocksize) {
545                         if (*vallen < sizeof(*blocksize))
546                                 RETURN(-EOVERFLOW);
547                         *blocksize = 1 << ofd->ofd_dt_conf.ddp_block_shift;
548                 }
549                 *vallen = sizeof(*blocksize);
550         } else if (KEY_IS(KEY_BLOCKSIZE_BITS)) {
551                 __u32 *blocksize_bits = val;
552                 if (blocksize_bits) {
553                         if (*vallen < sizeof(*blocksize_bits))
554                                 RETURN(-EOVERFLOW);
555                         *blocksize_bits = ofd->ofd_dt_conf.ddp_block_shift;
556                 }
557                 *vallen = sizeof(*blocksize_bits);
558         } else if (KEY_IS(KEY_LAST_ID)) {
559                 obd_id *last_id = val;
560                 if (last_id) {
561                         if (*vallen < sizeof(*last_id))
562                                 RETURN(-EOVERFLOW);
563                         *last_id = ofd_last_id(ofd,
564                                                exp->exp_filter_data.fed_group);
565                 }
566                 *vallen = sizeof(*last_id);
567         } else if (KEY_IS(KEY_FIEMAP)) {
568                 struct ofd_thread_info          *info;
569                 struct ofd_device               *ofd = ofd_exp(exp);
570                 struct ofd_object               *fo;
571                 struct ll_fiemap_info_key       *fm_key = key;
572
573                 if (val == NULL) {
574                         *vallen = fiemap_count_to_size(
575                                                fm_key->fiemap.fm_extent_count);
576                         RETURN(0);
577                 }
578
579                 info = ofd_info_init(env, exp);
580
581                 fid_ostid_unpack(&info->fti_fid, &fm_key->oa.o_oi, 0);
582
583                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n",
584                        PFID(&info->fti_fid));
585
586                 fo = ofd_object_find(env, ofd, &info->fti_fid);
587                 if (IS_ERR(fo)) {
588                         CERROR("%s: error finding object "DFID"\n",
589                                exp->exp_obd->obd_name, PFID(&info->fti_fid));
590                         rc = PTR_ERR(fo);
591                 } else {
592                         struct ll_user_fiemap *fiemap = val;
593
594                         ofd_read_lock(env, fo);
595                         if (ofd_object_exists(fo)) {
596                                 *fiemap = fm_key->fiemap;
597                                 rc = dt_fiemap_get(env,
598                                                    ofd_object_child(fo),
599                                                    fiemap);
600                         } else {
601                                 rc = -ENOENT;
602                         }
603                         ofd_read_unlock(env, fo);
604                         ofd_object_put(env, fo);
605                 }
606         } else if (KEY_IS(KEY_SYNC_LOCK_CANCEL)) {
607                 *((__u32 *) val) = ofd->ofd_sync_lock_cancel;
608                 *vallen = sizeof(__u32);
609         } else {
610                 CERROR("Not supported key %s\n", (char*)key);
611                 rc = -EOPNOTSUPP;
612         }
613
614         RETURN(rc);
615 }
616
617 /** helper function for statfs, also used by grant code */
618 int ofd_statfs_internal(const struct lu_env *env, struct ofd_device *ofd,
619                         struct obd_statfs *osfs, __u64 max_age, int *from_cache)
620 {
621         int rc;
622
623         cfs_spin_lock(&ofd->ofd_osfs_lock);
624         if (cfs_time_before_64(ofd->ofd_osfs_age, max_age) || max_age == 0) {
625                 obd_size unstable;
626
627                 /* statfs data are too old, get up-to-date one.
628                  * we must be cautious here since multiple threads might be
629                  * willing to update statfs data concurrently and we must
630                  * grant that cached statfs data are always consistent */
631
632                 if (ofd->ofd_statfs_inflight == 0)
633                         /* clear inflight counter if no users, although it would
634                          * take a while to overflow this 64-bit counter ... */
635                         ofd->ofd_osfs_inflight = 0;
636                 /* notify ofd_grant_commit() that we want to track writes
637                  * completed as of now */
638                 ofd->ofd_statfs_inflight++;
639                 /* record value of inflight counter before running statfs to
640                  * compute the diff once statfs is completed */
641                 unstable = ofd->ofd_osfs_inflight;
642                 cfs_spin_unlock(&ofd->ofd_osfs_lock);
643
644                 /* statfs can sleep ... hopefully not for too long since we can
645                  * call it fairly often as space fills up */
646                 rc = dt_statfs(env, ofd->ofd_osd, osfs);
647                 if (unlikely(rc))
648                         return rc;
649
650                 cfs_spin_lock(&ofd->ofd_grant_lock);
651                 cfs_spin_lock(&ofd->ofd_osfs_lock);
652                 /* calculate how much space was written while we released the
653                  * ofd_osfs_lock */
654                 unstable = ofd->ofd_osfs_inflight - unstable;
655                 ofd->ofd_osfs_unstable = 0;
656                 if (unstable) {
657                         /* some writes completed while we were running statfs
658                          * w/o the ofd_osfs_lock. Those ones got added to
659                          * the cached statfs data that we are about to crunch.
660                          * Take them into account in the new statfs data */
661                         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
662                                                unstable >> ofd->ofd_blockbits);
663                         /* However, we don't really know if those writes got
664                          * accounted in the statfs call, so tell
665                          * ofd_grant_space_left() there is some uncertainty
666                          * on the accounting of those writes.
667                          * The purpose is to prevent spurious error messages in
668                          * ofd_grant_space_left() since those writes might be
669                          * accounted twice. */
670                         ofd->ofd_osfs_unstable += unstable;
671                 }
672                 /* similarly, there is some uncertainty on write requests
673                  * between prepare & commit */
674                 ofd->ofd_osfs_unstable += ofd->ofd_tot_pending;
675                 cfs_spin_unlock(&ofd->ofd_grant_lock);
676
677                 /* finally udpate cached statfs data */
678                 ofd->ofd_osfs = *osfs;
679                 ofd->ofd_osfs_age = cfs_time_current_64();
680
681                 ofd->ofd_statfs_inflight--; /* stop tracking */
682                 if (ofd->ofd_statfs_inflight == 0)
683                         ofd->ofd_osfs_inflight = 0;
684                 cfs_spin_unlock(&ofd->ofd_osfs_lock);
685
686                 if (from_cache)
687                         *from_cache = 0;
688         } else {
689                 /* use cached statfs data */
690                 *osfs = ofd->ofd_osfs;
691                 cfs_spin_unlock(&ofd->ofd_osfs_lock);
692                 if (from_cache)
693                         *from_cache = 1;
694         }
695         return 0;
696 }
697
698 static int ofd_statfs(const struct lu_env *env,  struct obd_export *exp,
699                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
700 {
701         struct obd_device       *obd = class_exp2obd(exp);
702         struct ofd_device       *ofd = ofd_dev(exp->exp_obd->obd_lu_dev);
703         int                      rc;
704
705         ENTRY;
706
707         rc = ofd_statfs_internal(env, ofd, osfs, max_age, NULL);
708         if (unlikely(rc))
709                 GOTO(out, rc);
710
711         /* at least try to account for cached pages.  its still racy and
712          * might be under-reporting if clients haven't announced their
713          * caches with brw recently */
714
715         CDEBUG(D_SUPER | D_CACHE, "blocks cached "LPU64" granted "LPU64
716                " pending "LPU64" free "LPU64" avail "LPU64"\n",
717                ofd->ofd_tot_dirty, ofd->ofd_tot_granted, ofd->ofd_tot_pending,
718                osfs->os_bfree << ofd->ofd_blockbits,
719                osfs->os_bavail << ofd->ofd_blockbits);
720
721         osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
722                                  ((ofd->ofd_tot_dirty + ofd->ofd_tot_pending +
723                                    osfs->os_bsize - 1) >> ofd->ofd_blockbits));
724
725         /* The QoS code on the MDS does not care about space reserved for
726          * precreate, so take it out. */
727         if (exp->exp_connect_flags & OBD_CONNECT_MDS) {
728                 struct filter_export_data *fed;
729
730                 fed = &obd->obd_self_export->exp_filter_data;
731                 osfs->os_bavail -= min_t(obd_size, osfs->os_bavail,
732                                          fed->fed_grant >> ofd->ofd_blockbits);
733         }
734
735         ofd_grant_sanity_check(obd, __FUNCTION__);
736         CDEBUG(D_CACHE, LPU64" blocks: "LPU64" free, "LPU64" avail; "
737                LPU64" objects: "LPU64" free; state %x\n",
738                osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
739                osfs->os_files, osfs->os_ffree, osfs->os_state);
740
741         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOINO,
742                                  ofd->ofd_lut.lut_lsd.lsd_ost_index))
743                 osfs->os_ffree = 0;
744
745         /* OS_STATE_READONLY can be set by OSD already */
746         if (ofd->ofd_raid_degraded)
747                 osfs->os_state |= OS_STATE_DEGRADED;
748
749         if (obd->obd_self_export != exp && ofd_grant_compat(exp, ofd)) {
750                 /* clients which don't support OBD_CONNECT_GRANT_PARAM
751                  * should not see a block size > page size, otherwise
752                  * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
753                  * block size which is the biggest block size known to work
754                  * with all client's page size. */
755                 osfs->os_blocks <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
756                 osfs->os_bfree  <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
757                 osfs->os_bavail <<= ofd->ofd_blockbits - COMPAT_BSIZE_SHIFT;
758                 osfs->os_bsize    = 1 << COMPAT_BSIZE_SHIFT;
759         }
760
761         if (OBD_FAIL_CHECK_VALUE(OBD_FAIL_OST_ENOSPC,
762                                  ofd->ofd_lut.lut_lsd.lsd_ost_index))
763                 osfs->os_bfree = osfs->os_bavail = 2;
764
765         EXIT;
766 out:
767         return rc;
768 }
769
770 int ofd_setattr(const struct lu_env *env, struct obd_export *exp,
771                 struct obd_info *oinfo, struct obd_trans_info *oti)
772 {
773         struct ofd_thread_info  *info;
774         struct ofd_device       *ofd = ofd_exp(exp);
775         struct ldlm_namespace   *ns = ofd->ofd_namespace;
776         struct ldlm_resource    *res;
777         struct ofd_object       *fo;
778         struct obdo             *oa = oinfo->oi_oa;
779         struct filter_fid       *ff = NULL;
780         int                      rc = 0;
781
782         ENTRY;
783
784         info = ofd_info_init(env, exp);
785         ofd_oti2info(info, oti);
786
787         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
788         ofd_build_resid(&info->fti_fid, &info->fti_resid);
789
790         rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
791                            oinfo_capa(oinfo), CAPA_OPC_META_WRITE);
792         if (rc)
793                 GOTO(out, rc);
794
795         /* This would be very bad - accidentally truncating a file when
796          * changing the time or similar - bug 12203. */
797         if (oinfo->oi_oa->o_valid & OBD_MD_FLSIZE &&
798             oinfo->oi_policy.l_extent.end != OBD_OBJECT_EOF) {
799                 static char mdsinum[48];
800
801                 if (oinfo->oi_oa->o_valid & OBD_MD_FLFID)
802                         snprintf(mdsinum, sizeof(mdsinum) - 1,
803                                  "of parent "DFID, oinfo->oi_oa->o_parent_seq,
804                                  oinfo->oi_oa->o_parent_oid, 0);
805                 else
806                         mdsinum[0] = '\0';
807
808                 CERROR("%s: setattr from %s trying to truncate object "DFID
809                        " %s\n", exp->exp_obd->obd_name,
810                        obd_export_nid2str(exp), PFID(&info->fti_fid), mdsinum);
811                 GOTO(out, rc = -EPERM);
812         }
813
814         fo = ofd_object_find(env, ofd, &info->fti_fid);
815         if (IS_ERR(fo)) {
816                 CERROR("%s: can't find object "DFID"\n",
817                        exp->exp_obd->obd_name, PFID(&info->fti_fid));
818                 GOTO(out, rc = PTR_ERR(fo));
819         }
820
821         la_from_obdo(&info->fti_attr, oinfo->oi_oa, oinfo->oi_oa->o_valid);
822         info->fti_attr.la_valid &= ~LA_TYPE;
823
824         if (oa->o_valid & OBD_MD_FLFID) {
825                 ff = &info->fti_mds_fid;
826                 ofd_prepare_fidea(ff, oa);
827         }
828
829         /* setting objects attributes (including owner/group) */
830         rc = ofd_attr_set(env, fo, &info->fti_attr, ff);
831         if (rc)
832                 GOTO(out_unlock, rc);
833
834         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
835         if (res != NULL) {
836                 ldlm_res_lvbo_update(res, NULL, 0);
837                 ldlm_resource_putref(res);
838         }
839
840         oinfo->oi_oa->o_valid = OBD_MD_FLID;
841
842         /* Quota release needs uid/gid info */
843         rc = ofd_attr_get(env, fo, &info->fti_attr);
844         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
845                      OFD_VALID_FLAGS | LA_UID | LA_GID);
846         ofd_info2oti(info, oti);
847
848         ofd_counter_incr(exp, LPROC_OFD_STATS_SETATTR, oti->oti_jobid, 1);
849         EXIT;
850 out_unlock:
851         ofd_object_put(env, fo);
852 out:
853         return rc;
854 }
855
856 static int ofd_punch(const struct lu_env *env, struct obd_export *exp,
857                      struct obd_info *oinfo, struct obd_trans_info *oti,
858                      struct ptlrpc_request_set *rqset)
859 {
860         struct ofd_thread_info  *info;
861         struct ofd_device       *ofd = ofd_exp(exp);
862         struct ldlm_namespace   *ns = ofd->ofd_namespace;
863         struct ldlm_resource    *res;
864         struct ofd_object       *fo;
865         struct filter_fid       *ff = NULL;
866         int                      rc = 0;
867
868         ENTRY;
869
870         info = ofd_info_init(env, exp);
871         ofd_oti2info(info, oti);
872
873         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
874         ofd_build_resid(&info->fti_fid, &info->fti_resid);
875
876         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
877                ", start = "LPD64", end = "LPD64"\n", PFID(&info->fti_fid),
878                oinfo->oi_oa->o_valid, oinfo->oi_policy.l_extent.start,
879                oinfo->oi_policy.l_extent.end);
880
881         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
882                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
883         if (rc)
884                 GOTO(out_env, rc);
885
886         fo = ofd_object_find(env, ofd, &info->fti_fid);
887         if (IS_ERR(fo)) {
888                 CERROR("%s: error finding object "DFID": rc = %ld\n",
889                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
890                        PTR_ERR(fo));
891                 GOTO(out_env, rc = PTR_ERR(fo));
892         }
893
894         LASSERT(oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF);
895         if (oinfo->oi_policy.l_extent.end == OBD_OBJECT_EOF) {
896                 /* Truncate case */
897                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.start;
898         } else if (oinfo->oi_policy.l_extent.end >= oinfo->oi_oa->o_size) {
899                 oinfo->oi_oa->o_size = oinfo->oi_policy.l_extent.end;
900         }
901
902         la_from_obdo(&info->fti_attr, oinfo->oi_oa,
903                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
904         info->fti_attr.la_valid &= ~LA_TYPE;
905         info->fti_attr.la_size = oinfo->oi_policy.l_extent.start;
906         info->fti_attr.la_valid |= LA_SIZE;
907
908         if (oinfo->oi_oa->o_valid & OBD_MD_FLFID) {
909                 ff = &info->fti_mds_fid;
910                 ofd_prepare_fidea(ff, oinfo->oi_oa);
911         }
912
913         rc = ofd_object_punch(env, fo, oinfo->oi_policy.l_extent.start,
914                               oinfo->oi_policy.l_extent.end, &info->fti_attr,
915                               ff);
916         if (rc)
917                 GOTO(out, rc);
918
919         res = ldlm_resource_get(ns, NULL, &info->fti_resid, LDLM_EXTENT, 0);
920         if (res != NULL) {
921                 ldlm_res_lvbo_update(res, NULL, 0);
922                 ldlm_resource_putref(res);
923         }
924
925         oinfo->oi_oa->o_valid = OBD_MD_FLID;
926         /* Quota release needs uid/gid info */
927         rc = ofd_attr_get(env, fo, &info->fti_attr);
928         obdo_from_la(oinfo->oi_oa, &info->fti_attr,
929                      OFD_VALID_FLAGS | LA_UID | LA_GID);
930         ofd_info2oti(info, oti);
931
932         ofd_counter_incr(exp, LPROC_OFD_STATS_PUNCH, oti->oti_jobid, 1);
933         EXIT;
934 out:
935         ofd_object_put(env, fo);
936 out_env:
937         return rc;
938 }
939
940 static int ofd_destroy_by_fid(const struct lu_env *env,
941                               struct ofd_device *ofd,
942                               const struct lu_fid *fid, int orphan)
943 {
944         struct ofd_thread_info  *info = ofd_info(env);
945         struct lustre_handle     lockh;
946         int                      flags = LDLM_AST_DISCARD_DATA, rc = 0;
947         ldlm_policy_data_t       policy = {
948                                         .l_extent = { 0, OBD_OBJECT_EOF }
949                                  };
950         struct ofd_object       *fo;
951
952         ENTRY;
953
954         /* Tell the clients that the object is gone now and that they should
955          * throw away any cached pages. */
956         ofd_build_resid(fid, &info->fti_resid);
957         rc = ldlm_cli_enqueue_local(ofd->ofd_namespace, &info->fti_resid,
958                                     LDLM_EXTENT, &policy, LCK_PW, &flags,
959                                     ldlm_blocking_ast, ldlm_completion_ast,
960                                     NULL, NULL, 0, NULL, &lockh);
961
962         /* We only care about the side-effects, just drop the lock. */
963         if (rc == ELDLM_OK)
964                 ldlm_lock_decref(&lockh, LCK_PW);
965
966         fo = ofd_object_find(env, ofd, fid);
967         if (IS_ERR(fo))
968                 RETURN(PTR_ERR(fo));
969         LASSERT(fo != NULL);
970
971         rc = ofd_object_destroy(env, fo, orphan);
972
973         ofd_object_put(env, fo);
974         RETURN(rc);
975 }
976
977 int ofd_destroy(const struct lu_env *env, struct obd_export *exp,
978                 struct obdo *oa, struct lov_stripe_md *md,
979                 struct obd_trans_info *oti, struct obd_export *md_exp,
980                 void *capa)
981 {
982         struct ofd_device       *ofd = ofd_exp(exp);
983         struct ofd_thread_info  *info;
984         obd_count                count;
985         int                      rc = 0;
986
987         ENTRY;
988
989         info = ofd_info_init(env, exp);
990         ofd_oti2info(info, oti);
991
992         if (!(oa->o_valid & OBD_MD_FLGROUP))
993                 oa->o_seq = 0;
994
995         /* check that o_misc makes sense */
996         if (oa->o_valid & OBD_MD_FLOBJCOUNT)
997                 count = oa->o_misc;
998         else
999                 count = 1; /* default case - single destroy */
1000
1001         /**
1002          * There can be sequence of objects to destroy. Therefore this request
1003          * may have multiple transaction involved in. It is OK, we need only
1004          * the highest used transno to be reported back in reply but not for
1005          * replays, they must report their transno
1006          */
1007         if (info->fti_transno == 0) /* not replay */
1008                 info->fti_mult_trans = 1;
1009         while (count > 0) {
1010                 int lrc;
1011
1012                 fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
1013                 lrc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 0);
1014                 if (lrc == -ENOENT) {
1015                         CDEBUG(D_INODE,
1016                                "destroying non-existent object "LPU64"\n",
1017                                oa->o_id);
1018                         /* rewrite rc with -ENOENT only if it is 0 */
1019                         if (rc == 0)
1020                                 rc = lrc;
1021                 } else if (lrc != 0) {
1022                         CEMERG("error destroying object "LPU64": %d\n",
1023                                oa->o_id, rc);
1024                         rc = lrc;
1025                 }
1026                 count--;
1027                 oa->o_id++;
1028         }
1029
1030         /* if we have transaction then there were some deletions, we don't
1031          * need to return ENOENT in that case because it will not wait
1032          * for commit of these deletions. The ENOENT must be returned only
1033          * if there were no transations.
1034          */
1035         if (rc == -ENOENT) {
1036                 if (info->fti_transno != 0)
1037                         rc = 0;
1038         } else if (rc != 0) {
1039                 /*
1040                  * If we have at least one transaction then llog record
1041                  * on server will be removed upon commit, so for rc != 0
1042                  * we return no transno and llog record will be reprocessed.
1043                  */
1044                 info->fti_transno = 0;
1045         }
1046         ofd_info2oti(info, oti);
1047         RETURN(rc);
1048 }
1049
1050 static int ofd_orphans_destroy(const struct lu_env *env,
1051                                struct obd_export *exp, struct ofd_device *ofd,
1052                                struct obdo *oa)
1053 {
1054         struct ofd_thread_info  *info = ofd_info(env);
1055         obd_id                   last;
1056         int                      skip_orphan;
1057         int                      rc = 0;
1058         struct ost_id            oi = oa->o_oi;
1059
1060         ENTRY;
1061
1062         LASSERT(exp != NULL);
1063         skip_orphan = !!(exp->exp_connect_flags & OBD_CONNECT_SKIP_ORPHAN);
1064
1065         last = ofd_last_id(ofd, oa->o_seq);
1066         CWARN("%s: deleting orphan objects from "LPU64" to "LPU64"\n",
1067               ofd_obd(ofd)->obd_name, oa->o_id + 1, last);
1068
1069         for (oi.oi_id = last; oi.oi_id > oa->o_id; oi.oi_id--) {
1070                 fid_ostid_unpack(&info->fti_fid, &oi, 0);
1071                 rc = ofd_destroy_by_fid(env, ofd, &info->fti_fid, 1);
1072                 if (rc && rc != -ENOENT) /* this is pretty fatal... */
1073                         CEMERG("error destroying precreated id "LPU64": %d\n",
1074                                oi.oi_id, rc);
1075                 if (!skip_orphan) {
1076                         ofd_last_id_set(ofd, oi.oi_id - 1, oa->o_seq);
1077                         /* update last_id on disk periodically so that if we
1078                          * restart * we don't need to re-scan all of the just
1079                          * deleted objects. */
1080                         if ((oi.oi_id & 511) == 0)
1081                                 ofd_last_id_write(env, ofd, oa->o_seq);
1082                 }
1083         }
1084         CDEBUG(D_HA, "%s: after destroy: set last_objids["LPU64"] = "LPU64"\n",
1085                ofd_obd(ofd)->obd_name, oa->o_seq, oa->o_id);
1086         if (!skip_orphan) {
1087                 rc = ofd_last_id_write(env, ofd, oa->o_seq);
1088         } else {
1089                 /* don't reuse orphan object, return last used objid */
1090                 oa->o_id = last;
1091                 rc = 0;
1092         }
1093         RETURN(rc);
1094 }
1095
1096 int ofd_create(const struct lu_env *env, struct obd_export *exp,
1097                struct obdo *oa, struct lov_stripe_md **ea,
1098                struct obd_trans_info *oti)
1099 {
1100         struct ofd_device       *ofd = ofd_exp(exp);
1101         struct ofd_thread_info  *info;
1102         int                      rc = 0, diff;
1103
1104         ENTRY;
1105
1106         info = ofd_info_init(env, exp);
1107         ofd_oti2info(info, oti);
1108
1109         LASSERT(oa->o_seq >= FID_SEQ_OST_MDT0);
1110         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1111
1112         CDEBUG(D_INFO, "ofd_create(oa->o_seq="LPU64",oa->o_id="LPU64")\n",
1113                oa->o_seq, oa->o_id);
1114
1115         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1116             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1117                 if (!ofd_obd(ofd)->obd_recovering ||
1118                     oa->o_id > ofd_last_id(ofd, oa->o_seq)) {
1119                         CERROR("recreate objid "LPU64" > last id "LPU64"\n",
1120                                         oa->o_id, ofd_last_id(ofd, oa->o_seq));
1121                         GOTO(out_nolock, rc = -EINVAL);
1122                 }
1123                 /* do nothing because we create objects during first write */
1124                 GOTO(out_nolock, rc = 0);
1125         }
1126         /* former ofd_handle_precreate */
1127         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1128             (oa->o_flags & OBD_FL_DELORPHAN)) {
1129                 /* destroy orphans */
1130                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1131                         CERROR("%s: dropping old orphan cleanup request\n",
1132                                ofd_obd(ofd)->obd_name);
1133                         GOTO(out_nolock, rc = 0);
1134                 }
1135                 /* This causes inflight precreates to abort and drop lock */
1136                 cfs_set_bit(oa->o_seq, &ofd->ofd_destroys_in_progress);
1137                 cfs_mutex_lock(&ofd->ofd_create_locks[oa->o_seq]);
1138                 if (!cfs_test_bit(oa->o_seq, &ofd->ofd_destroys_in_progress)) {
1139                         CERROR("%s:["LPU64"] destroys_in_progress already cleared\n",
1140                                exp->exp_obd->obd_name, oa->o_seq);
1141                         GOTO(out, rc = 0);
1142                 }
1143                 diff = oa->o_id - ofd_last_id(ofd, oa->o_seq);
1144                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1145                        ofd_last_id(ofd, oa->o_seq), diff);
1146                 if (-diff > OST_MAX_PRECREATE) {
1147                         /* FIXME: should reset precreate_next_id on MDS */
1148                         rc = 0;
1149                 } else if (diff < 0) {
1150                         rc = ofd_orphans_destroy(env, exp, ofd, oa);
1151                         cfs_clear_bit(oa->o_seq, &ofd->ofd_destroys_in_progress);
1152                 } else {
1153                         /* XXX: Used by MDS for the first time! */
1154                         cfs_clear_bit(oa->o_seq, &ofd->ofd_destroys_in_progress);
1155                 }
1156         } else {
1157                 cfs_mutex_lock(&ofd->ofd_create_locks[oa->o_seq]);
1158                 if (oti->oti_conn_cnt < exp->exp_conn_cnt) {
1159                         CERROR("%s: dropping old precreate request\n",
1160                                ofd_obd(ofd)->obd_name);
1161                         GOTO(out, rc = 0);
1162                 }
1163                 /* only precreate if group == 0 and o_id is specfied */
1164                 if (!fid_seq_is_mdt(oa->o_seq) || oa->o_id == 0) {
1165                         diff = 1; /* shouldn't we create this right now? */
1166                 } else {
1167                         diff = oa->o_id - ofd_last_id(ofd, oa->o_seq);
1168                 }
1169         }
1170         if (diff > 0) {
1171                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1172                 obd_id           next_id;
1173                 int              created = 0;
1174                 int              count;
1175
1176                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1177                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1178                         /* don't enforce grant during orphan recovery */
1179                         rc = ofd_grant_create(env,
1180                                               ofd_obd(ofd)->obd_self_export,
1181                                               &diff);
1182                         if (rc) {
1183                                 CDEBUG(D_HA, "%s: failed to acquire grant space"
1184                                        "for precreate (%d)\n",
1185                                        ofd_obd(ofd)->obd_name, diff);
1186                                 diff = 0;
1187                         }
1188                 }
1189
1190                 while (diff > 0) {
1191                         next_id = ofd_last_id(ofd, oa->o_seq) + 1;
1192                         count = ofd_precreate_batch(ofd, diff);
1193
1194                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPU64
1195                                " at "LPU64"\n", ofd_obd(ofd)->obd_name,
1196                                count, oa->o_seq, next_id);
1197
1198                         if (cfs_time_after(jiffies, enough_time)) {
1199                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1200                                               " created at a rate of %d/s\n",
1201                                               ofd_obd(ofd)->obd_name,
1202                                               created, diff + created,
1203                                               created / DISK_TIMEOUT);
1204                                 break;
1205                 }
1206
1207                         rc = ofd_precreate_objects(env, ofd, next_id,
1208                                                    oa->o_seq, count);
1209                         if (rc > 0) {
1210                                 created += rc;
1211                                 diff -= rc;
1212                         } else if (rc < 0) {
1213                                 break;
1214                         }
1215                 }
1216                 if (created > 0) {
1217                         /* some objects got created, we can return
1218                          * them, even if last creation failed */
1219                         oa->o_id = ofd_last_id(ofd, oa->o_seq);
1220                         rc = 0;
1221                 } else {
1222                         CERROR("unable to precreate: %d\n", rc);
1223                         oa->o_id = ofd_last_id(ofd, oa->o_seq);
1224                 }
1225
1226                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1227
1228                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1229                     !(oa->o_flags & OBD_FL_DELORPHAN))
1230                         ofd_grant_commit(env, ofd_obd(ofd)->obd_self_export,
1231                                          rc);
1232         }
1233
1234         ofd_info2oti(info, oti);
1235 out:
1236         cfs_mutex_unlock(&ofd->ofd_create_locks[oa->o_seq]);
1237 out_nolock:
1238         if (rc == 0 && ea != NULL) {
1239                 struct lov_stripe_md *lsm = *ea;
1240
1241                 lsm->lsm_object_id = oa->o_id;
1242         }
1243         return rc;
1244 }
1245
1246 int ofd_getattr(const struct lu_env *env, struct obd_export *exp,
1247                 struct obd_info *oinfo)
1248 {
1249         struct ofd_device       *ofd = ofd_exp(exp);
1250         struct ofd_thread_info  *info;
1251         struct ofd_object       *fo;
1252         __u64                    curr_version;
1253         int                      rc = 0;
1254
1255         ENTRY;
1256
1257         info = ofd_info_init(env, exp);
1258
1259         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1260         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1261                            oinfo_capa(oinfo), CAPA_OPC_META_READ);
1262         if (rc)
1263                 GOTO(out, rc);
1264
1265         fo = ofd_object_find(env, ofd, &info->fti_fid);
1266         if (IS_ERR(fo))
1267                 GOTO(out, rc = PTR_ERR(fo));
1268         LASSERT(fo != NULL);
1269         rc = ofd_attr_get(env, fo, &info->fti_attr);
1270         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1271         if (rc == 0)
1272                 obdo_from_la(oinfo->oi_oa, &info->fti_attr,
1273                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1274
1275         /* Store object version in reply */
1276         curr_version = dt_version_get(env, ofd_object_child(fo));
1277         if ((__s64)curr_version != -EOPNOTSUPP) {
1278                 oinfo->oi_oa->o_valid |= OBD_MD_FLDATAVERSION;
1279                 oinfo->oi_oa->o_data_version = curr_version;
1280         }
1281         ofd_object_put(env, fo);
1282 out:
1283         RETURN(rc);
1284 }
1285
1286 static int ofd_sync(const struct lu_env *env, struct obd_export *exp,
1287                     struct obd_info *oinfo, obd_size start, obd_size end,
1288                     struct ptlrpc_request_set *set)
1289 {
1290         struct ofd_device       *ofd = ofd_exp(exp);
1291         struct ofd_thread_info  *info;
1292         struct ofd_object       *fo;
1293         int                      rc = 0;
1294
1295         ENTRY;
1296
1297         /* if no objid is specified, it means "sync whole filesystem" */
1298         if (oinfo->oi_oa == NULL || !(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
1299                 rc = dt_sync(env, ofd->ofd_osd);
1300                 GOTO(out, rc);
1301         }
1302
1303         info = ofd_info_init(env, exp);
1304         fid_ostid_unpack(&info->fti_fid, &oinfo->oi_oa->o_oi, 0);
1305
1306         rc = ofd_auth_capa(exp, &info->fti_fid, oinfo->oi_oa->o_seq,
1307                            oinfo_capa(oinfo), CAPA_OPC_OSS_TRUNC);
1308         if (rc)
1309                 GOTO(out, rc);
1310
1311         fo = ofd_object_find(env, ofd, &info->fti_fid);
1312         if (IS_ERR(fo)) {
1313                 CERROR("%s: error finding object "DFID": rc = %ld\n",
1314                        exp->exp_obd->obd_name, PFID(&info->fti_fid),
1315                        PTR_ERR(fo));
1316                 GOTO(out, rc = PTR_ERR(fo));
1317         }
1318
1319         if (!ofd_object_exists(fo))
1320                 GOTO(put, rc = -ENOENT);
1321
1322         if (dt_version_get(env, ofd_object_child(fo)) >
1323             ofd_obd(ofd)->obd_last_committed) {
1324                 rc = dt_object_sync(env, ofd_object_child(fo));
1325                 if (rc)
1326                         GOTO(put, rc);
1327         }
1328
1329         oinfo->oi_oa->o_valid = OBD_MD_FLID;
1330         rc = ofd_attr_get(env, fo, &info->fti_attr);
1331         obdo_from_la(oinfo->oi_oa, &info->fti_attr, OFD_VALID_FLAGS);
1332
1333         ofd_counter_incr(exp, LPROC_OFD_STATS_SYNC, oinfo->oi_jobid, 1);
1334         EXIT;
1335 put:
1336         ofd_object_put(env, fo);
1337 out:
1338         return rc;
1339 }
1340
1341 int ofd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1342                   void *karg, void *uarg)
1343 {
1344         struct lu_env            env;
1345         struct ofd_device       *ofd = ofd_exp(exp);
1346         struct obd_device       *obd = ofd_obd(ofd);
1347         int                      rc;
1348
1349         ENTRY;
1350
1351         CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1352         rc = lu_env_init(&env, LCT_LOCAL);
1353         if (rc)
1354                 RETURN(rc);
1355
1356         switch (cmd) {
1357         case OBD_IOC_ABORT_RECOVERY:
1358                 CERROR("%s: aborting recovery\n", obd->obd_name);
1359                 target_stop_recovery_thread(obd);
1360                 break;
1361         case OBD_IOC_SYNC:
1362                 CDEBUG(D_RPCTRACE, "syncing ost %s\n", obd->obd_name);
1363                 rc = dt_sync(&env, ofd->ofd_osd);
1364                 break;
1365         case OBD_IOC_SET_READONLY:
1366                 rc = dt_sync(&env, ofd->ofd_osd);
1367                 if (rc == 0)
1368                         rc = dt_ro(&env, ofd->ofd_osd);
1369                 break;
1370         default:
1371                 CERROR("%s: not supported cmd = %d\n", obd->obd_name, cmd);
1372                 rc = -ENOTTY;
1373         }
1374
1375         lu_env_fini(&env);
1376         RETURN(rc);
1377 }
1378
1379 static int ofd_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
1380 {
1381         int rc = 0;
1382
1383         ENTRY;
1384
1385         switch(stage) {
1386         case OBD_CLEANUP_EARLY:
1387                 break;
1388         case OBD_CLEANUP_EXPORTS:
1389                 target_cleanup_recovery(obd);
1390                 break;
1391         }
1392         RETURN(rc);
1393 }
1394
1395 static int ofd_ping(const struct lu_env *env, struct obd_export *exp)
1396 {
1397         ofd_fmd_expire(exp);
1398         return 0;
1399 }
1400
1401 static int ofd_health_check(const struct lu_env *nul, struct obd_device *obd)
1402 {
1403         struct ofd_device       *ofd = ofd_dev(obd->obd_lu_dev);
1404         struct ofd_thread_info  *info;
1405         struct lu_env            env;
1406 #ifdef USE_HEALTH_CHECK_WRITE
1407         struct thandle          *th;
1408 #endif
1409         int                      rc = 0;
1410
1411         /* obd_proc_read_health pass NULL env, we need real one */
1412         rc = lu_env_init(&env, LCT_DT_THREAD);
1413         if (rc)
1414                 RETURN(rc);
1415
1416         info = ofd_info_init(&env, NULL);
1417         rc = dt_statfs(&env, ofd->ofd_osd, &info->fti_u.osfs);
1418         if (unlikely(rc))
1419                 GOTO(out, rc);
1420
1421         if (info->fti_u.osfs.os_state == OS_STATE_READONLY)
1422                 GOTO(out, rc = -EROFS);
1423
1424 #ifdef USE_HEALTH_CHECK_WRITE
1425         OBD_ALLOC(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1426         if (info->fti_buf.lb_buf == NULL)
1427                 GOTO(out, rc = -ENOMEM);
1428
1429         info->fti_buf.lb_len = CFS_PAGE_SIZE;
1430         info->fti_off = 0;
1431
1432         th = dt_trans_create(&env, ofd->ofd_osd);
1433         if (IS_ERR(th))
1434                 GOTO(out, rc = PTR_ERR(th));
1435
1436         rc = dt_declare_record_write(&env, ofd->ofd_health_check_file,
1437                                      info->fti_buf.lb_len, info->fti_off, th);
1438         if (rc == 0) {
1439                 th->th_sync = 1; /* sync IO is needed */
1440                 rc = dt_trans_start_local(&env, ofd->ofd_osd, th);
1441                 if (rc == 0)
1442                         rc = dt_record_write(&env, ofd->ofd_health_check_file,
1443                                              &info->fti_buf, &info->fti_off,
1444                                              th);
1445         }
1446         dt_trans_stop(&env, ofd->ofd_osd, th);
1447
1448         OBD_FREE(info->fti_buf.lb_buf, CFS_PAGE_SIZE);
1449
1450         CDEBUG(D_INFO, "write 1 page synchronously for checking io rc %d\n",rc);
1451 #endif
1452 out:
1453         lu_env_fini(&env);
1454         return !!rc;
1455 }
1456
1457 static int ofd_obd_notify(struct obd_device *obd, struct obd_device *unused,
1458                           enum obd_notify_event ev, void *data)
1459 {
1460         switch (ev) {
1461         case OBD_NOTIFY_CONFIG:
1462                 LASSERT(obd->obd_no_conn);
1463                 cfs_spin_lock(&obd->obd_dev_lock);
1464                 obd->obd_no_conn = 0;
1465                 cfs_spin_unlock(&obd->obd_dev_lock);
1466                 break;
1467         default:
1468                 CDEBUG(D_INFO, "%s: Unhandled notification %#x\n",
1469                        obd->obd_name, ev);
1470         }
1471         return 0;
1472 }
1473
1474 /*
1475  * Handle quota control requests to consult current usage/limit.
1476  *
1477  * \param obd - is the obd device associated with the ofd
1478  * \param exp - is the client's export
1479  * \param oqctl - is the obd_quotactl request to be processed
1480  */
1481 static int ofd_quotactl(struct obd_device *obd, struct obd_export *exp,
1482                         struct obd_quotactl *oqctl)
1483 {
1484         struct ofd_device  *ofd = ofd_dev(obd->obd_lu_dev);
1485         struct lu_env       env;
1486         int                 rc;
1487         ENTRY;
1488
1489         /* report success for quota on/off for interoperability with current MDT
1490          * stack */
1491         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1492                 RETURN(0);
1493
1494         rc = lu_env_init(&env, LCT_DT_THREAD);
1495         if (rc)
1496                 RETURN(rc);
1497
1498         rc = lquotactl_slv(&env, ofd->ofd_osd, oqctl);
1499         lu_env_fini(&env);
1500
1501         RETURN(rc);
1502 }
1503
1504 struct obd_ops ofd_obd_ops = {
1505         .o_owner                = THIS_MODULE,
1506         .o_connect              = ofd_obd_connect,
1507         .o_reconnect            = ofd_obd_reconnect,
1508         .o_disconnect           = ofd_obd_disconnect,
1509         .o_set_info_async       = ofd_set_info_async,
1510         .o_get_info             = ofd_get_info,
1511         .o_create               = ofd_create,
1512         .o_statfs               = ofd_statfs,
1513         .o_setattr              = ofd_setattr,
1514         .o_preprw               = ofd_preprw,
1515         .o_commitrw             = ofd_commitrw,
1516         .o_destroy              = ofd_destroy,
1517         .o_init_export          = ofd_init_export,
1518         .o_destroy_export       = ofd_destroy_export,
1519         .o_postrecov            = ofd_obd_postrecov,
1520         .o_punch                = ofd_punch,
1521         .o_getattr              = ofd_getattr,
1522         .o_sync                 = ofd_sync,
1523         .o_iocontrol            = ofd_iocontrol,
1524         .o_precleanup           = ofd_precleanup,
1525         .o_ping                 = ofd_ping,
1526         .o_health_check         = ofd_health_check,
1527         .o_notify               = ofd_obd_notify,
1528         .o_quotactl             = ofd_quotactl,
1529 };