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