Whamcloud - gitweb
LU-6298 hsm: shutdown HSM CDTs in parallel
[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(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 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
342 {
343         struct osd_scrub  *scrub = &osd->od_scrub;
344         struct scrub_file *sf = &scrub->os_file;
345         struct osd_oi    **oi;
346         int                rc;
347         ENTRY;
348
349         OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
350         if (oi == NULL)
351                 RETURN(-ENOMEM);
352
353         mutex_lock(&oi_init_lock);
354         /* try to open existing multiple OIs first */
355         rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
356         if (rc < 0)
357                 GOTO(out, rc);
358
359         if (rc > 0) {
360                 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
361                         GOTO(out, rc);
362
363                 osd_scrub_file_reset(scrub,
364                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
365                                      SF_RECREATED);
366                 osd_oi_count = sf->sf_oi_count;
367                 goto create;
368         }
369
370         /* if previous failed then try found single OI from old filesystem */
371         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
372         if (rc == 0) { /* found single OI from old filesystem */
373                 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
374                 if (sf->sf_success_count == 0)
375                         /* XXX: There is one corner case that if the OI_scrub
376                          *      file crashed or lost and we regard it upgrade,
377                          *      then we allow IGIF lookup to bypass OI files.
378                          *
379                          *      The risk is that osd_fid_lookup() may found
380                          *      a wrong inode with the given IGIF especially
381                          *      when the MDT has performed file-level backup
382                          *      and restored after former upgrading from 1.8
383                          *      to 2.x. Fortunately, the osd_fid_lookup()can
384                          *      verify the inode to decrease the risk. */
385                         osd_scrub_file_reset(scrub,
386                                         LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
387                                         SF_UPGRADE);
388                 GOTO(out, rc = 1);
389         } else if (rc != -ENOENT) {
390                 CERROR("%.16s: can't open %s: rc = %d\n",
391                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
392                        OSD_OI_NAME_BASE, rc);
393                 GOTO(out, rc);
394         }
395
396         if (sf->sf_oi_count > 0) {
397                 int i;
398
399                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
400                 for (i = 0; i < osd_oi_count; i++)
401                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
402                 osd_scrub_file_reset(scrub,
403                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
404                                      SF_RECREATED);
405         }
406         sf->sf_oi_count = osd_oi_count;
407
408 create:
409         rc = osd_scrub_file_store(scrub);
410         if (rc < 0) {
411                 osd_oi_table_put(info, oi, sf->sf_oi_count);
412                 GOTO(out, rc);
413         }
414
415         /* No OIs exist, new filesystem, create OI objects */
416         rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
417         LASSERT(ergo(rc >= 0, rc == osd_oi_count));
418
419         GOTO(out, rc);
420
421 out:
422         if (rc < 0) {
423                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
424         } else {
425                 LASSERT((rc & (rc - 1)) == 0);
426                 osd->od_oi_table = oi;
427                 osd->od_oi_count = rc;
428                 if (sf->sf_oi_count != rc) {
429                         sf->sf_oi_count = rc;
430                         rc = osd_scrub_file_store(scrub);
431                         if (rc < 0) {
432                                 osd_oi_table_put(info, oi, sf->sf_oi_count);
433                                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
434                         }
435                 } else {
436                         rc = 0;
437                 }
438         }
439
440         mutex_unlock(&oi_init_lock);
441         return rc;
442 }
443
444 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
445 {
446         if (unlikely(osd->od_oi_table == NULL))
447                 return;
448
449         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
450
451         OBD_FREE(osd->od_oi_table,
452                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
453         osd->od_oi_table = NULL;
454 }
455
456 static inline int fid_is_fs_root(const struct lu_fid *fid)
457 {
458         /* Map root inode to special local object FID */
459         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
460                          fid_oid(fid) == OSD_FS_ROOT_OID));
461 }
462
463 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
464                              struct osd_oi *oi, struct dt_rec *rec,
465                              const struct dt_key *key)
466 {
467         struct iam_container  *bag;
468         struct iam_iterator   *it = &oti->oti_idx_it;
469         struct iam_path_descr *ipd;
470         int                    rc;
471         ENTRY;
472
473         LASSERT(oi);
474         LASSERT(oi->oi_inode);
475
476         bag = &oi->oi_dir.od_container;
477         ipd = osd_idx_ipd_get(oti->oti_env, bag);
478         if (IS_ERR(ipd))
479                 RETURN(-ENOMEM);
480
481         /* got ipd now we can start iterator. */
482         iam_it_init(it, bag, 0, ipd);
483
484         rc = iam_it_get(it, (struct iam_key *)key);
485         if (rc > 0)
486                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
487         iam_it_put(it);
488         iam_it_fini(it);
489         osd_ipd_put(oti->oti_env, bag, ipd);
490
491         LINVRNT(osd_invariant(obj));
492
493         RETURN(rc);
494 }
495
496 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
497                   const struct lu_fid *fid, enum oi_check_flags flags)
498 {
499         struct lu_seq_range     *range = &info->oti_seq_range;
500         int                     rc;
501         ENTRY;
502
503         if (flags & OI_KNOWN_ON_OST)
504                 RETURN(1);
505
506         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
507                      fid_is_llog(fid)) || fid_is_name_llog(fid) ||
508                      fid_is_quota(fid))
509                 RETURN(0);
510
511         if (fid_is_idif(fid) || fid_is_last_id(fid))
512                 RETURN(1);
513
514         if (!(flags & OI_CHECK_FLD))
515                 RETURN(0);
516
517         if (osd_seq_site(osd)->ss_server_fld == NULL)
518                 RETURN(0);
519
520         rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
521         if (rc != 0) {
522                 if (rc != -ENOENT)
523                         CERROR("%s: lookup FLD "DFID": rc = %d\n",
524                                osd_name(osd), PFID(fid), rc);
525                 RETURN(0);
526         }
527
528         if (fld_range_is_ost(range))
529                 RETURN(1);
530
531         RETURN(0);
532 }
533
534 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
535                            const struct lu_fid *fid, struct osd_inode_id *id)
536 {
537         struct lu_fid *oi_fid = &info->oti_fid2;
538         int            rc;
539
540         fid_cpu_to_be(oi_fid, fid);
541         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
542                                (const struct dt_key *)oi_fid);
543         if (rc > 0) {
544                 osd_id_unpack(id, id);
545                 rc = 0;
546         } else if (rc == 0) {
547                 rc = -ENOENT;
548         }
549         return rc;
550 }
551
552 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
553                   const struct lu_fid *fid, struct osd_inode_id *id,
554                   enum oi_check_flags flags)
555 {
556         if (unlikely(fid_is_last_id(fid)))
557                 return osd_obj_spec_lookup(info, osd, fid, id);
558
559         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
560                 return osd_obj_map_lookup(info, osd, fid, id);
561
562
563         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
564                 int rc;
565                 if (fid_is_fs_root(fid)) {
566                         osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
567                                    osd_sb(osd)->s_root->d_inode->i_generation);
568                         return 0;
569                 }
570                 if (unlikely(fid_is_acct(fid)))
571                         return osd_acct_obj_lookup(info, osd, fid, id);
572
573                 /* For other special FIDs, try OI first, then do spec lookup */
574                 rc = __osd_oi_lookup(info, osd, fid, id);
575                 if (rc == -ENOENT)
576                         return osd_obj_spec_lookup(info, osd, fid, id);
577                 return rc;
578         }
579
580         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
581                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
582                 return 0;
583         }
584
585         return __osd_oi_lookup(info, osd, fid, id);
586 }
587
588 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
589                              const struct dt_rec *rec, const struct dt_key *key,
590                              handle_t *th, bool insert)
591 {
592         struct iam_container    *bag;
593         struct iam_path_descr   *ipd;
594         int                     rc;
595         ENTRY;
596
597         LASSERT(oi);
598         LASSERT(oi->oi_inode);
599         ll_vfs_dq_init(oi->oi_inode);
600
601         bag = &oi->oi_dir.od_container;
602         ipd = osd_idx_ipd_get(oti->oti_env, bag);
603         if (unlikely(ipd == NULL))
604                 RETURN(-ENOMEM);
605
606         LASSERT(th != NULL);
607         LASSERT(th->h_transaction != NULL);
608         if (insert)
609                 rc = iam_insert(th, bag, (const struct iam_key *)key,
610                                 (const struct iam_rec *)rec, ipd);
611         else
612                 rc = iam_update(th, bag, (const struct iam_key *)key,
613                                 (const struct iam_rec *)rec, ipd);
614         osd_ipd_put(oti->oti_env, bag, ipd);
615         LINVRNT(osd_invariant(obj));
616         RETURN(rc);
617 }
618
619 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
620                   const struct lu_fid *fid, const struct osd_inode_id *id,
621                   handle_t *th, enum oi_check_flags flags)
622 {
623         struct lu_fid       *oi_fid = &info->oti_fid2;
624         struct osd_inode_id *oi_id  = &info->oti_id2;
625         int                  rc     = 0;
626
627         if (unlikely(fid_is_last_id(fid)))
628                 return osd_obj_spec_insert(info, osd, fid, id, th);
629
630         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
631                 return osd_obj_map_insert(info, osd, fid, id, th);
632
633         fid_cpu_to_be(oi_fid, fid);
634         osd_id_pack(oi_id, id);
635         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
636                                (const struct dt_rec *)oi_id,
637                                (const struct dt_key *)oi_fid, th, true);
638         if (rc != 0) {
639                 struct inode *inode;
640                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
641
642                 if (rc != -EEXIST)
643                         return rc;
644
645                 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
646                 if (rc != 0)
647                         return rc;
648
649                 if (unlikely(osd_id_eq(id, oi_id)))
650                         return 1;
651
652                 /* Check whether the mapping for oi_id is valid or not. */
653                 inode = osd_iget(info, osd, oi_id);
654                 if (IS_ERR(inode)) {
655                         rc = PTR_ERR(inode);
656                         if (rc == -ENOENT || rc == -ESTALE)
657                                 goto update;
658                         return rc;
659                 }
660
661                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
662                 iput(inode);
663                 if (rc == -ENODATA)
664                         goto update;
665
666                 if (rc != 0)
667                         return rc;
668
669                 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
670                     lu_fid_eq(fid, &lma->lma_self_fid)) {
671                         CERROR("%.16s: the FID "DFID" is used by two objects: "
672                                "%u/%u %u/%u\n",
673                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
674                                PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
675                                id->oii_ino, id->oii_gen);
676                         return -EEXIST;
677                 }
678
679 update:
680                 osd_id_pack(oi_id, id);
681                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
682                                         (const struct dt_rec *)oi_id,
683                                         (const struct dt_key *)oi_fid, th, false);
684                 if (rc != 0)
685                         return rc;
686         }
687
688         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
689                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
690         return rc;
691 }
692
693 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
694                              const struct dt_key *key, handle_t *th)
695 {
696         struct iam_container    *bag;
697         struct iam_path_descr   *ipd;
698         int                      rc;
699         ENTRY;
700
701         LASSERT(oi);
702         LASSERT(oi->oi_inode);
703         ll_vfs_dq_init(oi->oi_inode);
704
705         bag = &oi->oi_dir.od_container;
706         ipd = osd_idx_ipd_get(oti->oti_env, bag);
707         if (unlikely(ipd == NULL))
708                 RETURN(-ENOMEM);
709
710         LASSERT(th != NULL);
711         LASSERT(th->h_transaction != NULL);
712
713         rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
714         osd_ipd_put(oti->oti_env, bag, ipd);
715         LINVRNT(osd_invariant(obj));
716         RETURN(rc);
717 }
718
719 int osd_oi_delete(struct osd_thread_info *info,
720                   struct osd_device *osd, const struct lu_fid *fid,
721                   handle_t *th, enum oi_check_flags flags)
722 {
723         struct lu_fid *oi_fid = &info->oti_fid2;
724
725         /* clear idmap cache */
726         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
727                 fid_zero(&info->oti_cache.oic_fid);
728
729         if (fid_is_last_id(fid))
730                 return 0;
731
732         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
733                 return osd_obj_map_delete(info, osd, fid, th);
734
735         fid_cpu_to_be(oi_fid, fid);
736         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
737                                  (const struct dt_key *)oi_fid, th);
738 }
739
740 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
741                   const struct lu_fid *fid, const struct osd_inode_id *id,
742                   handle_t *th, enum oi_check_flags flags)
743 {
744         struct lu_fid       *oi_fid = &info->oti_fid2;
745         struct osd_inode_id *oi_id  = &info->oti_id2;
746         int                  rc     = 0;
747
748         if (unlikely(fid_is_last_id(fid)))
749                 return osd_obj_spec_update(info, osd, fid, id, th);
750
751         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
752                 return osd_obj_map_update(info, osd, fid, id, th);
753
754         fid_cpu_to_be(oi_fid, fid);
755         osd_id_pack(oi_id, id);
756         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
757                                (const struct dt_rec *)oi_id,
758                                (const struct dt_key *)oi_fid, th, false);
759         if (rc != 0)
760                 return rc;
761
762         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
763                 rc = osd_obj_spec_update(info, osd, fid, id, th);
764         return rc;
765 }
766
767 int osd_oi_mod_init(void)
768 {
769         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
770                 osd_oi_count = OSD_OI_FID_NR;
771
772         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
773                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
774                               osd_oi_count, size_roundup_power2(osd_oi_count));
775                 osd_oi_count = size_roundup_power2(osd_oi_count);
776         }
777
778         mutex_init(&oi_init_lock);
779         return 0;
780 }