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