Whamcloud - gitweb
LU-14487 modules: remove references to Sun Trademark.
[fs/lustre-release.git] / lustre / osd-zfs / osd_oi.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/osd-zfs/osd_oi.c
32  * OI functions to map fid to dnode
33  *
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mike Pershin <tappro@whamcloud.com>
36  * Author: Di Wang <di.wang@intel.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_OSD
40
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48
49 #include "osd_internal.h"
50
51 #include <sys/dnode.h>
52 #include <sys/dbuf.h>
53 #include <sys/spa.h>
54 #include <sys/stat.h>
55 #include <sys/zap.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
62 #include <sys/txg.h>
63 #include <lustre_scrub.h>
64
65 #define OSD_OI_FID_NR         (1UL << 7)
66 unsigned int osd_oi_count = OSD_OI_FID_NR;
67
68
69 /*
70  * zfs osd maintains names for known fids in the name hierarchy
71  * so that one can mount filesystem with regular ZFS stack and
72  * access files
73  */
74 struct named_oid {
75         unsigned long    oid;
76         char            *name;
77 };
78
79 static const struct named_oid oids[] = {
80         { .oid = LAST_RECV_OID,        .name = LAST_RCVD },
81         { .oid = OFD_LAST_GROUP_OID,   .name = "LAST_GROUP" },
82         { .oid = LLOG_CATALOGS_OID,    .name = "CATALOGS" },
83         { .oid = MGS_CONFIGS_OID,      /*MOUNT_CONFIGS_DIR*/ },
84         { .oid = FID_SEQ_SRV_OID,      .name = "seq_srv" },
85         { .oid = FID_SEQ_CTL_OID,      .name = "seq_ctl" },
86         { .oid = FLD_INDEX_OID,        .name = "fld" },
87         { .oid = MDD_LOV_OBJ_OID,      .name = LOV_OBJID },
88         { .oid = OFD_HEALTH_CHECK_OID, .name = HEALTH_CHECK },
89         { .oid = REPLY_DATA_OID,       .name = REPLY_DATA },
90         { .oid = MDD_LOV_OBJ_OSEQ,     .name = LOV_OBJSEQ },
91         { .oid = BATCHID_COMMITTED_OID, .name = "BATCHID" },
92         { .oid = 0 }
93 };
94
95 static inline bool fid_is_objseq(const struct lu_fid *fid)
96 {
97         return fid->f_seq == FID_SEQ_LOCAL_FILE &&
98                 fid->f_oid == MDD_LOV_OBJ_OSEQ;
99 }
100
101 static inline bool fid_is_batchid(const struct lu_fid *fid)
102 {
103         return fid->f_seq == FID_SEQ_LOCAL_FILE &&
104                 fid->f_oid == BATCHID_COMMITTED_OID;
105 }
106
107 static char *oid2name(const unsigned long oid)
108 {
109         int i = 0;
110
111         while (oids[i].oid) {
112                 if (oids[i].oid == oid)
113                         return oids[i].name;
114                 i++;
115         }
116         return NULL;
117 }
118
119 /**
120  * Lookup an existing OI by the given name.
121  */
122 static int
123 osd_oi_lookup(const struct lu_env *env, struct osd_device *o,
124               uint64_t parent, const char *name, struct osd_oi *oi)
125 {
126         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
127         int                      rc;
128
129         rc = -zap_lookup(o->od_os, parent, name, 8, 1, (void *)zde);
130         if (rc)
131                 return rc;
132
133         rc = strlcpy(oi->oi_name, name, sizeof(oi->oi_name));
134         if (rc >= sizeof(oi->oi_name))
135                 return -E2BIG;
136
137         oi->oi_zapid = zde->zde_dnode;
138
139         return 0;
140 }
141
142 static int osd_obj_create(const struct lu_env *env, struct osd_device *o,
143                           uint64_t parent, const char *name, uint64_t *child,
144                           const struct lu_fid *fid, bool isdir)
145 {
146         struct osd_thread_info *info = osd_oti_get(env);
147         struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
148         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
149         struct lu_attr *la = &info->oti_la;
150         sa_handle_t *sa_hdl = NULL;
151         nvlist_t *nvbuf = NULL;
152         dmu_tx_t *tx;
153         uint64_t oid;
154         __u32 compat = LMAC_NOT_IN_OI;
155         int rc;
156         ENTRY;
157
158         if (o->od_dt_dev.dd_rdonly)
159                 RETURN(-EROFS);
160
161         memset(la, 0, sizeof(*la));
162         la->la_valid = LA_MODE | LA_UID | LA_GID;
163         la->la_mode = S_IRUGO | S_IWUSR | (isdir ? S_IXUGO | S_IFDIR : S_IFREG);
164
165         if (fid) {
166                 rc = -nvlist_alloc(&nvbuf, NV_UNIQUE_NAME, KM_SLEEP);
167                 if (rc)
168                         RETURN(rc);
169
170                 if (o->od_is_ost)
171                         compat |= LMAC_FID_ON_OST;
172                 lustre_lma_init(lma, fid, compat, 0);
173                 lustre_lma_swab(lma);
174                 rc = -nvlist_add_byte_array(nvbuf, XATTR_NAME_LMA,
175                                             (uchar_t *)lma, sizeof(*lma));
176                 if (rc)
177                         GOTO(out, rc);
178         }
179
180         /* create fid-to-dnode index */
181         tx = dmu_tx_create(o->od_os);
182         if (!tx)
183                 GOTO(out, rc = -ENOMEM);
184
185         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
186         dmu_tx_hold_bonus(tx, parent);
187         dmu_tx_hold_zap(tx, parent, TRUE, name);
188         dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
189         rc = -dmu_tx_assign(tx, TXG_WAIT);
190         if (rc) {
191                 dmu_tx_abort(tx);
192                 GOTO(out, rc);
193         }
194
195         if (isdir)
196                 oid = osd_zap_create_flags(o->od_os, 0, ZAP_FLAG_HASH64,
197                                            DMU_OT_DIRECTORY_CONTENTS,
198                                            14, DN_MAX_INDBLKSHIFT, 0, tx);
199         else
200                 oid = osd_dmu_object_alloc(o->od_os, DMU_OTN_UINT8_METADATA,
201                                            0, 0, tx);
202         rc = -sa_handle_get(o->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
203         if (rc)
204                 GOTO(commit, rc);
205
206         rc = __osd_attr_init(env, o, NULL, sa_hdl, tx, la, parent, nvbuf);
207         sa_handle_destroy(sa_hdl);
208         if (rc)
209                 GOTO(commit, rc);
210
211         zde->zde_dnode = oid;
212         zde->zde_pad = 0;
213         zde->zde_type = S_DT(isdir ? S_IFDIR : S_IFREG);
214         rc = -zap_add(o->od_os, parent, name, 8, 1, (void *)zde, tx);
215
216         GOTO(commit, rc);
217
218 commit:
219         if (rc)
220                 dmu_object_free(o->od_os, oid, tx);
221         else
222                 *child = oid;
223         dmu_tx_commit(tx);
224 out:
225         if (nvbuf)
226                 nvlist_free(nvbuf);
227         return rc;
228 }
229
230 static int osd_oi_destroy(const struct lu_env *env, struct osd_device *o,
231                           const char *name)
232 {
233         struct osd_oi oi;
234         dmu_tx_t *tx;
235         dnode_t *rootdn;
236         uint64_t oid;
237         int rc;
238
239         ENTRY;
240
241         if (o->od_dt_dev.dd_rdonly)
242                 RETURN(-EROFS);
243
244         rc = osd_oi_lookup(env, o, o->od_rootid, name, &oi);
245         if (rc == -ENOENT)
246                 RETURN(0);
247         if (rc)
248                 RETURN(rc);
249
250         oid = oi.oi_zapid;
251
252         rc = __osd_obj2dnode(o->od_os, o->od_rootid, &rootdn);
253         if (rc)
254                 RETURN(rc);
255
256         tx = dmu_tx_create(o->od_os);
257         dmu_tx_mark_netfree(tx);
258         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
259         osd_tx_hold_zap(tx, oid, rootdn, FALSE, NULL);
260         rc = -dmu_tx_assign(tx, TXG_WAIT);
261         if (rc) {
262                 dmu_tx_abort(tx);
263                 GOTO(out, rc);
264         }
265
266         rc = -dmu_object_free(o->od_os, oid, tx);
267         if (rc) {
268                 CERROR("%s: failed to free %s %llu: rc = %d\n",
269                        o->od_svname, name, oid, rc);
270                 GOTO(commit, rc);
271         }
272
273         rc = osd_zap_remove(o, o->od_rootid, rootdn, name, tx);
274         if (rc) {
275                 CERROR("%s: zap_remove %s failed: rc = %d\n",
276                        o->od_svname, name, rc);
277                 GOTO(commit, rc);
278         }
279
280         EXIT;
281 commit:
282         dmu_tx_commit(tx);
283 out:
284         osd_dnode_rele(rootdn);
285
286         return rc;
287 }
288
289 static int
290 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
291                       uint64_t parent, const char *name, uint64_t *child)
292 {
293         struct osd_oi   oi;
294         int             rc;
295
296         rc = osd_oi_lookup(env, o, parent, name, &oi);
297         if (rc == 0)
298                 *child = oi.oi_zapid;
299         else if (rc == -ENOENT)
300                 rc = osd_obj_create(env, o, parent, name, child, NULL, true);
301
302         return rc;
303 }
304
305 int osd_obj_find_or_create(const struct lu_env *env, struct osd_device *o,
306                            uint64_t parent, const char *name, uint64_t *child,
307                            const struct lu_fid *fid, bool isdir)
308 {
309         struct osd_oi oi;
310         int rc;
311
312         rc = osd_oi_lookup(env, o, parent, name, &oi);
313         if (!rc)
314                 *child = oi.oi_zapid;
315         else if (rc == -ENOENT)
316                 rc = osd_obj_create(env, o, parent, name, child, fid, isdir);
317
318         return rc;
319 }
320
321 /**
322  * Lookup the target index/flags of the fid, so it will know where
323  * the object is located (tgt index) and it is MDT or OST object.
324  */
325 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
326                    u64 seq, struct lu_seq_range *range)
327 {
328         struct seq_server_site  *ss = osd_seq_site(osd);
329
330         if (fid_seq_is_idif(seq)) {
331                 fld_range_set_ost(range);
332                 range->lsr_index = idif_ost_idx(seq);
333                 return 0;
334         }
335
336         if (!fid_seq_in_fldb(seq)) {
337                 fld_range_set_mdt(range);
338                 if (ss != NULL)
339                         /* FIXME: If ss is NULL, it suppose not get lsr_index
340                          * at all */
341                         range->lsr_index = ss->ss_node_id;
342                 return 0;
343         }
344
345         /* The seq_server_site may be NOT ready during initial OI scrub */
346         if (unlikely(!ss || !ss->ss_server_fld ||
347                      !ss->ss_server_fld->lsf_cache))
348                 return -ENOENT;
349
350         fld_range_set_any(range);
351         /* OSD will only do local fld lookup */
352         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
353 }
354
355 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
356                   const struct lu_fid *fid)
357 {
358         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
359         int                     rc;
360         ENTRY;
361
362         if (fid_is_idif(fid))
363                 RETURN(1);
364
365         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
366                      fid_is_name_llog(fid) || fid_is_quota(fid) ||
367                      fid_is_igif(fid))
368                 RETURN(0);
369
370         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
371         if (rc != 0) {
372                 /* During upgrade, OST FLDB might not be loaded because
373                  * OST FLDB is not created until 2.6, so if some DNE
374                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
375                  * not be able to find the sequence from local FLDB
376                  * cache see fld_index_init(). */
377                 if (rc == -ENOENT && osd->od_is_ost)
378                         RETURN(1);
379
380                 if (rc != -ENOENT)
381                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
382                                osd_name(osd), PFID(fid), rc);
383                 RETURN(0);
384         }
385
386         if (fld_range_is_ost(range))
387                 RETURN(1);
388
389         RETURN(0);
390 }
391
392 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
393                                            u64 seq)
394 {
395         struct osd_seq *osd_seq;
396
397         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
398                 if (osd_seq->os_seq == seq)
399                         return osd_seq;
400         }
401         return NULL;
402 }
403
404 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
405 {
406         struct osd_seq *osd_seq;
407
408         read_lock(&seq_list->osl_seq_list_lock);
409         osd_seq = osd_seq_find_locked(seq_list, seq);
410         read_unlock(&seq_list->osl_seq_list_lock);
411
412         return osd_seq;
413 }
414
415 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
416                                            struct osd_device *osd, u64 seq)
417 {
418         struct osd_seq_list     *seq_list = &osd->od_seq_list;
419         struct osd_seq          *osd_seq;
420         char                    *key = osd_oti_get(env)->oti_buf;
421         char                    *seq_name = osd_oti_get(env)->oti_str;
422         struct osd_oi           oi;
423         uint64_t                sdb, odb;
424         int                     i;
425         int                     rc = 0;
426         ENTRY;
427
428         osd_seq = osd_seq_find(seq_list, seq);
429         if (osd_seq != NULL)
430                 RETURN(osd_seq);
431
432         down(&seq_list->osl_seq_init_sem);
433         /* Check again, in case some one else already add it
434          * to the list */
435         osd_seq = osd_seq_find(seq_list, seq);
436         if (osd_seq != NULL)
437                 GOTO(out, rc = 0);
438
439         OBD_ALLOC_PTR(osd_seq);
440         if (osd_seq == NULL)
441                 GOTO(out, rc = -ENOMEM);
442
443         INIT_LIST_HEAD(&osd_seq->os_seq_list);
444         osd_seq->os_seq = seq;
445
446         /* Init subdir count to be 32, but each seq can have
447          * different subdir count */
448         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
449         OBD_ALLOC_PTR_ARRAY(osd_seq->os_compat_dirs, osd_seq->os_subdir_count);
450         if (osd_seq->os_compat_dirs == NULL)
451                 GOTO(out, rc = -ENOMEM);
452
453         oi.oi_zapid = osd->od_O_id;
454         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
455                 fid_seq_is_mdt0(seq)) ?  "%llu" : "%llx",
456                 fid_seq_is_idif(seq) ? 0 : seq);
457
458         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
459         if (rc != 0) {
460                 CERROR("%s: Can not create %s : rc = %d\n",
461                        osd_name(osd), seq_name, rc);
462                 GOTO(out, rc);
463         }
464
465         osd_seq->os_oid = odb;
466         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
467                 sprintf(key, "d%d", i);
468                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
469                 if (rc)
470                         GOTO(out, rc);
471                 osd_seq->os_compat_dirs[i] = sdb;
472         }
473
474         write_lock(&seq_list->osl_seq_list_lock);
475         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
476         write_unlock(&seq_list->osl_seq_list_lock);
477 out:
478         up(&seq_list->osl_seq_init_sem);
479         if (rc != 0) {
480                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
481                         OBD_FREE_PTR_ARRAY(osd_seq->os_compat_dirs,
482                                            osd_seq->os_subdir_count);
483                 if (osd_seq != NULL)
484                         OBD_FREE_PTR(osd_seq);
485                 osd_seq = ERR_PTR(rc);
486         }
487         RETURN(osd_seq);
488 }
489
490 static uint64_t
491 osd_get_idx_for_ost_obj_compat(const struct lu_env *env, struct osd_device *osd,
492                                const struct lu_fid *fid, char *buf, int bufsize)
493 {
494         struct osd_seq  *osd_seq;
495         unsigned long   b;
496         u64             id;
497         int             rc;
498
499         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
500         if (IS_ERR(osd_seq)) {
501                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
502                        PFID(fid));
503                 return PTR_ERR(osd_seq);
504         }
505
506         if (fid_is_last_id(fid)) {
507                 id = 0;
508         } else {
509                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
510                 LASSERT(rc == 0); /* we should not get here with IGIF */
511                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
512         }
513
514         b = id % OSD_OST_MAP_SIZE;
515         LASSERT(osd_seq->os_compat_dirs[b]);
516
517         if (buf)
518                 snprintf(buf, bufsize, "%llu", id);
519
520         return osd_seq->os_compat_dirs[b];
521 }
522
523 /*
524  * objects w/o a natural reference (unlike a file on a MDS)
525  * are put under a special hierarchy /O/<seq>/d0..dXX
526  * this function returns a directory specific fid belongs to
527  */
528 static uint64_t
529 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
530                         const struct lu_fid *fid, char *buf, int bufsize)
531 {
532         struct osd_seq  *osd_seq;
533         unsigned long   b;
534         u64             id;
535         int             rc;
536
537         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
538         if (IS_ERR(osd_seq)) {
539                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
540                        PFID(fid));
541                 return PTR_ERR(osd_seq);
542         }
543
544         if (fid_is_last_id(fid)) {
545                 if (buf)
546                         snprintf(buf, bufsize, "LAST_ID");
547
548                 return osd_seq->os_oid;
549         }
550
551         rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
552         LASSERT(rc == 0); /* we should not get here with IGIF */
553
554         id = ostid_id(&osd_oti_get(env)->oti_ostid);
555         b = id % OSD_OST_MAP_SIZE;
556         LASSERT(osd_seq->os_compat_dirs[b]);
557
558         if (buf)
559                 snprintf(buf, bufsize, "%llu", id);
560
561         return osd_seq->os_compat_dirs[b];
562 }
563
564 /*
565  * Determine the zap object id which is being used as the OI for the
566  * given fid.  The lowest N bits in the sequence ID are used as the
567  * index key.  On failure 0 is returned which zfs treats internally
568  * as an invalid object id.
569  */
570 static uint64_t
571 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
572                     char *buf, dnode_t **zdn, int bufsize)
573 {
574         struct osd_oi *oi;
575
576         oi = osd_fid2oi(osd, fid);
577         if (buf)
578                 osd_fid2str(buf, fid, bufsize);
579         if (zdn)
580                 *zdn = oi->oi_dn;
581
582         return oi->oi_zapid;
583 }
584
585 uint64_t
586 osd_get_name_n_idx_compat(const struct lu_env *env, struct osd_device *osd,
587                           const struct lu_fid *fid, char *buf, int bufsize,
588                           dnode_t **zdn)
589 {
590         uint64_t zapid;
591
592         LASSERT(fid);
593         LASSERT(!fid_is_acct(fid));
594
595         if (zdn != NULL)
596                 *zdn = NULL;
597
598         if (fid_is_echo(fid) || fid_is_on_ost(env, osd, fid)) {
599                 zapid = osd_get_idx_for_ost_obj_compat(env, osd, fid,
600                                                        buf, bufsize);
601         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
602                 /* special objects with fixed known fids get their name */
603                 char *name = oid2name(fid_oid(fid));
604
605                 if (name) {
606                         zapid = osd->od_root;
607                         if (buf)
608                                 strncpy(buf, name, bufsize);
609                 } else {
610                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
611                                                     bufsize);
612                 }
613         } else {
614                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
615         }
616
617         return zapid;
618 }
619
620 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
621                             const struct lu_fid *fid, char *buf, int bufsize,
622                             dnode_t **zdn)
623 {
624         uint64_t zapid;
625
626         LASSERT(fid);
627         LASSERT(!fid_is_acct(fid));
628
629         if (zdn != NULL)
630                 *zdn = NULL;
631
632         if (fid_is_echo(fid) || fid_is_last_id(fid) ||
633             fid_is_on_ost(env, osd, fid)) {
634                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
635         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
636                 /* special objects with fixed known fids get their name */
637                 char *name = oid2name(fid_oid(fid));
638
639                 if (name) {
640                         zapid = osd->od_root;
641                         if (buf)
642                                 strncpy(buf, name, bufsize);
643                 } else {
644                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
645                                                     bufsize);
646                 }
647         } else {
648                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
649         }
650
651         return zapid;
652 }
653
654 static inline int fid_is_fs_root(const struct lu_fid *fid)
655 {
656         /* Map root inode to special local object FID */
657         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
658                 fid_oid(fid) == OSD_FS_ROOT_OID;
659 }
660
661 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
662                    const struct lu_fid *fid, uint64_t *oid)
663 {
664         struct osd_thread_info  *info = osd_oti_get(env);
665         char                    *buf = info->oti_buf;
666         dnode_t *zdn;
667         uint64_t zapid;
668         int                     rc = 0;
669         ENTRY;
670
671         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
672                 RETURN(-ENOENT);
673
674         LASSERT(!fid_is_acct(fid));
675
676         if (unlikely(fid_is_fs_root(fid))) {
677                 *oid = dev->od_root;
678         } else {
679                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
680                                            sizeof(info->oti_buf), &zdn);
681                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
682                                     8, 1, &info->oti_zde);
683                 if (rc == -ENOENT) {
684                         if (unlikely(fid_is_last_id(fid))) {
685                                 zapid = osd_get_name_n_idx_compat(env, dev, fid,
686                                         buf, sizeof(info->oti_buf), &zdn);
687                                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
688                                                     8, 1, &info->oti_zde);
689                         } else if (fid_is_objseq(fid) || fid_is_batchid(fid)) {
690                                 zapid = osd_get_idx_for_fid(dev, fid,
691                                         buf, NULL, sizeof(info->oti_buf));
692                                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
693                                                     8, 1, &info->oti_zde);
694                         }
695                 }
696
697                 if (rc)
698                         RETURN(rc);
699                 *oid = info->oti_zde.lzd_reg.zde_dnode;
700         }
701
702         if (rc == 0)
703                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
704                                  ZIO_PRIORITY_ASYNC_READ);
705
706         RETURN(rc);
707 }
708
709 /**
710  * Close an entry in a specific slot.
711  */
712 static void
713 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
714 {
715         struct osd_oi *oi;
716
717         LASSERT(key < o->od_oi_count);
718
719         oi = o->od_oi_table[key];
720         if (oi) {
721                 if (oi->oi_dn)
722                         osd_dnode_rele(oi->oi_dn);
723                 OBD_FREE_PTR(oi);
724                 o->od_oi_table[key] = NULL;
725         }
726 }
727
728 /**
729  * Allocate and open a new entry in the specified unused slot.
730  */
731 static int
732 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
733                  char *name, int key)
734 {
735         struct osd_oi *oi;
736         int rc;
737
738         LASSERT(key < o->od_oi_count);
739         LASSERT(o->od_oi_table[key] == NULL);
740
741         OBD_ALLOC_PTR(oi);
742         if (oi == NULL)
743                 return -ENOMEM;
744
745         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
746         if (rc) {
747                 OBD_FREE_PTR(oi);
748                 return rc;
749         }
750
751         o->od_oi_table[key] = oi;
752         __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
753
754         return 0;
755 }
756
757 /**
758  * Depopulate the OI table.
759  */
760 static void
761 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
762 {
763         int i;
764
765         for (i = 0; i < o->od_oi_count; i++)
766                 osd_oi_remove_table(env, o, i);
767 }
768
769 /**
770  * Populate the OI table based.
771  */
772 static int
773 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
774 {
775         char name[16];
776         int  i, rc = 0;
777         ENTRY;
778
779         for (i = 0; i < count; i++) {
780                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
781                 rc = osd_oi_add_table(env, o, name, i);
782                 if (rc) {
783                         osd_oi_close_table(env, o);
784                         break;
785                 }
786         }
787
788         RETURN(rc);
789 }
790
791 /**
792  * Determine if the type and number of OIs used by this file system.
793  */
794 static int osd_oi_probe(const struct lu_env *env, struct osd_device *o)
795 {
796         struct lustre_scrub *scrub = &o->od_scrub;
797         struct scrub_file *sf = &scrub->os_file;
798         struct osd_oi oi;
799         char name[16];
800         int max = sf->sf_oi_count > 0 ? sf->sf_oi_count : OSD_OI_FID_NR_MAX;
801         int count;
802         int rc;
803         ENTRY;
804
805         /*
806          * Check for multiple OIs and determine the count.  There is no
807          * gap handling, if an OI is missing the wrong size can be returned.
808          * The only safeguard is that we know the number of OIs must be a
809          * power of two and this is checked for basic sanity.
810          */
811         for (count = 0; count < max; count++) {
812                 snprintf(name, sizeof(name) - 1, "%s.%d",
813                          DMU_OSD_OI_NAME_BASE, count);
814                 rc = osd_oi_lookup(env, o, o->od_root, name, &oi);
815                 if (!rc)
816                         continue;
817
818                 if (rc == -ENOENT) {
819                         if (sf->sf_oi_count == 0)
820                                 RETURN(count);
821
822                         zfs_set_bit(count, sf->sf_oi_bitmap);
823                         continue;
824                 }
825
826                 if (rc)
827                         RETURN(rc);
828         }
829
830         RETURN(count);
831 }
832
833 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
834 {
835         struct osd_seq_list     *osl = &osd->od_seq_list;
836         struct osd_seq          *osd_seq, *tmp;
837
838         write_lock(&osl->osl_seq_list_lock);
839         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
840                                  os_seq_list) {
841                 list_del(&osd_seq->os_seq_list);
842                 OBD_FREE_PTR_ARRAY(osd_seq->os_compat_dirs,
843                                    osd_seq->os_subdir_count);
844                 OBD_FREE(osd_seq, sizeof(*osd_seq));
845         }
846         write_unlock(&osl->osl_seq_list_lock);
847 }
848
849 /**
850  * Create /O subdirectory to map legacy OST objects for compatibility.
851  */
852 static int
853 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
854 {
855         uint64_t sdb;
856         int rc;
857         ENTRY;
858
859         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
860         if (!rc)
861                 o->od_O_id = sdb;
862
863         RETURN(rc);
864 }
865
866 static int
867 osd_oi_init_index_backup(const struct lu_env *env, struct osd_device *o)
868 {
869         struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
870         int rc;
871         ENTRY;
872
873         lu_local_obj_fid(fid, INDEX_BACKUP_OID);
874         rc = osd_obj_find_or_create(env, o, o->od_root, INDEX_BACKUP_DIR,
875                                     &o->od_index_backup_id, fid, true);
876
877         RETURN(rc);
878 }
879
880 static void
881 osd_oi_init_remote_parent(const struct lu_env *env, struct osd_device *o)
882 {
883         uint64_t sdb;
884         int rc;
885         ENTRY;
886
887         if (o->od_is_ost) {
888                 o->od_remote_parent_dir = ZFS_NO_OBJECT;
889         } else {
890                 /* Remote parent only used for cross-MDT objects,
891                  * it is usless for single MDT case or under read
892                  * only mode. So ignore the failure. */
893                 rc = osd_oi_find_or_create(env, o, o->od_root,
894                                            REMOTE_PARENT_DIR, &sdb);
895                 if (!rc)
896                         o->od_remote_parent_dir = sdb;
897                 else
898                         o->od_remote_parent_dir = ZFS_NO_OBJECT;
899         }
900 }
901
902 /**
903  * Initialize the OIs by either opening or creating them as needed.
904  */
905 int osd_oi_init(const struct lu_env *env, struct osd_device *o, bool reset)
906 {
907         struct lustre_scrub *scrub = &o->od_scrub;
908         struct scrub_file *sf = &scrub->os_file;
909         char *key = osd_oti_get(env)->oti_buf;
910         uint64_t sdb;
911         int i, rc, count;
912         ENTRY;
913
914         LASSERTF((sf->sf_oi_count & (sf->sf_oi_count - 1)) == 0,
915                  "Invalid OI count in scrub file %d\n", sf->sf_oi_count);
916
917         rc = osd_oi_init_index_backup(env, o);
918         if (rc)
919                 RETURN(rc);
920
921         osd_oi_init_remote_parent(env, o);
922
923         rc = osd_oi_init_compat(env, o);
924         if (rc)
925                 RETURN(rc);
926
927         count = osd_oi_probe(env, o);
928         if (count < 0)
929                 GOTO(out, rc = count);
930
931         if (count > 0) {
932                 if (count == sf->sf_oi_count && !reset)
933                         goto open;
934
935                 if (sf->sf_oi_count == 0) {
936                         if (likely((count & (count - 1)) == 0)) {
937                                 sf->sf_oi_count = count;
938                                 rc = scrub_file_store(env, scrub);
939                                 if (rc)
940                                         GOTO(out, rc);
941
942                                 goto open;
943                         }
944
945                         LCONSOLE_ERROR("%s: invalid oi count %d. You can "
946                                        "remove all OIs, then remount it\n",
947                                        osd_name(o), count);
948                         GOTO(out, rc = -EDOM);
949                 }
950
951                 scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
952                 count = sf->sf_oi_count;
953         } else {
954                 if (sf->sf_oi_count > 0) {
955                         count = sf->sf_oi_count;
956                         memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
957                         for (i = 0; i < count; i++)
958                                 zfs_set_bit(i, sf->sf_oi_bitmap);
959                         scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
960                 } else {
961                         count = sf->sf_oi_count = osd_oi_count;
962                 }
963         }
964
965         rc = scrub_file_store(env, scrub);
966         if (rc)
967                 GOTO(out, rc);
968
969         if (reset)
970                 LCONSOLE_WARN("%s: reset Object Index mappings\n",
971                               osd_name(o));
972
973         for (i = 0; i < count; i++) {
974                 LASSERT(sizeof(osd_oti_get(env)->oti_buf) >= 32);
975
976                 snprintf(key, sizeof(osd_oti_get(env)->oti_buf) - 1,
977                          "%s.%d", DMU_OSD_OI_NAME_BASE, i);
978                 if (reset) {
979                         rc = osd_oi_destroy(env, o, key);
980                         if (rc)
981                                 GOTO(out, rc);
982                 }
983                 rc = osd_oi_find_or_create(env, o, o->od_root, key, &sdb);
984                 if (rc)
985                         GOTO(out, rc);
986         }
987
988 open:
989         LASSERT((count & (count - 1)) == 0);
990         o->od_oi_count = count;
991         OBD_ALLOC_PTR_ARRAY(o->od_oi_table, count);
992         if (o->od_oi_table == NULL)
993                 GOTO(out, rc = -ENOMEM);
994
995         rc = osd_oi_open_table(env, o, count);
996
997         GOTO(out, rc);
998
999 out:
1000         if (rc) {
1001                 osd_ost_seq_fini(env, o);
1002
1003                 if (o->od_oi_table) {
1004                         OBD_FREE_PTR_ARRAY(o->od_oi_table, count);
1005                         o->od_oi_table = NULL;
1006                 }
1007         }
1008
1009         return rc;
1010 }
1011
1012 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
1013 {
1014         ENTRY;
1015
1016         osd_ost_seq_fini(env, o);
1017
1018         if (o->od_oi_table != NULL) {
1019                 (void) osd_oi_close_table(env, o);
1020                 OBD_FREE_PTR_ARRAY(o->od_oi_table, o->od_oi_count);
1021                 o->od_oi_table = NULL;
1022                 o->od_oi_count = 0;
1023         }
1024
1025         EXIT;
1026 }
1027
1028 int osd_options_init(void)
1029 {
1030         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
1031         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
1032                 osd_oi_count = OSD_OI_FID_NR;
1033
1034         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
1035                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
1036                         osd_oi_count, size_roundup_power2(osd_oi_count));
1037                 osd_oi_count = size_roundup_power2(osd_oi_count);
1038         }
1039
1040         return 0;
1041 }
1042
1043 /*
1044  * the following set of functions are used to maintain per-thread
1045  * cache of FID->ino mapping. this mechanism is used to avoid
1046  * expensive LU/OI lookups.
1047  */
1048 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
1049                                      struct osd_device *osd,
1050                                      const struct lu_fid *fid)
1051 {
1052         struct osd_thread_info *oti = osd_oti_get(env);
1053         struct osd_idmap_cache *idc = oti->oti_ins_cache;
1054         int i;
1055
1056         for (i = 0; i < oti->oti_ins_cache_used; i++) {
1057                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
1058                         continue;
1059                 if (idc[i].oic_dev != osd)
1060                         continue;
1061
1062                 return idc + i;
1063         }
1064
1065         return NULL;
1066 }
1067
1068 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
1069                                     struct osd_device *osd,
1070                                     const struct lu_fid *fid)
1071 {
1072         struct osd_thread_info *oti = osd_oti_get(env);
1073         struct osd_idmap_cache *idc;
1074         int i;
1075
1076         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
1077                 i = oti->oti_ins_cache_size * 2;
1078                 if (i == 0)
1079                         i = OSD_INS_CACHE_SIZE;
1080                 OBD_ALLOC_PTR_ARRAY_LARGE(idc, i);
1081                 if (idc == NULL)
1082                         return ERR_PTR(-ENOMEM);
1083                 if (oti->oti_ins_cache != NULL) {
1084                         memcpy(idc, oti->oti_ins_cache,
1085                                oti->oti_ins_cache_used * sizeof(*idc));
1086                         OBD_FREE_PTR_ARRAY_LARGE(oti->oti_ins_cache,
1087                                                  oti->oti_ins_cache_used);
1088                 }
1089                 oti->oti_ins_cache = idc;
1090                 oti->oti_ins_cache_size = i;
1091         }
1092
1093         idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
1094         idc->oic_fid = *fid;
1095         idc->oic_dev = osd;
1096         idc->oic_dnode = 0;
1097         idc->oic_remote = 0;
1098
1099         return idc;
1100 }
1101
1102 /**
1103  * Lookup mapping for the given fid in the cache
1104  *
1105  * Initialize a new one if not found. the initialization checks whether
1106  * the object is local or remote. for the local objects, OI is used to
1107  * learn dnode#. the function is used when the caller has no information
1108  * about the object, e.g. at dt_insert().
1109  */
1110 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
1111                                              struct osd_device *osd,
1112                                              const struct lu_fid *fid)
1113 {
1114         struct osd_idmap_cache *idc;
1115         int rc;
1116
1117         LASSERT(!fid_is_acct(fid));
1118
1119         idc = osd_idc_find(env, osd, fid);
1120         if (idc != NULL)
1121                 return idc;
1122
1123         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1124                osd->od_svname, PFID(fid));
1125
1126         /* new mapping is needed */
1127         idc = osd_idc_add(env, osd, fid);
1128         if (IS_ERR(idc)) {
1129                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1130                        osd->od_svname, PFID(fid), PTR_ERR(idc));
1131                 return idc;
1132         }
1133
1134         /* initialize it */
1135         rc = osd_remote_fid(env, osd, fid);
1136         if (unlikely(rc < 0))
1137                 return ERR_PTR(rc);
1138
1139         if (rc == 0) {
1140                 /* the object is local, lookup in OI */
1141                 uint64_t dnode;
1142
1143                 rc = osd_fid_lookup(env, osd, fid, &dnode);
1144                 if (unlikely(rc < 0)) {
1145                         CERROR("%s: can't lookup: rc = %d\n",
1146                                osd->od_svname, rc);
1147                         return ERR_PTR(rc);
1148                 }
1149                 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
1150                 idc->oic_dnode = dnode;
1151         } else {
1152                 /* the object is remote */
1153                 idc->oic_remote = 1;
1154         }
1155
1156         return idc;
1157 }
1158
1159 /*
1160  * lookup mapping for given FID and fill it from the given object.
1161  * the object is local by definition.
1162  */
1163 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
1164                           struct osd_object *obj)
1165 {
1166         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1167         struct osd_idmap_cache *idc;
1168
1169         idc = osd_idc_find(env, osd, fid);
1170         if (idc != NULL) {
1171                 if (obj->oo_dn == NULL)
1172                         return 0;
1173                 idc->oic_dnode = obj->oo_dn->dn_object;
1174                 return 0;
1175         }
1176
1177         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1178                osd->od_svname, PFID(fid));
1179
1180         /* new mapping is needed */
1181         idc = osd_idc_add(env, osd, fid);
1182         if (IS_ERR(idc)) {
1183                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1184                        osd->od_svname, PFID(fid), PTR_ERR(idc));
1185                 return PTR_ERR(idc);
1186         }
1187
1188         if (obj->oo_dn)
1189                 idc->oic_dnode = obj->oo_dn->dn_object;
1190
1191         return 0;
1192 }
1193
1194 int osd_idc_find_and_init_with_oid(const struct lu_env *env,
1195                                    struct osd_device *osd,
1196                                    const struct lu_fid *fid,
1197                                    uint64_t oid)
1198 {
1199         struct osd_idmap_cache *idc;
1200
1201         idc = osd_idc_find(env, osd, fid);
1202         if (!idc) {
1203                 idc = osd_idc_add(env, osd, fid);
1204                 if (IS_ERR(idc))
1205                         return PTR_ERR(idc);
1206         }
1207
1208         idc->oic_dnode = oid;
1209         idc->oic_remote = 0;
1210
1211         return 0;
1212 }