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