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