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