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