Whamcloud - gitweb
LU-11838 scrub: handle s_uuid change to uuid_t
[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, osd->od_uuid, SF_RECREATED);
436                 count = sf->sf_oi_count;
437                 goto create;
438         }
439
440         /* if previous failed then try found single OI from old filesystem */
441         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
442         if (rc == 0) { /* found single OI from old filesystem */
443                 count = 1;
444                 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
445                 if (sf->sf_success_count == 0)
446                         /* XXX: There is one corner case that if the OI_scrub
447                          *      file crashed or lost and we regard it upgrade,
448                          *      then we allow IGIF lookup to bypass OI files.
449                          *
450                          *      The risk is that osd_fid_lookup() may found
451                          *      a wrong inode with the given IGIF especially
452                          *      when the MDT has performed file-level backup
453                          *      and restored after former upgrading from 1.8
454                          *      to 2.x. Fortunately, the osd_fid_lookup()can
455                          *      verify the inode to decrease the risk. */
456                         scrub_file_reset(scrub, osd->od_uuid, SF_UPGRADE);
457                 GOTO(out, rc = 1);
458         } else if (rc != -ENOENT) {
459                 CERROR("%s: can't open %s: rc = %d\n",
460                        osd_dev2name(osd), OSD_OI_NAME_BASE, rc);
461                 GOTO(out, rc);
462         }
463
464         if (sf->sf_oi_count > 0) {
465                 int i;
466
467                 count = sf->sf_oi_count;
468                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
469                 for (i = 0; i < count; i++)
470                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
471                 scrub_file_reset(scrub, osd->od_uuid, SF_RECREATED);
472         } else {
473                 count = sf->sf_oi_count = osd_oi_count;
474         }
475
476 create:
477         rc = scrub_file_store(info->oti_env, scrub);
478         if (rc < 0) {
479                 osd_oi_table_put(info, oi, count);
480                 GOTO(out, rc);
481         }
482
483         /* No OIs exist, new filesystem, create OI objects */
484         rc = osd_oi_table_open(info, osd, oi, count, true);
485         LASSERT(ergo(rc >= 0, rc == count));
486
487         GOTO(out, rc);
488
489 out:
490         if (rc < 0) {
491                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
492         } else {
493                 LASSERTF((rc & (rc - 1)) == 0, "Invalid OI count %d\n", rc);
494
495                 osd->od_oi_table = oi;
496                 osd->od_oi_count = rc;
497                 if (sf->sf_oi_count != rc) {
498                         sf->sf_oi_count = rc;
499                         rc = scrub_file_store(info->oti_env, scrub);
500                         if (rc < 0) {
501                                 osd_oi_table_put(info, oi, count);
502                                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
503                         }
504                 } else {
505                         rc = 0;
506                 }
507         }
508
509         return rc;
510 }
511
512 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
513 {
514         if (unlikely(!osd->od_oi_table))
515                 return;
516
517         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
518
519         OBD_FREE(osd->od_oi_table,
520                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
521         osd->od_oi_table = NULL;
522 }
523
524 static inline int fid_is_fs_root(const struct lu_fid *fid)
525 {
526         /* Map root inode to special local object FID */
527         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
528                          fid_oid(fid) == OSD_FS_ROOT_OID));
529 }
530
531 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
532                              struct osd_oi *oi, struct dt_rec *rec,
533                              const struct dt_key *key)
534 {
535         struct iam_container  *bag;
536         struct iam_iterator   *it = &oti->oti_idx_it;
537         struct iam_path_descr *ipd;
538         int                    rc;
539         ENTRY;
540
541         LASSERT(oi);
542         LASSERT(oi->oi_inode);
543
544         bag = &oi->oi_dir.od_container;
545         ipd = osd_idx_ipd_get(oti->oti_env, bag);
546         if (IS_ERR(ipd))
547                 RETURN(-ENOMEM);
548
549         /* got ipd now we can start iterator. */
550         iam_it_init(it, bag, 0, ipd);
551
552         rc = iam_it_get(it, (struct iam_key *)key);
553         if (rc > 0)
554                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
555         iam_it_put(it);
556         iam_it_fini(it);
557         osd_ipd_put(oti->oti_env, bag, ipd);
558
559         LINVRNT(osd_invariant(obj));
560
561         RETURN(rc);
562 }
563
564 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
565                   const struct lu_fid *fid, enum oi_check_flags flags)
566 {
567         struct lu_seq_range     *range = &info->oti_seq_range;
568         int                     rc;
569         ENTRY;
570
571         if (flags & OI_KNOWN_ON_OST)
572                 RETURN(1);
573
574         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
575                      fid_is_llog(fid)) || fid_is_name_llog(fid) ||
576                      fid_is_quota(fid))
577                 RETURN(0);
578
579         if (fid_is_idif(fid) || fid_is_last_id(fid))
580                 RETURN(1);
581
582         if (!(flags & OI_CHECK_FLD))
583                 RETURN(0);
584
585         if (osd_seq_site(osd)->ss_server_fld == NULL)
586                 RETURN(0);
587
588         rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
589         if (rc != 0) {
590                 /* During upgrade, OST FLDB might not be loaded because
591                  * OST FLDB is not created until 2.6, so if some DNE
592                  * filesystem upgrade from 2.5 to 2.7/2.8, they will
593                  * not be able to find the sequence from local FLDB
594                  * cache see fld_index_init(). */
595                 if (rc == -ENOENT && osd->od_is_ost)
596                         RETURN(1);
597
598                 if (rc != -ENOENT)
599                         CERROR("%s: lookup FLD "DFID": rc = %d\n",
600                                osd_name(osd), PFID(fid), rc);
601                 RETURN(0);
602         }
603
604         if (fld_range_is_ost(range))
605                 RETURN(1);
606
607         RETURN(0);
608 }
609
610 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
611                            const struct lu_fid *fid, struct osd_inode_id *id)
612 {
613         struct lu_fid *oi_fid = &info->oti_fid2;
614         int            rc;
615
616         fid_cpu_to_be(oi_fid, fid);
617         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
618                                (const struct dt_key *)oi_fid);
619         if (rc > 0) {
620                 osd_id_unpack(id, id);
621                 rc = 0;
622         } else if (rc == 0) {
623                 rc = -ENOENT;
624         }
625         return rc;
626 }
627
628 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
629                   const struct lu_fid *fid, struct osd_inode_id *id,
630                   enum oi_check_flags flags)
631 {
632         if (unlikely(fid_is_last_id(fid)))
633                 return osd_obj_spec_lookup(info, osd, fid, id);
634
635         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
636                 return osd_obj_map_lookup(info, osd, fid, id);
637
638
639         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
640                 int rc;
641                 if (fid_is_fs_root(fid)) {
642                         osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
643                                    osd_sb(osd)->s_root->d_inode->i_generation);
644                         return 0;
645                 }
646                 if (unlikely(fid_is_acct(fid)))
647                         return osd_acct_obj_lookup(info, osd, fid, id);
648
649                 /* For other special FIDs, try OI first, then do spec lookup */
650                 rc = __osd_oi_lookup(info, osd, fid, id);
651                 if (rc == -ENOENT)
652                         return osd_obj_spec_lookup(info, osd, fid, id);
653                 return rc;
654         }
655
656         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
657                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
658                 return 0;
659         }
660
661         return __osd_oi_lookup(info, osd, fid, id);
662 }
663
664 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
665                              const struct dt_rec *rec, const struct dt_key *key,
666                              handle_t *th, bool insert)
667 {
668         struct iam_container    *bag;
669         struct iam_path_descr   *ipd;
670         int                     rc;
671         ENTRY;
672
673         LASSERT(oi);
674         LASSERT(oi->oi_inode);
675         ll_vfs_dq_init(oi->oi_inode);
676
677         bag = &oi->oi_dir.od_container;
678         ipd = osd_idx_ipd_get(oti->oti_env, bag);
679         if (unlikely(ipd == NULL))
680                 RETURN(-ENOMEM);
681
682         LASSERT(th != NULL);
683         LASSERT(th->h_transaction != NULL);
684         if (insert)
685                 rc = iam_insert(th, bag, (const struct iam_key *)key,
686                                 (const struct iam_rec *)rec, ipd);
687         else
688                 rc = iam_update(th, bag, (const struct iam_key *)key,
689                                 (const struct iam_rec *)rec, ipd);
690         osd_ipd_put(oti->oti_env, bag, ipd);
691         LINVRNT(osd_invariant(obj));
692         RETURN(rc);
693 }
694
695 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
696                   const struct lu_fid *fid, const struct osd_inode_id *id,
697                   handle_t *th, enum oi_check_flags flags, bool *exist)
698 {
699         struct lu_fid       *oi_fid = &info->oti_fid2;
700         struct osd_inode_id *oi_id  = &info->oti_id2;
701         int                  rc     = 0;
702
703         if (unlikely(fid_is_last_id(fid)))
704                 return osd_obj_spec_insert(info, osd, fid, id, th);
705
706         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
707                 return osd_obj_map_insert(info, osd, fid, id, th);
708
709         fid_cpu_to_be(oi_fid, fid);
710         osd_id_pack(oi_id, id);
711         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
712                                (const struct dt_rec *)oi_id,
713                                (const struct dt_key *)oi_fid, th, true);
714         if (rc != 0) {
715                 struct inode *inode;
716                 struct lustre_mdt_attrs *lma = &info->oti_ost_attrs.loa_lma;
717
718                 if (rc != -EEXIST)
719                         return rc;
720
721                 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
722                 if (rc != 0)
723                         return rc;
724
725                 if (unlikely(osd_id_eq(id, oi_id)))
726                         return 1;
727
728                 /* Check whether the mapping for oi_id is valid or not. */
729                 inode = osd_iget(info, osd, oi_id);
730                 if (IS_ERR(inode)) {
731                         rc = PTR_ERR(inode);
732                         if (rc == -ENOENT || rc == -ESTALE)
733                                 goto update;
734                         return rc;
735                 }
736
737                 /* The EA inode should NOT be in OI, old OI scrub may added
738                  * such OI mapping by wrong, replace it. */
739                 if (unlikely(osd_is_ea_inode(inode))) {
740                         iput(inode);
741                         goto update;
742                 }
743
744                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry,
745                                  &info->oti_ost_attrs);
746                 iput(inode);
747                 if (rc == -ENODATA)
748                         goto update;
749
750                 if (rc != 0)
751                         return rc;
752
753                 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
754                     lu_fid_eq(fid, &lma->lma_self_fid)) {
755                         CERROR("%s: the FID "DFID" is used by two objects: "
756                                "%u/%u %u/%u\n", osd_dev2name(osd),
757                                PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
758                                id->oii_ino, id->oii_gen);
759                         return -EEXIST;
760                 }
761
762 update:
763                 osd_id_pack(oi_id, id);
764                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
765                                         (const struct dt_rec *)oi_id,
766                                         (const struct dt_key *)oi_fid, th, false);
767                 if (rc != 0)
768                         return rc;
769
770                 if (exist != NULL)
771                         *exist = true;
772         }
773
774         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
775                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
776         return rc;
777 }
778
779 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
780                              const struct dt_key *key, handle_t *th)
781 {
782         struct iam_container    *bag;
783         struct iam_path_descr   *ipd;
784         int                      rc;
785         ENTRY;
786
787         LASSERT(oi);
788         LASSERT(oi->oi_inode);
789         ll_vfs_dq_init(oi->oi_inode);
790
791         bag = &oi->oi_dir.od_container;
792         ipd = osd_idx_ipd_get(oti->oti_env, bag);
793         if (unlikely(ipd == NULL))
794                 RETURN(-ENOMEM);
795
796         LASSERT(th != NULL);
797         LASSERT(th->h_transaction != NULL);
798
799         rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
800         osd_ipd_put(oti->oti_env, bag, ipd);
801         LINVRNT(osd_invariant(obj));
802         RETURN(rc);
803 }
804
805 int osd_oi_delete(struct osd_thread_info *info,
806                   struct osd_device *osd, const struct lu_fid *fid,
807                   handle_t *th, enum oi_check_flags flags)
808 {
809         struct lu_fid *oi_fid = &info->oti_fid2;
810
811         /* clear idmap cache */
812         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
813                 fid_zero(&info->oti_cache.oic_fid);
814
815         if (fid_is_last_id(fid))
816                 return 0;
817
818         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
819                 return osd_obj_map_delete(info, osd, fid, th);
820
821         fid_cpu_to_be(oi_fid, fid);
822         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
823                                  (const struct dt_key *)oi_fid, th);
824 }
825
826 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
827                   const struct lu_fid *fid, const struct osd_inode_id *id,
828                   handle_t *th, enum oi_check_flags flags)
829 {
830         struct lu_fid       *oi_fid = &info->oti_fid2;
831         struct osd_inode_id *oi_id  = &info->oti_id2;
832         int                  rc     = 0;
833
834         if (unlikely(fid_is_last_id(fid)))
835                 return osd_obj_spec_update(info, osd, fid, id, th);
836
837         if (fid_is_llog(fid) || fid_is_on_ost(info, osd, fid, flags))
838                 return osd_obj_map_update(info, osd, fid, id, th);
839
840         fid_cpu_to_be(oi_fid, fid);
841         osd_id_pack(oi_id, id);
842         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
843                                (const struct dt_rec *)oi_id,
844                                (const struct dt_key *)oi_fid, th, false);
845         if (rc != 0)
846                 return rc;
847
848         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
849                 rc = osd_obj_spec_update(info, osd, fid, id, th);
850         return rc;
851 }
852
853 int osd_oi_mod_init(void)
854 {
855         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
856                 osd_oi_count = OSD_OI_FID_NR;
857
858         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
859                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
860                               osd_oi_count, size_roundup_power2(osd_oi_count));
861                 osd_oi_count = size_roundup_power2(osd_oi_count);
862         }
863
864         return 0;
865 }