Whamcloud - gitweb
LU-3335 scrub: recover OST-objects from /lost+found
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_compat.c
37  *
38  * on-disk structure for managing /O
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  */
42
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/fs.h>
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
51
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  * OBD_FAIL_CHECK
55  */
56 #include <obd_support.h>
57
58 #include "osd_internal.h"
59 #include "osd_oi.h"
60
61 static void osd_push_ctxt(const struct osd_device *dev,
62                           struct lvfs_run_ctxt *newctxt,
63                           struct lvfs_run_ctxt *save)
64 {
65         OBD_SET_CTXT_MAGIC(newctxt);
66         newctxt->pwdmnt = dev->od_mnt;
67         newctxt->pwd = dev->od_mnt->mnt_root;
68         newctxt->fs = get_ds();
69
70         push_ctxt(save, newctxt, NULL);
71 }
72
73 static void osd_pop_ctxt(const struct osd_device *dev,
74                          struct lvfs_run_ctxt *new,
75                          struct lvfs_run_ctxt *save)
76 {
77         pop_ctxt(save, new, NULL);
78 }
79
80 /* utility to make a directory */
81 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
82                                    const char *name, int mode, int fix)
83 {
84         struct dentry *dchild;
85         int err = 0;
86         ENTRY;
87
88         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
89         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
90         dchild = ll_lookup_one_len(name, dir, strlen(name));
91         if (IS_ERR(dchild))
92                 GOTO(out_up, dchild);
93
94         if (dchild->d_inode) {
95                 int old_mode = dchild->d_inode->i_mode;
96                 if (!S_ISDIR(old_mode)) {
97                         CERROR("found %s (%lu/%u) is mode %o\n", name,
98                                dchild->d_inode->i_ino,
99                                dchild->d_inode->i_generation, old_mode);
100                         GOTO(out_err, err = -ENOTDIR);
101                 }
102
103                 /* Fixup directory permissions if necessary */
104                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
105                         CDEBUG(D_CONFIG,
106                                "fixing permissions on %s from %o to %o\n",
107                                name, old_mode, mode);
108                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
109                                                   (old_mode & ~S_IALLUGO);
110                         mark_inode_dirty(dchild->d_inode);
111                 }
112                 GOTO(out_up, dchild);
113         }
114
115         err = ll_vfs_mkdir(dir->d_inode, dchild, mnt, mode);
116         if (err)
117                 GOTO(out_err, err);
118
119         RETURN(dchild);
120
121 out_err:
122         dput(dchild);
123         dchild = ERR_PTR(err);
124 out_up:
125         return dchild;
126 }
127
128 int osd_last_rcvd_subdir_count(struct osd_device *osd)
129 {
130         struct lr_server_data lsd;
131         struct dentry        *dlast;
132         loff_t                off;
133         int                   rc = 0;
134         int                   count = FILTER_SUBDIR_COUNT;
135
136         ENTRY;
137
138         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
139                                   strlen(LAST_RCVD));
140         if (IS_ERR(dlast))
141                 return PTR_ERR(dlast);
142         else if (dlast->d_inode == NULL)
143                 goto out;
144
145         off = 0;
146         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
147         if (rc == sizeof(lsd)) {
148                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
149                        "subdir count = %d\n", lsd.lsd_uuid,
150                        lsd.lsd_subdir_count);
151                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
152                         count = le16_to_cpu(lsd.lsd_subdir_count);
153         } else if (rc != 0) {
154                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
155                 if (rc > 0)
156                         rc = -EFAULT;
157                 dput(dlast);
158                 return rc;
159         }
160 out:
161         dput(dlast);
162         LASSERT(count > 0);
163         return count;
164 }
165
166 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
167 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
168 {
169         struct lvfs_run_ctxt    new;
170         struct lvfs_run_ctxt    save;
171         struct dentry           *parent;
172         struct osd_mdobj_map    *omm;
173         struct dentry           *d;
174         struct osd_thread_info  *info = osd_oti_get(env);
175         struct lu_fid           *fid = &info->oti_fid3;
176         int                     rc = 0;
177         ENTRY;
178
179         OBD_ALLOC_PTR(dev->od_mdt_map);
180         if (dev->od_mdt_map == NULL)
181                 RETURN(-ENOMEM);
182
183         omm = dev->od_mdt_map;
184
185         LASSERT(dev->od_fsops);
186
187         parent = osd_sb(dev)->s_root;
188         osd_push_ctxt(dev, &new, &save);
189
190         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
191                          0755, 1);
192         if (IS_ERR(d))
193                 GOTO(cleanup, rc = PTR_ERR(d));
194
195         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
196         omm->omm_remote_parent = d;
197
198         /* Set LMA for remote parent inode */
199         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
200         rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
201
202         GOTO(cleanup, rc);
203
204 cleanup:
205         pop_ctxt(&save, &new, NULL);
206         if (rc) {
207                 if (omm->omm_remote_parent != NULL)
208                         dput(omm->omm_remote_parent);
209                 OBD_FREE_PTR(omm);
210                 dev->od_mdt_map = NULL;
211         }
212         return rc;
213 }
214
215 static void osd_mdt_fini(struct osd_device *osd)
216 {
217         struct osd_mdobj_map *omm = osd->od_mdt_map;
218
219         if (omm == NULL)
220                 return;
221
222         if (omm->omm_remote_parent)
223                 dput(omm->omm_remote_parent);
224
225         OBD_FREE_PTR(omm);
226         osd->od_ost_map = NULL;
227 }
228
229 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
230                              struct osd_object *obj, struct osd_thandle *oh)
231 {
232         struct osd_mdobj_map    *omm = osd->od_mdt_map;
233         struct osd_thread_info  *oti = osd_oti_get(env);
234         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
235         char                    *name = oti->oti_name;
236         struct dentry           *dentry;
237         struct dentry           *parent;
238         int                     rc;
239
240         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
241          * can identify this object quickly */
242         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
243         if (rc != 0)
244                 RETURN(rc);
245
246         lma->lma_incompat |= LMAI_REMOTE_PARENT;
247         lustre_lma_swab(lma);
248         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
249                              sizeof(*lma), XATTR_REPLACE);
250         if (rc != 0)
251                 RETURN(rc);
252
253         parent = omm->omm_remote_parent;
254         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
255         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
256                                            name, strlen(name));
257         mutex_lock(&parent->d_inode->i_mutex);
258         rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
259                                    NULL);
260         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
261                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
262         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
263                  osd_name(osd), parent->d_inode->i_ino,
264                  parent->d_inode->i_nlink);
265         parent->d_inode->i_nlink++;
266         mark_inode_dirty(parent->d_inode);
267         mutex_unlock(&parent->d_inode->i_mutex);
268         RETURN(rc);
269 }
270
271 int osd_delete_from_remote_parent(const struct lu_env *env,
272                                   struct osd_device *osd,
273                                   struct osd_object *obj,
274                                   struct osd_thandle *oh)
275 {
276         struct osd_mdobj_map       *omm = osd->od_mdt_map;
277         struct osd_thread_info     *oti = osd_oti_get(env);
278         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
279         char                       *name = oti->oti_name;
280         struct dentry              *dentry;
281         struct dentry              *parent;
282         struct ldiskfs_dir_entry_2 *de;
283         struct buffer_head         *bh;
284         int                        rc;
285
286         /* Check lma to see whether it is remote object */
287         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
288         if (rc != 0)
289                 RETURN(rc);
290
291         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
292                 RETURN(0);
293
294         parent = omm->omm_remote_parent;
295         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
296         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
297                                            name, strlen(name));
298         mutex_lock(&parent->d_inode->i_mutex);
299         bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
300         if (bh == NULL) {
301                 mutex_unlock(&parent->d_inode->i_mutex);
302                 RETURN(-ENOENT);
303         }
304         CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
305                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
306         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
307         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
308                  osd_name(osd), parent->d_inode->i_ino,
309                  parent->d_inode->i_nlink);
310         parent->d_inode->i_nlink--;
311         mark_inode_dirty(parent->d_inode);
312         mutex_unlock(&parent->d_inode->i_mutex);
313         brelse(bh);
314
315         /* Get rid of REMOTE_PARENT flag from incompat */
316         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
317         lustre_lma_swab(lma);
318         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
319                              sizeof(*lma), XATTR_REPLACE);
320         RETURN(rc);
321 }
322
323 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
324                                 struct osd_device *osd,
325                                 const struct lu_fid *fid,
326                                 struct osd_inode_id *id)
327 {
328         struct osd_mdobj_map        *omm = osd->od_mdt_map;
329         char                        *name = oti->oti_name;
330         struct dentry               *parent;
331         struct dentry               *dentry;
332         struct ldiskfs_dir_entry_2 *de;
333         struct buffer_head         *bh;
334         int                         rc;
335         ENTRY;
336
337         parent = omm->omm_remote_parent;
338         sprintf(name, DFID_NOBRACE, PFID(fid));
339         dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
340                                            name, strlen(name));
341         mutex_lock(&parent->d_inode->i_mutex);
342         bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
343         if (bh == NULL) {
344                 rc = -ENOENT;
345         } else {
346                 rc = 0;
347                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
348                 brelse(bh);
349         }
350         mutex_unlock(&parent->d_inode->i_mutex);
351         if (rc == 0)
352                 osd_add_oi_cache(oti, osd, id, fid);
353         RETURN(rc);
354 }
355
356 /*
357  * directory structure on legacy OST:
358  *
359  * O/<seq>/d0-31/<objid>
360  * O/<seq>/LAST_ID
361  * last_rcvd
362  * LAST_GROUP
363  * CONFIGS
364  *
365  */
366 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
367 {
368         struct lvfs_run_ctxt     new;
369         struct lvfs_run_ctxt     save;
370         struct dentry           *rootd = osd_sb(dev)->s_root;
371         struct dentry           *d;
372         struct osd_thread_info  *info = osd_oti_get(env);
373         struct inode            *inode;
374         struct lu_fid           *fid = &info->oti_fid3;
375         int                      rc;
376         ENTRY;
377
378         OBD_ALLOC_PTR(dev->od_ost_map);
379         if (dev->od_ost_map == NULL)
380                 RETURN(-ENOMEM);
381
382         /* to get subdir count from last_rcvd */
383         rc = osd_last_rcvd_subdir_count(dev);
384         if (rc < 0) {
385                 OBD_FREE_PTR(dev->od_ost_map);
386                 RETURN(rc);
387         }
388
389         dev->od_ost_map->om_subdir_count = rc;
390         rc = 0;
391
392         CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
393         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
394         sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
395
396         LASSERT(dev->od_fsops);
397         osd_push_ctxt(dev, &new, &save);
398
399         d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
400         if (IS_ERR(d))
401                 GOTO(cleanup, rc = PTR_ERR(d));
402
403         inode = d->d_inode;
404         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
405         dev->od_ost_map->om_root = d;
406
407         /* 'What the @fid is' is not imporatant, because the object
408          * has no OI mapping, and only is visible inside the OSD.*/
409         lu_igif_build(fid, inode->i_ino, inode->i_generation);
410         rc = osd_ea_fid_set(info, inode, fid,
411                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
412
413         GOTO(cleanup, rc);
414
415 cleanup:
416         osd_pop_ctxt(dev, &new, &save);
417         if (IS_ERR(d)) {
418                 OBD_FREE_PTR(dev->od_ost_map);
419                 RETURN(PTR_ERR(d));
420         }
421         return rc;
422 }
423
424 static void osd_seq_free(struct osd_obj_map *map,
425                          struct osd_obj_seq *osd_seq)
426 {
427         int j;
428
429         cfs_list_del_init(&osd_seq->oos_seq_list);
430
431         if (osd_seq->oos_dirs) {
432                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
433                         if (osd_seq->oos_dirs[j])
434                                 dput(osd_seq->oos_dirs[j]);
435                 }
436                 OBD_FREE(osd_seq->oos_dirs,
437                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
438         }
439
440         if (osd_seq->oos_root)
441                 dput(osd_seq->oos_root);
442
443         OBD_FREE_PTR(osd_seq);
444 }
445
446 static void osd_ost_fini(struct osd_device *osd)
447 {
448         struct osd_obj_seq    *osd_seq;
449         struct osd_obj_seq    *tmp;
450         struct osd_obj_map    *map = osd->od_ost_map;
451         ENTRY;
452
453         if (map == NULL)
454                 return;
455
456         write_lock(&map->om_seq_list_lock);
457         cfs_list_for_each_entry_safe(osd_seq, tmp,
458                                      &map->om_seq_list,
459                                      oos_seq_list) {
460                 osd_seq_free(map, osd_seq);
461         }
462         write_unlock(&map->om_seq_list_lock);
463         if (map->om_root)
464                 dput(map->om_root);
465         OBD_FREE_PTR(map);
466         osd->od_ost_map = NULL;
467         EXIT;
468 }
469
470 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
471 {
472         int rc;
473         ENTRY;
474
475         /* prepare structures for OST */
476         rc = osd_ost_init(env, dev);
477         if (rc)
478                 RETURN(rc);
479
480         /* prepare structures for MDS */
481         rc = osd_mdt_init(env, dev);
482
483         RETURN(rc);
484 }
485
486 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
487 {
488         struct osd_obj_seq *osd_seq;
489
490         cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
491                 if (osd_seq->oos_seq == seq)
492                         return osd_seq;
493         }
494         return NULL;
495 }
496
497 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
498 {
499         struct osd_obj_seq *osd_seq;
500
501         read_lock(&map->om_seq_list_lock);
502         osd_seq = osd_seq_find_locked(map, seq);
503         read_unlock(&map->om_seq_list_lock);
504         return osd_seq;
505 }
506
507 void osd_obj_map_fini(struct osd_device *dev)
508 {
509         osd_ost_fini(dev);
510         osd_mdt_fini(dev);
511 }
512
513 /**
514  * Update the specified OI mapping.
515  *
516  * \retval   1, changed nothing
517  * \retval   0, changed successfully
518  * \retval -ve, on error
519  */
520 static int osd_obj_update_entry(struct osd_thread_info *info,
521                                 struct osd_device *osd,
522                                 struct dentry *dir, const char *name,
523                                 const struct lu_fid *fid,
524                                 const struct osd_inode_id *id,
525                                 struct thandle *th)
526 {
527         struct inode               *parent = dir->d_inode;
528         struct osd_thandle         *oh;
529         struct dentry              *child;
530         struct ldiskfs_dir_entry_2 *de;
531         struct buffer_head         *bh;
532         struct inode               *inode;
533         struct dentry              *dentry = &info->oti_obj_dentry;
534         struct osd_inode_id        *oi_id  = &info->oti_id3;
535         struct lustre_mdt_attrs    *lma    = &info->oti_mdt_attrs;
536         struct lu_fid              *oi_fid = &lma->lma_self_fid;
537         int                         rc;
538         ENTRY;
539
540         oh = container_of(th, struct osd_thandle, ot_super);
541         LASSERT(oh->ot_handle != NULL);
542         LASSERT(oh->ot_handle->h_transaction != NULL);
543
544         child = &info->oti_child_dentry;
545         child->d_parent = dir;
546         child->d_name.hash = 0;
547         child->d_name.name = name;
548         child->d_name.len = strlen(name);
549
550         ll_vfs_dq_init(parent);
551         mutex_lock(&parent->i_mutex);
552         bh = osd_ldiskfs_find_entry(parent, child, &de, NULL);
553         if (bh == NULL)
554                 GOTO(out, rc = -ENOENT);
555
556         if (le32_to_cpu(de->inode) == id->oii_ino)
557                 GOTO(out, rc = 1);
558
559         osd_id_gen(oi_id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
560         inode = osd_iget(info, osd, oi_id);
561         if (IS_ERR(inode)) {
562                 rc = PTR_ERR(inode);
563                 if (rc == -ENOENT || rc == -ESTALE)
564                         goto update;
565                 GOTO(out, rc);
566         }
567
568         rc = osd_get_lma(info, inode, dentry, lma);
569         if (rc == -ENODATA) {
570                 rc = osd_get_idif(info, inode, dentry, oi_fid);
571                 if (rc > 0) {
572                         oi_fid = NULL;
573                         rc = 0;
574                 }
575         }
576         iput(inode);
577
578         /* If the OST-object has neither FID-in-LMA nor FID-in-ff, it is
579          * either a crashed object or a uninitialized one. Replace it. */
580         if (rc == -ENODATA || oi_fid == NULL)
581                 goto update;
582
583         if (rc != 0)
584                 GOTO(out, rc);
585
586         if (lu_fid_eq(fid, oi_fid)) {
587                 CERROR("%s: the FID "DFID" is used by two objects: "
588                        "%u/%u %u/%u\n", osd_name(osd), PFID(fid),
589                        oi_id->oii_ino, oi_id->oii_gen,
590                        id->oii_ino, id->oii_gen);
591                 GOTO(out, rc = -EEXIST);
592         }
593
594 update:
595         /* There may be temporary inconsistency: On one hand, the new
596          * object may be referenced by multiple entries, which is out
597          * of our control unless we traverse the whole /O completely,
598          * which is non-flat order and inefficient, should be avoided;
599          * On the other hand, the old object may become orphan if it
600          * is still valid. Since it was referenced by an invalid entry,
601          * making it as invisible temporary may be not worse. OI scrub
602          * will process it later. */
603         rc = ldiskfs_journal_get_write_access(oh->ot_handle, bh);
604         if (rc != 0)
605                 GOTO(out, rc);
606
607         de->inode = cpu_to_le32(id->oii_ino);
608         rc = ldiskfs_journal_dirty_metadata(oh->ot_handle, bh);
609
610         GOTO(out, rc);
611
612 out:
613         brelse(bh);
614         mutex_unlock(&parent->i_mutex);
615         return rc;
616 }
617
618 static int osd_obj_del_entry(struct osd_thread_info *info,
619                              struct osd_device *osd,
620                              struct dentry *dird, char *name,
621                              struct thandle *th)
622 {
623         struct ldiskfs_dir_entry_2 *de;
624         struct buffer_head         *bh;
625         struct osd_thandle         *oh;
626         struct dentry              *child;
627         struct inode               *dir = dird->d_inode;
628         int                         rc;
629         ENTRY;
630
631         oh = container_of(th, struct osd_thandle, ot_super);
632         LASSERT(oh->ot_handle != NULL);
633         LASSERT(oh->ot_handle->h_transaction != NULL);
634
635
636         child = &info->oti_child_dentry;
637         child->d_name.hash = 0;
638         child->d_name.name = name;
639         child->d_name.len = strlen(name);
640         child->d_parent = dird;
641         child->d_inode = NULL;
642
643         ll_vfs_dq_init(dir);
644         mutex_lock(&dir->i_mutex);
645         rc = -ENOENT;
646         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
647         if (bh) {
648                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
649                 brelse(bh);
650         }
651         mutex_unlock(&dir->i_mutex);
652
653         RETURN(rc);
654 }
655
656 int osd_obj_add_entry(struct osd_thread_info *info,
657                       struct osd_device *osd,
658                       struct dentry *dir, char *name,
659                       const struct osd_inode_id *id,
660                       struct thandle *th)
661 {
662         struct osd_thandle *oh;
663         struct dentry *child;
664         struct inode *inode;
665         int rc;
666
667         ENTRY;
668
669         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_NO_ENTRY))
670                 RETURN(0);
671
672         oh = container_of(th, struct osd_thandle, ot_super);
673         LASSERT(oh->ot_handle != NULL);
674         LASSERT(oh->ot_handle->h_transaction != NULL);
675
676         inode = &info->oti_inode;
677         inode->i_sb = osd_sb(osd);
678         osd_id_to_inode(inode, id);
679         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
680
681         child = &info->oti_child_dentry;
682         child->d_name.hash = 0;
683         child->d_name.name = name;
684         child->d_name.len = strlen(name);
685         child->d_parent = dir;
686         child->d_inode = inode;
687
688         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_COMPAT_INVALID_ENTRY))
689                 inode->i_ino++;
690
691         ll_vfs_dq_init(dir->d_inode);
692         mutex_lock(&dir->d_inode->i_mutex);
693         rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
694         mutex_unlock(&dir->d_inode->i_mutex);
695
696         RETURN(rc);
697 }
698
699 /**
700  * Use LPU64 for legacy OST sequences, but use LPX64i for new
701  * sequences names, so that the O/{seq}/dN/{oid} more closely
702  * follows the DFID/PFID format. This makes it easier to map from
703  * debug messages to objects in the future, and the legacy space
704  * of FID_SEQ_OST_MDT0 will be unused in the future.
705  **/
706 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
707 {
708         snprintf(seq_name, name_size,
709                  (fid_seq_is_rsvd(seq) ||
710                   fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
711                  fid_seq_is_idif(seq) ? 0 : seq);
712 }
713
714 static inline void osd_oid_name(char *name, size_t name_size,
715                                 const struct lu_fid *fid, obd_id id)
716 {
717         snprintf(name, name_size,
718                  (fid_seq_is_rsvd(fid_seq(fid)) ||
719                   fid_seq_is_mdt0(fid_seq(fid)) ||
720                   fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
721 }
722
723 /* external locking is required */
724 static int osd_seq_load_locked(struct osd_thread_info *info,
725                                struct osd_device *osd,
726                                struct osd_obj_seq *osd_seq)
727 {
728         struct osd_obj_map  *map = osd->od_ost_map;
729         struct dentry       *seq_dir;
730         struct inode        *inode;
731         struct lu_fid       *fid = &info->oti_fid3;
732         int                 rc = 0;
733         int                 i;
734         char                dir_name[32];
735         ENTRY;
736
737         if (osd_seq->oos_root != NULL)
738                 RETURN(0);
739
740         LASSERT(map);
741         LASSERT(map->om_root);
742
743         osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
744
745         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
746         if (IS_ERR(seq_dir))
747                 GOTO(out_err, rc = PTR_ERR(seq_dir));
748         else if (seq_dir->d_inode == NULL)
749                 GOTO(out_put, rc = -EFAULT);
750
751         inode = seq_dir->d_inode;
752         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
753         osd_seq->oos_root = seq_dir;
754
755         /* 'What the @fid is' is not imporatant, because the object
756          * has no OI mapping, and only is visible inside the OSD.*/
757         lu_igif_build(fid, inode->i_ino, inode->i_generation);
758         rc = osd_ea_fid_set(info, inode, fid,
759                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
760         if (rc != 0)
761                 GOTO(out_put, rc);
762
763         LASSERT(osd_seq->oos_dirs == NULL);
764         OBD_ALLOC(osd_seq->oos_dirs,
765                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
766         if (osd_seq->oos_dirs == NULL)
767                 GOTO(out_put, rc = -ENOMEM);
768
769         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
770                 struct dentry   *dir;
771
772                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
773                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
774                                    0700, 1);
775                 if (IS_ERR(dir)) {
776                         GOTO(out_free, rc = PTR_ERR(dir));
777                 } else if (dir->d_inode == NULL) {
778                         dput(dir);
779                         GOTO(out_free, rc = -EFAULT);
780                 }
781
782                 inode = dir->d_inode;
783                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
784                 osd_seq->oos_dirs[i] = dir;
785
786                 /* 'What the @fid is' is not imporatant, because the object
787                  * has no OI mapping, and only is visible inside the OSD.*/
788                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
789                 rc = osd_ea_fid_set(info, inode, fid,
790                                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
791                 if (rc != 0)
792                         GOTO(out_free, rc);
793         }
794
795         if (rc != 0) {
796 out_free:
797                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
798                         if (osd_seq->oos_dirs[i] != NULL)
799                                 dput(osd_seq->oos_dirs[i]);
800                 }
801                 OBD_FREE(osd_seq->oos_dirs,
802                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
803 out_put:
804                 dput(seq_dir);
805                 osd_seq->oos_root = NULL;
806         }
807 out_err:
808         RETURN(rc);
809 }
810
811 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
812                                         struct osd_device *osd, obd_seq seq)
813 {
814         struct osd_obj_map      *map;
815         struct osd_obj_seq      *osd_seq;
816         int                     rc = 0;
817         ENTRY;
818
819         map = osd->od_ost_map;
820         LASSERT(map);
821         LASSERT(map->om_root);
822
823         osd_seq = osd_seq_find(map, seq);
824         if (likely(osd_seq != NULL))
825                 RETURN(osd_seq);
826
827         /* Serializing init process */
828         down(&map->om_dir_init_sem);
829
830         /* Check whether the seq has been added */
831         read_lock(&map->om_seq_list_lock);
832         osd_seq = osd_seq_find_locked(map, seq);
833         if (osd_seq != NULL) {
834                 read_unlock(&map->om_seq_list_lock);
835                 GOTO(cleanup, rc = 0);
836         }
837         read_unlock(&map->om_seq_list_lock);
838
839         OBD_ALLOC_PTR(osd_seq);
840         if (osd_seq == NULL)
841                 GOTO(cleanup, rc = -ENOMEM);
842
843         CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
844         osd_seq->oos_seq = seq;
845         /* Init subdir count to be 32, but each seq can have
846          * different subdir count */
847         osd_seq->oos_subdir_count = map->om_subdir_count;
848         rc = osd_seq_load_locked(info, osd, osd_seq);
849         if (rc != 0)
850                 GOTO(cleanup, rc);
851
852         write_lock(&map->om_seq_list_lock);
853         cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
854         write_unlock(&map->om_seq_list_lock);
855
856 cleanup:
857         up(&map->om_dir_init_sem);
858         if (rc != 0) {
859                 if (osd_seq != NULL)
860                         OBD_FREE_PTR(osd_seq);
861                 RETURN(ERR_PTR(rc));
862         }
863
864         RETURN(osd_seq);
865 }
866
867 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
868                        const struct lu_fid *fid, struct osd_inode_id *id)
869 {
870         struct osd_obj_map              *map;
871         struct osd_obj_seq              *osd_seq;
872         struct dentry                   *d_seq;
873         struct dentry                   *child;
874         struct ost_id                   *ostid = &info->oti_ostid;
875         int                             dirn;
876         char                            name[32];
877         struct ldiskfs_dir_entry_2      *de;
878         struct buffer_head              *bh;
879         struct inode                    *dir;
880         struct inode                    *inode;
881         ENTRY;
882
883         /* on the very first lookup we find and open directories */
884
885         map = dev->od_ost_map;
886         LASSERT(map);
887         LASSERT(map->om_root);
888
889         fid_to_ostid(fid, ostid);
890         osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
891         if (IS_ERR(osd_seq))
892                 RETURN(PTR_ERR(osd_seq));
893
894         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
895         d_seq = osd_seq->oos_dirs[dirn];
896         LASSERT(d_seq);
897
898         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
899
900         child = &info->oti_child_dentry;
901         child->d_parent = d_seq;
902         child->d_name.hash = 0;
903         child->d_name.name = name;
904         /* XXX: we can use rc from sprintf() instead of strlen() */
905         child->d_name.len = strlen(name);
906
907         dir = d_seq->d_inode;
908         mutex_lock(&dir->i_mutex);
909         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
910         mutex_unlock(&dir->i_mutex);
911
912         if (bh == NULL)
913                 RETURN(-ENOENT);
914
915         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
916         brelse(bh);
917
918         inode = osd_iget(info, dev, id);
919         if (IS_ERR(inode))
920                 RETURN(PTR_ERR(inode));
921
922         iput(inode);
923         RETURN(0);
924 }
925
926 int osd_obj_map_insert(struct osd_thread_info *info,
927                        struct osd_device *osd,
928                        const struct lu_fid *fid,
929                        const struct osd_inode_id *id,
930                        struct thandle *th)
931 {
932         struct osd_obj_map      *map;
933         struct osd_obj_seq      *osd_seq;
934         struct dentry           *d;
935         struct ost_id           *ostid = &info->oti_ostid;
936         obd_id                   oid;
937         int                     dirn, rc = 0;
938         char                    name[32];
939         ENTRY;
940
941         map = osd->od_ost_map;
942         LASSERT(map);
943
944         /* map fid to seq:objid */
945         fid_to_ostid(fid, ostid);
946
947         oid = ostid_id(ostid);
948         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
949         if (IS_ERR(osd_seq))
950                 RETURN(PTR_ERR(osd_seq));
951
952         dirn = oid & (osd_seq->oos_subdir_count - 1);
953         d = osd_seq->oos_dirs[dirn];
954         LASSERT(d);
955
956         osd_oid_name(name, sizeof(name), fid, oid);
957
958 again:
959         rc = osd_obj_add_entry(info, osd, d, name, id, th);
960         if (rc == -EEXIST) {
961                 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
962                 if (unlikely(rc == -ENOENT))
963                         goto again;
964
965                 if (unlikely(rc == 1))
966                         RETURN(0);
967         }
968
969         RETURN(rc);
970 }
971
972 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
973                        const struct lu_fid *fid, struct thandle *th)
974 {
975         struct osd_obj_map      *map;
976         struct osd_obj_seq      *osd_seq;
977         struct dentry           *d;
978         struct ost_id           *ostid = &info->oti_ostid;
979         int                     dirn, rc = 0;
980         char                    name[32];
981         ENTRY;
982
983         map = osd->od_ost_map;
984         LASSERT(map);
985
986         /* map fid to seq:objid */
987         fid_to_ostid(fid, ostid);
988
989         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
990         if (IS_ERR(osd_seq))
991                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
992
993         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
994         d = osd_seq->oos_dirs[dirn];
995         LASSERT(d);
996
997         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
998         rc = osd_obj_del_entry(info, osd, d, name, th);
999 cleanup:
1000         RETURN(rc);
1001 }
1002
1003 int osd_obj_map_update(struct osd_thread_info *info,
1004                        struct osd_device *osd,
1005                        const struct lu_fid *fid,
1006                        const struct osd_inode_id *id,
1007                        struct thandle *th)
1008 {
1009         struct osd_obj_seq      *osd_seq;
1010         struct dentry           *d;
1011         struct ost_id           *ostid = &info->oti_ostid;
1012         int                     dirn, rc = 0;
1013         char                    name[32];
1014         ENTRY;
1015
1016         fid_to_ostid(fid, ostid);
1017         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1018         if (IS_ERR(osd_seq))
1019                 RETURN(PTR_ERR(osd_seq));
1020
1021         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1022         d = osd_seq->oos_dirs[dirn];
1023         LASSERT(d);
1024
1025         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1026         rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1027
1028         RETURN(rc);
1029 }
1030
1031 int osd_obj_map_recover(struct osd_thread_info *info,
1032                         struct osd_device *osd,
1033                         struct inode *src_parent,
1034                         struct dentry *src_child,
1035                         const struct lu_fid *fid)
1036 {
1037         struct osd_obj_seq         *osd_seq;
1038         struct dentry              *tgt_parent;
1039         struct dentry              *tgt_child = &info->oti_child_dentry;
1040         struct inode               *dir;
1041         struct inode               *inode     = src_child->d_inode;
1042         struct ost_id              *ostid     = &info->oti_ostid;
1043         handle_t                   *jh;
1044         struct ldiskfs_dir_entry_2 *de;
1045         struct buffer_head         *bh;
1046         char                        name[32];
1047         int                         dirn;
1048         int                         rc        = 0;
1049         ENTRY;
1050
1051         if (fid_is_last_id(fid)) {
1052                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1053                 if (IS_ERR(osd_seq))
1054                         RETURN(PTR_ERR(osd_seq));
1055
1056                 tgt_parent = osd_seq->oos_root;
1057                 tgt_child->d_name.name = "LAST_ID";
1058                 tgt_child->d_name.len = strlen("LAST_ID");
1059         } else {
1060                 fid_to_ostid(fid, ostid);
1061                 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1062                 if (IS_ERR(osd_seq))
1063                         RETURN(PTR_ERR(osd_seq));
1064
1065                 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1066                 tgt_parent = osd_seq->oos_dirs[dirn];
1067                 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1068                 tgt_child->d_name.name = name;
1069                 tgt_child->d_name.len = strlen(name);
1070         }
1071         LASSERT(tgt_parent != NULL);
1072
1073         dir = tgt_parent->d_inode;
1074         tgt_child->d_name.hash = 0;
1075         tgt_child->d_parent = tgt_parent;
1076         tgt_child->d_inode = inode;
1077
1078         /* The non-initialized src_child may be destroyed. */
1079         jh = ldiskfs_journal_start_sb(osd_sb(osd),
1080                                 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1081                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1082                                 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1083         if (IS_ERR(jh))
1084                 RETURN(PTR_ERR(jh));
1085
1086         ll_vfs_dq_init(src_parent);
1087         ll_vfs_dq_init(dir);
1088
1089         mutex_lock(&src_parent->i_mutex);
1090         mutex_lock(&dir->i_mutex);
1091         bh = osd_ldiskfs_find_entry(dir, tgt_child, &de, NULL);
1092         if (bh != NULL) {
1093                 /* XXX: If some other object occupied the same slot. And If such
1094                  *      inode is zero-sized and with SUID+SGID, then means it is
1095                  *      a new created one. Maybe we can remove it and insert the
1096                  *      original one back to the /O/<seq>/d<x>. But there are
1097                  *      something to be considered:
1098                  *
1099                  *      1) The OST-object under /lost+found has crashed LMA.
1100                  *         So it should not conflict with the current one.
1101                  *
1102                  *      2) There are race conditions that: someone may just want
1103                  *         to modify the current one. Even if the OI scrub takes
1104                  *         the object lock when remove the current one, it still
1105                  *         cause the modification to be lost becasue the target
1106                  *         has been removed when the RPC service thread waiting
1107                  *         for the lock.
1108                  *
1109                  *      So keep it there before we have suitable solution. */
1110                 brelse(bh);
1111
1112                 /* If the src object has never been modified, then remove it. */
1113                 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1114                     inode->i_mode & S_ISGID)
1115                         ll_vfs_unlink(src_parent, src_child, osd->od_mnt);
1116                 GOTO(unlock_src, rc = 0);
1117         }
1118
1119         bh = osd_ldiskfs_find_entry(src_parent, src_child, &de, NULL);
1120         if (unlikely(bh == NULL))
1121                 GOTO(unlock, rc = -ENOENT);
1122
1123         rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1124         brelse(bh);
1125         if (rc != 0)
1126                 GOTO(unlock, rc);
1127
1128         rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1129
1130         GOTO(unlock, rc);
1131
1132 unlock:
1133         mutex_unlock(&dir->i_mutex);
1134
1135 unlock_src:
1136         mutex_unlock(&src_parent->i_mutex);
1137         ldiskfs_journal_stop(jh);
1138         return rc;
1139 }
1140
1141 static struct dentry *
1142 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1143                      const struct lu_fid *fid, char **name)
1144 {
1145         struct dentry *root = ERR_PTR(-ENOENT);
1146
1147         if (fid_is_last_id(fid)) {
1148                 struct osd_obj_seq *osd_seq;
1149
1150                 /* on creation of LAST_ID we create O/<seq> hierarchy */
1151                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1152                 if (IS_ERR(osd_seq))
1153                         RETURN((struct dentry *)osd_seq);
1154
1155                 *name = "LAST_ID";
1156                 root = osd_seq->oos_root;
1157         } else {
1158                 *name = osd_lf_fid2name(fid);
1159                 if (*name == NULL)
1160                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1161                 else if ((*name)[0])
1162                         root = osd_sb(osd)->s_root;
1163         }
1164
1165         return root;
1166 }
1167
1168 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1169                         const struct lu_fid *fid, const struct osd_inode_id *id,
1170                         struct thandle *th)
1171 {
1172         struct dentry   *root;
1173         char            *name;
1174         int              rc;
1175         ENTRY;
1176
1177         root = osd_object_spec_find(info, osd, fid, &name);
1178         if (!IS_ERR(root)) {
1179                 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1180         } else {
1181                 rc = PTR_ERR(root);
1182                 if (rc == -ENOENT)
1183                         rc = 1;
1184         }
1185
1186         RETURN(rc);
1187 }
1188
1189 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1190                         const struct lu_fid *fid, const struct osd_inode_id *id,
1191                         struct thandle *th)
1192 {
1193         struct dentry   *root;
1194         char            *name;
1195         int              rc;
1196         ENTRY;
1197
1198         root = osd_object_spec_find(info, osd, fid, &name);
1199         if (!IS_ERR(root)) {
1200                 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1201         } else {
1202                 rc = PTR_ERR(root);
1203                 if (rc == -ENOENT)
1204                         rc = 0;
1205         }
1206
1207         RETURN(rc);
1208 }
1209
1210 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1211                         const struct lu_fid *fid, struct osd_inode_id *id)
1212 {
1213         struct dentry   *root;
1214         struct dentry   *dentry;
1215         struct inode    *inode;
1216         char            *name;
1217         int             rc = -ENOENT;
1218         ENTRY;
1219
1220         if (fid_is_last_id(fid)) {
1221                 struct osd_obj_seq *osd_seq;
1222
1223                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1224                 if (IS_ERR(osd_seq))
1225                         RETURN(PTR_ERR(osd_seq));
1226                 root = osd_seq->oos_root;
1227                 name = "LAST_ID";
1228         } else {
1229                 root = osd_sb(osd)->s_root;
1230                 name = osd_lf_fid2name(fid);
1231                 if (name == NULL || strlen(name) == 0)
1232                         RETURN(-ENOENT);
1233         }
1234
1235         dentry = ll_lookup_one_len(name, root, strlen(name));
1236         if (!IS_ERR(dentry)) {
1237                 inode = dentry->d_inode;
1238                 if (inode) {
1239                         if (is_bad_inode(inode)) {
1240                                 rc = -EIO;
1241                         } else {
1242                                 osd_id_gen(id, inode->i_ino,
1243                                            inode->i_generation);
1244                                 rc = 0;
1245                         }
1246                 }
1247                 /* if dentry is accessible after osd_compat_spec_insert it
1248                  * will still contain NULL inode, so don't keep it in cache */
1249                 d_invalidate(dentry);
1250                 dput(dentry);
1251         }
1252
1253         RETURN(rc);
1254 }