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