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