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