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