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