Whamcloud - gitweb
LU-5510 scrub: ldiskfs_create_inode returns locked inode
[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, 2014, 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_OSD
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->d_name, &de, NULL, 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 = osd_journal_start_sb(sb, LDISKFS_HT_MISC, 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         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
143         unlock_new_inode(inode);
144
145         if (feat->dif_flags & DT_IND_VARKEY)
146                 rc = iam_lvar_create(inode, feat->dif_keysize_max,
147                                      feat->dif_ptrsize, feat->dif_recsize_max,
148                                      jh);
149         else
150                 rc = iam_lfix_create(inode, feat->dif_keysize_max,
151                                      feat->dif_ptrsize, feat->dif_recsize_max,
152                                      jh);
153         dentry = osd_child_dentry_by_inode(env, dir, name, strlen(name));
154         rc = osd_ldiskfs_add_entry(jh, dentry, inode, NULL);
155         ldiskfs_journal_stop(jh);
156         iput(inode);
157         return rc;
158 }
159
160 static struct inode *osd_oi_index_open(struct osd_thread_info *info,
161                                        struct osd_device *osd,
162                                        const char *name,
163                                        struct dt_index_features *f,
164                                        bool create)
165 {
166         struct dentry *dentry;
167         struct inode  *inode;
168         int            rc;
169
170         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
171         if (IS_ERR(dentry))
172                 return (void *) dentry;
173
174         if (dentry->d_inode) {
175                 LASSERT(!is_bad_inode(dentry->d_inode));
176                 inode = dentry->d_inode;
177                 atomic_inc(&inode->i_count);
178                 dput(dentry);
179                 return inode;
180         }
181
182         /* create */
183         dput(dentry);
184         shrink_dcache_parent(osd_sb(osd)->s_root);
185         if (!create)
186                 return ERR_PTR(-ENOENT);
187
188         rc = osd_oi_index_create_one(info, osd, name, f);
189         if (rc)
190                 return ERR_PTR(rc);
191
192         dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
193         if (IS_ERR(dentry))
194                 return (void *) dentry;
195
196         if (dentry->d_inode) {
197                 LASSERT(!is_bad_inode(dentry->d_inode));
198                 inode = dentry->d_inode;
199                 atomic_inc(&inode->i_count);
200                 dput(dentry);
201                 return inode;
202         }
203
204         return ERR_PTR(-ENOENT);
205 }
206
207 /**
208  * Open an OI(Ojbect Index) container.
209  *
210  * \param       name    Name of OI container
211  * \param       objp    Pointer of returned OI
212  *
213  * \retval      0       success
214  * \retval      -ve     failure
215  */
216 static int osd_oi_open(struct osd_thread_info *info, struct osd_device *osd,
217                        char *name, struct osd_oi **oi_slot, bool create)
218 {
219         struct osd_directory *dir;
220         struct iam_container *bag;
221         struct inode         *inode;
222         struct osd_oi        *oi;
223         int                   rc;
224
225         ENTRY;
226
227         oi_feat.dif_keysize_min = sizeof(struct lu_fid);
228         oi_feat.dif_keysize_max = sizeof(struct lu_fid);
229
230         inode = osd_oi_index_open(info, osd, name, &oi_feat, create);
231         if (IS_ERR(inode))
232                 RETURN(PTR_ERR(inode));
233
234         /* 'What the @fid is' is not imporatant, because these objects
235          * have no OI mappings, and only are visible inside the OSD.*/
236         lu_igif_build(&info->oti_fid, inode->i_ino, inode->i_generation);
237         rc = osd_ea_fid_set(info, inode, &info->oti_fid, LMAC_NOT_IN_OI, 0);
238         if (rc != 0)
239                 GOTO(out_inode, rc);
240
241         OBD_ALLOC_PTR(oi);
242         if (oi == NULL)
243                 GOTO(out_inode, rc = -ENOMEM);
244
245         oi->oi_inode = inode;
246         dir = &oi->oi_dir;
247
248         bag = &dir->od_container;
249         rc = iam_container_init(bag, &dir->od_descr, inode);
250         if (rc < 0)
251                 GOTO(out_free, rc);
252
253         rc = iam_container_setup(bag);
254         if (rc < 0)
255                 GOTO(out_container, rc);
256
257         *oi_slot = oi;
258         RETURN(0);
259
260 out_container:
261         iam_container_fini(bag);
262 out_free:
263         OBD_FREE_PTR(oi);
264 out_inode:
265         iput(inode);
266         return rc;
267 }
268
269 /**
270  * Open OI(Object Index) table.
271  * If \a oi_count is zero, which means caller doesn't know how many OIs there
272  * will be, this function can either return 0 for new filesystem, or number
273  * of OIs on existed filesystem.
274  *
275  * If \a oi_count is non-zero, which means caller does know number of OIs on
276  * filesystem, this function should return the exactly same number on
277  * success, or error code in failure.
278  *
279  * \param     oi_count  Number of expected OI containers
280  * \param     create    Create OIs if doesn't exist
281  *
282  * \retval    +ve       number of opened OI containers
283  * \retval      0       no OI containers found
284  * \retval    -ve       failure
285  */
286 static int
287 osd_oi_table_open(struct osd_thread_info *info, struct osd_device *osd,
288                   struct osd_oi **oi_table, unsigned oi_count, bool create)
289 {
290         struct scrub_file *sf = &osd->od_scrub.os_file;
291         int                count = 0;
292         int                rc = 0;
293         int                i;
294         ENTRY;
295
296         /* NB: oi_count != 0 means that we have already created/known all OIs
297          * and have known exact number of OIs. */
298         LASSERT(oi_count <= OSD_OI_FID_NR_MAX);
299
300         for (i = 0; i < (oi_count != 0 ? oi_count : OSD_OI_FID_NR_MAX); i++) {
301                 char name[12];
302
303                 if (oi_table[i] != NULL) {
304                         count++;
305                         continue;
306                 }
307
308                 sprintf(name, "%s.%d", OSD_OI_NAME_BASE, i);
309                 rc = osd_oi_open(info, osd, name, &oi_table[i], create);
310                 if (rc == 0) {
311                         count++;
312                         continue;
313                 }
314
315                 if (rc == -ENOENT && create == false) {
316                         if (oi_count == 0)
317                                 return count;
318
319                         rc = 0;
320                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
321                         continue;
322                 }
323
324                 CERROR("%.16s: can't open %s: rc = %d\n",
325                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name, name, rc);
326                 if (oi_count > 0)
327                         CERROR("%.16s: expect to open total %d OI files.\n",
328                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
329                                oi_count);
330                 break;
331         }
332
333         if (rc < 0) {
334                 osd_oi_table_put(info, oi_table, oi_count > 0 ? oi_count : i);
335                 count = rc;
336         }
337
338         RETURN(count);
339 }
340
341 int osd_oi_init(struct osd_thread_info *info, struct osd_device *osd)
342 {
343         struct osd_scrub  *scrub = &osd->od_scrub;
344         struct scrub_file *sf = &scrub->os_file;
345         struct osd_oi    **oi;
346         int                rc;
347         ENTRY;
348
349         OBD_ALLOC(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
350         if (oi == NULL)
351                 RETURN(-ENOMEM);
352
353         mutex_lock(&oi_init_lock);
354         /* try to open existing multiple OIs first */
355         rc = osd_oi_table_open(info, osd, oi, sf->sf_oi_count, false);
356         if (rc < 0)
357                 GOTO(out, rc);
358
359         if (rc > 0) {
360                 if (rc == sf->sf_oi_count || sf->sf_oi_count == 0)
361                         GOTO(out, rc);
362
363                 osd_scrub_file_reset(scrub,
364                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
365                                      SF_RECREATED);
366                 osd_oi_count = sf->sf_oi_count;
367                 goto create;
368         }
369
370         /* if previous failed then try found single OI from old filesystem */
371         rc = osd_oi_open(info, osd, OSD_OI_NAME_BASE, &oi[0], false);
372         if (rc == 0) { /* found single OI from old filesystem */
373                 ldiskfs_clear_bit(0, sf->sf_oi_bitmap);
374                 if (sf->sf_success_count == 0)
375                         /* XXX: There is one corner case that if the OI_scrub
376                          *      file crashed or lost and we regard it upgrade,
377                          *      then we allow IGIF lookup to bypass OI files.
378                          *
379                          *      The risk is that osd_fid_lookup() may found
380                          *      a wrong inode with the given IGIF especially
381                          *      when the MDT has performed file-level backup
382                          *      and restored after former upgrading from 1.8
383                          *      to 2.x. Fortunately, the osd_fid_lookup()can
384                          *      verify the inode to decrease the risk. */
385                         osd_scrub_file_reset(scrub,
386                                         LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
387                                         SF_UPGRADE);
388                 GOTO(out, rc = 1);
389         } else if (rc != -ENOENT) {
390                 CERROR("%.16s: can't open %s: rc = %d\n",
391                        LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
392                        OSD_OI_NAME_BASE, rc);
393                 GOTO(out, rc);
394         }
395
396         if (sf->sf_oi_count > 0) {
397                 int i;
398
399                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
400                 for (i = 0; i < osd_oi_count; i++)
401                         ldiskfs_set_bit(i, sf->sf_oi_bitmap);
402                 osd_scrub_file_reset(scrub,
403                                      LDISKFS_SB(osd_sb(osd))->s_es->s_uuid,
404                                      SF_RECREATED);
405         }
406         sf->sf_oi_count = osd_oi_count;
407
408 create:
409         rc = osd_scrub_file_store(scrub);
410         if (rc < 0) {
411                 osd_oi_table_put(info, oi, sf->sf_oi_count);
412                 GOTO(out, rc);
413         }
414
415         /* No OIs exist, new filesystem, create OI objects */
416         rc = osd_oi_table_open(info, osd, oi, osd_oi_count, true);
417         LASSERT(ergo(rc >= 0, rc == osd_oi_count));
418
419         GOTO(out, rc);
420
421 out:
422         if (rc < 0) {
423                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
424         } else {
425                 LASSERT((rc & (rc - 1)) == 0);
426                 osd->od_oi_table = oi;
427                 osd->od_oi_count = rc;
428                 if (sf->sf_oi_count != rc) {
429                         sf->sf_oi_count = rc;
430                         rc = osd_scrub_file_store(scrub);
431                         if (rc < 0) {
432                                 osd_oi_table_put(info, oi, sf->sf_oi_count);
433                                 OBD_FREE(oi, sizeof(*oi) * OSD_OI_FID_NR_MAX);
434                         }
435                 } else {
436                         rc = 0;
437                 }
438         }
439
440         mutex_unlock(&oi_init_lock);
441         return rc;
442 }
443
444 void osd_oi_fini(struct osd_thread_info *info, struct osd_device *osd)
445 {
446         if (unlikely(osd->od_oi_table == NULL))
447                 return;
448
449         osd_oi_table_put(info, osd->od_oi_table, osd->od_oi_count);
450
451         OBD_FREE(osd->od_oi_table,
452                  sizeof(*(osd->od_oi_table)) * OSD_OI_FID_NR_MAX);
453         osd->od_oi_table = NULL;
454 }
455
456 static inline int fid_is_fs_root(const struct lu_fid *fid)
457 {
458         /* Map root inode to special local object FID */
459         return (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE &&
460                          fid_oid(fid) == OSD_FS_ROOT_OID));
461 }
462
463 static int osd_oi_iam_lookup(struct osd_thread_info *oti,
464                              struct osd_oi *oi, struct dt_rec *rec,
465                              const struct dt_key *key)
466 {
467         struct iam_container  *bag;
468         struct iam_iterator   *it = &oti->oti_idx_it;
469         struct iam_path_descr *ipd;
470         int                    rc;
471         ENTRY;
472
473         LASSERT(oi);
474         LASSERT(oi->oi_inode);
475
476         bag = &oi->oi_dir.od_container;
477         ipd = osd_idx_ipd_get(oti->oti_env, bag);
478         if (IS_ERR(ipd))
479                 RETURN(-ENOMEM);
480
481         /* got ipd now we can start iterator. */
482         iam_it_init(it, bag, 0, ipd);
483
484         rc = iam_it_get(it, (struct iam_key *)key);
485         if (rc > 0)
486                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)rec);
487         iam_it_put(it);
488         iam_it_fini(it);
489         osd_ipd_put(oti->oti_env, bag, ipd);
490
491         LINVRNT(osd_invariant(obj));
492
493         RETURN(rc);
494 }
495
496 int fid_is_on_ost(struct osd_thread_info *info, struct osd_device *osd,
497                   const struct lu_fid *fid, enum oi_check_flags flags)
498 {
499         struct lu_seq_range     *range = &info->oti_seq_range;
500         int                     rc;
501         ENTRY;
502
503         if (flags & OI_KNOWN_ON_OST)
504                 RETURN(1);
505
506         if (unlikely(fid_is_local_file(fid) || fid_is_igif(fid) ||
507                      fid_is_llog(fid)) || fid_is_name_llog(fid) ||
508                      fid_is_quota(fid))
509                 RETURN(0);
510
511         if (fid_is_idif(fid) || fid_is_last_id(fid))
512                 RETURN(1);
513
514         if (!(flags & OI_CHECK_FLD))
515                 RETURN(0);
516
517         rc = osd_fld_lookup(info->oti_env, osd, fid_seq(fid), range);
518         if (rc != 0) {
519                 if (rc != -ENOENT)
520                         CERROR("%s: lookup FLD "DFID": rc = %d\n",
521                                osd_name(osd), PFID(fid), rc);
522                 RETURN(0);
523         }
524
525         if (fld_range_is_ost(range))
526                 RETURN(1);
527
528         RETURN(0);
529 }
530
531 static int __osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
532                            const struct lu_fid *fid, struct osd_inode_id *id)
533 {
534         struct lu_fid *oi_fid = &info->oti_fid2;
535         int            rc;
536
537         fid_cpu_to_be(oi_fid, fid);
538         rc = osd_oi_iam_lookup(info, osd_fid2oi(osd, fid), (struct dt_rec *)id,
539                                (const struct dt_key *)oi_fid);
540         if (rc > 0) {
541                 osd_id_unpack(id, id);
542                 rc = 0;
543         } else if (rc == 0) {
544                 rc = -ENOENT;
545         }
546         return rc;
547 }
548
549 int osd_oi_lookup(struct osd_thread_info *info, struct osd_device *osd,
550                   const struct lu_fid *fid, struct osd_inode_id *id,
551                   enum oi_check_flags flags)
552 {
553         if (unlikely(fid_is_last_id(fid)))
554                 return osd_obj_spec_lookup(info, osd, fid, id);
555
556         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
557                 return osd_obj_map_lookup(info, osd, fid, id);
558
559
560         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
561                 int rc;
562                 if (fid_is_fs_root(fid)) {
563                         osd_id_gen(id, osd_sb(osd)->s_root->d_inode->i_ino,
564                                    osd_sb(osd)->s_root->d_inode->i_generation);
565                         return 0;
566                 }
567                 if (unlikely(fid_is_acct(fid)))
568                         return osd_acct_obj_lookup(info, osd, fid, id);
569
570                 /* For other special FIDs, try OI first, then do spec lookup */
571                 rc = __osd_oi_lookup(info, osd, fid, id);
572                 if (rc == -ENOENT)
573                         return osd_obj_spec_lookup(info, osd, fid, id);
574                 return rc;
575         }
576
577         if (!osd->od_igif_inoi && fid_is_igif(fid)) {
578                 osd_id_gen(id, lu_igif_ino(fid), lu_igif_gen(fid));
579                 return 0;
580         }
581
582         return __osd_oi_lookup(info, osd, fid, id);
583 }
584
585 static int osd_oi_iam_refresh(struct osd_thread_info *oti, struct osd_oi *oi,
586                              const struct dt_rec *rec, const struct dt_key *key,
587                              handle_t *th, bool insert)
588 {
589         struct iam_container    *bag;
590         struct iam_path_descr   *ipd;
591         int                     rc;
592         ENTRY;
593
594         LASSERT(oi);
595         LASSERT(oi->oi_inode);
596         ll_vfs_dq_init(oi->oi_inode);
597
598         bag = &oi->oi_dir.od_container;
599         ipd = osd_idx_ipd_get(oti->oti_env, bag);
600         if (unlikely(ipd == NULL))
601                 RETURN(-ENOMEM);
602
603         LASSERT(th != NULL);
604         LASSERT(th->h_transaction != NULL);
605         if (insert)
606                 rc = iam_insert(th, bag, (const struct iam_key *)key,
607                                 (const struct iam_rec *)rec, ipd);
608         else
609                 rc = iam_update(th, bag, (const struct iam_key *)key,
610                                 (const struct iam_rec *)rec, ipd);
611         osd_ipd_put(oti->oti_env, bag, ipd);
612         LINVRNT(osd_invariant(obj));
613         RETURN(rc);
614 }
615
616 int osd_oi_insert(struct osd_thread_info *info, struct osd_device *osd,
617                   const struct lu_fid *fid, const struct osd_inode_id *id,
618                   handle_t *th, enum oi_check_flags flags)
619 {
620         struct lu_fid       *oi_fid = &info->oti_fid2;
621         struct osd_inode_id *oi_id  = &info->oti_id2;
622         int                  rc     = 0;
623
624         if (unlikely(fid_is_last_id(fid)))
625                 return osd_obj_spec_insert(info, osd, fid, id, th);
626
627         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
628                 return osd_obj_map_insert(info, osd, fid, id, th);
629
630         fid_cpu_to_be(oi_fid, fid);
631         osd_id_pack(oi_id, id);
632         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
633                                (const struct dt_rec *)oi_id,
634                                (const struct dt_key *)oi_fid, th, true);
635         if (rc != 0) {
636                 struct inode *inode;
637                 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
638
639                 if (rc != -EEXIST)
640                         return rc;
641
642                 rc = osd_oi_lookup(info, osd, fid, oi_id, 0);
643                 if (rc != 0)
644                         return rc;
645
646                 if (unlikely(osd_id_eq(id, oi_id)))
647                         return 0;
648
649                 /* Check whether the mapping for oi_id is valid or not. */
650                 inode = osd_iget(info, osd, oi_id);
651                 if (IS_ERR(inode)) {
652                         rc = PTR_ERR(inode);
653                         if (rc == -ENOENT || rc == -ESTALE)
654                                 goto update;
655                         return rc;
656                 }
657
658                 rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
659                 iput(inode);
660                 if (rc == -ENODATA)
661                         goto update;
662
663                 if (rc != 0)
664                         return rc;
665
666                 if (!(lma->lma_compat & LMAC_NOT_IN_OI) &&
667                     lu_fid_eq(fid, &lma->lma_self_fid)) {
668                         CERROR("%.16s: the FID "DFID" is used by two objects: "
669                                "%u/%u %u/%u\n",
670                                LDISKFS_SB(osd_sb(osd))->s_es->s_volume_name,
671                                PFID(fid), oi_id->oii_ino, oi_id->oii_gen,
672                                id->oii_ino, id->oii_gen);
673                         return -EEXIST;
674                 }
675
676 update:
677                 osd_id_pack(oi_id, id);
678                 rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
679                                         (const struct dt_rec *)oi_id,
680                                         (const struct dt_key *)oi_fid, th, false);
681                 if (rc != 0)
682                         return rc;
683         }
684
685         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
686                 rc = osd_obj_spec_insert(info, osd, fid, id, th);
687         return rc;
688 }
689
690 static int osd_oi_iam_delete(struct osd_thread_info *oti, struct osd_oi *oi,
691                              const struct dt_key *key, handle_t *th)
692 {
693         struct iam_container    *bag;
694         struct iam_path_descr   *ipd;
695         int                      rc;
696         ENTRY;
697
698         LASSERT(oi);
699         LASSERT(oi->oi_inode);
700         ll_vfs_dq_init(oi->oi_inode);
701
702         bag = &oi->oi_dir.od_container;
703         ipd = osd_idx_ipd_get(oti->oti_env, bag);
704         if (unlikely(ipd == NULL))
705                 RETURN(-ENOMEM);
706
707         LASSERT(th != NULL);
708         LASSERT(th->h_transaction != NULL);
709
710         rc = iam_delete(th, bag, (const struct iam_key *)key, ipd);
711         osd_ipd_put(oti->oti_env, bag, ipd);
712         LINVRNT(osd_invariant(obj));
713         RETURN(rc);
714 }
715
716 int osd_oi_delete(struct osd_thread_info *info,
717                   struct osd_device *osd, const struct lu_fid *fid,
718                   handle_t *th, enum oi_check_flags flags)
719 {
720         struct lu_fid *oi_fid = &info->oti_fid2;
721
722         /* clear idmap cache */
723         if (lu_fid_eq(fid, &info->oti_cache.oic_fid))
724                 fid_zero(&info->oti_cache.oic_fid);
725
726         if (fid_is_last_id(fid))
727                 return 0;
728
729         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
730                 return osd_obj_map_delete(info, osd, fid, th);
731
732         fid_cpu_to_be(oi_fid, fid);
733         return osd_oi_iam_delete(info, osd_fid2oi(osd, fid),
734                                  (const struct dt_key *)oi_fid, th);
735 }
736
737 int osd_oi_update(struct osd_thread_info *info, struct osd_device *osd,
738                   const struct lu_fid *fid, const struct osd_inode_id *id,
739                   handle_t *th, enum oi_check_flags flags)
740 {
741         struct lu_fid       *oi_fid = &info->oti_fid2;
742         struct osd_inode_id *oi_id  = &info->oti_id2;
743         int                  rc     = 0;
744
745         if (unlikely(fid_is_last_id(fid)))
746                 return osd_obj_spec_update(info, osd, fid, id, th);
747
748         if (fid_is_on_ost(info, osd, fid, flags) || fid_is_llog(fid))
749                 return osd_obj_map_update(info, osd, fid, id, th);
750
751         fid_cpu_to_be(oi_fid, fid);
752         osd_id_pack(oi_id, id);
753         rc = osd_oi_iam_refresh(info, osd_fid2oi(osd, fid),
754                                (const struct dt_rec *)oi_id,
755                                (const struct dt_key *)oi_fid, th, false);
756         if (rc != 0)
757                 return rc;
758
759         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE))
760                 rc = osd_obj_spec_update(info, osd, fid, id, th);
761         return rc;
762 }
763
764 int osd_oi_mod_init(void)
765 {
766         if (osd_oi_count == 0 || osd_oi_count > OSD_OI_FID_NR_MAX)
767                 osd_oi_count = OSD_OI_FID_NR;
768
769         if ((osd_oi_count & (osd_oi_count - 1)) != 0) {
770                 LCONSOLE_WARN("Round up oi_count %d to power2 %d\n",
771                               osd_oi_count, size_roundup_power2(osd_oi_count));
772                 osd_oi_count = size_roundup_power2(osd_oi_count);
773         }
774
775         mutex_init(&oi_init_lock);
776         return 0;
777 }