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