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