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