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