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