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