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