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