Whamcloud - gitweb
LU-6142 tests: Fix style issues for chownmany.c
[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 = IFTODT(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
232 osd_oi_find_or_create(const struct lu_env *env, struct osd_device *o,
233                       uint64_t parent, const char *name, uint64_t *child)
234 {
235         struct osd_oi   oi;
236         int             rc;
237
238         rc = osd_oi_lookup(env, o, parent, name, &oi);
239         if (rc == 0)
240                 *child = oi.oi_zapid;
241         else if (rc == -ENOENT)
242                 rc = osd_obj_create(env, o, parent, name, child, NULL, true);
243
244         return rc;
245 }
246
247 int osd_obj_find_or_create(const struct lu_env *env, struct osd_device *o,
248                            uint64_t parent, const char *name, uint64_t *child,
249                            const struct lu_fid *fid, bool isdir)
250 {
251         struct osd_oi oi;
252         int rc;
253
254         rc = osd_oi_lookup(env, o, parent, name, &oi);
255         if (!rc)
256                 *child = oi.oi_zapid;
257         else if (rc == -ENOENT)
258                 rc = osd_obj_create(env, o, parent, name, child, fid, isdir);
259
260         return rc;
261 }
262
263 /**
264  * Lookup the target index/flags of the fid, so it will know where
265  * the object is located (tgt index) and it is MDT or OST object.
266  */
267 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
268                    u64 seq, struct lu_seq_range *range)
269 {
270         struct seq_server_site  *ss = osd_seq_site(osd);
271
272         if (fid_seq_is_idif(seq)) {
273                 fld_range_set_ost(range);
274                 range->lsr_index = idif_ost_idx(seq);
275                 return 0;
276         }
277
278         if (!fid_seq_in_fldb(seq)) {
279                 fld_range_set_mdt(range);
280                 if (ss != NULL)
281                         /* FIXME: If ss is NULL, it suppose not get lsr_index
282                          * at all */
283                         range->lsr_index = ss->ss_node_id;
284                 return 0;
285         }
286
287         /* The seq_server_site may be NOT ready during initial OI scrub */
288         if (unlikely(!ss || !ss->ss_server_fld ||
289                      !ss->ss_server_fld->lsf_cache))
290                 return -ENOENT;
291
292         fld_range_set_any(range);
293         /* OSD will only do local fld lookup */
294         return fld_local_lookup(env, ss->ss_server_fld, seq, range);
295 }
296
297 int fid_is_on_ost(const struct lu_env *env, struct osd_device *osd,
298                   const struct lu_fid *fid)
299 {
300         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
301         int                     rc;
302         ENTRY;
303
304         if (fid_is_idif(fid))
305                 RETURN(1);
306
307         if (unlikely(fid_is_local_file(fid) || fid_is_llog(fid)) ||
308                      fid_is_name_llog(fid) || fid_is_quota(fid) ||
309                      fid_is_igif(fid))
310                 RETURN(0);
311
312         rc = osd_fld_lookup(env, osd, fid_seq(fid), range);
313         if (rc != 0) {
314                 /* During upgrade, OST FLDB might not be loaded because
315                  * OST FLDB is not created until 2.6, so if some DNE
316                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
317                  * not be able to find the sequence from local FLDB
318                  * cache see fld_index_init(). */
319                 if (rc == -ENOENT && osd->od_is_ost)
320                         RETURN(1);
321
322                 if (rc != -ENOENT)
323                         CERROR("%s: "DFID" lookup failed: rc = %d\n",
324                                osd_name(osd), PFID(fid), rc);
325                 RETURN(0);
326         }
327
328         if (fld_range_is_ost(range))
329                 RETURN(1);
330
331         RETURN(0);
332 }
333
334 static struct osd_seq *osd_seq_find_locked(struct osd_seq_list *seq_list,
335                                            u64 seq)
336 {
337         struct osd_seq *osd_seq;
338
339         list_for_each_entry(osd_seq, &seq_list->osl_seq_list, os_seq_list) {
340                 if (osd_seq->os_seq == seq)
341                         return osd_seq;
342         }
343         return NULL;
344 }
345
346 static struct osd_seq *osd_seq_find(struct osd_seq_list *seq_list, u64 seq)
347 {
348         struct osd_seq *osd_seq;
349
350         read_lock(&seq_list->osl_seq_list_lock);
351         osd_seq = osd_seq_find_locked(seq_list, seq);
352         read_unlock(&seq_list->osl_seq_list_lock);
353
354         return osd_seq;
355 }
356
357 static struct osd_seq *osd_find_or_add_seq(const struct lu_env *env,
358                                            struct osd_device *osd, u64 seq)
359 {
360         struct osd_seq_list     *seq_list = &osd->od_seq_list;
361         struct osd_seq          *osd_seq;
362         char                    *key = osd_oti_get(env)->oti_buf;
363         char                    *seq_name = osd_oti_get(env)->oti_str;
364         struct osd_oi           oi;
365         uint64_t                sdb, odb;
366         int                     i;
367         int                     rc = 0;
368         ENTRY;
369
370         osd_seq = osd_seq_find(seq_list, seq);
371         if (osd_seq != NULL)
372                 RETURN(osd_seq);
373
374         down(&seq_list->osl_seq_init_sem);
375         /* Check again, in case some one else already add it
376          * to the list */
377         osd_seq = osd_seq_find(seq_list, seq);
378         if (osd_seq != NULL)
379                 GOTO(out, rc = 0);
380
381         OBD_ALLOC_PTR(osd_seq);
382         if (osd_seq == NULL)
383                 GOTO(out, rc = -ENOMEM);
384
385         INIT_LIST_HEAD(&osd_seq->os_seq_list);
386         osd_seq->os_seq = seq;
387
388         /* Init subdir count to be 32, but each seq can have
389          * different subdir count */
390         osd_seq->os_subdir_count = OSD_OST_MAP_SIZE;
391         OBD_ALLOC(osd_seq->os_compat_dirs,
392                   sizeof(uint64_t) * osd_seq->os_subdir_count);
393         if (osd_seq->os_compat_dirs == NULL)
394                 GOTO(out, rc = -ENOMEM);
395
396         oi.oi_zapid = osd->od_O_id;
397         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
398                 fid_seq_is_mdt0(seq)) ?  "%llu" : "%llx",
399                 fid_seq_is_idif(seq) ? 0 : seq);
400
401         rc = osd_oi_find_or_create(env, osd, oi.oi_zapid, seq_name, &odb);
402         if (rc != 0) {
403                 CERROR("%s: Can not create %s : rc = %d\n",
404                        osd_name(osd), seq_name, rc);
405                 GOTO(out, rc);
406         }
407
408         osd_seq->os_oid = odb;
409         for (i = 0; i < OSD_OST_MAP_SIZE; i++) {
410                 sprintf(key, "d%d", i);
411                 rc = osd_oi_find_or_create(env, osd, odb, key, &sdb);
412                 if (rc)
413                         GOTO(out, rc);
414                 osd_seq->os_compat_dirs[i] = sdb;
415         }
416
417         write_lock(&seq_list->osl_seq_list_lock);
418         list_add(&osd_seq->os_seq_list, &seq_list->osl_seq_list);
419         write_unlock(&seq_list->osl_seq_list_lock);
420 out:
421         up(&seq_list->osl_seq_init_sem);
422         if (rc != 0) {
423                 if (osd_seq != NULL && osd_seq->os_compat_dirs != NULL)
424                         OBD_FREE(osd_seq->os_compat_dirs,
425                                  sizeof(uint64_t) * osd_seq->os_subdir_count);
426                 if (osd_seq != NULL)
427                         OBD_FREE_PTR(osd_seq);
428                 osd_seq = ERR_PTR(rc);
429         }
430         RETURN(osd_seq);
431 }
432
433 static uint64_t
434 osd_get_idx_for_ost_obj_compat(const struct lu_env *env, struct osd_device *osd,
435                                const struct lu_fid *fid, char *buf, int bufsize)
436 {
437         struct osd_seq  *osd_seq;
438         unsigned long   b;
439         u64             id;
440         int             rc;
441
442         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
443         if (IS_ERR(osd_seq)) {
444                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
445                        PFID(fid));
446                 return PTR_ERR(osd_seq);
447         }
448
449         if (fid_is_last_id(fid)) {
450                 id = 0;
451         } else {
452                 rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
453                 LASSERT(rc == 0); /* we should not get here with IGIF */
454                 id = ostid_id(&osd_oti_get(env)->oti_ostid);
455         }
456
457         b = id % OSD_OST_MAP_SIZE;
458         LASSERT(osd_seq->os_compat_dirs[b]);
459
460         if (buf)
461                 snprintf(buf, bufsize, "%llu", id);
462
463         return osd_seq->os_compat_dirs[b];
464 }
465
466 /*
467  * objects w/o a natural reference (unlike a file on a MDS)
468  * are put under a special hierarchy /O/<seq>/d0..dXX
469  * this function returns a directory specific fid belongs to
470  */
471 static uint64_t
472 osd_get_idx_for_ost_obj(const struct lu_env *env, struct osd_device *osd,
473                         const struct lu_fid *fid, char *buf, int bufsize)
474 {
475         struct osd_seq  *osd_seq;
476         unsigned long   b;
477         u64             id;
478         int             rc;
479
480         osd_seq = osd_find_or_add_seq(env, osd, fid_seq(fid));
481         if (IS_ERR(osd_seq)) {
482                 CERROR("%s: Can not find seq group "DFID"\n", osd_name(osd),
483                        PFID(fid));
484                 return PTR_ERR(osd_seq);
485         }
486
487         if (fid_is_last_id(fid)) {
488                 if (buf)
489                         snprintf(buf, bufsize, "LAST_ID");
490
491                 return osd_seq->os_oid;
492         }
493
494         rc = fid_to_ostid(fid, &osd_oti_get(env)->oti_ostid);
495         LASSERT(rc == 0); /* we should not get here with IGIF */
496
497         id = ostid_id(&osd_oti_get(env)->oti_ostid);
498         b = id % OSD_OST_MAP_SIZE;
499         LASSERT(osd_seq->os_compat_dirs[b]);
500
501         if (buf)
502                 snprintf(buf, bufsize, "%llu", id);
503
504         return osd_seq->os_compat_dirs[b];
505 }
506
507 /*
508  * Determine the zap object id which is being used as the OI for the
509  * given fid.  The lowest N bits in the sequence ID are used as the
510  * index key.  On failure 0 is returned which zfs treats internally
511  * as an invalid object id.
512  */
513 static uint64_t
514 osd_get_idx_for_fid(struct osd_device *osd, const struct lu_fid *fid,
515                     char *buf, dnode_t **zdn, int bufsize)
516 {
517         struct osd_oi *oi;
518
519         oi = osd_fid2oi(osd, fid);
520         if (buf)
521                 osd_fid2str(buf, fid, bufsize);
522         if (zdn)
523                 *zdn = oi->oi_dn;
524
525         return oi->oi_zapid;
526 }
527
528 uint64_t
529 osd_get_name_n_idx_compat(const struct lu_env *env, struct osd_device *osd,
530                           const struct lu_fid *fid, char *buf, int bufsize,
531                           dnode_t **zdn)
532 {
533         uint64_t zapid;
534
535         LASSERT(fid);
536         LASSERT(!fid_is_acct(fid));
537
538         if (zdn != NULL)
539                 *zdn = NULL;
540
541         if (fid_is_echo(fid) || fid_is_on_ost(env, osd, fid)) {
542                 zapid = osd_get_idx_for_ost_obj_compat(env, osd, fid,
543                                                        buf, bufsize);
544         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
545                 /* special objects with fixed known fids get their name */
546                 char *name = oid2name(fid_oid(fid));
547
548                 if (name) {
549                         zapid = osd->od_root;
550                         if (buf)
551                                 strncpy(buf, name, bufsize);
552                 } else {
553                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
554                                                     bufsize);
555                 }
556         } else {
557                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
558         }
559
560         return zapid;
561 }
562
563 uint64_t osd_get_name_n_idx(const struct lu_env *env, struct osd_device *osd,
564                             const struct lu_fid *fid, char *buf, int bufsize,
565                             dnode_t **zdn)
566 {
567         uint64_t zapid;
568
569         LASSERT(fid);
570         LASSERT(!fid_is_acct(fid));
571
572         if (zdn != NULL)
573                 *zdn = NULL;
574
575         if (fid_is_echo(fid) || fid_is_last_id(fid) ||
576             fid_is_on_ost(env, osd, fid)) {
577                 zapid = osd_get_idx_for_ost_obj(env, osd, fid, buf, bufsize);
578         } else if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
579                 /* special objects with fixed known fids get their name */
580                 char *name = oid2name(fid_oid(fid));
581
582                 if (name) {
583                         zapid = osd->od_root;
584                         if (buf)
585                                 strncpy(buf, name, bufsize);
586                 } else {
587                         zapid = osd_get_idx_for_fid(osd, fid, buf, NULL,
588                                                     bufsize);
589                 }
590         } else {
591                 zapid = osd_get_idx_for_fid(osd, fid, buf, zdn, bufsize);
592         }
593
594         return zapid;
595 }
596
597 static inline int fid_is_fs_root(const struct lu_fid *fid)
598 {
599         /* Map root inode to special local object FID */
600         return fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
601                 fid_oid(fid) == OSD_FS_ROOT_OID;
602 }
603
604 int osd_fid_lookup(const struct lu_env *env, struct osd_device *dev,
605                    const struct lu_fid *fid, uint64_t *oid)
606 {
607         struct osd_thread_info  *info = osd_oti_get(env);
608         char                    *buf = info->oti_buf;
609         dnode_t *zdn;
610         uint64_t zapid;
611         int                     rc = 0;
612         ENTRY;
613
614         if (OBD_FAIL_CHECK(OBD_FAIL_SRV_ENOENT))
615                 RETURN(-ENOENT);
616
617         LASSERT(!fid_is_acct(fid));
618
619         if (unlikely(fid_is_fs_root(fid))) {
620                 *oid = dev->od_root;
621         } else {
622                 zapid = osd_get_name_n_idx(env, dev, fid, buf,
623                                            sizeof(info->oti_buf), &zdn);
624                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
625                                     8, 1, &info->oti_zde);
626                 if (rc == -ENOENT) {
627                         if (unlikely(fid_is_last_id(fid))) {
628                                 zapid = osd_get_name_n_idx_compat(env, dev, fid,
629                                         buf, sizeof(info->oti_buf), &zdn);
630                                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
631                                                     8, 1, &info->oti_zde);
632                         } else if (fid_is_objseq(fid) || fid_is_batchid(fid)) {
633                                 zapid = osd_get_idx_for_fid(dev, fid,
634                                         buf, NULL, sizeof(info->oti_buf));
635                                 rc = osd_zap_lookup(dev, zapid, zdn, buf,
636                                                     8, 1, &info->oti_zde);
637                         }
638                 }
639
640                 if (rc)
641                         RETURN(rc);
642                 *oid = info->oti_zde.lzd_reg.zde_dnode;
643         }
644
645         if (rc == 0)
646                 osd_dmu_prefetch(dev->od_os, *oid, 0, 0, 0,
647                                  ZIO_PRIORITY_ASYNC_READ);
648
649         RETURN(rc);
650 }
651
652 /**
653  * Close an entry in a specific slot.
654  */
655 static void
656 osd_oi_remove_table(const struct lu_env *env, struct osd_device *o, int key)
657 {
658         struct osd_oi *oi;
659
660         LASSERT(key < o->od_oi_count);
661
662         oi = o->od_oi_table[key];
663         if (oi) {
664                 if (oi->oi_dn)
665                         osd_dnode_rele(oi->oi_dn);
666                 OBD_FREE_PTR(oi);
667                 o->od_oi_table[key] = NULL;
668         }
669 }
670
671 /**
672  * Allocate and open a new entry in the specified unused slot.
673  */
674 static int
675 osd_oi_add_table(const struct lu_env *env, struct osd_device *o,
676                  char *name, int key)
677 {
678         struct osd_oi *oi;
679         int rc;
680
681         LASSERT(key < o->od_oi_count);
682         LASSERT(o->od_oi_table[key] == NULL);
683
684         OBD_ALLOC_PTR(oi);
685         if (oi == NULL)
686                 return -ENOMEM;
687
688         rc = osd_oi_lookup(env, o, o->od_root, name, oi);
689         if (rc) {
690                 OBD_FREE_PTR(oi);
691                 return rc;
692         }
693
694         o->od_oi_table[key] = oi;
695         __osd_obj2dnode(o->od_os, oi->oi_zapid, &oi->oi_dn);
696
697         return 0;
698 }
699
700 /**
701  * Depopulate the OI table.
702  */
703 static void
704 osd_oi_close_table(const struct lu_env *env, struct osd_device *o)
705 {
706         int i;
707
708         for (i = 0; i < o->od_oi_count; i++)
709                 osd_oi_remove_table(env, o, i);
710 }
711
712 /**
713  * Populate the OI table based.
714  */
715 static int
716 osd_oi_open_table(const struct lu_env *env, struct osd_device *o, int count)
717 {
718         char name[16];
719         int  i, rc = 0;
720         ENTRY;
721
722         for (i = 0; i < count; i++) {
723                 sprintf(name, "%s.%d", DMU_OSD_OI_NAME_BASE, i);
724                 rc = osd_oi_add_table(env, o, name, i);
725                 if (rc) {
726                         osd_oi_close_table(env, o);
727                         break;
728                 }
729         }
730
731         RETURN(rc);
732 }
733
734 /**
735  * Determine if the type and number of OIs used by this file system.
736  */
737 static int osd_oi_probe(const struct lu_env *env, struct osd_device *o)
738 {
739         struct lustre_scrub *scrub = &o->od_scrub;
740         struct scrub_file *sf = &scrub->os_file;
741         struct osd_oi oi;
742         char name[16];
743         int max = sf->sf_oi_count > 0 ? sf->sf_oi_count : OSD_OI_FID_NR_MAX;
744         int count;
745         int rc;
746         ENTRY;
747
748         /*
749          * Check for multiple OIs and determine the count.  There is no
750          * gap handling, if an OI is missing the wrong size can be returned.
751          * The only safeguard is that we know the number of OIs must be a
752          * power of two and this is checked for basic sanity.
753          */
754         for (count = 0; count < max; count++) {
755                 snprintf(name, sizeof(name) - 1, "%s.%d",
756                          DMU_OSD_OI_NAME_BASE, count);
757                 rc = osd_oi_lookup(env, o, o->od_root, name, &oi);
758                 if (!rc)
759                         continue;
760
761                 if (rc == -ENOENT) {
762                         if (sf->sf_oi_count == 0)
763                                 RETURN(count);
764
765                         zfs_set_bit(count, sf->sf_oi_bitmap);
766                         continue;
767                 }
768
769                 if (rc)
770                         RETURN(rc);
771         }
772
773         RETURN(count);
774 }
775
776 static void osd_ost_seq_fini(const struct lu_env *env, struct osd_device *osd)
777 {
778         struct osd_seq_list     *osl = &osd->od_seq_list;
779         struct osd_seq          *osd_seq, *tmp;
780
781         write_lock(&osl->osl_seq_list_lock);
782         list_for_each_entry_safe(osd_seq, tmp, &osl->osl_seq_list,
783                                  os_seq_list) {
784                 list_del(&osd_seq->os_seq_list);
785                 OBD_FREE(osd_seq->os_compat_dirs,
786                          sizeof(uint64_t) * osd_seq->os_subdir_count);
787                 OBD_FREE(osd_seq, sizeof(*osd_seq));
788         }
789         write_unlock(&osl->osl_seq_list_lock);
790
791         return;
792 }
793
794 /**
795  * Create /O subdirectory to map legacy OST objects for compatibility.
796  */
797 static int
798 osd_oi_init_compat(const struct lu_env *env, struct osd_device *o)
799 {
800         uint64_t sdb;
801         int rc;
802         ENTRY;
803
804         rc = osd_oi_find_or_create(env, o, o->od_root, "O", &sdb);
805         if (!rc)
806                 o->od_O_id = sdb;
807
808         RETURN(rc);
809 }
810
811 static int
812 osd_oi_init_index_backup(const struct lu_env *env, struct osd_device *o)
813 {
814         struct lu_fid *fid = &osd_oti_get(env)->oti_fid;
815         int rc;
816         ENTRY;
817
818         lu_local_obj_fid(fid, INDEX_BACKUP_OID);
819         rc = osd_obj_find_or_create(env, o, o->od_root, INDEX_BACKUP_DIR,
820                                     &o->od_index_backup_id, fid, true);
821
822         RETURN(rc);
823 }
824
825 static void
826 osd_oi_init_remote_parent(const struct lu_env *env, struct osd_device *o)
827 {
828         uint64_t sdb;
829         int rc;
830         ENTRY;
831
832         if (o->od_is_ost) {
833                 o->od_remote_parent_dir = ZFS_NO_OBJECT;
834         } else {
835                 /* Remote parent only used for cross-MDT objects,
836                  * it is usless for single MDT case or under read
837                  * only mode. So ignore the failure. */
838                 rc = osd_oi_find_or_create(env, o, o->od_root,
839                                            REMOTE_PARENT_DIR, &sdb);
840                 if (!rc)
841                         o->od_remote_parent_dir = sdb;
842                 else
843                         o->od_remote_parent_dir = ZFS_NO_OBJECT;
844         }
845 }
846
847 /**
848  * Initialize the OIs by either opening or creating them as needed.
849  */
850 int osd_oi_init(const struct lu_env *env, struct osd_device *o)
851 {
852         struct lustre_scrub *scrub = &o->od_scrub;
853         struct scrub_file *sf = &scrub->os_file;
854         char *key = osd_oti_get(env)->oti_buf;
855         uint64_t sdb;
856         int i, rc, count;
857         ENTRY;
858
859         LASSERTF((sf->sf_oi_count & (sf->sf_oi_count - 1)) == 0,
860                  "Invalid OI count in scrub file %d\n", sf->sf_oi_count);
861
862         rc = osd_oi_init_index_backup(env, o);
863         if (rc)
864                 RETURN(rc);
865
866         osd_oi_init_remote_parent(env, o);
867
868         rc = osd_oi_init_compat(env, o);
869         if (rc)
870                 RETURN(rc);
871
872         count = osd_oi_probe(env, o);
873         if (count < 0)
874                 GOTO(out, rc = count);
875
876         if (count > 0) {
877                 if (count == sf->sf_oi_count)
878                         goto open;
879
880                 if (sf->sf_oi_count == 0) {
881                         if (likely((count & (count - 1)) == 0)) {
882                                 sf->sf_oi_count = count;
883                                 rc = scrub_file_store(env, scrub);
884                                 if (rc)
885                                         GOTO(out, rc);
886
887                                 goto open;
888                         }
889
890                         LCONSOLE_ERROR("%s: invalid oi count %d. You can "
891                                        "remove all OIs, then remount it\n",
892                                        osd_name(o), count);
893                         GOTO(out, rc = -EDOM);
894                 }
895
896                 scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
897                 count = sf->sf_oi_count;
898         } else {
899                 if (sf->sf_oi_count > 0) {
900                         count = sf->sf_oi_count;
901                         memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
902                         for (i = 0; i < count; i++)
903                                 zfs_set_bit(i, sf->sf_oi_bitmap);
904                         scrub_file_reset(scrub, o->od_uuid, SF_RECREATED);
905                 } else {
906                         count = sf->sf_oi_count = osd_oi_count;
907                 }
908         }
909
910         rc = scrub_file_store(env, scrub);
911         if (rc)
912                 GOTO(out, rc);
913
914         for (i = 0; i < count; i++) {
915                 LASSERT(sizeof(osd_oti_get(env)->oti_buf) >= 32);
916
917                 snprintf(key, sizeof(osd_oti_get(env)->oti_buf) - 1,
918                          "%s.%d", DMU_OSD_OI_NAME_BASE, i);
919                 rc = osd_oi_find_or_create(env, o, o->od_root, key, &sdb);
920                 if (rc)
921                         GOTO(out, rc);
922         }
923
924 open:
925         LASSERT((count & (count - 1)) == 0);
926         o->od_oi_count = count;
927         OBD_ALLOC(o->od_oi_table, sizeof(struct osd_oi *) * count);
928         if (o->od_oi_table == NULL)
929                 GOTO(out, rc = -ENOMEM);
930
931         rc = osd_oi_open_table(env, o, count);
932
933         GOTO(out, rc);
934
935 out:
936         if (rc) {
937                 osd_ost_seq_fini(env, o);
938
939                 if (o->od_oi_table) {
940                         OBD_FREE(o->od_oi_table,
941                                  sizeof(struct osd_oi *) * count);
942                         o->od_oi_table = NULL;
943                 }
944         }
945
946         return rc;
947 }
948
949 void osd_oi_fini(const struct lu_env *env, struct osd_device *o)
950 {
951         ENTRY;
952
953         osd_ost_seq_fini(env, o);
954
955         if (o->od_oi_table != NULL) {
956                 (void) osd_oi_close_table(env, o);
957                 OBD_FREE(o->od_oi_table,
958                          sizeof(struct osd_oi *) * o->od_oi_count);
959                 o->od_oi_table = NULL;
960                 o->od_oi_count = 0;
961         }
962
963         EXIT;
964 }
965
966 int osd_options_init(void)
967 {
968         /* osd_oi_count - Default number of OIs, 128 works well for ZFS */
969         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
970                 osd_oi_count = OSD_OI_FID_NR;
971
972         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
973                 LCONSOLE_WARN("Round up osd_oi_count %d to power2 %d\n",
974                         osd_oi_count, size_roundup_power2(osd_oi_count));
975                 osd_oi_count = size_roundup_power2(osd_oi_count);
976         }
977
978         return 0;
979 }
980
981 /*
982  * the following set of functions are used to maintain per-thread
983  * cache of FID->ino mapping. this mechanism is used to avoid
984  * expensive LU/OI lookups.
985  */
986 struct osd_idmap_cache *osd_idc_find(const struct lu_env *env,
987                                      struct osd_device *osd,
988                                      const struct lu_fid *fid)
989 {
990         struct osd_thread_info *oti = osd_oti_get(env);
991         struct osd_idmap_cache *idc = oti->oti_ins_cache;
992         int i;
993
994         for (i = 0; i < oti->oti_ins_cache_used; i++) {
995                 if (!lu_fid_eq(&idc[i].oic_fid, fid))
996                         continue;
997                 if (idc[i].oic_dev != osd)
998                         continue;
999
1000                 return idc + i;
1001         }
1002
1003         return NULL;
1004 }
1005
1006 struct osd_idmap_cache *osd_idc_add(const struct lu_env *env,
1007                                     struct osd_device *osd,
1008                                     const struct lu_fid *fid)
1009 {
1010         struct osd_thread_info *oti = osd_oti_get(env);
1011         struct osd_idmap_cache *idc;
1012         int i;
1013
1014         if (unlikely(oti->oti_ins_cache_used >= oti->oti_ins_cache_size)) {
1015                 i = oti->oti_ins_cache_size * 2;
1016                 LASSERT(i < 1000);
1017                 if (i == 0)
1018                         i = OSD_INS_CACHE_SIZE;
1019                 OBD_ALLOC(idc, sizeof(*idc) * i);
1020                 if (idc == NULL)
1021                         return ERR_PTR(-ENOMEM);
1022                 if (oti->oti_ins_cache != NULL) {
1023                         memcpy(idc, oti->oti_ins_cache,
1024                                oti->oti_ins_cache_used * sizeof(*idc));
1025                         OBD_FREE(oti->oti_ins_cache,
1026                                  oti->oti_ins_cache_used * sizeof(*idc));
1027                 }
1028                 oti->oti_ins_cache = idc;
1029                 oti->oti_ins_cache_size = i;
1030         }
1031
1032         idc = &oti->oti_ins_cache[oti->oti_ins_cache_used++];
1033         idc->oic_fid = *fid;
1034         idc->oic_dev = osd;
1035         idc->oic_dnode = 0;
1036         idc->oic_remote = 0;
1037
1038         return idc;
1039 }
1040
1041 /**
1042  * Lookup mapping for the given fid in the cache
1043  *
1044  * Initialize a new one if not found. the initialization checks whether
1045  * the object is local or remote. for the local objects, OI is used to
1046  * learn dnode#. the function is used when the caller has no information
1047  * about the object, e.g. at dt_insert().
1048  */
1049 struct osd_idmap_cache *osd_idc_find_or_init(const struct lu_env *env,
1050                                              struct osd_device *osd,
1051                                              const struct lu_fid *fid)
1052 {
1053         struct osd_idmap_cache *idc;
1054         int rc;
1055
1056         LASSERT(!fid_is_acct(fid));
1057
1058         idc = osd_idc_find(env, osd, fid);
1059         if (idc != NULL)
1060                 return idc;
1061
1062         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1063                osd->od_svname, PFID(fid));
1064
1065         /* new mapping is needed */
1066         idc = osd_idc_add(env, osd, fid);
1067         if (IS_ERR(idc)) {
1068                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1069                        osd->od_svname, PFID(fid), PTR_ERR(idc));
1070                 return idc;
1071         }
1072
1073         /* initialize it */
1074         rc = osd_remote_fid(env, osd, fid);
1075         if (unlikely(rc < 0))
1076                 return ERR_PTR(rc);
1077
1078         if (rc == 0) {
1079                 /* the object is local, lookup in OI */
1080                 uint64_t dnode;
1081
1082                 rc = osd_fid_lookup(env, osd, fid, &dnode);
1083                 if (unlikely(rc < 0)) {
1084                         CERROR("%s: can't lookup: rc = %d\n",
1085                                osd->od_svname, rc);
1086                         return ERR_PTR(rc);
1087                 }
1088                 LASSERT(dnode < (1ULL << DN_MAX_OBJECT_SHIFT));
1089                 idc->oic_dnode = dnode;
1090         } else {
1091                 /* the object is remote */
1092                 idc->oic_remote = 1;
1093         }
1094
1095         return idc;
1096 }
1097
1098 /*
1099  * lookup mapping for given FID and fill it from the given object.
1100  * the object is local by definition.
1101  */
1102 int osd_idc_find_and_init(const struct lu_env *env, struct osd_device *osd,
1103                           struct osd_object *obj)
1104 {
1105         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1106         struct osd_idmap_cache *idc;
1107
1108         idc = osd_idc_find(env, osd, fid);
1109         if (idc != NULL) {
1110                 if (obj->oo_dn == NULL)
1111                         return 0;
1112                 idc->oic_dnode = obj->oo_dn->dn_object;
1113                 return 0;
1114         }
1115
1116         CDEBUG(D_INODE, "%s: FID "DFID" not in the id map cache\n",
1117                osd->od_svname, PFID(fid));
1118
1119         /* new mapping is needed */
1120         idc = osd_idc_add(env, osd, fid);
1121         if (IS_ERR(idc)) {
1122                 CERROR("%s: FID "DFID" add id map cache failed: %ld\n",
1123                        osd->od_svname, PFID(fid), PTR_ERR(idc));
1124                 return PTR_ERR(idc);
1125         }
1126
1127         if (obj->oo_dn)
1128                 idc->oic_dnode = obj->oo_dn->dn_object;
1129
1130         return 0;
1131 }
1132
1133 int osd_idc_find_and_init_with_oid(const struct lu_env *env,
1134                                    struct osd_device *osd,
1135                                    const struct lu_fid *fid,
1136                                    uint64_t oid)
1137 {
1138         struct osd_idmap_cache *idc;
1139
1140         idc = osd_idc_find(env, osd, fid);
1141         if (!idc) {
1142                 idc = osd_idc_add(env, osd, fid);
1143                 if (IS_ERR(idc))
1144                         return PTR_ERR(idc);
1145         }
1146
1147         idc->oic_dnode = oid;
1148         idc->oic_remote = 0;
1149
1150         return 0;
1151 }