Whamcloud - gitweb
LU-7988 hsm: update many cookie status at once
[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 /* prerequisite for linux/xattr.h */
40 #include <linux/types.h>
41 /* prerequisite for linux/xattr.h */
42 #include <linux/fs.h>
43 /* XATTR_{REPLACE,CREATE} */
44 #include <linux/xattr.h>
45
46 /*
47  * struct OBD_{ALLOC,FREE}*()
48  * OBD_FAIL_CHECK
49  */
50 #include <obd_support.h>
51
52 #include "osd_internal.h"
53 #include "osd_oi.h"
54
55 static void osd_push_ctxt(const struct osd_device *dev,
56                           struct lvfs_run_ctxt *newctxt,
57                           struct lvfs_run_ctxt *save)
58 {
59         OBD_SET_CTXT_MAGIC(newctxt);
60         newctxt->pwdmnt = dev->od_mnt;
61         newctxt->pwd = dev->od_mnt->mnt_root;
62         newctxt->fs = get_ds();
63         newctxt->umask = current_umask();
64         newctxt->dt = NULL;
65
66         push_ctxt(save, newctxt);
67 }
68
69 /* utility to make a directory */
70 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
71                                    const char *name, int mode, int fix)
72 {
73         struct dentry *dchild;
74         int err = 0;
75         ENTRY;
76
77         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
78         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
79         dchild = ll_lookup_one_len(name, dir, strlen(name));
80         if (IS_ERR(dchild))
81                 GOTO(out_up, dchild);
82
83         if (dchild->d_inode) {
84                 int old_mode = dchild->d_inode->i_mode;
85                 if (!S_ISDIR(old_mode)) {
86                         CERROR("found %s (%lu/%u) is mode %o\n", name,
87                                dchild->d_inode->i_ino,
88                                dchild->d_inode->i_generation, old_mode);
89                         GOTO(out_err, err = -ENOTDIR);
90                 }
91
92                 /* Fixup directory permissions if necessary */
93                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
94                         CDEBUG(D_CONFIG,
95                                "fixing permissions on %s from %o to %o\n",
96                                name, old_mode, mode);
97                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
98                                                   (old_mode & ~S_IALLUGO);
99                         mark_inode_dirty(dchild->d_inode);
100                 }
101                 GOTO(out_up, dchild);
102         }
103
104         err = vfs_mkdir(dir->d_inode, dchild, mode);
105         if (err)
106                 GOTO(out_err, err);
107
108         RETURN(dchild);
109
110 out_err:
111         dput(dchild);
112         dchild = ERR_PTR(err);
113 out_up:
114         return dchild;
115 }
116
117 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
118 {
119         struct lr_server_data lsd;
120         struct dentry        *dlast;
121         loff_t                off;
122         int                   rc = 0;
123         int                   count = OBJ_SUBDIR_COUNT;
124
125         ENTRY;
126
127         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
128                                   strlen(LAST_RCVD));
129         if (IS_ERR(dlast))
130                 return PTR_ERR(dlast);
131         else if (dlast->d_inode == NULL)
132                 goto out;
133
134         off = 0;
135         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
136         if (rc == sizeof(lsd)) {
137                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
138                        "subdir count = %d\n", lsd.lsd_uuid,
139                        lsd.lsd_subdir_count);
140                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
141                         count = le16_to_cpu(lsd.lsd_subdir_count);
142         } else if (rc != 0) {
143                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
144                 if (rc > 0)
145                         rc = -EFAULT;
146                 dput(dlast);
147                 return rc;
148         }
149 out:
150         dput(dlast);
151         LASSERT(count > 0);
152         return count;
153 }
154
155 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
156 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
157 {
158         struct lvfs_run_ctxt    new;
159         struct lvfs_run_ctxt    save;
160         struct dentry           *parent;
161         struct osd_mdobj_map    *omm;
162         struct dentry           *d;
163         struct osd_thread_info  *info = osd_oti_get(env);
164         struct lu_fid           *fid = &info->oti_fid3;
165         int                     rc = 0;
166         ENTRY;
167
168         OBD_ALLOC_PTR(dev->od_mdt_map);
169         if (dev->od_mdt_map == NULL)
170                 RETURN(-ENOMEM);
171
172         omm = dev->od_mdt_map;
173
174         parent = osd_sb(dev)->s_root;
175         osd_push_ctxt(dev, &new, &save);
176
177         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
178                          0755, 1);
179         if (IS_ERR(d))
180                 GOTO(cleanup, rc = PTR_ERR(d));
181
182         omm->omm_remote_parent = d;
183
184         /* Set LMA for remote parent inode */
185         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
186         rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
187
188         GOTO(cleanup, rc);
189
190 cleanup:
191         pop_ctxt(&save, &new);
192         if (rc) {
193                 if (omm->omm_remote_parent != NULL)
194                         dput(omm->omm_remote_parent);
195                 OBD_FREE_PTR(omm);
196                 dev->od_mdt_map = NULL;
197         }
198         return rc;
199 }
200
201 static void osd_mdt_fini(struct osd_device *osd)
202 {
203         struct osd_mdobj_map *omm = osd->od_mdt_map;
204
205         if (omm == NULL)
206                 return;
207
208         if (omm->omm_remote_parent)
209                 dput(omm->omm_remote_parent);
210
211         OBD_FREE_PTR(omm);
212         osd->od_ost_map = NULL;
213 }
214
215 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
216                              struct osd_object *obj, struct osd_thandle *oh)
217 {
218         struct osd_mdobj_map    *omm = osd->od_mdt_map;
219         struct osd_thread_info  *oti = osd_oti_get(env);
220         struct lustre_mdt_attrs *lma = &oti->oti_ost_attrs.loa_lma;
221         char                    *name = oti->oti_name;
222         struct osd_thread_info  *info = osd_oti_get(env);
223         struct dentry           *dentry;
224         struct dentry           *parent;
225         int                     rc;
226
227         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
228          * can identify this object quickly */
229         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
230                          &oti->oti_ost_attrs);
231         if (rc)
232                 RETURN(rc);
233
234         lma->lma_incompat |= LMAI_REMOTE_PARENT;
235         lustre_lma_swab(lma);
236         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
237                              sizeof(*lma), XATTR_REPLACE);
238         if (rc)
239                 RETURN(rc);
240
241         parent = omm->omm_remote_parent;
242         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
243         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
244                                            name, strlen(name));
245         mutex_lock(&parent->d_inode->i_mutex);
246         rc = osd_ldiskfs_add_entry(info, osd, oh->ot_handle, dentry,
247                                    obj->oo_inode, NULL);
248         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
249                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
250         ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
251         mark_inode_dirty(parent->d_inode);
252         mutex_unlock(&parent->d_inode->i_mutex);
253         RETURN(rc);
254 }
255
256 int osd_delete_from_remote_parent(const struct lu_env *env,
257                                   struct osd_device *osd,
258                                   struct osd_object *obj,
259                                   struct osd_thandle *oh)
260 {
261         struct osd_mdobj_map       *omm = osd->od_mdt_map;
262         struct osd_thread_info     *oti = osd_oti_get(env);
263         struct lustre_mdt_attrs    *lma = &oti->oti_ost_attrs.loa_lma;
264         char                       *name = oti->oti_name;
265         struct dentry              *dentry;
266         struct dentry              *parent;
267         struct ldiskfs_dir_entry_2 *de;
268         struct buffer_head         *bh;
269         int                        rc;
270
271         /* Check lma to see whether it is remote object */
272         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry,
273                          &oti->oti_ost_attrs);
274         if (rc != 0) {
275                 /* No LMA if the directory is created before 2.0 */
276                 if (rc == -ENODATA)
277                         rc = 0;
278                 RETURN(rc);
279         }
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 (IS_ERR(bh)) {
292                 mutex_unlock(&parent->d_inode->i_mutex);
293                 RETURN(PTR_ERR(bh));
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 (IS_ERR(bh)) {
333                 rc = PTR_ERR(bh);
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_ost_attrs.loa_lma;
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 (IS_ERR(bh))
563                 GOTO(out, rc = PTR_ERR(bh));
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, &info->oti_ost_attrs);
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         if (!IS_ERR(bh))
666                 brelse(bh);
667         mutex_unlock(&parent->i_mutex);
668         return rc;
669 }
670
671 static int osd_obj_del_entry(struct osd_thread_info *info,
672                              struct osd_device *osd,
673                              struct dentry *dird, char *name,
674                              handle_t *th)
675 {
676         struct ldiskfs_dir_entry_2 *de;
677         struct buffer_head         *bh;
678         struct dentry              *child;
679         struct inode               *dir = dird->d_inode;
680         int                         rc;
681         ENTRY;
682
683         LASSERT(th != NULL);
684         LASSERT(th->h_transaction != NULL);
685
686
687         child = &info->oti_child_dentry;
688         child->d_name.hash = 0;
689         child->d_name.name = name;
690         child->d_name.len = strlen(name);
691         child->d_parent = dird;
692         child->d_inode = NULL;
693
694         ll_vfs_dq_init(dir);
695         mutex_lock(&dir->i_mutex);
696         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
697         if (IS_ERR(bh)) {
698                 rc = PTR_ERR(bh);
699         } else {
700                 rc = ldiskfs_delete_entry(th, dir, de, bh);
701                 brelse(bh);
702         }
703         mutex_unlock(&dir->i_mutex);
704
705         RETURN(rc);
706 }
707
708 static int osd_obj_add_entry(struct osd_thread_info *info,
709                              struct osd_device *osd,
710                              struct dentry *dir, char *name,
711                              const struct osd_inode_id *id,
712                              handle_t *th)
713 {
714         struct dentry *child;
715         struct inode *inode;
716         int rc;
717
718         ENTRY;
719
720         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
721                 RETURN(0);
722
723         LASSERT(th != NULL);
724         LASSERT(th->h_transaction != NULL);
725
726         inode = info->oti_inode;
727         if (unlikely(inode == NULL)) {
728                 struct ldiskfs_inode_info *lii;
729                 OBD_ALLOC_PTR(lii);
730                 if (lii == NULL)
731                         RETURN(-ENOMEM);
732                 inode = info->oti_inode = &lii->vfs_inode;
733         }
734
735         inode->i_sb = osd_sb(osd);
736         osd_id_to_inode(inode, id);
737         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
738
739         child = &info->oti_child_dentry;
740         child->d_name.hash = 0;
741         child->d_name.name = name;
742         child->d_name.len = strlen(name);
743         child->d_parent = dir;
744         child->d_inode = inode;
745
746         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
747                 inode->i_ino++;
748
749         ll_vfs_dq_init(dir->d_inode);
750         mutex_lock(&dir->d_inode->i_mutex);
751         rc = osd_ldiskfs_add_entry(info, osd, th, child, inode, NULL);
752         mutex_unlock(&dir->d_inode->i_mutex);
753
754         RETURN(rc);
755 }
756
757 /**
758  * Use %llu for legacy OST sequences, but use %llx for new
759  * sequences names, so that the O/{seq}/dN/{oid} more closely
760  * follows the DFID/PFID format. This makes it easier to map from
761  * debug messages to objects in the future, and the legacy space
762  * of FID_SEQ_OST_MDT0 will be unused in the future.
763  **/
764 static inline void osd_seq_name(char *seq_name, size_t name_size, u64 seq)
765 {
766         snprintf(seq_name, name_size,
767                  (fid_seq_is_rsvd(seq) ||
768                   fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
769                  fid_seq_is_idif(seq) ? 0 : seq);
770 }
771
772 static inline void osd_oid_name(char *name, size_t name_size,
773                                 const struct lu_fid *fid, u64 id)
774 {
775         snprintf(name, name_size,
776                  (fid_seq_is_rsvd(fid_seq(fid)) ||
777                   fid_seq_is_mdt0(fid_seq(fid)) ||
778                   fid_seq_is_idif(fid_seq(fid))) ? "%llu" : "%llx", id);
779 }
780
781 /* external locking is required */
782 static int osd_seq_load_locked(struct osd_thread_info *info,
783                                struct osd_device *osd,
784                                struct osd_obj_seq *osd_seq)
785 {
786         struct osd_obj_map  *map = osd->od_ost_map;
787         struct dentry       *seq_dir;
788         struct inode        *inode;
789         struct lu_fid       *fid = &info->oti_fid3;
790         int                 rc = 0;
791         int                 i;
792         char                dir_name[32];
793         ENTRY;
794
795         if (osd_seq->oos_root != NULL)
796                 RETURN(0);
797
798         LASSERT(map);
799         LASSERT(map->om_root);
800
801         osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
802
803         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
804         if (IS_ERR(seq_dir))
805                 GOTO(out_err, rc = PTR_ERR(seq_dir));
806         else if (seq_dir->d_inode == NULL)
807                 GOTO(out_put, rc = -EFAULT);
808
809         inode = seq_dir->d_inode;
810         osd_seq->oos_root = seq_dir;
811
812         /* 'What the @fid is' is not imporatant, because the object
813          * has no OI mapping, and only is visible inside the OSD.*/
814         lu_igif_build(fid, inode->i_ino, inode->i_generation);
815         rc = osd_ea_fid_set(info, inode, fid,
816                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
817         if (rc != 0)
818                 GOTO(out_put, rc);
819
820         LASSERT(osd_seq->oos_dirs == NULL);
821         OBD_ALLOC(osd_seq->oos_dirs,
822                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
823         if (osd_seq->oos_dirs == NULL)
824                 GOTO(out_put, rc = -ENOMEM);
825
826         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
827                 struct dentry   *dir;
828
829                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
830                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
831                                    0700, 1);
832                 if (IS_ERR(dir)) {
833                         GOTO(out_free, rc = PTR_ERR(dir));
834                 } else if (dir->d_inode == NULL) {
835                         dput(dir);
836                         GOTO(out_free, rc = -EFAULT);
837                 }
838
839                 inode = dir->d_inode;
840                 osd_seq->oos_dirs[i] = dir;
841
842                 /* 'What the @fid is' is not imporatant, because the object
843                  * has no OI mapping, and only is visible inside the OSD.*/
844                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
845                 rc = osd_ea_fid_set(info, inode, fid,
846                                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
847                 if (rc != 0)
848                         GOTO(out_free, rc);
849         }
850
851         if (rc != 0) {
852 out_free:
853                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
854                         if (osd_seq->oos_dirs[i] != NULL)
855                                 dput(osd_seq->oos_dirs[i]);
856                 }
857                 OBD_FREE(osd_seq->oos_dirs,
858                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
859 out_put:
860                 dput(seq_dir);
861                 osd_seq->oos_root = NULL;
862         }
863 out_err:
864         RETURN(rc);
865 }
866
867 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
868                                         struct osd_device *osd, u64 seq)
869 {
870         struct osd_obj_map      *map;
871         struct osd_obj_seq      *osd_seq;
872         int                     rc = 0;
873         ENTRY;
874
875         map = osd->od_ost_map;
876         LASSERT(map);
877         LASSERT(map->om_root);
878
879         osd_seq = osd_seq_find(map, seq);
880         if (likely(osd_seq != NULL))
881                 RETURN(osd_seq);
882
883         /* Serializing init process */
884         mutex_lock(&map->om_dir_init_mutex);
885
886         /* Check whether the seq has been added */
887         read_lock(&map->om_seq_list_lock);
888         osd_seq = osd_seq_find_locked(map, seq);
889         if (osd_seq != NULL) {
890                 read_unlock(&map->om_seq_list_lock);
891                 GOTO(cleanup, rc = 0);
892         }
893         read_unlock(&map->om_seq_list_lock);
894
895         OBD_ALLOC_PTR(osd_seq);
896         if (osd_seq == NULL)
897                 GOTO(cleanup, rc = -ENOMEM);
898
899         INIT_LIST_HEAD(&osd_seq->oos_seq_list);
900         osd_seq->oos_seq = seq;
901         /* Init subdir count to be 32, but each seq can have
902          * different subdir count */
903         osd_seq->oos_subdir_count = map->om_subdir_count;
904         rc = osd_seq_load_locked(info, osd, osd_seq);
905         if (rc != 0)
906                 GOTO(cleanup, rc);
907
908         write_lock(&map->om_seq_list_lock);
909         list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
910         write_unlock(&map->om_seq_list_lock);
911
912 cleanup:
913         mutex_unlock(&map->om_dir_init_mutex);
914         if (rc != 0) {
915                 if (osd_seq != NULL)
916                         OBD_FREE_PTR(osd_seq);
917                 RETURN(ERR_PTR(rc));
918         }
919
920         RETURN(osd_seq);
921 }
922
923 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
924                        const struct lu_fid *fid, struct osd_inode_id *id)
925 {
926         struct osd_obj_map              *map;
927         struct osd_obj_seq              *osd_seq;
928         struct dentry                   *d_seq;
929         struct dentry                   *child;
930         struct ost_id                   *ostid = &info->oti_ostid;
931         int                             dirn;
932         char                            name[32];
933         struct ldiskfs_dir_entry_2      *de;
934         struct buffer_head              *bh;
935         struct inode                    *dir;
936         struct inode                    *inode;
937         ENTRY;
938
939         /* on the very first lookup we find and open directories */
940
941         map = dev->od_ost_map;
942         LASSERT(map);
943         LASSERT(map->om_root);
944
945         fid_to_ostid(fid, ostid);
946         osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
947         if (IS_ERR(osd_seq))
948                 RETURN(PTR_ERR(osd_seq));
949
950         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
951         d_seq = osd_seq->oos_dirs[dirn];
952         LASSERT(d_seq);
953
954         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
955
956         child = &info->oti_child_dentry;
957         child->d_parent = d_seq;
958         child->d_name.hash = 0;
959         child->d_name.name = name;
960         /* XXX: we can use rc from sprintf() instead of strlen() */
961         child->d_name.len = strlen(name);
962
963         dir = d_seq->d_inode;
964         mutex_lock(&dir->i_mutex);
965         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
966         mutex_unlock(&dir->i_mutex);
967
968         if (IS_ERR(bh))
969                 RETURN(PTR_ERR(bh));
970
971         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
972         brelse(bh);
973
974         inode = osd_iget(info, dev, id);
975         if (IS_ERR(inode)) {
976                 int rc = PTR_ERR(inode);
977
978                 RETURN(rc == -ENOENT ? -ESTALE : rc);
979         }
980
981         iput(inode);
982         RETURN(0);
983 }
984
985 int osd_obj_map_insert(struct osd_thread_info *info,
986                        struct osd_device *osd,
987                        const struct lu_fid *fid,
988                        const struct osd_inode_id *id,
989                        handle_t *th)
990 {
991         struct osd_obj_map      *map;
992         struct osd_obj_seq      *osd_seq;
993         struct dentry           *d;
994         struct ost_id           *ostid = &info->oti_ostid;
995         u64                      oid;
996         int                     dirn, rc = 0;
997         char                    name[32];
998         ENTRY;
999
1000         map = osd->od_ost_map;
1001         LASSERT(map);
1002
1003         /* map fid to seq:objid */
1004         fid_to_ostid(fid, ostid);
1005
1006         oid = ostid_id(ostid);
1007         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1008         if (IS_ERR(osd_seq))
1009                 RETURN(PTR_ERR(osd_seq));
1010
1011         dirn = oid & (osd_seq->oos_subdir_count - 1);
1012         d = osd_seq->oos_dirs[dirn];
1013         LASSERT(d);
1014
1015         osd_oid_name(name, sizeof(name), fid, oid);
1016
1017 again:
1018         rc = osd_obj_add_entry(info, osd, d, name, id, th);
1019         if (rc == -EEXIST) {
1020                 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1021                 if (unlikely(rc == -ENOENT))
1022                         goto again;
1023
1024                 if (unlikely(rc == 1))
1025                         RETURN(0);
1026         }
1027
1028         RETURN(rc);
1029 }
1030
1031 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
1032                        const struct lu_fid *fid, handle_t *th)
1033 {
1034         struct osd_obj_map      *map;
1035         struct osd_obj_seq      *osd_seq;
1036         struct dentry           *d;
1037         struct ost_id           *ostid = &info->oti_ostid;
1038         int                     dirn, rc = 0;
1039         char                    name[32];
1040         ENTRY;
1041
1042         map = osd->od_ost_map;
1043         LASSERT(map);
1044
1045         /* map fid to seq:objid */
1046         fid_to_ostid(fid, ostid);
1047
1048         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1049         if (IS_ERR(osd_seq))
1050                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
1051
1052         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1053         d = osd_seq->oos_dirs[dirn];
1054         LASSERT(d);
1055
1056         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1057         rc = osd_obj_del_entry(info, osd, d, name, th);
1058 cleanup:
1059         RETURN(rc);
1060 }
1061
1062 int osd_obj_map_update(struct osd_thread_info *info,
1063                        struct osd_device *osd,
1064                        const struct lu_fid *fid,
1065                        const struct osd_inode_id *id,
1066                        handle_t *th)
1067 {
1068         struct osd_obj_seq      *osd_seq;
1069         struct dentry           *d;
1070         struct ost_id           *ostid = &info->oti_ostid;
1071         int                     dirn, rc = 0;
1072         char                    name[32];
1073         ENTRY;
1074
1075         fid_to_ostid(fid, ostid);
1076         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1077         if (IS_ERR(osd_seq))
1078                 RETURN(PTR_ERR(osd_seq));
1079
1080         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1081         d = osd_seq->oos_dirs[dirn];
1082         LASSERT(d);
1083
1084         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1085         rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1086
1087         RETURN(rc);
1088 }
1089
1090 int osd_obj_map_recover(struct osd_thread_info *info,
1091                         struct osd_device *osd,
1092                         struct inode *src_parent,
1093                         struct dentry *src_child,
1094                         const struct lu_fid *fid)
1095 {
1096         struct osd_obj_seq         *osd_seq;
1097         struct dentry              *tgt_parent;
1098         struct dentry              *tgt_child = &info->oti_child_dentry;
1099         struct inode               *dir;
1100         struct inode               *inode     = src_child->d_inode;
1101         struct ost_id              *ostid     = &info->oti_ostid;
1102         handle_t                   *jh;
1103         struct ldiskfs_dir_entry_2 *de;
1104         struct buffer_head         *bh;
1105         char                        name[32];
1106         int                         dirn;
1107         int                         rc        = 0;
1108         ENTRY;
1109
1110         if (fid_is_last_id(fid)) {
1111                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1112                 if (IS_ERR(osd_seq))
1113                         RETURN(PTR_ERR(osd_seq));
1114
1115                 tgt_parent = osd_seq->oos_root;
1116                 tgt_child->d_name.name = "LAST_ID";
1117                 tgt_child->d_name.len = strlen("LAST_ID");
1118         } else {
1119                 fid_to_ostid(fid, ostid);
1120                 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1121                 if (IS_ERR(osd_seq))
1122                         RETURN(PTR_ERR(osd_seq));
1123
1124                 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1125                 tgt_parent = osd_seq->oos_dirs[dirn];
1126                 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1127                 tgt_child->d_name.name = name;
1128                 tgt_child->d_name.len = strlen(name);
1129         }
1130         LASSERT(tgt_parent != NULL);
1131
1132         dir = tgt_parent->d_inode;
1133         tgt_child->d_name.hash = 0;
1134         tgt_child->d_parent = tgt_parent;
1135         tgt_child->d_inode = inode;
1136
1137         /* The non-initialized src_child may be destroyed. */
1138         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1139                                 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1140                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1141                                 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1142         if (IS_ERR(jh))
1143                 RETURN(PTR_ERR(jh));
1144
1145         ll_vfs_dq_init(src_parent);
1146         ll_vfs_dq_init(dir);
1147
1148         mutex_lock(&src_parent->i_mutex);
1149         mutex_lock(&dir->i_mutex);
1150         bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1151         if (!IS_ERR(bh)) {
1152                 /* XXX: If some other object occupied the same slot. And If such
1153                  *      inode is zero-sized and with SUID+SGID, then means it is
1154                  *      a new created one. Maybe we can remove it and insert the
1155                  *      original one back to the /O/<seq>/d<x>. But there are
1156                  *      something to be considered:
1157                  *
1158                  *      1) The OST-object under /lost+found has crashed LMA.
1159                  *         So it should not conflict with the current one.
1160                  *
1161                  *      2) There are race conditions that: someone may just want
1162                  *         to modify the current one. Even if the OI scrub takes
1163                  *         the object lock when remove the current one, it still
1164                  *         cause the modification to be lost becasue the target
1165                  *         has been removed when the RPC service thread waiting
1166                  *         for the lock.
1167                  *
1168                  *      So keep it there before we have suitable solution. */
1169                 brelse(bh);
1170                 mutex_unlock(&dir->i_mutex);
1171                 mutex_unlock(&src_parent->i_mutex);
1172                 ldiskfs_journal_stop(jh);
1173
1174                 rc = -EEXIST;
1175                 /* If the src object has never been modified, then remove it. */
1176                 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1177                     inode->i_mode & S_ISGID) {
1178                         rc = ll_vfs_unlink(src_parent, src_child);
1179                         if (unlikely(rc == -ENOENT))
1180                                 rc = 0;
1181                 }
1182                 RETURN(rc);
1183         }
1184
1185         bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1186                                     NULL, NULL);
1187         if (unlikely(IS_ERR(bh)))
1188                 GOTO(unlock, rc = PTR_ERR(bh));
1189
1190         rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1191         brelse(bh);
1192         if (rc != 0)
1193                 GOTO(unlock, rc);
1194
1195         rc = osd_ldiskfs_add_entry(info, osd, jh, tgt_child, inode, NULL);
1196
1197         GOTO(unlock, rc);
1198
1199 unlock:
1200         mutex_unlock(&dir->i_mutex);
1201         mutex_unlock(&src_parent->i_mutex);
1202         ldiskfs_journal_stop(jh);
1203         return rc;
1204 }
1205
1206 static struct dentry *
1207 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1208                      const struct lu_fid *fid, char **name)
1209 {
1210         struct dentry *root = ERR_PTR(-ENOENT);
1211
1212         if (fid_is_last_id(fid)) {
1213                 struct osd_obj_seq *osd_seq;
1214
1215                 /* on creation of LAST_ID we create O/<seq> hierarchy */
1216                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1217                 if (IS_ERR(osd_seq))
1218                         RETURN((struct dentry *)osd_seq);
1219
1220                 *name = "LAST_ID";
1221                 root = osd_seq->oos_root;
1222         } else {
1223                 *name = osd_lf_fid2name(fid);
1224                 if (*name == NULL)
1225                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1226                 else if ((*name)[0])
1227                         root = osd_sb(osd)->s_root;
1228         }
1229
1230         return root;
1231 }
1232
1233 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1234                         const struct lu_fid *fid, const struct osd_inode_id *id,
1235                         handle_t *th)
1236 {
1237         struct dentry   *root;
1238         char            *name = NULL;
1239         int              rc;
1240         ENTRY;
1241
1242         root = osd_object_spec_find(info, osd, fid, &name);
1243         if (!IS_ERR(root)) {
1244                 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1245         } else {
1246                 rc = PTR_ERR(root);
1247                 if (rc == -ENOENT)
1248                         rc = 1;
1249         }
1250
1251         RETURN(rc);
1252 }
1253
1254 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1255                         const struct lu_fid *fid, const struct osd_inode_id *id,
1256                         handle_t *th)
1257 {
1258         struct dentry   *root;
1259         char            *name = NULL;
1260         int              rc;
1261         ENTRY;
1262
1263         root = osd_object_spec_find(info, osd, fid, &name);
1264         if (!IS_ERR(root)) {
1265                 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1266         } else {
1267                 rc = PTR_ERR(root);
1268                 if (rc == -ENOENT)
1269                         rc = 0;
1270         }
1271
1272         RETURN(rc);
1273 }
1274
1275 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1276                         const struct lu_fid *fid, struct osd_inode_id *id)
1277 {
1278         struct dentry   *root;
1279         struct dentry   *dentry;
1280         struct inode    *inode;
1281         char            *name = NULL;
1282         int             rc = -ENOENT;
1283         ENTRY;
1284
1285         if (fid_is_last_id(fid)) {
1286                 struct osd_obj_seq *osd_seq;
1287
1288                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1289                 if (IS_ERR(osd_seq))
1290                         RETURN(PTR_ERR(osd_seq));
1291                 root = osd_seq->oos_root;
1292                 name = "LAST_ID";
1293         } else {
1294                 root = osd_sb(osd)->s_root;
1295                 name = osd_lf_fid2name(fid);
1296                 if (name == NULL || strlen(name) == 0)
1297                         RETURN(-ENOENT);
1298         }
1299
1300         dentry = ll_lookup_one_len(name, root, strlen(name));
1301         if (!IS_ERR(dentry)) {
1302                 inode = dentry->d_inode;
1303                 if (inode) {
1304                         if (is_bad_inode(inode)) {
1305                                 rc = -EIO;
1306                         } else {
1307                                 osd_id_gen(id, inode->i_ino,
1308                                            inode->i_generation);
1309                                 rc = 0;
1310                         }
1311                 }
1312                 /* if dentry is accessible after osd_compat_spec_insert it
1313                  * will still contain NULL inode, so don't keep it in cache */
1314                 d_invalidate(dentry);
1315                 dput(dentry);
1316         }
1317
1318         RETURN(rc);
1319 }