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