Whamcloud - gitweb
d58b6c87ba7e432b3c37200b726db9bec9f5247c
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_compat.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) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, 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_compat.c
37  *
38  * on-disk structure for managing /O
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  */
42
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/fs.h>
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
51
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  * OBD_FAIL_CHECK
55  */
56 #include <obd_support.h>
57
58 #include "osd_internal.h"
59 #include "osd_oi.h"
60
61 static void osd_push_ctxt(const struct osd_device *dev,
62                           struct lvfs_run_ctxt *newctxt,
63                           struct lvfs_run_ctxt *save)
64 {
65         OBD_SET_CTXT_MAGIC(newctxt);
66         newctxt->pwdmnt = dev->od_mnt;
67         newctxt->pwd = dev->od_mnt->mnt_root;
68         newctxt->fs = get_ds();
69         newctxt->umask = current_umask();
70         newctxt->dt = NULL;
71
72         push_ctxt(save, newctxt);
73 }
74
75 /* utility to make a directory */
76 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
77                                    const char *name, int mode, int fix)
78 {
79         struct dentry *dchild;
80         int err = 0;
81         ENTRY;
82
83         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
84         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
85         dchild = ll_lookup_one_len(name, dir, strlen(name));
86         if (IS_ERR(dchild))
87                 GOTO(out_up, dchild);
88
89         if (dchild->d_inode) {
90                 int old_mode = dchild->d_inode->i_mode;
91                 if (!S_ISDIR(old_mode)) {
92                         CERROR("found %s (%lu/%u) is mode %o\n", name,
93                                dchild->d_inode->i_ino,
94                                dchild->d_inode->i_generation, old_mode);
95                         GOTO(out_err, err = -ENOTDIR);
96                 }
97
98                 /* Fixup directory permissions if necessary */
99                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
100                         CDEBUG(D_CONFIG,
101                                "fixing permissions on %s from %o to %o\n",
102                                name, old_mode, mode);
103                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
104                                                   (old_mode & ~S_IALLUGO);
105                         mark_inode_dirty(dchild->d_inode);
106                 }
107                 GOTO(out_up, dchild);
108         }
109
110         err = vfs_mkdir(dir->d_inode, dchild, mode);
111         if (err)
112                 GOTO(out_err, err);
113
114         RETURN(dchild);
115
116 out_err:
117         dput(dchild);
118         dchild = ERR_PTR(err);
119 out_up:
120         return dchild;
121 }
122
123 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
124 {
125         struct lr_server_data lsd;
126         struct dentry        *dlast;
127         loff_t                off;
128         int                   rc = 0;
129         int                   count = OBJ_SUBDIR_COUNT;
130
131         ENTRY;
132
133         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
134                                   strlen(LAST_RCVD));
135         if (IS_ERR(dlast))
136                 return PTR_ERR(dlast);
137         else if (dlast->d_inode == NULL)
138                 goto out;
139
140         off = 0;
141         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
142         if (rc == sizeof(lsd)) {
143                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
144                        "subdir count = %d\n", lsd.lsd_uuid,
145                        lsd.lsd_subdir_count);
146                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
147                         count = le16_to_cpu(lsd.lsd_subdir_count);
148         } else if (rc != 0) {
149                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
150                 if (rc > 0)
151                         rc = -EFAULT;
152                 dput(dlast);
153                 return rc;
154         }
155 out:
156         dput(dlast);
157         LASSERT(count > 0);
158         return count;
159 }
160
161 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
162 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
163 {
164         struct lvfs_run_ctxt    new;
165         struct lvfs_run_ctxt    save;
166         struct dentry           *parent;
167         struct osd_mdobj_map    *omm;
168         struct dentry           *d;
169         struct osd_thread_info  *info = osd_oti_get(env);
170         struct lu_fid           *fid = &info->oti_fid3;
171         int                     rc = 0;
172         ENTRY;
173
174         OBD_ALLOC_PTR(dev->od_mdt_map);
175         if (dev->od_mdt_map == NULL)
176                 RETURN(-ENOMEM);
177
178         omm = dev->od_mdt_map;
179
180         parent = osd_sb(dev)->s_root;
181         osd_push_ctxt(dev, &new, &save);
182
183         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
184                          0755, 1);
185         if (IS_ERR(d))
186                 GOTO(cleanup, rc = PTR_ERR(d));
187
188         omm->omm_remote_parent = d;
189
190         /* Set LMA for remote parent inode */
191         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
192         rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
193
194         GOTO(cleanup, rc);
195
196 cleanup:
197         pop_ctxt(&save, &new);
198         if (rc) {
199                 if (omm->omm_remote_parent != NULL)
200                         dput(omm->omm_remote_parent);
201                 OBD_FREE_PTR(omm);
202                 dev->od_mdt_map = NULL;
203         }
204         return rc;
205 }
206
207 static void osd_mdt_fini(struct osd_device *osd)
208 {
209         struct osd_mdobj_map *omm = osd->od_mdt_map;
210
211         if (omm == NULL)
212                 return;
213
214         if (omm->omm_remote_parent)
215                 dput(omm->omm_remote_parent);
216
217         OBD_FREE_PTR(omm);
218         osd->od_ost_map = NULL;
219 }
220
221 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
222                              struct osd_object *obj, struct osd_thandle *oh)
223 {
224         struct osd_mdobj_map    *omm = osd->od_mdt_map;
225         struct osd_thread_info  *oti = osd_oti_get(env);
226         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
227         char                    *name = oti->oti_name;
228         struct osd_thread_info  *info = osd_oti_get(env);
229         struct dentry           *dentry;
230         struct dentry           *parent;
231         int                     rc;
232
233         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
234          * can identify this object quickly */
235         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
236         if (rc != 0)
237                 RETURN(rc);
238
239         lma->lma_incompat |= LMAI_REMOTE_PARENT;
240         lustre_lma_swab(lma);
241         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
242                              sizeof(*lma), XATTR_REPLACE);
243         if (rc != 0)
244                 RETURN(rc);
245
246         parent = omm->omm_remote_parent;
247         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
248         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
249                                            name, strlen(name));
250         mutex_lock(&parent->d_inode->i_mutex);
251         rc = osd_ldiskfs_add_entry(info, oh->ot_handle, dentry,
252                                    obj->oo_inode, NULL);
253         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
254                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
255         ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
256         mark_inode_dirty(parent->d_inode);
257         mutex_unlock(&parent->d_inode->i_mutex);
258         RETURN(rc);
259 }
260
261 int osd_delete_from_remote_parent(const struct lu_env *env,
262                                   struct osd_device *osd,
263                                   struct osd_object *obj,
264                                   struct osd_thandle *oh)
265 {
266         struct osd_mdobj_map       *omm = osd->od_mdt_map;
267         struct osd_thread_info     *oti = osd_oti_get(env);
268         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
269         char                       *name = oti->oti_name;
270         struct dentry              *dentry;
271         struct dentry              *parent;
272         struct ldiskfs_dir_entry_2 *de;
273         struct buffer_head         *bh;
274         int                        rc;
275
276         /* Check lma to see whether it is remote object */
277         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
278         if (rc != 0)
279                 RETURN(rc);
280
281         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
282                 RETURN(0);
283
284         parent = omm->omm_remote_parent;
285         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
286         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
287                                            name, strlen(name));
288         mutex_lock(&parent->d_inode->i_mutex);
289         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
290                                     NULL, NULL);
291         if (bh == NULL) {
292                 mutex_unlock(&parent->d_inode->i_mutex);
293                 RETURN(-ENOENT);
294         }
295         CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
296                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
297         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
298         ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
299         mark_inode_dirty(parent->d_inode);
300         mutex_unlock(&parent->d_inode->i_mutex);
301         brelse(bh);
302
303         /* Get rid of REMOTE_PARENT flag from incompat */
304         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
305         lustre_lma_swab(lma);
306         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
307                              sizeof(*lma), XATTR_REPLACE);
308         RETURN(rc);
309 }
310
311 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
312                                 struct osd_device *osd,
313                                 const struct lu_fid *fid,
314                                 struct osd_inode_id *id)
315 {
316         struct osd_mdobj_map        *omm = osd->od_mdt_map;
317         char                        *name = oti->oti_name;
318         struct dentry               *parent;
319         struct dentry               *dentry;
320         struct ldiskfs_dir_entry_2 *de;
321         struct buffer_head         *bh;
322         int                         rc;
323         ENTRY;
324
325         parent = omm->omm_remote_parent;
326         sprintf(name, DFID_NOBRACE, PFID(fid));
327         dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
328                                            name, strlen(name));
329         mutex_lock(&parent->d_inode->i_mutex);
330         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
331                                     NULL, NULL);
332         if (bh == NULL) {
333                 rc = -ENOENT;
334         } else {
335                 rc = 0;
336                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
337                 brelse(bh);
338         }
339         mutex_unlock(&parent->d_inode->i_mutex);
340         if (rc == 0)
341                 osd_add_oi_cache(oti, osd, id, fid);
342         RETURN(rc);
343 }
344
345 /*
346  * directory structure on legacy OST:
347  *
348  * O/<seq>/d0-31/<objid>
349  * O/<seq>/LAST_ID
350  * last_rcvd
351  * LAST_GROUP
352  * CONFIGS
353  *
354  */
355 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
356 {
357         struct lvfs_run_ctxt     new;
358         struct lvfs_run_ctxt     save;
359         struct dentry           *rootd = osd_sb(dev)->s_root;
360         struct dentry           *d;
361         struct osd_thread_info  *info = osd_oti_get(env);
362         struct inode            *inode;
363         struct lu_fid           *fid = &info->oti_fid3;
364         int                      rc;
365         ENTRY;
366
367         OBD_ALLOC_PTR(dev->od_ost_map);
368         if (dev->od_ost_map == NULL)
369                 RETURN(-ENOMEM);
370
371         /* to get subdir count from last_rcvd */
372         rc = osd_last_rcvd_subdir_count(dev);
373         if (rc < 0)
374                 GOTO(cleanup_alloc, rc);
375
376         dev->od_ost_map->om_subdir_count = rc;
377         rc = 0;
378
379         INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
380         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
381         mutex_init(&dev->od_ost_map->om_dir_init_mutex);
382
383         osd_push_ctxt(dev, &new, &save);
384
385         d = ll_lookup_one_len("O", rootd, strlen("O"));
386         if (IS_ERR(d))
387                 GOTO(cleanup_ctxt, rc = PTR_ERR(d));
388         if (d->d_inode == NULL) {
389                 dput(d);
390                 /* The lookup() may be called again inside simple_mkdir().
391                  * Since the repeated lookup() only be called for "/O" at
392                  * mount time, it will not affect the whole performance. */
393                 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
394                 if (IS_ERR(d))
395                         GOTO(cleanup_ctxt, rc = PTR_ERR(d));
396
397                 /* It is quite probably that the device is new formatted. */
398                 dev->od_maybe_new = 1;
399         }
400
401         inode = d->d_inode;
402         dev->od_ost_map->om_root = d;
403
404         /* 'What the @fid is' is not imporatant, because the object
405          * has no OI mapping, and only is visible inside the OSD.*/
406         lu_igif_build(fid, inode->i_ino, inode->i_generation);
407         rc = osd_ea_fid_set(info, inode, fid,
408                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
409         if (rc)
410                 GOTO(cleanup_dentry, rc);
411
412         pop_ctxt(&save, &new);
413         RETURN(0);
414
415 cleanup_dentry:
416         dput(d);
417 cleanup_ctxt:
418         pop_ctxt(&save, &new);
419 cleanup_alloc:
420         OBD_FREE_PTR(dev->od_ost_map);
421         return rc;
422 }
423
424 static void osd_seq_free(struct osd_obj_map *map,
425                          struct osd_obj_seq *osd_seq)
426 {
427         int j;
428
429         list_del_init(&osd_seq->oos_seq_list);
430
431         if (osd_seq->oos_dirs) {
432                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
433                         if (osd_seq->oos_dirs[j])
434                                 dput(osd_seq->oos_dirs[j]);
435                 }
436                 OBD_FREE(osd_seq->oos_dirs,
437                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
438         }
439
440         if (osd_seq->oos_root)
441                 dput(osd_seq->oos_root);
442
443         OBD_FREE_PTR(osd_seq);
444 }
445
446 static void osd_ost_fini(struct osd_device *osd)
447 {
448         struct osd_obj_seq    *osd_seq;
449         struct osd_obj_seq    *tmp;
450         struct osd_obj_map    *map = osd->od_ost_map;
451         ENTRY;
452
453         if (map == NULL)
454                 return;
455
456         write_lock(&map->om_seq_list_lock);
457         list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
458                                  oos_seq_list) {
459                 osd_seq_free(map, osd_seq);
460         }
461         write_unlock(&map->om_seq_list_lock);
462         if (map->om_root)
463                 dput(map->om_root);
464         OBD_FREE_PTR(map);
465         osd->od_ost_map = NULL;
466         EXIT;
467 }
468
469 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
470 {
471         int rc;
472         ENTRY;
473
474         /* prepare structures for OST */
475         rc = osd_ost_init(env, dev);
476         if (rc)
477                 RETURN(rc);
478
479         /* prepare structures for MDS */
480         rc = osd_mdt_init(env, dev);
481         if (rc)
482                 osd_ost_fini(dev);
483
484         RETURN(rc);
485 }
486
487 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, u64 seq)
488 {
489         struct osd_obj_seq *osd_seq;
490
491         list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
492                 if (osd_seq->oos_seq == seq)
493                         return osd_seq;
494         }
495         return NULL;
496 }
497
498 static struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, u64 seq)
499 {
500         struct osd_obj_seq *osd_seq;
501
502         read_lock(&map->om_seq_list_lock);
503         osd_seq = osd_seq_find_locked(map, seq);
504         read_unlock(&map->om_seq_list_lock);
505         return osd_seq;
506 }
507
508 void osd_obj_map_fini(struct osd_device *dev)
509 {
510         osd_ost_fini(dev);
511         osd_mdt_fini(dev);
512 }
513
514 /**
515  * Update the specified OI mapping.
516  *
517  * \retval   1, changed nothing
518  * \retval   0, changed successfully
519  * \retval -ve, on error
520  */
521 static int osd_obj_update_entry(struct osd_thread_info *info,
522                                 struct osd_device *osd,
523                                 struct dentry *dir, const char *name,
524                                 const struct lu_fid *fid,
525                                 const struct osd_inode_id *id,
526                                 handle_t *th)
527 {
528         struct inode               *parent = dir->d_inode;
529         struct dentry              *child;
530         struct ldiskfs_dir_entry_2 *de;
531         struct buffer_head         *bh;
532         struct inode               *inode;
533         struct dentry              *dentry = &info->oti_obj_dentry;
534         struct osd_inode_id        *oi_id  = &info->oti_id3;
535         struct lustre_mdt_attrs    *lma    = &info->oti_mdt_attrs;
536         struct lu_fid              *oi_fid = &lma->lma_self_fid;
537         int                         rc;
538         ENTRY;
539
540         LASSERT(th != NULL);
541         LASSERT(th->h_transaction != NULL);
542
543         child = &info->oti_child_dentry;
544         child->d_parent = dir;
545         child->d_name.hash = 0;
546         child->d_name.name = name;
547         child->d_name.len = strlen(name);
548
549         ll_vfs_dq_init(parent);
550         mutex_lock(&parent->i_mutex);
551         bh = osd_ldiskfs_find_entry(parent, &child->d_name, &de, NULL, NULL);
552         if (bh == NULL)
553                 GOTO(out, rc = -ENOENT);
554
555         if (le32_to_cpu(de->inode) == id->oii_ino)
556                 GOTO(out, rc = 1);
557
558         osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
559         inode = osd_iget(info, osd, oi_id);
560         if (IS_ERR(inode)) {
561                 rc = PTR_ERR(inode);
562                 if (rc == -ENOENT || rc == -ESTALE)
563                         goto update;
564                 GOTO(out, rc);
565         }
566
567         /* The EA inode should NOT be in OI, old OI scrub may added
568          * such OI mapping by wrong, replace it. */
569         if (unlikely(osd_is_ea_inode(inode))) {
570                 iput(inode);
571                 goto update;
572         }
573
574         rc = osd_get_lma(info, inode, dentry, lma);
575         if (rc == -ENODATA) {
576                 rc = osd_get_idif(info, inode, dentry, oi_fid);
577                 if (rc > 0 || rc == -ENODATA) {
578                         oi_fid = NULL;
579                         rc = 0;
580                 }
581         }
582         iput(inode);
583
584         if (rc != 0)
585                 GOTO(out, rc);
586
587         /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
588          * either a crashed object or a uninitialized one. Replace it. */
589         if (oi_fid != NULL && lu_fid_eq(fid, oi_fid)) {
590                 CERROR("%s: the FID "DFID" is used by two objects: "
591                        "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
592                        oi_id->oii_ino, oi_id->oii_gen,
593                        id->oii_ino, id->oii_gen);
594                 GOTO(out, rc = -EEXIST);
595         }
596
597         if (fid_is_idif(fid) && oi_fid != NULL && fid_is_idif(oi_fid)) {
598                 __u32 idx1 = fid_idif_ost_idx(fid);
599                 __u32 idx2 = fid_idif_ost_idx(oi_fid);
600                 struct ost_id *ostid = &info->oti_ostid;
601                 struct lu_fid *tfid = &info->oti_fid3;
602
603                 LASSERTF(idx1 == 0 || idx1 == osd->od_index,
604                          "invalid given FID "DFID", not match the "
605                          "device index %u\n", PFID(fid), osd->od_index);
606
607                 if (idx1 != idx2) {
608                         if (idx1 == 0 && idx2 == osd->od_index) {
609                                 fid_to_ostid(fid, ostid);
610                                 ostid_to_fid(tfid, ostid, idx2);
611                                 if (lu_fid_eq(tfid, oi_fid)) {
612                                         CERROR("%s: the FID "DFID" is used by "
613                                                "two objects(2): %u/%u %u/%u\n",
614                                                osd_name(osd), PFID(fid),
615                                                oi_id->oii_ino, oi_id->oii_gen,
616                                                id->oii_ino, id->oii_gen);
617
618                                         GOTO(out, rc = -EEXIST);
619                                 }
620                         } else if (idx2 == 0 && idx1 == osd->od_index) {
621                                 fid_to_ostid(oi_fid, ostid);
622                                 ostid_to_fid(tfid, ostid, idx1);
623                                 if (lu_fid_eq(tfid, fid)) {
624                                         CERROR("%s: the FID "DFID" is used by "
625                                                "two objects(2): %u/%u %u/%u\n",
626                                                osd_name(osd), PFID(fid),
627                                                oi_id->oii_ino, oi_id->oii_gen,
628                                                id->oii_ino, id->oii_gen);
629
630                                         GOTO(out, rc = -EEXIST);
631                                 }
632                         }
633                 }
634         }
635
636 update:
637         /* There may be temporary inconsistency: On one hand, the new
638          * object may be referenced by multiple entries, which is out
639          * of our control unless we traverse the whole /O completely,
640          * which is non-flat order and inefficient, should be avoided;
641          * On the other hand, the old object may become orphan if it
642          * is still valid. Since it was referenced by an invalid entry,
643          * making it as invisible temporary may be not worse. OI scrub
644          * will process it later. */
645         rc = ldiskfs_journal_get_write_access(th, bh);
646         if (rc != 0)
647                 GOTO(out, rc);
648
649         de->inode = cpu_to_le32(id->oii_ino);
650         rc = ldiskfs_handle_dirty_metadata(th, NULL, bh);
651
652         GOTO(out, rc);
653
654 out:
655         brelse(bh);
656         mutex_unlock(&parent->i_mutex);
657         return rc;
658 }
659
660 static int osd_obj_del_entry(struct osd_thread_info *info,
661                              struct osd_device *osd,
662                              struct dentry *dird, char *name,
663                              handle_t *th)
664 {
665         struct ldiskfs_dir_entry_2 *de;
666         struct buffer_head         *bh;
667         struct dentry              *child;
668         struct inode               *dir = dird->d_inode;
669         int                         rc;
670         ENTRY;
671
672         LASSERT(th != NULL);
673         LASSERT(th->h_transaction != NULL);
674
675
676         child = &info->oti_child_dentry;
677         child->d_name.hash = 0;
678         child->d_name.name = name;
679         child->d_name.len = strlen(name);
680         child->d_parent = dird;
681         child->d_inode = NULL;
682
683         ll_vfs_dq_init(dir);
684         mutex_lock(&dir->i_mutex);
685         rc = -ENOENT;
686         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
687         if (bh) {
688                 rc = ldiskfs_delete_entry(th, dir, de, bh);
689                 brelse(bh);
690         }
691         mutex_unlock(&dir->i_mutex);
692
693         RETURN(rc);
694 }
695
696 static int osd_obj_add_entry(struct osd_thread_info *info,
697                              struct osd_device *osd,
698                              struct dentry *dir, char *name,
699                              const struct osd_inode_id *id,
700                              handle_t *th)
701 {
702         struct dentry *child;
703         struct inode *inode;
704         int rc;
705
706         ENTRY;
707
708         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
709                 RETURN(0);
710
711         LASSERT(th != NULL);
712         LASSERT(th->h_transaction != NULL);
713
714         inode = info->oti_inode;
715         if (unlikely(inode == NULL)) {
716                 struct ldiskfs_inode_info *lii;
717                 OBD_ALLOC_PTR(lii);
718                 if (lii == NULL)
719                         RETURN(-ENOMEM);
720                 inode = info->oti_inode = &lii->vfs_inode;
721         }
722
723         inode->i_sb = osd_sb(osd);
724         osd_id_to_inode(inode, id);
725         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
726
727         child = &info->oti_child_dentry;
728         child->d_name.hash = 0;
729         child->d_name.name = name;
730         child->d_name.len = strlen(name);
731         child->d_parent = dir;
732         child->d_inode = inode;
733
734         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
735                 inode->i_ino++;
736
737         ll_vfs_dq_init(dir->d_inode);
738         mutex_lock(&dir->d_inode->i_mutex);
739         rc = osd_ldiskfs_add_entry(info, th, child, inode, NULL);
740         mutex_unlock(&dir->d_inode->i_mutex);
741
742         RETURN(rc);
743 }
744
745 /**
746  * Use LPU64 for legacy OST sequences, but use LPX64i for new
747  * sequences names, so that the O/{seq}/dN/{oid} more closely
748  * follows the DFID/PFID format. This makes it easier to map from
749  * debug messages to objects in the future, and the legacy space
750  * of FID_SEQ_OST_MDT0 will be unused in the future.
751  **/
752 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
753 {
754         snprintf(seq_name, name_size,
755                  (fid_seq_is_rsvd(seq) ||
756                   fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
757                  fid_seq_is_idif(seq) ? 0 : seq);
758 }
759
760 static inline void osd_oid_name(char *name, size_t name_size,
761                                 const struct lu_fid *fid, u64 id)
762 {
763         snprintf(name, name_size,
764                  (fid_seq_is_rsvd(fid_seq(fid)) ||
765                   fid_seq_is_mdt0(fid_seq(fid)) ||
766                   fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
767 }
768
769 /* external locking is required */
770 static int osd_seq_load_locked(struct osd_thread_info *info,
771                                struct osd_device *osd,
772                                struct osd_obj_seq *osd_seq)
773 {
774         struct osd_obj_map  *map = osd->od_ost_map;
775         struct dentry       *seq_dir;
776         struct inode        *inode;
777         struct lu_fid       *fid = &info->oti_fid3;
778         int                 rc = 0;
779         int                 i;
780         char                dir_name[32];
781         ENTRY;
782
783         if (osd_seq->oos_root != NULL)
784                 RETURN(0);
785
786         LASSERT(map);
787         LASSERT(map->om_root);
788
789         osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
790
791         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
792         if (IS_ERR(seq_dir))
793                 GOTO(out_err, rc = PTR_ERR(seq_dir));
794         else if (seq_dir->d_inode == NULL)
795                 GOTO(out_put, rc = -EFAULT);
796
797         inode = seq_dir->d_inode;
798         osd_seq->oos_root = seq_dir;
799
800         /* 'What the @fid is' is not imporatant, because the object
801          * has no OI mapping, and only is visible inside the OSD.*/
802         lu_igif_build(fid, inode->i_ino, inode->i_generation);
803         rc = osd_ea_fid_set(info, inode, fid,
804                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
805         if (rc != 0)
806                 GOTO(out_put, rc);
807
808         LASSERT(osd_seq->oos_dirs == NULL);
809         OBD_ALLOC(osd_seq->oos_dirs,
810                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
811         if (osd_seq->oos_dirs == NULL)
812                 GOTO(out_put, rc = -ENOMEM);
813
814         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
815                 struct dentry   *dir;
816
817                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
818                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
819                                    0700, 1);
820                 if (IS_ERR(dir)) {
821                         GOTO(out_free, rc = PTR_ERR(dir));
822                 } else if (dir->d_inode == NULL) {
823                         dput(dir);
824                         GOTO(out_free, rc = -EFAULT);
825                 }
826
827                 inode = dir->d_inode;
828                 osd_seq->oos_dirs[i] = dir;
829
830                 /* 'What the @fid is' is not imporatant, because the object
831                  * has no OI mapping, and only is visible inside the OSD.*/
832                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
833                 rc = osd_ea_fid_set(info, inode, fid,
834                                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
835                 if (rc != 0)
836                         GOTO(out_free, rc);
837         }
838
839         if (rc != 0) {
840 out_free:
841                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
842                         if (osd_seq->oos_dirs[i] != NULL)
843                                 dput(osd_seq->oos_dirs[i]);
844                 }
845                 OBD_FREE(osd_seq->oos_dirs,
846                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
847 out_put:
848                 dput(seq_dir);
849                 osd_seq->oos_root = NULL;
850         }
851 out_err:
852         RETURN(rc);
853 }
854
855 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
856                                         struct osd_device *osd, u64 seq)
857 {
858         struct osd_obj_map      *map;
859         struct osd_obj_seq      *osd_seq;
860         int                     rc = 0;
861         ENTRY;
862
863         map = osd->od_ost_map;
864         LASSERT(map);
865         LASSERT(map->om_root);
866
867         osd_seq = osd_seq_find(map, seq);
868         if (likely(osd_seq != NULL))
869                 RETURN(osd_seq);
870
871         /* Serializing init process */
872         mutex_lock(&map->om_dir_init_mutex);
873
874         /* Check whether the seq has been added */
875         read_lock(&map->om_seq_list_lock);
876         osd_seq = osd_seq_find_locked(map, seq);
877         if (osd_seq != NULL) {
878                 read_unlock(&map->om_seq_list_lock);
879                 GOTO(cleanup, rc = 0);
880         }
881         read_unlock(&map->om_seq_list_lock);
882
883         OBD_ALLOC_PTR(osd_seq);
884         if (osd_seq == NULL)
885                 GOTO(cleanup, rc = -ENOMEM);
886
887         INIT_LIST_HEAD(&osd_seq->oos_seq_list);
888         osd_seq->oos_seq = seq;
889         /* Init subdir count to be 32, but each seq can have
890          * different subdir count */
891         osd_seq->oos_subdir_count = map->om_subdir_count;
892         rc = osd_seq_load_locked(info, osd, osd_seq);
893         if (rc != 0)
894                 GOTO(cleanup, rc);
895
896         write_lock(&map->om_seq_list_lock);
897         list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
898         write_unlock(&map->om_seq_list_lock);
899
900 cleanup:
901         mutex_unlock(&map->om_dir_init_mutex);
902         if (rc != 0) {
903                 if (osd_seq != NULL)
904                         OBD_FREE_PTR(osd_seq);
905                 RETURN(ERR_PTR(rc));
906         }
907
908         RETURN(osd_seq);
909 }
910
911 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
912                        const struct lu_fid *fid, struct osd_inode_id *id)
913 {
914         struct osd_obj_map              *map;
915         struct osd_obj_seq              *osd_seq;
916         struct dentry                   *d_seq;
917         struct dentry                   *child;
918         struct ost_id                   *ostid = &info->oti_ostid;
919         int                             dirn;
920         char                            name[32];
921         struct ldiskfs_dir_entry_2      *de;
922         struct buffer_head              *bh;
923         struct inode                    *dir;
924         struct inode                    *inode;
925         ENTRY;
926
927         /* on the very first lookup we find and open directories */
928
929         map = dev->od_ost_map;
930         LASSERT(map);
931         LASSERT(map->om_root);
932
933         fid_to_ostid(fid, ostid);
934         osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
935         if (IS_ERR(osd_seq))
936                 RETURN(PTR_ERR(osd_seq));
937
938         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
939         d_seq = osd_seq->oos_dirs[dirn];
940         LASSERT(d_seq);
941
942         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
943
944         child = &info->oti_child_dentry;
945         child->d_parent = d_seq;
946         child->d_name.hash = 0;
947         child->d_name.name = name;
948         /* XXX: we can use rc from sprintf() instead of strlen() */
949         child->d_name.len = strlen(name);
950
951         dir = d_seq->d_inode;
952         mutex_lock(&dir->i_mutex);
953         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
954         mutex_unlock(&dir->i_mutex);
955
956         if (bh == NULL)
957                 RETURN(-ENOENT);
958
959         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
960         brelse(bh);
961
962         inode = osd_iget(info, dev, id);
963         if (IS_ERR(inode)) {
964                 int rc = PTR_ERR(inode);
965
966                 RETURN(rc == -ENOENT ? -ESTALE : rc);
967         }
968
969         iput(inode);
970         RETURN(0);
971 }
972
973 int osd_obj_map_insert(struct osd_thread_info *info,
974                        struct osd_device *osd,
975                        const struct lu_fid *fid,
976                        const struct osd_inode_id *id,
977                        handle_t *th)
978 {
979         struct osd_obj_map      *map;
980         struct osd_obj_seq      *osd_seq;
981         struct dentry           *d;
982         struct ost_id           *ostid = &info->oti_ostid;
983         u64                      oid;
984         int                     dirn, rc = 0;
985         char                    name[32];
986         ENTRY;
987
988         map = osd->od_ost_map;
989         LASSERT(map);
990
991         /* map fid to seq:objid */
992         fid_to_ostid(fid, ostid);
993
994         oid = ostid_id(ostid);
995         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
996         if (IS_ERR(osd_seq))
997                 RETURN(PTR_ERR(osd_seq));
998
999         dirn = oid & (osd_seq->oos_subdir_count - 1);
1000         d = osd_seq->oos_dirs[dirn];
1001         LASSERT(d);
1002
1003         osd_oid_name(name, sizeof(name), fid, oid);
1004
1005 again:
1006         rc = osd_obj_add_entry(info, osd, d, name, id, th);
1007         if (rc == -EEXIST) {
1008                 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1009                 if (unlikely(rc == -ENOENT))
1010                         goto again;
1011
1012                 if (unlikely(rc == 1))
1013                         RETURN(0);
1014         }
1015
1016         RETURN(rc);
1017 }
1018
1019 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1020                        const struct lu_fid *fid, handle_t *th)
1021 {
1022         struct osd_obj_map      *map;
1023         struct osd_obj_seq      *osd_seq;
1024         struct dentry           *d;
1025         struct ost_id           *ostid = &info->oti_ostid;
1026         int                     dirn, rc = 0;
1027         char                    name[32];
1028         ENTRY;
1029
1030         map = osd->od_ost_map;
1031         LASSERT(map);
1032
1033         /* map fid to seq:objid */
1034         fid_to_ostid(fid, ostid);
1035
1036         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1037         if (IS_ERR(osd_seq))
1038                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1039
1040         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1041         d = osd_seq->oos_dirs[dirn];
1042         LASSERT(d);
1043
1044         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1045         rc = osd_obj_del_entry(info, osd, d, name, th);
1046 cleanup:
1047         RETURN(rc);
1048 }
1049
1050 int osd_obj_map_update(struct osd_thread_info *info,
1051                        struct osd_device *osd,
1052                        const struct lu_fid *fid,
1053                        const struct osd_inode_id *id,
1054                        handle_t *th)
1055 {
1056         struct osd_obj_seq      *osd_seq;
1057         struct dentry           *d;
1058         struct ost_id           *ostid = &info->oti_ostid;
1059         int                     dirn, rc = 0;
1060         char                    name[32];
1061         ENTRY;
1062
1063         fid_to_ostid(fid, ostid);
1064         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1065         if (IS_ERR(osd_seq))
1066                 RETURN(PTR_ERR(osd_seq));
1067
1068         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1069         d = osd_seq->oos_dirs[dirn];
1070         LASSERT(d);
1071
1072         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1073         rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1074
1075         RETURN(rc);
1076 }
1077
1078 int osd_obj_map_recover(struct osd_thread_info *info,
1079                         struct osd_device *osd,
1080                         struct inode *src_parent,
1081                         struct dentry *src_child,
1082                         const struct lu_fid *fid)
1083 {
1084         struct osd_obj_seq         *osd_seq;
1085         struct dentry              *tgt_parent;
1086         struct dentry              *tgt_child = &info->oti_child_dentry;
1087         struct inode               *dir;
1088         struct inode               *inode     = src_child->d_inode;
1089         struct ost_id              *ostid     = &info->oti_ostid;
1090         handle_t                   *jh;
1091         struct ldiskfs_dir_entry_2 *de;
1092         struct buffer_head         *bh;
1093         char                        name[32];
1094         int                         dirn;
1095         int                         rc        = 0;
1096         ENTRY;
1097
1098         if (fid_is_last_id(fid)) {
1099                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1100                 if (IS_ERR(osd_seq))
1101                         RETURN(PTR_ERR(osd_seq));
1102
1103                 tgt_parent = osd_seq->oos_root;
1104                 tgt_child->d_name.name = "LAST_ID";
1105                 tgt_child->d_name.len = strlen("LAST_ID");
1106         } else {
1107                 fid_to_ostid(fid, ostid);
1108                 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1109                 if (IS_ERR(osd_seq))
1110                         RETURN(PTR_ERR(osd_seq));
1111
1112                 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1113                 tgt_parent = osd_seq->oos_dirs[dirn];
1114                 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1115                 tgt_child->d_name.name = name;
1116                 tgt_child->d_name.len = strlen(name);
1117         }
1118         LASSERT(tgt_parent != NULL);
1119
1120         dir = tgt_parent->d_inode;
1121         tgt_child->d_name.hash = 0;
1122         tgt_child->d_parent = tgt_parent;
1123         tgt_child->d_inode = inode;
1124
1125         /* The non-initialized src_child may be destroyed. */
1126         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1127                                 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1128                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1129                                 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1130         if (IS_ERR(jh))
1131                 RETURN(PTR_ERR(jh));
1132
1133         ll_vfs_dq_init(src_parent);
1134         ll_vfs_dq_init(dir);
1135
1136         mutex_lock(&src_parent->i_mutex);
1137         mutex_lock(&dir->i_mutex);
1138         bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1139         if (bh != NULL) {
1140                 /* XXX: If some other object occupied the same slot. And If such
1141                  *      inode is zero-sized and with SUID+SGID, then means it is
1142                  *      a new created one. Maybe we can remove it and insert the
1143                  *      original one back to the /O/<seq>/d<x>. But there are
1144                  *      something to be considered:
1145                  *
1146                  *      1) The OST-object under /lost+found has crashed LMA.
1147                  *         So it should not conflict with the current one.
1148                  *
1149                  *      2) There are race conditions that: someone may just want
1150                  *         to modify the current one. Even if the OI scrub takes
1151                  *         the object lock when remove the current one, it still
1152                  *         cause the modification to be lost becasue the target
1153                  *         has been removed when the RPC service thread waiting
1154                  *         for the lock.
1155                  *
1156                  *      So keep it there before we have suitable solution. */
1157                 brelse(bh);
1158                 mutex_unlock(&dir->i_mutex);
1159                 mutex_unlock(&src_parent->i_mutex);
1160                 ldiskfs_journal_stop(jh);
1161
1162                 rc = -EEXIST;
1163                 /* If the src object has never been modified, then remove it. */
1164                 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1165                     inode->i_mode & S_ISGID) {
1166                         rc = ll_vfs_unlink(src_parent, src_child);
1167                         if (unlikely(rc == -ENOENT))
1168                                 rc = 0;
1169                 }
1170                 RETURN(rc);
1171         }
1172
1173         bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1174                                     NULL, NULL);
1175         if (unlikely(bh == NULL))
1176                 GOTO(unlock, rc = -ENOENT);
1177
1178         rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1179         brelse(bh);
1180         if (rc != 0)
1181                 GOTO(unlock, rc);
1182
1183         rc = osd_ldiskfs_add_entry(info, jh, tgt_child, inode, NULL);
1184
1185         GOTO(unlock, rc);
1186
1187 unlock:
1188         mutex_unlock(&dir->i_mutex);
1189         mutex_unlock(&src_parent->i_mutex);
1190         ldiskfs_journal_stop(jh);
1191         return rc;
1192 }
1193
1194 static struct dentry *
1195 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1196                      const struct lu_fid *fid, char **name)
1197 {
1198         struct dentry *root = ERR_PTR(-ENOENT);
1199
1200         if (fid_is_last_id(fid)) {
1201                 struct osd_obj_seq *osd_seq;
1202
1203                 /* on creation of LAST_ID we create O/<seq> hierarchy */
1204                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1205                 if (IS_ERR(osd_seq))
1206                         RETURN((struct dentry *)osd_seq);
1207
1208                 *name = "LAST_ID";
1209                 root = osd_seq->oos_root;
1210         } else {
1211                 *name = osd_lf_fid2name(fid);
1212                 if (*name == NULL)
1213                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1214                 else if ((*name)[0])
1215                         root = osd_sb(osd)->s_root;
1216         }
1217
1218         return root;
1219 }
1220
1221 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1222                         const struct lu_fid *fid, const struct osd_inode_id *id,
1223                         handle_t *th)
1224 {
1225         struct dentry   *root;
1226         char            *name = NULL;
1227         int              rc;
1228         ENTRY;
1229
1230         root = osd_object_spec_find(info, osd, fid, &name);
1231         if (!IS_ERR(root)) {
1232                 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1233         } else {
1234                 rc = PTR_ERR(root);
1235                 if (rc == -ENOENT)
1236                         rc = 1;
1237         }
1238
1239         RETURN(rc);
1240 }
1241
1242 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1243                         const struct lu_fid *fid, const struct osd_inode_id *id,
1244                         handle_t *th)
1245 {
1246         struct dentry   *root;
1247         char            *name = NULL;
1248         int              rc;
1249         ENTRY;
1250
1251         root = osd_object_spec_find(info, osd, fid, &name);
1252         if (!IS_ERR(root)) {
1253                 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1254         } else {
1255                 rc = PTR_ERR(root);
1256                 if (rc == -ENOENT)
1257                         rc = 0;
1258         }
1259
1260         RETURN(rc);
1261 }
1262
1263 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1264                         const struct lu_fid *fid, struct osd_inode_id *id)
1265 {
1266         struct dentry   *root;
1267         struct dentry   *dentry;
1268         struct inode    *inode;
1269         char            *name = NULL;
1270         int             rc = -ENOENT;
1271         ENTRY;
1272
1273         if (fid_is_last_id(fid)) {
1274                 struct osd_obj_seq *osd_seq;
1275
1276                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1277                 if (IS_ERR(osd_seq))
1278                         RETURN(PTR_ERR(osd_seq));
1279                 root = osd_seq->oos_root;
1280                 name = "LAST_ID";
1281         } else {
1282                 root = osd_sb(osd)->s_root;
1283                 name = osd_lf_fid2name(fid);
1284                 if (name == NULL || strlen(name) == 0)
1285                         RETURN(-ENOENT);
1286         }
1287
1288         dentry = ll_lookup_one_len(name, root, strlen(name));
1289         if (!IS_ERR(dentry)) {
1290                 inode = dentry->d_inode;
1291                 if (inode) {
1292                         if (is_bad_inode(inode)) {
1293                                 rc = -EIO;
1294                         } else {
1295                                 osd_id_gen(id, inode->i_ino,
1296                                            inode->i_generation);
1297                                 rc = 0;
1298                         }
1299                 }
1300                 /* if dentry is accessible after osd_compat_spec_insert it
1301                  * will still contain NULL inode, so don't keep it in cache */
1302                 d_invalidate(dentry);
1303                 dput(dentry);
1304         }
1305
1306         RETURN(rc);
1307 }