Whamcloud - gitweb
b44af0454ef3d0679a3ea9d1443f90943d5f1f7d
[fs/lustre-release.git] / lustre / osd-ldiskfs / 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) 2007, 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/osd_oi.c
33  *
34  * Object Index.
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_OSD
40
41 #include <linux/module.h>
42
43 /*
44  * struct OBD_{ALLOC,FREE}*()
45  * OBD_FAIL_CHECK
46  */
47 #include <obd.h>
48 #include <obd_support.h>
49
50 /* fid_cpu_to_be() */
51 #include <lustre_fid.h>
52 #include <dt_object.h>
53 #include <lustre_scrub.h>
54
55 #include "osd_oi.h"
56 /* osd_lookup(), struct osd_thread_info */
57 #include "osd_internal.h"
58
59 unsigned int osd_oi_count = OSD_OI_FID_NR;
60 module_param(osd_oi_count, int, 0444);
61 MODULE_PARM_DESC(osd_oi_count, "Number of Object Index containers to be created, it's only valid for new filesystem.");
62
63 static struct dt_index_features oi_feat = {
64         .dif_flags       = DT_IND_UPDATE,
65         .dif_recsize_min = sizeof(struct osd_inode_id),
66         .dif_recsize_max = sizeof(struct osd_inode_id),
67         .dif_ptrsize     = 4
68 };
69
70 #define OSD_OI_NAME_BASE        "oi.16"
71
72 static void osd_oi_table_put(struct osd_thread_info *info,
73                              struct osd_oi **oi_table, unsigned int oi_count)
74 {
75         struct iam_container *bag;
76         int i;
77
78         for (i = 0; i < oi_count; i++) {
79                 if (oi_table[i] == NULL)
80                         continue;
81
82                 LASSERT(oi_table[i]->oi_inode != NULL);
83
84                 bag = &(oi_table[i]->oi_dir.od_container);
85                 if (bag->ic_object == oi_table[i]->oi_inode)
86                         iam_container_fini(bag);
87                 iput(oi_table[i]->oi_inode);
88                 oi_table[i]->oi_inode = NULL;
89                 OBD_FREE_PTR(oi_table[i]);
90                 oi_table[i] = NULL;
91         }
92 }
93
94 static int osd_oi_index_create_one(struct osd_thread_info *info,
95                                    struct osd_device *osd, const char *name,
96                                    struct dt_index_features *feat)
97 {
98         const struct lu_env             *env = info->oti_env;
99         struct osd_inode_id             *id  = &info->oti_id;
100         struct buffer_head              *bh;
101         struct inode                    *inode;
102         struct ldiskfs_dir_entry_2      *de;
103         struct dentry                   *dentry;
104         struct super_block              *sb  = osd_sb(osd);
105         struct inode                    *dir = sb->s_root->d_inode;
106         handle_t                        *jh;
107         int                              rc;
108
109         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
110         bh = osd_ldiskfs_find_entry(dir, &dentry->d_name, &de, NULL, NULL);
111         if (!IS_ERR(bh)) {
112                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
113                 brelse(bh);
114                 inode = osd_iget(info, osd, id);
115                 if (!IS_ERR(inode)) {
116                         iput(inode);
117                         inode = ERR_PTR(-EEXIST);
118                 }
119                 return PTR_ERR(inode);
120         }
121
122         if (osd->od_dt_dev.dd_rdonly)
123                 RETURN(-EROFS);
124
125         jh = osd_journal_start_sb(sb, LDISKFS_HT_MISC, 100);
126         if (IS_ERR(jh))
127                 return PTR_ERR(jh);
128
129         inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR),
130                                      NULL);
131         if (IS_ERR(inode)) {
132                 ldiskfs_journal_stop(jh);
133                 return PTR_ERR(inode);
134         }
135
136         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
137         unlock_new_inode(inode);
138
139         if (feat->dif_flags & DT_IND_VARKEY)
140                 rc = iam_lvar_create(inode, feat->dif_keysize_max,
141                                      feat->dif_ptrsize, feat->dif_recsize_max,
142                                      jh);
143         else
144                 rc = iam_lfix_create(inode, feat->dif_keysize_max,
145                                      feat->dif_ptrsize, feat->dif_recsize_max,
146                                      jh);
147         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
148         rc = osd_ldiskfs_add_entry(info, osd, jh, dentry, inode, NULL);
149         ldiskfs_journal_stop(jh);
150         iput(inode);
151         return rc;
152 }
153
154 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
155                                        struct osd_device *osd,
156                                        const char *name,
157                                        struct dt_index_features *f,
158                                        bool create)
159 {
160         struct dentry *dentry;
161         struct inode  *inode;
162         int rc;
163
164         dentry = osd_lookup_one_len_unlocked(osd, name, osd_sb(osd)->s_root,
165                                              strlen(name));
166         if (IS_ERR(dentry))
167                 return ERR_CAST(dentry);
168
169         if (dentry->d_inode) {
170                 LASSERT(!is_bad_inode(dentry->d_inode));
171                 inode = dentry->d_inode;
172                 atomic_inc(&inode->i_count);
173                 dput(dentry);
174                 return inode;
175         }
176
177         /* create */
178         dput(dentry);
179         shrink_dcache_parent(osd_sb(osd)->s_root);
180         if (!create)
181                 return ERR_PTR(-ENOENT);
182
183         rc = osd_oi_index_create_one(info, osd, name, f);
184         if (rc)
185                 return ERR_PTR(rc);
186
187         dentry = osd_lookup_one_len_unlocked(osd, name, osd_sb(osd)->s_root,
188                                              strlen(name));
189         if (IS_ERR(dentry))
190                 return ERR_CAST(dentry);
191
192         if (dentry->d_inode) {
193                 LASSERT(!is_bad_inode(dentry->d_inode));
194                 inode = dentry->d_inode;
195                 atomic_inc(&inode->i_count);
196                 dput(dentry);
197                 return inode;
198         }
199
200         return ERR_PTR(-ENOENT);
201 }
202
203 /**
204  * Open an OI(Ojbect Index) container.
205  *
206  * \param       name    Name of OI container
207  * \param       objp    Pointer of returned OI
208  *
209  * \retval      0       success
210  * \retval      -ve     failure
211  */
212 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
213                        char *name, struct osd_oi **oi_slot, bool create)
214 {
215         struct osd_directory *dir;
216         struct iam_container *bag;
217         struct inode *inode;
218         struct osd_oi *oi;
219         int rc;
220
221         ENTRY;
222
223         oi_feat.dif_keysize_min = sizeof(struct lu_fid);
224         oi_feat.dif_keysize_max = sizeof(struct lu_fid);
225
226         inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
227         if (IS_ERR(inode))
228                 RETURN(PTR_ERR(inode));
229         ldiskfs_set_inode_state(inode, LDISKFS_STATE_IAM);
230
231         if (!osd->od_dt_dev.dd_rdonly) {
232                 /* 'What the @fid is' is not imporatant, because these objects
233                  * have no OI mappings, and only are visible inside the OSD.
234                  */
235                 lu_igif_build(&info->oti_fid, inode->i_ino,
236                               inode->i_generation);
237                 rc = osd_ea_fid_set(info, inode, &info->oti_fid,
238                                     LMAC_NOT_IN_OI, 0);
239                 if (rc)
240                         GOTO(out_inode, rc);
241         }
242
243         OBD_ALLOC_PTR(oi);
244         if (oi == NULL)
245                 GOTO(out_inode, rc = -ENOMEM);
246
247         oi->oi_inode = inode;
248         dir = &oi->oi_dir;
249
250         bag = &dir->od_container;
251         rc = iam_container_init(bag, &dir->od_descr, inode);
252         if (rc < 0)
253                 GOTO(out_free, rc);
254
255         rc = iam_container_setup(bag);
256         if (rc < 0)
257                 GOTO(out_container, rc);
258
259         *oi_slot = oi;
260         RETURN(0);
261
262 out_container:
263         iam_container_fini(bag);
264 out_free:
265         OBD_FREE_PTR(oi);
266 out_inode:
267         iput(inode);
268         return rc;
269 }
270
271 /**
272  * Open OI(Object Index) table.
273  * If \a oi_count is zero, which means caller doesn't know how many OIs there
274  * will be, this function can either return 0 for new filesystem, or number
275  * of OIs on existed filesystem.
276  *
277  * If \a oi_count is non-zero, which means caller does know number of OIs on
278  * filesystem, this function should return the exactly same number on
279  * success, or error code in failure.
280  *
281  * \param     oi_count  Number of expected OI containers
282  * \param     create    Create OIs if doesn't exist
283  *
284  * \retval    +ve       number of opened OI containers
285  * \retval      0       no OI containers found
286  * \retval    -ve       failure
287  */
288 static int
289 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
290                   struct osd_oi **oi_table, unsigned int oi_count, bool create)
291 {
292         struct scrub_file *sf = &osd->od_scrub.os_scrub.os_file;
293         int count = 0;
294         int rc = 0;
295         int i;
296         ENTRY;
297
298         /* NB: oi_count != 0 means that we have already created/known all OIs
299          * and have known exact number of OIs.
300          */
301         LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
302
303         for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
304                 char name[sizeof(OSD_OI_NAME_BASE) + 3 * sizeof(i) + 1];
305
306                 if (oi_table[i] != NULL) {
307                         count++;
308                         continue;
309                 }
310
311                 snprintf(name, sizeof(name), "%s.%d", OSD_OI_NAME_BASE, i);
312                 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
313                 if (rc == 0) {
314                         count++;
315                         continue;
316                 }
317
318                 if (rc == -ENOENT && create == false) {
319                         if (oi_count == 0)
320                                 return count;
321
322                         rc = 0;
323                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
324                         continue;
325                 }
326
327                 CERROR("%s: can't open %s: rc = %d\n",
328                        osd_dev2name(osd), name, rc);
329                 if (oi_count > 0)
330                         CERROR("%s: expect to open total %d OI files.\n",
331                                osd_dev2name(osd), oi_count);
332                 break;
333         }
334
335         if (rc < 0) {
336                 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
337                 count = rc;
338         }
339
340         RETURN(count);
341 }
342
343 static int osd_remove_oi_one(struct osd_device *osd, struct dentry *parent,
344                              const char *name, int namelen)
345 {
346         struct dentry *child;
347         int rc;
348
349         child = osd_lookup_one_len_unlocked(osd, name, parent, namelen);
350         if (IS_ERR(child)) {
351                 rc = PTR_ERR(child);
352         } else {
353                 rc = ll_vfs_unlink(parent->d_inode, child);
354                 dput(child);
355         }
356
357         return rc == -ENOENT ? 0 : rc;
358 }
359
360 static int osd_remove_ois(struct osd_thread_info *info, struct osd_device *osd)
361 {
362         char name[16];
363         int namelen;
364         int rc;
365         int i;
366
367         if (osd->od_dt_dev.dd_rdonly)
368                 RETURN(-EROFS);
369
370         for (i = 0; i < OSD_OI_FID_NR_MAX; i++) {
371                 namelen = scnprintf(name, sizeof(name), "%s.%d",
372                                     OSD_OI_NAME_BASE, i);
373                 rc = osd_remove_oi_one(osd, osd_sb(osd)->s_root, name, namelen);
374                 if (rc != 0) {
375                         CERROR(
376                                "%s: fail to remove the stale OI file %s: rc = %d\n",
377                                osd_dev2name(osd), name, rc);
378                         return rc;
379                 }
380         }
381
382         namelen = scnprintf(name, sizeof(name), "%s", OSD_OI_NAME_BASE);
383         rc = osd_remove_oi_one(osd, osd_sb(osd)->s_root, name, namelen);
384         if (rc != 0)
385                 CERROR("%s: fail to remove the stale OI file %s: rc = %d\n",
386                        osd_dev2name(osd), name, rc);
387
388         return rc;
389 }
390
391 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd,
392                 bool restored)
393 {
394         struct lustre_scrub *scrub = &osd->od_scrub.os_scrub;
395         struct scrub_file *sf = &scrub->os_file;
396         struct osd_oi **oi;
397         int count;
398         int rc;
399         ENTRY;
400
401         if (unlikely(sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
402                 LCONSOLE_WARN("%s: Invalid OI count in scrub file %d\n",
403                               osd_dev2name(osd), sf->sf_oi_count);
404                 sf->sf_oi_count = 0;
405         }
406
407         if (restored) {
408                 LCONSOLE_WARN("%s: reset Object Index mappings\n",
409                               osd_dev2name(osd));
410                 rc = osd_remove_ois(info, osd);
411                 if (rc)
412                         RETURN(rc);
413         }
414
415         OBD_ALLOC_PTR_ARRAY(oi, OSD_OI_FID_NR_MAX);
416         if (oi == NULL)
417                 RETURN(-ENOMEM);
418
419         /* try to open existing multiple OIs first */
420         count = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
421         if (count < 0)
422                 GOTO(out, rc = count);
423
424         if (count > 0) {
425                 if (count == sf->sf_oi_count)
426                         GOTO(out, rc = count);
427
428                 if (sf->sf_oi_count == 0) {
429                         if (likely((count & (count - 1)) == 0))
430                                 GOTO(out, rc = count);
431
432                         LCONSOLE_WARN(
433                                       "%s: invalid oi count %d, remove them, then set it to %d\n",
434                                       osd_dev2name(osd), count, osd_oi_count);
435                         osd_oi_table_put(info, oi, count);
436                         rc = osd_remove_ois(info, osd);
437                         if (rc)
438                                 GOTO(out, rc);
439
440                         sf->sf_oi_count = osd_oi_count;
441                 }
442
443                 scrub_file_reset(scrub, osd->od_uuid, SF_RECREATED);
444                 count = sf->sf_oi_count;
445                 goto create;
446         }
447
448         /* if previous failed then try found single OI from old filesystem */
449         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
450         if (rc == 0) { /* found single OI from old filesystem */
451                 count = 1;
452                 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
453                 if (sf->sf_success_count == 0)
454                         /* XXX: There is one corner case that if the OI_scrub
455                          *      file crashed or lost and we regard it upgrade,
456                          *      then we allow IGIF lookup to bypass OI files.
457                          *
458                          *      The risk is that osd_fid_lookup() may found
459                          *      a wrong inode with the given IGIF especially
460                          *      when the MDT has performed file-level backup
461                          *      and restored after former upgrading from 1.8
462                          *      to 2.x. Fortunately, the osd_fid_lookup()can
463                          *      verify the inode to decrease the risk.
464                          */
465                         scrub_file_reset(scrub, osd->od_uuid, SF_UPGRADE);
466                 GOTO(out, rc = 1);
467         } else if (rc != -ENOENT) {
468                 CERROR("%s: can't open %s: rc = %d\n",
469                        osd_dev2name(osd), OSD_OI_NAME_BASE, rc);
470                 GOTO(out, rc);
471         }
472
473         if (sf->sf_oi_count > 0) {
474                 int i;
475
476                 count = sf->sf_oi_count;
477                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
478                 for (i = 0; i < count; i++)
479                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
480                 scrub_file_reset(scrub, osd->od_uuid, SF_RECREATED);
481         } else {
482                 count = sf->sf_oi_count = osd_oi_count;
483         }
484
485 create:
486         rc = scrub_file_store(info->oti_env, scrub);
487         if (rc < 0) {
488                 osd_oi_table_put(info, oi, count);
489                 GOTO(out, rc);
490         }
491
492         /* No OIs exist, new filesystem, create OI objects */
493         rc = osd_oi_table_open(info, osd, oi, count, true);
494         LASSERT(ergo(rc >= 0, rc == count));
495
496         GOTO(out, rc);
497
498 out:
499         if (rc < 0) {
500                 OBD_FREE_PTR_ARRAY(oi, OSD_OI_FID_NR_MAX);
501         } else {
502                 LASSERTF((rc & (rc - 1)) == 0, "Invalid OI count %d\n", rc);
503
504                 osd->od_oi_table = oi;
505                 osd->od_oi_count = rc;
506                 if (sf->sf_oi_count != rc) {
507                         sf->sf_oi_count = rc;
508                         rc = scrub_file_store(info->oti_env, scrub);
509                         if (rc < 0) {
510                                 osd_oi_table_put(info, oi, count);
511                                 OBD_FREE_PTR_ARRAY(oi, OSD_OI_FID_NR_MAX);
512                         }
513                 } else {
514                         rc = 0;
515                 }
516         }
517
518         return rc;
519 }
520
521 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
522 {
523         if (unlikely(!osd->od_oi_table))
524                 return;
525
526         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
527
528         OBD_FREE_PTR_ARRAY(osd->od_oi_table, OSD_OI_FID_NR_MAX);
529         osd->od_oi_table = NULL;
530 }
531
532 static inline int fid_is_fs_root(const struct lu_fid *fid)
533 {
534         /* Map root inode to special local object FID */
535         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
536                          fid_oid(fid) == OSD_FS_ROOT_OID));
537 }
538
539 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
540                              struct osd_oi *oi, struct dt_rec *rec,
541                              const struct dt_key *key)
542 {
543         struct iam_container  *bag;
544         struct iam_iterator   *it = &oti->oti_idx_it;
545         struct iam_path_descr *ipd;
546         int rc;
547         ENTRY;
548
549         LASSERT(oi);
550         LASSERT(oi->oi_inode);
551
552         bag = &oi->oi_dir.od_container;
553         ipd = osd_idx_ipd_get(oti->oti_env, bag);
554         if (IS_ERR(ipd))
555                 RETURN(-ENOMEM);
556
557         /* got ipd now we can start iterator. */
558         iam_it_init(it, bag, 0, ipd);
559
560         rc = iam_it_get(it, (struct iam_key *)key);
561         if (rc > 0)
562                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
563         iam_it_put(it);
564         iam_it_fini(it);
565         osd_ipd_put(oti->oti_env, bag, ipd);
566
567         LINVRNT(osd_invariant(obj));
568
569         RETURN(rc);
570 }
571
572 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
573                   const struct lu_fid *fid, enum oi_check_flags flags)
574 {
575         struct lu_seq_range *range = &info->oti_seq_range;
576         int rc;
577         ENTRY;
578
579         if (flags & OI_KNOWN_ON_OST)
580                 RETURN(1);
581
582         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
583                      fid_is_llog(fid)) || fid_is_name_llog(fid) ||
584                      fid_is_quota(fid))
585                 RETURN(0);
586
587         if (fid_is_idif(fid) || fid_is_last_id(fid))
588                 RETURN(1);
589
590         if (!(flags & OI_CHECK_FLD))
591                 RETURN(0);
592
593         if (osd_seq_site(osd)->ss_server_fld == NULL)
594                 RETURN(0);
595
596         rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
597         if (rc != 0) {
598                 /* During upgrade, OST FLDB might not be loaded because
599                  * OST FLDB is not created until 2.6, so if some DNE
600                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
601                  * not be able to find the sequence from local FLDB
602                  * cache see fld_index_init().
603                  */
604                 if (rc == -ENOENT && osd->od_is_ost)
605                         RETURN(1);
606
607                 if (rc != -ENOENT)
608                         CERROR("%s: lookup FLD "DFID": rc = %d\n",
609                                osd_name(osd), PFID(fid), rc);
610                 RETURN(0);
611         }
612
613         if (fld_range_is_ost(range))
614                 RETURN(1);
615
616         RETURN(0);
617 }
618
619 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
620                            const struct lu_fid *fid, struct osd_inode_id *id)
621 {
622         struct lu_fid *oi_fid = &info->oti_fid2;
623         int rc;
624
625         fid_cpu_to_be(oi_fid, fid);
626         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
627                                (const struct dt_key *)oi_fid);
628         if (rc > 0) {
629                 osd_id_unpack(id, id);
630                 rc = 0;
631         } else if (rc == 0) {
632                 rc = -ENOENT;
633         }
634         return rc;
635 }
636
637 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
638                   const struct lu_fid *fid, struct osd_inode_id *id,
639                   enum oi_check_flags flags)
640 {
641         if (unlikely(fid_is_last_id(fid)))
642                 return osd_obj_spec_lookup(info, osd, fid, id, flags);
643
644         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
645                 return osd_obj_map_lookup(info, osd, fid, id);
646
647         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
648                 int rc;
649
650                 if (fid_is_fs_root(fid)) {
651                         osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
652                                    osd_sb(osd)->s_root->d_inode->i_generation);
653                         return 0;
654                 }
655                 if (unlikely(fid_is_acct(fid)))
656                         return osd_acct_obj_lookup(info, osd, fid, id);
657
658                 /* For other special FIDs, try OI first, then do spec lookup */
659                 rc = __osd_oi_lookup(info, osd, fid, id);
660                 if (rc == -ENOENT)
661                         return osd_obj_spec_lookup(info, osd, fid, id, flags);
662                 return rc;
663         }
664
665         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
666                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
667                 return 0;
668         }
669
670         return __osd_oi_lookup(info, osd, fid, id);
671 }
672
673 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
674                              const struct dt_rec *rec, const struct dt_key *key,
675                              handle_t *th, bool insert)
676 {
677         struct iam_container *bag;
678         struct iam_path_descr *ipd;
679         int rc;
680         ENTRY;
681
682         LASSERT(oi);
683         LASSERT(oi->oi_inode);
684         dquot_initialize(oi->oi_inode);
685
686         bag = &oi->oi_dir.od_container;
687         ipd = osd_idx_ipd_get(oti->oti_env, bag);
688         if (unlikely(ipd == NULL))
689                 RETURN(-ENOMEM);
690
691         LASSERT(th != NULL);
692         LASSERT(th->h_transaction != NULL);
693         if (insert)
694                 rc = iam_insert(th, bag, (const struct iam_key *)key,
695                                 (const struct iam_rec *)rec, ipd);
696         else
697                 rc = iam_update(th, bag, (const struct iam_key *)key,
698                                 (const struct iam_rec *)rec, ipd);
699         osd_ipd_put(oti->oti_env, bag, ipd);
700         LINVRNT(osd_invariant(obj));
701         RETURN(rc);
702 }
703
704 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
705                   const struct lu_fid *fid, const struct osd_inode_id *id,
706                   handle_t *th, enum oi_check_flags flags, bool *exist)
707 {
708         struct lu_fid       *oi_fid = &info->oti_fid2;
709         struct osd_inode_id *oi_id  = &info->oti_id2;
710         int                  rc     = 0;
711
712         if (unlikely(fid_is_last_id(fid)))
713                 return osd_obj_spec_insert(info, osd, fid, id, th);
714
715         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
716                 return osd_obj_map_insert(info, osd, fid, id, th);
717
718         fid_cpu_to_be(oi_fid, fid);
719         osd_id_pack(oi_id, id);
720         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
721                                (const struct dt_rec *)oi_id,
722                                (const struct dt_key *)oi_fid, th, true);
723         if (rc != 0) {
724                 struct inode *inode;
725                 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
726
727                 if (rc != -EEXIST)
728                         return rc;
729
730                 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
731                 if (rc != 0)
732                         return rc;
733
734                 if (unlikely(osd_id_eq(id, oi_id)))
735                         return 1;
736
737                 /* Check whether the mapping for oi_id is valid or not. */
738                 inode = osd_iget(info, osd, oi_id);
739                 if (IS_ERR(inode)) {
740                         rc = PTR_ERR(inode);
741                         if (rc == -ENOENT || rc == -ESTALE)
742                                 goto update;
743                         return rc;
744                 }
745
746                 /* The EA inode should NOT be in OI, old OI scrub may added
747                  * such OI mapping by wrong, replace it.
748                  */
749                 if (unlikely(osd_is_ea_inode(inode))) {
750                         iput(inode);
751                         goto update;
752                 }
753
754                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
755                                  &info->oti_ost_attrs);
756                 iput(inode);
757                 if (rc == -ENODATA)
758                         goto update;
759
760                 if (rc != 0)
761                         return rc;
762
763                 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
764                     lu_fid_eq(fid, &lma->lma_self_fid)) {
765                         CERROR(
766                                "%s: the FID "DFID" is used by two objects: %u/%u %u/%u\n",
767                                osd_dev2name(osd), PFID(fid), oi_id->oii_ino,
768                                oi_id->oii_gen, id->oii_ino, id->oii_gen);
769                         return -EEXIST;
770                 }
771
772 update:
773                 osd_id_pack(oi_id, id);
774                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
775                                         (const struct dt_rec *)oi_id,
776                                         (const struct dt_key *)oi_fid, th,
777                                         false);
778                 if (rc != 0)
779                         return rc;
780
781                 if (exist != NULL)
782                         *exist = true;
783         }
784
785         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
786                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
787         return rc;
788 }
789
790 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
791                              const struct dt_key *key, handle_t *th)
792 {
793         struct iam_container    *bag;
794         struct iam_path_descr   *ipd;
795         int                      rc;
796         ENTRY;
797
798         LASSERT(oi);
799         LASSERT(oi->oi_inode);
800         dquot_initialize(oi->oi_inode);
801
802         bag = &oi->oi_dir.od_container;
803         ipd = osd_idx_ipd_get(oti->oti_env, bag);
804         if (unlikely(ipd == NULL))
805                 RETURN(-ENOMEM);
806
807         LASSERT(th != NULL);
808         LASSERT(th->h_transaction != NULL);
809
810         rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
811         osd_ipd_put(oti->oti_env, bag, ipd);
812         LINVRNT(osd_invariant(obj));
813         RETURN(rc);
814 }
815
816 int osd_oi_delete(struct osd_thread_info *info,
817                   struct osd_device *osd, const struct lu_fid *fid,
818                   handle_t *th, enum oi_check_flags flags)
819 {
820         struct lu_fid *oi_fid = &info->oti_fid2;
821
822         /* clear idmap cache */
823         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
824                 fid_zero(&info->oti_cache.oic_fid);
825
826         if (fid_is_last_id(fid))
827                 return 0;
828
829         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
830                 return osd_obj_map_delete(info, osd, fid, th);
831
832         fid_cpu_to_be(oi_fid, fid);
833         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
834                                  (const struct dt_key *)oi_fid, th);
835 }
836
837 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
838                   const struct lu_fid *fid, const struct osd_inode_id *id,
839                   handle_t *th, enum oi_check_flags flags)
840 {
841         struct lu_fid       *oi_fid = &info->oti_fid2;
842         struct osd_inode_id *oi_id  = &info->oti_id2;
843         int                  rc     = 0;
844
845         if (unlikely(fid_is_last_id(fid)))
846                 return osd_obj_spec_update(info, osd, fid, id, th);
847
848         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
849                 return osd_obj_map_update(info, osd, fid, id, th);
850
851         fid_cpu_to_be(oi_fid, fid);
852         osd_id_pack(oi_id, id);
853         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
854                                (const struct dt_rec *)oi_id,
855                                (const struct dt_key *)oi_fid, th, false);
856         if (rc != 0)
857                 return rc;
858
859         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
860                 rc = osd_obj_spec_update(info, osd, fid, id, th);
861         return rc;
862 }
863
864 int osd_oi_mod_init(void)
865 {
866         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
867                 osd_oi_count = OSD_OI_FID_NR;
868
869         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
870                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
871                               osd_oi_count, size_roundup_power2(osd_oi_count));
872                 osd_oi_count = size_roundup_power2(osd_oi_count);
873         }
874
875         return 0;
876 }