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