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