Whamcloud - gitweb
91946725d2c07e3ce89a20434bf08b0b6a9179ec
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_oi.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_oi.c
37  *
38  * Object Index.
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <linux/module.h>
46
47 /* LUSTRE_VERSION_CODE */
48 #include <lustre_ver.h>
49 /*
50  * struct OBD_{ALLOC,FREE}*()
51  * OBD_FAIL_CHECK
52  */
53 #include <obd.h>
54 #include <obd_support.h>
55
56 /* fid_cpu_to_be() */
57 #include <lustre_fid.h>
58 #include <dt_object.h>
59
60 #include "osd_oi.h"
61 /* osd_lookup(), struct osd_thread_info */
62 #include "osd_internal.h"
63 #include "osd_scrub.h"
64
65 static unsigned int osd_oi_count = OSD_OI_FID_NR;
66 CFS_MODULE_PARM(osd_oi_count, "i", int, 0444,
67                 "Number of Object Index containers to be created, "
68                 "it's only valid for new filesystem.");
69
70 /** to serialize concurrent OI index initialization */
71 static struct mutex oi_init_lock;
72
73 static struct dt_index_features oi_feat = {
74         .dif_flags       = DT_IND_UPDATE,
75         .dif_recsize_min = sizeof(struct osd_inode_id),
76         .dif_recsize_max = sizeof(struct osd_inode_id),
77         .dif_ptrsize     = 4
78 };
79
80 #define OSD_OI_NAME_BASE        "oi.16"
81
82 static void osd_oi_table_put(struct osd_thread_info *info,
83                              struct osd_oi **oi_table, unsigned oi_count)
84 {
85         struct iam_container *bag;
86         int                   i;
87
88         for (i = 0; i < oi_count; i++) {
89                 if (oi_table[i] == NULL)
90                         continue;
91
92                 LASSERT(oi_table[i]->oi_inode != NULL);
93
94                 bag = &(oi_table[i]->oi_dir.od_container);
95                 if (bag->ic_object == oi_table[i]->oi_inode)
96                         iam_container_fini(bag);
97                 iput(oi_table[i]->oi_inode);
98                 oi_table[i]->oi_inode = NULL;
99                 OBD_FREE_PTR(oi_table[i]);
100                 oi_table[i] = NULL;
101         }
102 }
103
104 static int osd_oi_index_create_one(struct osd_thread_info *info,
105                                    struct osd_device *osd, const char *name,
106                                    struct dt_index_features *feat)
107 {
108         const struct lu_env             *env = info->oti_env;
109         struct osd_inode_id             *id  = &info->oti_id;
110         struct buffer_head              *bh;
111         struct inode                    *inode;
112         struct ldiskfs_dir_entry_2      *de;
113         struct dentry                   *dentry;
114         struct super_block              *sb  = osd_sb(osd);
115         struct inode                    *dir = sb->s_root->d_inode;
116         handle_t                        *jh;
117         int                              rc;
118
119         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
120         bh = osd_ldiskfs_find_entry(dir, dentry, &de, NULL);
121         if (bh) {
122                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
123                 brelse(bh);
124                 inode = osd_iget(info, osd, id);
125                 if (!IS_ERR(inode)) {
126                         iput(inode);
127                         inode = ERR_PTR(-EEXIST);
128                 }
129                 return PTR_ERR(inode);
130         }
131
132         jh = ldiskfs_journal_start_sb(sb, 100);
133         if (IS_ERR(jh))
134                 return PTR_ERR(jh);
135
136         inode = ldiskfs_create_inode(jh, dir, (S_IFREG | S_IRUGO | S_IWUSR));
137         if (IS_ERR(inode)) {
138                 ldiskfs_journal_stop(jh);
139                 return PTR_ERR(inode);
140         }
141
142         if (feat->dif_flags & DT_IND_VARKEY)
143                 rc = iam_lvar_create(inode, feat->dif_keysize_max,
144                                      feat->dif_ptrsize, feat->dif_recsize_max,
145                                      jh);
146         else
147                 rc = iam_lfix_create(inode, feat->dif_keysize_max,
148                                      feat->dif_ptrsize, feat->dif_recsize_max,
149                                      jh);
150         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
151         rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
152         ldiskfs_journal_stop(jh);
153         iput(inode);
154         return rc;
155 }
156
157 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
158                                        struct osd_device *osd,
159                                        const char *name,
160                                        struct dt_index_features *f,
161                                        bool create)
162 {
163         struct dentry *dentry;
164         struct inode  *inode;
165         int            rc;
166
167         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
168         if (IS_ERR(dentry))
169                 return (void *) dentry;
170
171         if (dentry->d_inode) {
172                 LASSERT(!is_bad_inode(dentry->d_inode));
173                 inode = dentry->d_inode;
174                 atomic_inc(&inode->i_count);
175                 dput(dentry);
176                 return inode;
177         }
178
179         /* create */
180         dput(dentry);
181         shrink_dcache_parent(osd_sb(osd)->s_root);
182         if (!create)
183                 return ERR_PTR(-ENOENT);
184
185         rc = osd_oi_index_create_one(info, osd, name, f);
186         if (rc)
187                 return ERR_PTR(rc);
188
189         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
190         if (IS_ERR(dentry))
191                 return (void *) dentry;
192
193         if (dentry->d_inode) {
194                 LASSERT(!is_bad_inode(dentry->d_inode));
195                 inode = dentry->d_inode;
196                 atomic_inc(&inode->i_count);
197                 dput(dentry);
198                 return inode;
199         }
200
201         return ERR_PTR(-ENOENT);
202 }
203
204 /**
205  * Open an OI(Ojbect Index) container.
206  *
207  * \param       name    Name of OI container
208  * \param       objp    Pointer of returned OI
209  *
210  * \retval      0       success
211  * \retval      -ve     failure
212  */
213 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
214                        char *name, struct osd_oi **oi_slot, bool create)
215 {
216         struct osd_directory *dir;
217         struct iam_container *bag;
218         struct inode         *inode;
219         struct osd_oi        *oi;
220         int                   rc;
221
222         ENTRY;
223
224         oi_feat.dif_keysize_min = sizeof(struct lu_fid);
225         oi_feat.dif_keysize_max = sizeof(struct lu_fid);
226
227         inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
228         if (IS_ERR(inode))
229                 RETURN(PTR_ERR(inode));
230
231         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
232         OBD_ALLOC_PTR(oi);
233         if (oi == NULL)
234                 GOTO(out_inode, rc = -ENOMEM);
235
236         oi->oi_inode = inode;
237         dir = &oi->oi_dir;
238
239         bag = &dir->od_container;
240         rc = iam_container_init(bag, &dir->od_descr, inode);
241         if (rc < 0)
242                 GOTO(out_free, rc);
243
244         rc = iam_container_setup(bag);
245         if (rc < 0)
246                 GOTO(out_container, rc);
247
248         *oi_slot = oi;
249         RETURN(0);
250
251 out_container:
252         iam_container_fini(bag);
253 out_free:
254         OBD_FREE_PTR(oi);
255 out_inode:
256         iput(inode);
257         return rc;
258 }
259
260 /**
261  * Open OI(Object Index) table.
262  * If \a oi_count is zero, which means caller doesn't know how many OIs there
263  * will be, this function can either return 0 for new filesystem, or number
264  * of OIs on existed filesystem.
265  *
266  * If \a oi_count is non-zero, which means caller does know number of OIs on
267  * filesystem, this function should return the exactly same number on
268  * success, or error code in failure.
269  *
270  * \param     oi_count  Number of expected OI containers
271  * \param     create    Create OIs if doesn't exist
272  *
273  * \retval    +ve       number of opened OI containers
274  * \retval      0       no OI containers found
275  * \retval    -ve       failure
276  */
277 static int
278 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
279                   struct osd_oi **oi_table, unsigned oi_count, bool create)
280 {
281         struct scrub_file *sf = &osd->od_scrub.os_file;
282         int                count = 0;
283         int                rc = 0;
284         int                i;
285         ENTRY;
286
287         /* NB: oi_count != 0 means that we have already created/known all OIs
288          * and have known exact number of OIs. */
289         LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
290
291         for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
292                 char name[12];
293
294                 if (oi_table[i] != NULL) {
295                         count++;
296                         continue;
297                 }
298
299                 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
300                 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
301                 if (rc == 0) {
302                         count++;
303                         continue;
304                 }
305
306                 if (rc == -ENOENT && create == false) {
307                         if (oi_count == 0)
308                                 return count;
309
310                         rc = 0;
311                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
312                         continue;
313                 }
314
315                 CERROR("%.16s: can't open %s: rc = %d\n",
316                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
317                 if (oi_count > 0)
318                         CERROR("%.16s: expect to open total %d OI files.\n",
319                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
320                                oi_count);
321                 break;
322         }
323
324         if (rc < 0) {
325                 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
326                 count = rc;
327         }
328
329         RETURN(count);
330 }
331
332 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
333 {
334         struct osd_scrub  *scrub = &osd->od_scrub;
335         struct scrub_file *sf = &scrub->os_file;
336         struct osd_oi    **oi;
337         int                rc;
338         ENTRY;
339
340         OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
341         if (oi == NULL)
342                 RETURN(-ENOMEM);
343
344         mutex_lock(&oi_init_lock);
345         /* try to open existing multiple OIs first */
346         rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
347         if (rc < 0)
348                 GOTO(out, rc);
349
350         if (rc > 0) {
351                 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
352                         GOTO(out, rc);
353
354                 osd_scrub_file_reset(scrub,
355                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
356                                      SF_RECREATED);
357                 osd_oi_count = sf->sf_oi_count;
358                 goto create;
359         }
360
361         /* if previous failed then try found single OI from old filesystem */
362         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
363         if (rc == 0) { /* found single OI from old filesystem */
364                 GOTO(out, rc = 1);
365         } else if (rc != -ENOENT) {
366                 CERROR("%.16s: can't open %s: rc = %d\n",
367                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
368                        OSD_OI_NAME_BASE, rc);
369                 GOTO(out, rc);
370         }
371
372         if (sf->sf_oi_count > 0) {
373                 int i;
374
375                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
376                 for (i = 0; i < osd_oi_count; i++)
377                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
378                 osd_scrub_file_reset(scrub,
379                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
380                                      SF_RECREATED);
381         }
382         sf->sf_oi_count = osd_oi_count;
383
384 create:
385         rc = osd_scrub_file_store(scrub);
386         if (rc < 0) {
387                 osd_oi_table_put(info, oi, sf->sf_oi_count);
388                 GOTO(out, rc);
389         }
390
391         /* No OIs exist, new filesystem, create OI objects */
392         rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
393         LASSERT(ergo(rc >= 0, rc == osd_oi_count));
394
395         GOTO(out, rc);
396
397 out:
398         if (rc < 0) {
399                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
400         } else {
401                 LASSERT((rc & (rc - 1)) == 0);
402                 osd->od_oi_table = oi;
403                 osd->od_oi_count = rc;
404                 rc = 0;
405         }
406
407         mutex_unlock(&oi_init_lock);
408         return rc;
409 }
410
411 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
412 {
413         if (unlikely(osd->od_oi_table == NULL))
414                 return;
415
416         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
417
418         OBD_FREE(osd->od_oi_table,
419                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
420         osd->od_oi_table = NULL;
421 }
422
423 static inline int fid_is_fs_root(const struct lu_fid *fid)
424 {
425         /* Map root inode to special local object FID */
426         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
427                          fid_oid(fid) == OSD_FS_ROOT_OID));
428 }
429
430 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
431                              struct osd_oi *oi, struct dt_rec *rec,
432                              const struct dt_key *key)
433 {
434         struct iam_container  *bag;
435         struct iam_iterator   *it = &oti->oti_idx_it;
436         struct iam_path_descr *ipd;
437         int                    rc;
438         ENTRY;
439
440         LASSERT(oi);
441         LASSERT(oi->oi_inode);
442
443         bag = &oi->oi_dir.od_container;
444         ipd = osd_idx_ipd_get(oti->oti_env, bag);
445         if (IS_ERR(ipd))
446                 RETURN(-ENOMEM);
447
448         /* got ipd now we can start iterator. */
449         iam_it_init(it, bag, 0, ipd);
450
451         rc = iam_it_get(it, (struct iam_key *)key);
452         if (rc > 0)
453                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
454         iam_it_put(it);
455         iam_it_fini(it);
456         osd_ipd_put(oti->oti_env, bag, ipd);
457
458         LINVRNT(osd_invariant(obj));
459
460         RETURN(rc);
461 }
462
463 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
464                   const struct lu_fid *fid)
465 {
466         struct lu_seq_range *range = &info->oti_seq_range;
467         int rc;
468         ENTRY;
469
470         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
471                      fid_is_llog(fid)))
472                 RETURN(0);
473
474         if (fid_is_idif(fid) || fid_is_last_id(fid))
475                 RETURN(1);
476
477         rc = osd_fld_lookup(info->oti_env, osd, fid, range);
478         if (rc != 0) {
479                 CERROR("%s: Can not lookup fld for "DFID"\n",
480                        osd_name(osd), PFID(fid));
481                 RETURN(rc);
482         }
483
484         CDEBUG(D_INFO, "fid "DFID" range "DRANGE"\n", PFID(fid),
485                PRANGE(range));
486
487         if (fld_range_is_ost(range))
488                 RETURN(1);
489
490         RETURN(0);
491 }
492
493 int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
494                     const struct lu_fid *fid, struct osd_inode_id *id)
495 {
496         struct lu_fid *oi_fid = &info->oti_fid2;
497         int            rc;
498
499         fid_cpu_to_be(oi_fid, fid);
500         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
501                                (const struct dt_key *)oi_fid);
502         if (rc > 0) {
503                 osd_id_unpack(id, id);
504                 rc = 0;
505         } else if (rc == 0) {
506                 rc = -ENOENT;
507         }
508         return rc;
509 }
510
511 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
512                   const struct lu_fid *fid, struct osd_inode_id *id,
513                   bool check_fld)
514 {
515         if (unlikely(fid_is_last_id(fid)))
516                 return osd_obj_spec_lookup(info, osd, fid, id);
517
518         if ((check_fld && fid_is_on_ost(info, osd, fid)) || fid_is_llog(fid))
519                 return osd_obj_map_lookup(info, osd, fid, id);
520
521         if (fid_is_fs_root(fid)) {
522                 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
523                            osd_sb(osd)->s_root->d_inode->i_generation);
524                 return 0;
525         }
526
527         if (unlikely(fid_is_acct(fid)))
528                 return osd_acct_obj_lookup(info, osd, fid, id);
529
530         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
531                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
532                 return 0;
533         }
534
535         return __osd_oi_lookup(info, osd, fid, id);
536 }
537
538 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
539                              const struct dt_rec *rec, const struct dt_key *key,
540                              struct thandle *th, bool insert)
541 {
542         struct iam_container    *bag;
543         struct iam_path_descr   *ipd;
544         struct osd_thandle      *oh;
545         int                     rc;
546         ENTRY;
547
548         LASSERT(oi);
549         LASSERT(oi->oi_inode);
550         ll_vfs_dq_init(oi->oi_inode);
551
552         bag = &oi->oi_dir.od_container;
553         ipd = osd_idx_ipd_get(oti->oti_env, bag);
554         if (unlikely(ipd == NULL))
555                 RETURN(-ENOMEM);
556
557         oh = container_of0(th, struct osd_thandle, ot_super);
558         LASSERT(oh->ot_handle != NULL);
559         LASSERT(oh->ot_handle->h_transaction != NULL);
560         if (insert)
561                 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
562                                 (const struct iam_rec *)rec, ipd);
563         else
564                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)key,
565                                 (const struct iam_rec *)rec, ipd);
566         osd_ipd_put(oti->oti_env, bag, ipd);
567         LINVRNT(osd_invariant(obj));
568         RETURN(rc);
569 }
570
571 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
572                   const struct lu_fid *fid, const struct osd_inode_id *id,
573                   struct thandle *th)
574 {
575         struct lu_fid       *oi_fid = &info->oti_fid2;
576         struct osd_inode_id *oi_id  = &info->oti_id2;
577         int                  rc     = 0;
578
579         if (unlikely(fid_is_last_id(fid)))
580                 return osd_obj_spec_insert(info, osd, fid, id, th);
581
582         if (fid_is_on_ost(info, osd, fid) || fid_is_llog(fid))
583                 return osd_obj_map_insert(info, osd, fid, id, th);
584
585         fid_cpu_to_be(oi_fid, fid);
586         osd_id_pack(oi_id, id);
587         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
588                                (const struct dt_rec *)oi_id,
589                                (const struct dt_key *)oi_fid, th, true);
590         if (rc != 0) {
591                 struct inode *inode;
592                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
593
594                 if (rc != -EEXIST)
595                         return rc;
596
597                 rc = osd_oi_lookup(info, osd, fid, oi_id, false);
598                 if (unlikely(rc != 0))
599                         return rc;
600
601                 if (osd_id_eq(id, oi_id)) {
602                         CERROR("%.16s: the FID "DFID" is there already:%u/%u\n",
603                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
604                                PFID(fid), id->oii_ino, id->oii_gen);
605                         return -EEXIST;
606                 }
607
608                 /* Check whether the mapping for oi_id is valid or not. */
609                 inode = osd_iget(info, osd, oi_id);
610                 if (IS_ERR(inode)) {
611                         rc = PTR_ERR(inode);
612                         if (rc == -ENOENT || rc == -ESTALE)
613                                 goto update;
614                         return rc;
615                 }
616
617                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
618                 iput(inode);
619                 if (rc == -ENODATA)
620                         goto update;
621
622                 if (rc != 0)
623                         return rc;
624
625                 if (lu_fid_eq(fid, &lma->lma_self_fid)) {
626                         CERROR("%.16s: the FID "DFID" is used by two objects: "
627                                "%u/%u %u/%u\n",
628                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
629                                PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
630                                id->oii_ino, id->oii_gen);
631                         return -EEXIST;
632                 }
633
634 update:
635                 osd_id_pack(oi_id, id);
636                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
637                                         (const struct dt_rec *)oi_id,
638                                         (const struct dt_key *)oi_fid, th, false);
639                 if (rc != 0)
640                         return rc;
641         }
642
643         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
644                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
645         return rc;
646 }
647
648 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
649                              const struct dt_key *key, struct thandle *handle)
650 {
651         struct iam_container  *bag;
652         struct iam_path_descr *ipd;
653         struct osd_thandle    *oh;
654         int                    rc;
655         ENTRY;
656
657         LASSERT(oi);
658         LASSERT(oi->oi_inode);
659         ll_vfs_dq_init(oi->oi_inode);
660
661         bag = &oi->oi_dir.od_container;
662         ipd = osd_idx_ipd_get(oti->oti_env, bag);
663         if (unlikely(ipd == NULL))
664                 RETURN(-ENOMEM);
665
666         oh = container_of0(handle, struct osd_thandle, ot_super);
667         LASSERT(oh->ot_handle != NULL);
668         LASSERT(oh->ot_handle->h_transaction != NULL);
669
670         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
671         osd_ipd_put(oti->oti_env, bag, ipd);
672         LINVRNT(osd_invariant(obj));
673         RETURN(rc);
674 }
675
676 int osd_oi_delete(struct osd_thread_info *info,
677                   struct osd_device *osd, const struct lu_fid *fid,
678                   struct thandle *th)
679 {
680         struct lu_fid *oi_fid = &info->oti_fid2;
681
682         /* clear idmap cache */
683         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
684                 fid_zero(&info->oti_cache.oic_fid);
685
686         if (fid_is_last_id(fid))
687                 return 0;
688
689         if (fid_is_on_ost(info, osd, fid) || fid_is_llog(fid))
690                 return osd_obj_map_delete(info, osd, fid, th);
691
692         fid_cpu_to_be(oi_fid, fid);
693         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
694                                  (const struct dt_key *)oi_fid, th);
695 }
696
697 int osd_oi_mod_init(void)
698 {
699         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
700                 osd_oi_count = OSD_OI_FID_NR;
701
702         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
703                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
704                               osd_oi_count, size_roundup_power2(osd_oi_count));
705                 osd_oi_count = size_roundup_power2(osd_oi_count);
706         }
707
708         mutex_init(&oi_init_lock);
709         return 0;
710 }