Whamcloud - gitweb
230f01590e3c8d21ff2f3c7326fc0c126238012b
[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         /* 'What the @fid is' is not imporatant, because these objects
233          * have no OI mappings, and only are visible inside the OSD.*/
234         lu_igif_build(&info->oti_fid, inode->i_ino, inode->i_generation);
235         rc = osd_ea_fid_set(info, inode, &info->oti_fid, LMAC_NOT_IN_OI, 0);
236         if (rc != 0)
237                 GOTO(out_inode, rc);
238
239         OBD_ALLOC_PTR(oi);
240         if (oi == NULL)
241                 GOTO(out_inode, rc = -ENOMEM);
242
243         oi->oi_inode = inode;
244         dir = &oi->oi_dir;
245
246         bag = &dir->od_container;
247         rc = iam_container_init(bag, &dir->od_descr, inode);
248         if (rc < 0)
249                 GOTO(out_free, rc);
250
251         rc = iam_container_setup(bag);
252         if (rc < 0)
253                 GOTO(out_container, rc);
254
255         *oi_slot = oi;
256         RETURN(0);
257
258 out_container:
259         iam_container_fini(bag);
260 out_free:
261         OBD_FREE_PTR(oi);
262 out_inode:
263         iput(inode);
264         return rc;
265 }
266
267 /**
268  * Open OI(Object Index) table.
269  * If \a oi_count is zero, which means caller doesn't know how many OIs there
270  * will be, this function can either return 0 for new filesystem, or number
271  * of OIs on existed filesystem.
272  *
273  * If \a oi_count is non-zero, which means caller does know number of OIs on
274  * filesystem, this function should return the exactly same number on
275  * success, or error code in failure.
276  *
277  * \param     oi_count  Number of expected OI containers
278  * \param     create    Create OIs if doesn't exist
279  *
280  * \retval    +ve       number of opened OI containers
281  * \retval      0       no OI containers found
282  * \retval    -ve       failure
283  */
284 static int
285 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
286                   struct osd_oi **oi_table, unsigned oi_count, bool create)
287 {
288         struct scrub_file *sf = &osd->od_scrub.os_file;
289         int                count = 0;
290         int                rc = 0;
291         int                i;
292         ENTRY;
293
294         /* NB: oi_count != 0 means that we have already created/known all OIs
295          * and have known exact number of OIs. */
296         LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
297
298         for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
299                 char name[12];
300
301                 if (oi_table[i] != NULL) {
302                         count++;
303                         continue;
304                 }
305
306                 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
307                 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
308                 if (rc == 0) {
309                         count++;
310                         continue;
311                 }
312
313                 if (rc == -ENOENT && create == false) {
314                         if (oi_count == 0)
315                                 return count;
316
317                         rc = 0;
318                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
319                         continue;
320                 }
321
322                 CERROR("%.16s: can't open %s: rc = %d\n",
323                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
324                 if (oi_count > 0)
325                         CERROR("%.16s: expect to open total %d OI files.\n",
326                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
327                                oi_count);
328                 break;
329         }
330
331         if (rc < 0) {
332                 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
333                 count = rc;
334         }
335
336         RETURN(count);
337 }
338
339 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
340 {
341         struct osd_scrub  *scrub = &osd->od_scrub;
342         struct scrub_file *sf = &scrub->os_file;
343         struct osd_oi    **oi;
344         int                rc;
345         ENTRY;
346
347         OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
348         if (oi == NULL)
349                 RETURN(-ENOMEM);
350
351         mutex_lock(&oi_init_lock);
352         /* try to open existing multiple OIs first */
353         rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
354         if (rc < 0)
355                 GOTO(out, rc);
356
357         if (rc > 0) {
358                 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
359                         GOTO(out, rc);
360
361                 osd_scrub_file_reset(scrub,
362                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
363                                      SF_RECREATED);
364                 osd_oi_count = sf->sf_oi_count;
365                 goto create;
366         }
367
368         /* if previous failed then try found single OI from old filesystem */
369         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
370         if (rc == 0) { /* found single OI from old filesystem */
371                 GOTO(out, rc = 1);
372         } else if (rc != -ENOENT) {
373                 CERROR("%.16s: can't open %s: rc = %d\n",
374                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
375                        OSD_OI_NAME_BASE, rc);
376                 GOTO(out, rc);
377         }
378
379         if (sf->sf_oi_count > 0) {
380                 int i;
381
382                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
383                 for (i = 0; i < osd_oi_count; i++)
384                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
385                 osd_scrub_file_reset(scrub,
386                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
387                                      SF_RECREATED);
388         }
389         sf->sf_oi_count = osd_oi_count;
390
391 create:
392         rc = osd_scrub_file_store(scrub);
393         if (rc < 0) {
394                 osd_oi_table_put(info, oi, sf->sf_oi_count);
395                 GOTO(out, rc);
396         }
397
398         /* No OIs exist, new filesystem, create OI objects */
399         rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
400         LASSERT(ergo(rc >= 0, rc == osd_oi_count));
401
402         GOTO(out, rc);
403
404 out:
405         if (rc < 0) {
406                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
407         } else {
408                 LASSERT((rc & (rc - 1)) == 0);
409                 osd->od_oi_table = oi;
410                 osd->od_oi_count = rc;
411                 rc = 0;
412         }
413
414         mutex_unlock(&oi_init_lock);
415         return rc;
416 }
417
418 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
419 {
420         if (unlikely(osd->od_oi_table == NULL))
421                 return;
422
423         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
424
425         OBD_FREE(osd->od_oi_table,
426                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
427         osd->od_oi_table = NULL;
428 }
429
430 static inline int fid_is_fs_root(const struct lu_fid *fid)
431 {
432         /* Map root inode to special local object FID */
433         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
434                          fid_oid(fid) == OSD_FS_ROOT_OID));
435 }
436
437 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
438                              struct osd_oi *oi, struct dt_rec *rec,
439                              const struct dt_key *key)
440 {
441         struct iam_container  *bag;
442         struct iam_iterator   *it = &oti->oti_idx_it;
443         struct iam_path_descr *ipd;
444         int                    rc;
445         ENTRY;
446
447         LASSERT(oi);
448         LASSERT(oi->oi_inode);
449
450         bag = &oi->oi_dir.od_container;
451         ipd = osd_idx_ipd_get(oti->oti_env, bag);
452         if (IS_ERR(ipd))
453                 RETURN(-ENOMEM);
454
455         /* got ipd now we can start iterator. */
456         iam_it_init(it, bag, 0, ipd);
457
458         rc = iam_it_get(it, (struct iam_key *)key);
459         if (rc > 0)
460                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
461         iam_it_put(it);
462         iam_it_fini(it);
463         osd_ipd_put(oti->oti_env, bag, ipd);
464
465         LINVRNT(osd_invariant(obj));
466
467         RETURN(rc);
468 }
469
470 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
471                   const struct lu_fid *fid, enum oi_check_flags flags)
472 {
473         struct lu_seq_range *range = &info->oti_seq_range;
474         int rc;
475         ENTRY;
476
477         if (flags & OI_KNOWN_ON_OST)
478                 RETURN(1);
479
480         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
481                      fid_is_llog(fid)))
482                 RETURN(0);
483
484         if (fid_is_idif(fid) || fid_is_last_id(fid))
485                 RETURN(1);
486
487         if (!(flags & OI_CHECK_FLD))
488                 RETURN(0);
489
490         rc = osd_fld_lookup(info->oti_env, osd, fid, range);
491         if (rc != 0) {
492                 CERROR("%s: Can not lookup fld for "DFID"\n",
493                        osd_name(osd), PFID(fid));
494                 RETURN(rc);
495         }
496
497         CDEBUG(D_INFO, "fid "DFID" range "DRANGE"\n", PFID(fid),
498                PRANGE(range));
499
500         if (fld_range_is_ost(range))
501                 RETURN(1);
502
503         RETURN(0);
504 }
505
506 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
507                            const struct lu_fid *fid, struct osd_inode_id *id)
508 {
509         struct lu_fid *oi_fid = &info->oti_fid2;
510         int            rc;
511
512         fid_cpu_to_be(oi_fid, fid);
513         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
514                                (const struct dt_key *)oi_fid);
515         if (rc > 0) {
516                 osd_id_unpack(id, id);
517                 rc = 0;
518         } else if (rc == 0) {
519                 rc = -ENOENT;
520         }
521         return rc;
522 }
523
524 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
525                   const struct lu_fid *fid, struct osd_inode_id *id,
526                   enum oi_check_flags flags)
527 {
528         if (unlikely(fid_is_last_id(fid)))
529                 return osd_obj_spec_lookup(info, osd, fid, id);
530
531         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
532                 return osd_obj_map_lookup(info, osd, fid, id);
533
534         if (fid_is_fs_root(fid)) {
535                 osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
536                            osd_sb(osd)->s_root->d_inode->i_generation);
537                 return 0;
538         }
539
540         if (unlikely(fid_is_acct(fid)))
541                 return osd_acct_obj_lookup(info, osd, fid, id);
542
543         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
544                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
545                 return 0;
546         }
547
548         return __osd_oi_lookup(info, osd, fid, id);
549 }
550
551 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
552                              const struct dt_rec *rec, const struct dt_key *key,
553                              struct thandle *th, bool insert)
554 {
555         struct iam_container    *bag;
556         struct iam_path_descr   *ipd;
557         struct osd_thandle      *oh;
558         int                     rc;
559         ENTRY;
560
561         LASSERT(oi);
562         LASSERT(oi->oi_inode);
563         ll_vfs_dq_init(oi->oi_inode);
564
565         bag = &oi->oi_dir.od_container;
566         ipd = osd_idx_ipd_get(oti->oti_env, bag);
567         if (unlikely(ipd == NULL))
568                 RETURN(-ENOMEM);
569
570         oh = container_of0(th, struct osd_thandle, ot_super);
571         LASSERT(oh->ot_handle != NULL);
572         LASSERT(oh->ot_handle->h_transaction != NULL);
573         if (insert)
574                 rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
575                                 (const struct iam_rec *)rec, ipd);
576         else
577                 rc = iam_update(oh->ot_handle, bag, (const struct iam_key *)key,
578                                 (const struct iam_rec *)rec, ipd);
579         osd_ipd_put(oti->oti_env, bag, ipd);
580         LINVRNT(osd_invariant(obj));
581         RETURN(rc);
582 }
583
584 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
585                   const struct lu_fid *fid, const struct osd_inode_id *id,
586                   struct thandle *th, enum oi_check_flags flags)
587 {
588         struct lu_fid       *oi_fid = &info->oti_fid2;
589         struct osd_inode_id *oi_id  = &info->oti_id2;
590         int                  rc     = 0;
591
592         if (unlikely(fid_is_last_id(fid)))
593                 return osd_obj_spec_insert(info, osd, fid, id, th);
594
595         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
596                 return osd_obj_map_insert(info, osd, fid, id, th);
597
598         fid_cpu_to_be(oi_fid, fid);
599         osd_id_pack(oi_id, id);
600         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
601                                (const struct dt_rec *)oi_id,
602                                (const struct dt_key *)oi_fid, th, true);
603         if (rc != 0) {
604                 struct inode *inode;
605                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
606
607                 if (rc != -EEXIST)
608                         return rc;
609
610                 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
611                 if (rc != 0)
612                         return rc;
613
614                 if (unlikely(osd_id_eq(id, oi_id)))
615                         return 0;
616
617                 /* Check whether the mapping for oi_id is valid or not. */
618                 inode = osd_iget(info, osd, oi_id);
619                 if (IS_ERR(inode)) {
620                         rc = PTR_ERR(inode);
621                         if (rc == -ENOENT || rc == -ESTALE)
622                                 goto update;
623                         return rc;
624                 }
625
626                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
627                 iput(inode);
628                 if (rc == -ENODATA)
629                         goto update;
630
631                 if (rc != 0)
632                         return rc;
633
634                 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
635                     lu_fid_eq(fid, &lma->lma_self_fid)) {
636                         CERROR("%.16s: the FID "DFID" is used by two objects: "
637                                "%u/%u %u/%u\n",
638                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
639                                PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
640                                id->oii_ino, id->oii_gen);
641                         return -EEXIST;
642                 }
643
644 update:
645                 osd_id_pack(oi_id, id);
646                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
647                                         (const struct dt_rec *)oi_id,
648                                         (const struct dt_key *)oi_fid, th, false);
649                 if (rc != 0)
650                         return rc;
651         }
652
653         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
654                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
655         return rc;
656 }
657
658 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
659                              const struct dt_key *key, struct thandle *handle)
660 {
661         struct iam_container  *bag;
662         struct iam_path_descr *ipd;
663         struct osd_thandle    *oh;
664         int                    rc;
665         ENTRY;
666
667         LASSERT(oi);
668         LASSERT(oi->oi_inode);
669         ll_vfs_dq_init(oi->oi_inode);
670
671         bag = &oi->oi_dir.od_container;
672         ipd = osd_idx_ipd_get(oti->oti_env, bag);
673         if (unlikely(ipd == NULL))
674                 RETURN(-ENOMEM);
675
676         oh = container_of0(handle, struct osd_thandle, ot_super);
677         LASSERT(oh->ot_handle != NULL);
678         LASSERT(oh->ot_handle->h_transaction != NULL);
679
680         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
681         osd_ipd_put(oti->oti_env, bag, ipd);
682         LINVRNT(osd_invariant(obj));
683         RETURN(rc);
684 }
685
686 int osd_oi_delete(struct osd_thread_info *info,
687                   struct osd_device *osd, const struct lu_fid *fid,
688                   struct thandle *th, enum oi_check_flags flags)
689 {
690         struct lu_fid *oi_fid = &info->oti_fid2;
691
692         /* clear idmap cache */
693         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
694                 fid_zero(&info->oti_cache.oic_fid);
695
696         if (fid_is_last_id(fid))
697                 return 0;
698
699         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
700                 return osd_obj_map_delete(info, osd, fid, th);
701
702         fid_cpu_to_be(oi_fid, fid);
703         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
704                                  (const struct dt_key *)oi_fid, th);
705 }
706
707 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
708                   const struct lu_fid *fid, const struct osd_inode_id *id,
709                   struct thandle *th, enum oi_check_flags flags)
710 {
711         struct lu_fid       *oi_fid = &info->oti_fid2;
712         struct osd_inode_id *oi_id  = &info->oti_id2;
713         int                  rc     = 0;
714
715         if (unlikely(fid_is_last_id(fid)))
716                 return osd_obj_spec_update(info, osd, fid, id, th);
717
718         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
719                 return osd_obj_map_update(info, osd, fid, id, th);
720
721         fid_cpu_to_be(oi_fid, fid);
722         osd_id_pack(oi_id, id);
723         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
724                                (const struct dt_rec *)oi_id,
725                                (const struct dt_key *)oi_fid, th, false);
726         if (rc != 0)
727                 return rc;
728
729         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
730                 rc = osd_obj_spec_update(info, osd, fid, id, th);
731         return rc;
732 }
733
734 int osd_oi_mod_init(void)
735 {
736         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
737                 osd_oi_count = OSD_OI_FID_NR;
738
739         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
740                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
741                               osd_oi_count, size_roundup_power2(osd_oi_count));
742                 osd_oi_count = size_roundup_power2(osd_oi_count);
743         }
744
745         mutex_init(&oi_init_lock);
746         return 0;
747 }