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