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