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