Whamcloud - gitweb
LU-5396 mdc: (and lmv, mgc, osc) make some functions static
[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         newctxt->umask = current_umask();
70         newctxt->dt = NULL;
71
72         push_ctxt(save, newctxt);
73 }
74
75 /* utility to make a directory */
76 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
77                                    const char *name, int mode, int fix)
78 {
79         struct dentry *dchild;
80         int err = 0;
81         ENTRY;
82
83         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
84         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
85         dchild = ll_lookup_one_len(name, dir, strlen(name));
86         if (IS_ERR(dchild))
87                 GOTO(out_up, dchild);
88
89         if (dchild->d_inode) {
90                 int old_mode = dchild->d_inode->i_mode;
91                 if (!S_ISDIR(old_mode)) {
92                         CERROR("found %s (%lu/%u) is mode %o\n", name,
93                                dchild->d_inode->i_ino,
94                                dchild->d_inode->i_generation, old_mode);
95                         GOTO(out_err, err = -ENOTDIR);
96                 }
97
98                 /* Fixup directory permissions if necessary */
99                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
100                         CDEBUG(D_CONFIG,
101                                "fixing permissions on %s from %o to %o\n",
102                                name, old_mode, mode);
103                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
104                                                   (old_mode & ~S_IALLUGO);
105                         mark_inode_dirty(dchild->d_inode);
106                 }
107                 GOTO(out_up, dchild);
108         }
109
110         err = vfs_mkdir(dir->d_inode, dchild, mode);
111         if (err)
112                 GOTO(out_err, err);
113
114         RETURN(dchild);
115
116 out_err:
117         dput(dchild);
118         dchild = ERR_PTR(err);
119 out_up:
120         return dchild;
121 }
122
123 static int osd_last_rcvd_subdir_count(struct osd_device *osd)
124 {
125         struct lr_server_data lsd;
126         struct dentry        *dlast;
127         loff_t                off;
128         int                   rc = 0;
129         int                   count = OBJ_SUBDIR_COUNT;
130
131         ENTRY;
132
133         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
134                                   strlen(LAST_RCVD));
135         if (IS_ERR(dlast))
136                 return PTR_ERR(dlast);
137         else if (dlast->d_inode == NULL)
138                 goto out;
139
140         off = 0;
141         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
142         if (rc == sizeof(lsd)) {
143                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
144                        "subdir count = %d\n", lsd.lsd_uuid,
145                        lsd.lsd_subdir_count);
146                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
147                         count = le16_to_cpu(lsd.lsd_subdir_count);
148         } else if (rc != 0) {
149                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
150                 if (rc > 0)
151                         rc = -EFAULT;
152                 dput(dlast);
153                 return rc;
154         }
155 out:
156         dput(dlast);
157         LASSERT(count > 0);
158         return count;
159 }
160
161 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
162 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
163 {
164         struct lvfs_run_ctxt    new;
165         struct lvfs_run_ctxt    save;
166         struct dentry           *parent;
167         struct osd_mdobj_map    *omm;
168         struct dentry           *d;
169         struct osd_thread_info  *info = osd_oti_get(env);
170         struct lu_fid           *fid = &info->oti_fid3;
171         int                     rc = 0;
172         ENTRY;
173
174         OBD_ALLOC_PTR(dev->od_mdt_map);
175         if (dev->od_mdt_map == NULL)
176                 RETURN(-ENOMEM);
177
178         omm = dev->od_mdt_map;
179
180         parent = osd_sb(dev)->s_root;
181         osd_push_ctxt(dev, &new, &save);
182
183         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
184                          0755, 1);
185         if (IS_ERR(d))
186                 GOTO(cleanup, rc = PTR_ERR(d));
187
188         omm->omm_remote_parent = d;
189
190         /* Set LMA for remote parent inode */
191         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
192         rc = osd_ea_fid_set(info, d->d_inode, fid, LMAC_NOT_IN_OI, 0);
193
194         GOTO(cleanup, rc);
195
196 cleanup:
197         pop_ctxt(&save, &new);
198         if (rc) {
199                 if (omm->omm_remote_parent != NULL)
200                         dput(omm->omm_remote_parent);
201                 OBD_FREE_PTR(omm);
202                 dev->od_mdt_map = NULL;
203         }
204         return rc;
205 }
206
207 static void osd_mdt_fini(struct osd_device *osd)
208 {
209         struct osd_mdobj_map *omm = osd->od_mdt_map;
210
211         if (omm == NULL)
212                 return;
213
214         if (omm->omm_remote_parent)
215                 dput(omm->omm_remote_parent);
216
217         OBD_FREE_PTR(omm);
218         osd->od_ost_map = NULL;
219 }
220
221 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
222                              struct osd_object *obj, struct osd_thandle *oh)
223 {
224         struct osd_mdobj_map    *omm = osd->od_mdt_map;
225         struct osd_thread_info  *oti = osd_oti_get(env);
226         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
227         char                    *name = oti->oti_name;
228         struct dentry           *dentry;
229         struct dentry           *parent;
230         int                     rc;
231
232         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
233          * can identify this object quickly */
234         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
235         if (rc != 0)
236                 RETURN(rc);
237
238         lma->lma_incompat |= LMAI_REMOTE_PARENT;
239         lustre_lma_swab(lma);
240         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
241                              sizeof(*lma), XATTR_REPLACE);
242         if (rc != 0)
243                 RETURN(rc);
244
245         parent = omm->omm_remote_parent;
246         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
247         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
248                                            name, strlen(name));
249         mutex_lock(&parent->d_inode->i_mutex);
250         rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
251                                    NULL);
252         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
253                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
254         ldiskfs_inc_count(oh->ot_handle, parent->d_inode);
255         mark_inode_dirty(parent->d_inode);
256         mutex_unlock(&parent->d_inode->i_mutex);
257         RETURN(rc);
258 }
259
260 int osd_delete_from_remote_parent(const struct lu_env *env,
261                                   struct osd_device *osd,
262                                   struct osd_object *obj,
263                                   struct osd_thandle *oh)
264 {
265         struct osd_mdobj_map       *omm = osd->od_mdt_map;
266         struct osd_thread_info     *oti = osd_oti_get(env);
267         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
268         char                       *name = oti->oti_name;
269         struct dentry              *dentry;
270         struct dentry              *parent;
271         struct ldiskfs_dir_entry_2 *de;
272         struct buffer_head         *bh;
273         int                        rc;
274
275         /* Check lma to see whether it is remote object */
276         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
277         if (rc != 0)
278                 RETURN(rc);
279
280         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
281                 RETURN(0);
282
283         parent = omm->omm_remote_parent;
284         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
285         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
286                                            name, strlen(name));
287         mutex_lock(&parent->d_inode->i_mutex);
288         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
289                                     NULL, NULL);
290         if (bh == NULL) {
291                 mutex_unlock(&parent->d_inode->i_mutex);
292                 RETURN(-ENOENT);
293         }
294         CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
295                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
296         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
297         ldiskfs_dec_count(oh->ot_handle, parent->d_inode);
298         mark_inode_dirty(parent->d_inode);
299         mutex_unlock(&parent->d_inode->i_mutex);
300         brelse(bh);
301
302         /* Get rid of REMOTE_PARENT flag from incompat */
303         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
304         lustre_lma_swab(lma);
305         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
306                              sizeof(*lma), XATTR_REPLACE);
307         RETURN(rc);
308 }
309
310 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
311                                 struct osd_device *osd,
312                                 const struct lu_fid *fid,
313                                 struct osd_inode_id *id)
314 {
315         struct osd_mdobj_map        *omm = osd->od_mdt_map;
316         char                        *name = oti->oti_name;
317         struct dentry               *parent;
318         struct dentry               *dentry;
319         struct ldiskfs_dir_entry_2 *de;
320         struct buffer_head         *bh;
321         int                         rc;
322         ENTRY;
323
324         parent = omm->omm_remote_parent;
325         sprintf(name, DFID_NOBRACE, PFID(fid));
326         dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
327                                            name, strlen(name));
328         mutex_lock(&parent->d_inode->i_mutex);
329         bh = osd_ldiskfs_find_entry(parent->d_inode, &dentry->d_name, &de,
330                                     NULL, NULL);
331         if (bh == NULL) {
332                 rc = -ENOENT;
333         } else {
334                 rc = 0;
335                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
336                 brelse(bh);
337         }
338         mutex_unlock(&parent->d_inode->i_mutex);
339         if (rc == 0)
340                 osd_add_oi_cache(oti, osd, id, fid);
341         RETURN(rc);
342 }
343
344 /*
345  * directory structure on legacy OST:
346  *
347  * O/<seq>/d0-31/<objid>
348  * O/<seq>/LAST_ID
349  * last_rcvd
350  * LAST_GROUP
351  * CONFIGS
352  *
353  */
354 static int osd_ost_init(const struct lu_env *env, struct osd_device *dev)
355 {
356         struct lvfs_run_ctxt     new;
357         struct lvfs_run_ctxt     save;
358         struct dentry           *rootd = osd_sb(dev)->s_root;
359         struct dentry           *d;
360         struct osd_thread_info  *info = osd_oti_get(env);
361         struct inode            *inode;
362         struct lu_fid           *fid = &info->oti_fid3;
363         int                      rc;
364         ENTRY;
365
366         OBD_ALLOC_PTR(dev->od_ost_map);
367         if (dev->od_ost_map == NULL)
368                 RETURN(-ENOMEM);
369
370         /* to get subdir count from last_rcvd */
371         rc = osd_last_rcvd_subdir_count(dev);
372         if (rc < 0) {
373                 OBD_FREE_PTR(dev->od_ost_map);
374                 RETURN(rc);
375         }
376
377         dev->od_ost_map->om_subdir_count = rc;
378         rc = 0;
379
380         INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
381         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
382         mutex_init(&dev->od_ost_map->om_dir_init_mutex);
383
384         osd_push_ctxt(dev, &new, &save);
385
386         d = ll_lookup_one_len("O", rootd, strlen("O"));
387         if (IS_ERR(d))
388                 GOTO(cleanup, rc = PTR_ERR(d));
389         if (d->d_inode == NULL) {
390                 dput(d);
391                 /* The lookup() may be called again inside simple_mkdir().
392                  * Since the repeated lookup() only be called for "/O" at
393                  * mount time, it will not affect the whole performance. */
394                 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
395                 if (IS_ERR(d))
396                         GOTO(cleanup, rc = PTR_ERR(d));
397
398                 /* It is quite probably that the device is new formatted. */
399                 dev->od_maybe_new = 1;
400         }
401
402         inode = d->d_inode;
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         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         list_for_each_entry_safe(osd_seq, tmp, &map->om_seq_list,
456                                  oos_seq_list) {
457                 osd_seq_free(map, osd_seq);
458         }
459         write_unlock(&map->om_seq_list_lock);
460         if (map->om_root)
461                 dput(map->om_root);
462         OBD_FREE_PTR(map);
463         osd->od_ost_map = NULL;
464         EXIT;
465 }
466
467 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
468 {
469         int rc;
470         ENTRY;
471
472         /* prepare structures for OST */
473         rc = osd_ost_init(env, dev);
474         if (rc)
475                 RETURN(rc);
476
477         /* prepare structures for MDS */
478         rc = osd_mdt_init(env, dev);
479
480         RETURN(rc);
481 }
482
483 static struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map,
484                                                obd_seq seq)
485 {
486         struct osd_obj_seq *osd_seq;
487
488         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 static 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 static 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         osd_seq->oos_root = seq_dir;
745
746         /* 'What the @fid is' is not imporatant, because the object
747          * has no OI mapping, and only is visible inside the OSD.*/
748         lu_igif_build(fid, inode->i_ino, inode->i_generation);
749         rc = osd_ea_fid_set(info, inode, fid,
750                             LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
751         if (rc != 0)
752                 GOTO(out_put, rc);
753
754         LASSERT(osd_seq->oos_dirs == NULL);
755         OBD_ALLOC(osd_seq->oos_dirs,
756                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
757         if (osd_seq->oos_dirs == NULL)
758                 GOTO(out_put, rc = -ENOMEM);
759
760         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
761                 struct dentry   *dir;
762
763                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
764                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
765                                    0700, 1);
766                 if (IS_ERR(dir)) {
767                         GOTO(out_free, rc = PTR_ERR(dir));
768                 } else if (dir->d_inode == NULL) {
769                         dput(dir);
770                         GOTO(out_free, rc = -EFAULT);
771                 }
772
773                 inode = dir->d_inode;
774                 osd_seq->oos_dirs[i] = dir;
775
776                 /* 'What the @fid is' is not imporatant, because the object
777                  * has no OI mapping, and only is visible inside the OSD.*/
778                 lu_igif_build(fid, inode->i_ino, inode->i_generation);
779                 rc = osd_ea_fid_set(info, inode, fid,
780                                     LMAC_NOT_IN_OI | LMAC_FID_ON_OST, 0);
781                 if (rc != 0)
782                         GOTO(out_free, rc);
783         }
784
785         if (rc != 0) {
786 out_free:
787                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
788                         if (osd_seq->oos_dirs[i] != NULL)
789                                 dput(osd_seq->oos_dirs[i]);
790                 }
791                 OBD_FREE(osd_seq->oos_dirs,
792                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
793 out_put:
794                 dput(seq_dir);
795                 osd_seq->oos_root = NULL;
796         }
797 out_err:
798         RETURN(rc);
799 }
800
801 static struct osd_obj_seq *osd_seq_load(struct osd_thread_info *info,
802                                         struct osd_device *osd, obd_seq seq)
803 {
804         struct osd_obj_map      *map;
805         struct osd_obj_seq      *osd_seq;
806         int                     rc = 0;
807         ENTRY;
808
809         map = osd->od_ost_map;
810         LASSERT(map);
811         LASSERT(map->om_root);
812
813         osd_seq = osd_seq_find(map, seq);
814         if (likely(osd_seq != NULL))
815                 RETURN(osd_seq);
816
817         /* Serializing init process */
818         mutex_lock(&map->om_dir_init_mutex);
819
820         /* Check whether the seq has been added */
821         read_lock(&map->om_seq_list_lock);
822         osd_seq = osd_seq_find_locked(map, seq);
823         if (osd_seq != NULL) {
824                 read_unlock(&map->om_seq_list_lock);
825                 GOTO(cleanup, rc = 0);
826         }
827         read_unlock(&map->om_seq_list_lock);
828
829         OBD_ALLOC_PTR(osd_seq);
830         if (osd_seq == NULL)
831                 GOTO(cleanup, rc = -ENOMEM);
832
833         INIT_LIST_HEAD(&osd_seq->oos_seq_list);
834         osd_seq->oos_seq = seq;
835         /* Init subdir count to be 32, but each seq can have
836          * different subdir count */
837         osd_seq->oos_subdir_count = map->om_subdir_count;
838         rc = osd_seq_load_locked(info, osd, osd_seq);
839         if (rc != 0)
840                 GOTO(cleanup, rc);
841
842         write_lock(&map->om_seq_list_lock);
843         list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
844         write_unlock(&map->om_seq_list_lock);
845
846 cleanup:
847         mutex_unlock(&map->om_dir_init_mutex);
848         if (rc != 0) {
849                 if (osd_seq != NULL)
850                         OBD_FREE_PTR(osd_seq);
851                 RETURN(ERR_PTR(rc));
852         }
853
854         RETURN(osd_seq);
855 }
856
857 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
858                        const struct lu_fid *fid, struct osd_inode_id *id)
859 {
860         struct osd_obj_map              *map;
861         struct osd_obj_seq              *osd_seq;
862         struct dentry                   *d_seq;
863         struct dentry                   *child;
864         struct ost_id                   *ostid = &info->oti_ostid;
865         int                             dirn;
866         char                            name[32];
867         struct ldiskfs_dir_entry_2      *de;
868         struct buffer_head              *bh;
869         struct inode                    *dir;
870         struct inode                    *inode;
871         ENTRY;
872
873         /* on the very first lookup we find and open directories */
874
875         map = dev->od_ost_map;
876         LASSERT(map);
877         LASSERT(map->om_root);
878
879         fid_to_ostid(fid, ostid);
880         osd_seq = osd_seq_load(info, dev, ostid_seq(ostid));
881         if (IS_ERR(osd_seq))
882                 RETURN(PTR_ERR(osd_seq));
883
884         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
885         d_seq = osd_seq->oos_dirs[dirn];
886         LASSERT(d_seq);
887
888         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
889
890         child = &info->oti_child_dentry;
891         child->d_parent = d_seq;
892         child->d_name.hash = 0;
893         child->d_name.name = name;
894         /* XXX: we can use rc from sprintf() instead of strlen() */
895         child->d_name.len = strlen(name);
896
897         dir = d_seq->d_inode;
898         mutex_lock(&dir->i_mutex);
899         bh = osd_ldiskfs_find_entry(dir, &child->d_name, &de, NULL, NULL);
900         mutex_unlock(&dir->i_mutex);
901
902         if (bh == NULL)
903                 RETURN(-ENOENT);
904
905         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
906         brelse(bh);
907
908         inode = osd_iget(info, dev, id);
909         if (IS_ERR(inode))
910                 RETURN(PTR_ERR(inode));
911
912         iput(inode);
913         RETURN(0);
914 }
915
916 int osd_obj_map_insert(struct osd_thread_info *info,
917                        struct osd_device *osd,
918                        const struct lu_fid *fid,
919                        const struct osd_inode_id *id,
920                        handle_t *th)
921 {
922         struct osd_obj_map      *map;
923         struct osd_obj_seq      *osd_seq;
924         struct dentry           *d;
925         struct ost_id           *ostid = &info->oti_ostid;
926         obd_id                   oid;
927         int                     dirn, rc = 0;
928         char                    name[32];
929         ENTRY;
930
931         map = osd->od_ost_map;
932         LASSERT(map);
933
934         /* map fid to seq:objid */
935         fid_to_ostid(fid, ostid);
936
937         oid = ostid_id(ostid);
938         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
939         if (IS_ERR(osd_seq))
940                 RETURN(PTR_ERR(osd_seq));
941
942         dirn = oid & (osd_seq->oos_subdir_count - 1);
943         d = osd_seq->oos_dirs[dirn];
944         LASSERT(d);
945
946         osd_oid_name(name, sizeof(name), fid, oid);
947
948 again:
949         rc = osd_obj_add_entry(info, osd, d, name, id, th);
950         if (rc == -EEXIST) {
951                 rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
952                 if (unlikely(rc == -ENOENT))
953                         goto again;
954
955                 if (unlikely(rc == 1))
956                         RETURN(0);
957         }
958
959         RETURN(rc);
960 }
961
962 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
963                        const struct lu_fid *fid, handle_t *th)
964 {
965         struct osd_obj_map      *map;
966         struct osd_obj_seq      *osd_seq;
967         struct dentry           *d;
968         struct ost_id           *ostid = &info->oti_ostid;
969         int                     dirn, rc = 0;
970         char                    name[32];
971         ENTRY;
972
973         map = osd->od_ost_map;
974         LASSERT(map);
975
976         /* map fid to seq:objid */
977         fid_to_ostid(fid, ostid);
978
979         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
980         if (IS_ERR(osd_seq))
981                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
982
983         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
984         d = osd_seq->oos_dirs[dirn];
985         LASSERT(d);
986
987         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
988         rc = osd_obj_del_entry(info, osd, d, name, th);
989 cleanup:
990         RETURN(rc);
991 }
992
993 int osd_obj_map_update(struct osd_thread_info *info,
994                        struct osd_device *osd,
995                        const struct lu_fid *fid,
996                        const struct osd_inode_id *id,
997                        handle_t *th)
998 {
999         struct osd_obj_seq      *osd_seq;
1000         struct dentry           *d;
1001         struct ost_id           *ostid = &info->oti_ostid;
1002         int                     dirn, rc = 0;
1003         char                    name[32];
1004         ENTRY;
1005
1006         fid_to_ostid(fid, ostid);
1007         osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1008         if (IS_ERR(osd_seq))
1009                 RETURN(PTR_ERR(osd_seq));
1010
1011         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1012         d = osd_seq->oos_dirs[dirn];
1013         LASSERT(d);
1014
1015         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1016         rc = osd_obj_update_entry(info, osd, d, name, fid, id, th);
1017
1018         RETURN(rc);
1019 }
1020
1021 int osd_obj_map_recover(struct osd_thread_info *info,
1022                         struct osd_device *osd,
1023                         struct inode *src_parent,
1024                         struct dentry *src_child,
1025                         const struct lu_fid *fid)
1026 {
1027         struct osd_obj_seq         *osd_seq;
1028         struct dentry              *tgt_parent;
1029         struct dentry              *tgt_child = &info->oti_child_dentry;
1030         struct inode               *dir;
1031         struct inode               *inode     = src_child->d_inode;
1032         struct ost_id              *ostid     = &info->oti_ostid;
1033         handle_t                   *jh;
1034         struct ldiskfs_dir_entry_2 *de;
1035         struct buffer_head         *bh;
1036         char                        name[32];
1037         int                         dirn;
1038         int                         rc        = 0;
1039         ENTRY;
1040
1041         if (fid_is_last_id(fid)) {
1042                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1043                 if (IS_ERR(osd_seq))
1044                         RETURN(PTR_ERR(osd_seq));
1045
1046                 tgt_parent = osd_seq->oos_root;
1047                 tgt_child->d_name.name = "LAST_ID";
1048                 tgt_child->d_name.len = strlen("LAST_ID");
1049         } else {
1050                 fid_to_ostid(fid, ostid);
1051                 osd_seq = osd_seq_load(info, osd, ostid_seq(ostid));
1052                 if (IS_ERR(osd_seq))
1053                         RETURN(PTR_ERR(osd_seq));
1054
1055                 dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
1056                 tgt_parent = osd_seq->oos_dirs[dirn];
1057                 osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
1058                 tgt_child->d_name.name = name;
1059                 tgt_child->d_name.len = strlen(name);
1060         }
1061         LASSERT(tgt_parent != NULL);
1062
1063         dir = tgt_parent->d_inode;
1064         tgt_child->d_name.hash = 0;
1065         tgt_child->d_parent = tgt_parent;
1066         tgt_child->d_inode = inode;
1067
1068         /* The non-initialized src_child may be destroyed. */
1069         jh = osd_journal_start_sb(osd_sb(osd), LDISKFS_HT_MISC,
1070                                 osd_dto_credits_noquota[DTO_INDEX_DELETE] +
1071                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] +
1072                                 osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
1073         if (IS_ERR(jh))
1074                 RETURN(PTR_ERR(jh));
1075
1076         ll_vfs_dq_init(src_parent);
1077         ll_vfs_dq_init(dir);
1078
1079         mutex_lock(&src_parent->i_mutex);
1080         mutex_lock(&dir->i_mutex);
1081         bh = osd_ldiskfs_find_entry(dir, &tgt_child->d_name, &de, NULL, NULL);
1082         if (bh != NULL) {
1083                 /* XXX: If some other object occupied the same slot. And If such
1084                  *      inode is zero-sized and with SUID+SGID, then means it is
1085                  *      a new created one. Maybe we can remove it and insert the
1086                  *      original one back to the /O/<seq>/d<x>. But there are
1087                  *      something to be considered:
1088                  *
1089                  *      1) The OST-object under /lost+found has crashed LMA.
1090                  *         So it should not conflict with the current one.
1091                  *
1092                  *      2) There are race conditions that: someone may just want
1093                  *         to modify the current one. Even if the OI scrub takes
1094                  *         the object lock when remove the current one, it still
1095                  *         cause the modification to be lost becasue the target
1096                  *         has been removed when the RPC service thread waiting
1097                  *         for the lock.
1098                  *
1099                  *      So keep it there before we have suitable solution. */
1100                 brelse(bh);
1101                 mutex_unlock(&dir->i_mutex);
1102                 mutex_unlock(&src_parent->i_mutex);
1103                 ldiskfs_journal_stop(jh);
1104
1105                 rc = -EEXIST;
1106                 /* If the src object has never been modified, then remove it. */
1107                 if (inode->i_size == 0 && inode->i_mode & S_ISUID &&
1108                     inode->i_mode & S_ISGID) {
1109                         rc = ll_vfs_unlink(src_parent, src_child);
1110                         if (unlikely(rc == -ENOENT))
1111                                 rc = 0;
1112                 }
1113                 RETURN(rc);
1114         }
1115
1116         bh = osd_ldiskfs_find_entry(src_parent, &src_child->d_name, &de,
1117                                     NULL, NULL);
1118         if (unlikely(bh == NULL))
1119                 GOTO(unlock, rc = -ENOENT);
1120
1121         rc = ldiskfs_delete_entry(jh, src_parent, de, bh);
1122         brelse(bh);
1123         if (rc != 0)
1124                 GOTO(unlock, rc);
1125
1126         rc = osd_ldiskfs_add_entry(jh, tgt_child, inode, NULL);
1127
1128         GOTO(unlock, rc);
1129
1130 unlock:
1131         mutex_unlock(&dir->i_mutex);
1132         mutex_unlock(&src_parent->i_mutex);
1133         ldiskfs_journal_stop(jh);
1134         return rc;
1135 }
1136
1137 static struct dentry *
1138 osd_object_spec_find(struct osd_thread_info *info, struct osd_device *osd,
1139                      const struct lu_fid *fid, char **name)
1140 {
1141         struct dentry *root = ERR_PTR(-ENOENT);
1142
1143         if (fid_is_last_id(fid)) {
1144                 struct osd_obj_seq *osd_seq;
1145
1146                 /* on creation of LAST_ID we create O/<seq> hierarchy */
1147                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1148                 if (IS_ERR(osd_seq))
1149                         RETURN((struct dentry *)osd_seq);
1150
1151                 *name = "LAST_ID";
1152                 root = osd_seq->oos_root;
1153         } else {
1154                 *name = osd_lf_fid2name(fid);
1155                 if (*name == NULL)
1156                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
1157                 else if ((*name)[0])
1158                         root = osd_sb(osd)->s_root;
1159         }
1160
1161         return root;
1162 }
1163
1164 int osd_obj_spec_update(struct osd_thread_info *info, struct osd_device *osd,
1165                         const struct lu_fid *fid, const struct osd_inode_id *id,
1166                         handle_t *th)
1167 {
1168         struct dentry   *root;
1169         char            *name = NULL;
1170         int              rc;
1171         ENTRY;
1172
1173         root = osd_object_spec_find(info, osd, fid, &name);
1174         if (!IS_ERR(root)) {
1175                 rc = osd_obj_update_entry(info, osd, root, name, fid, id, th);
1176         } else {
1177                 rc = PTR_ERR(root);
1178                 if (rc == -ENOENT)
1179                         rc = 1;
1180         }
1181
1182         RETURN(rc);
1183 }
1184
1185 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
1186                         const struct lu_fid *fid, const struct osd_inode_id *id,
1187                         handle_t *th)
1188 {
1189         struct dentry   *root;
1190         char            *name = NULL;
1191         int              rc;
1192         ENTRY;
1193
1194         root = osd_object_spec_find(info, osd, fid, &name);
1195         if (!IS_ERR(root)) {
1196                 rc = osd_obj_add_entry(info, osd, root, name, id, th);
1197         } else {
1198                 rc = PTR_ERR(root);
1199                 if (rc == -ENOENT)
1200                         rc = 0;
1201         }
1202
1203         RETURN(rc);
1204 }
1205
1206 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
1207                         const struct lu_fid *fid, struct osd_inode_id *id)
1208 {
1209         struct dentry   *root;
1210         struct dentry   *dentry;
1211         struct inode    *inode;
1212         char            *name = NULL;
1213         int             rc = -ENOENT;
1214         ENTRY;
1215
1216         if (fid_is_last_id(fid)) {
1217                 struct osd_obj_seq *osd_seq;
1218
1219                 osd_seq = osd_seq_load(info, osd, fid_seq(fid));
1220                 if (IS_ERR(osd_seq))
1221                         RETURN(PTR_ERR(osd_seq));
1222                 root = osd_seq->oos_root;
1223                 name = "LAST_ID";
1224         } else {
1225                 root = osd_sb(osd)->s_root;
1226                 name = osd_lf_fid2name(fid);
1227                 if (name == NULL || strlen(name) == 0)
1228                         RETURN(-ENOENT);
1229         }
1230
1231         dentry = ll_lookup_one_len(name, root, strlen(name));
1232         if (!IS_ERR(dentry)) {
1233                 inode = dentry->d_inode;
1234                 if (inode) {
1235                         if (is_bad_inode(inode)) {
1236                                 rc = -EIO;
1237                         } else {
1238                                 osd_id_gen(id, inode->i_ino,
1239                                            inode->i_generation);
1240                                 rc = 0;
1241                         }
1242                 }
1243                 /* if dentry is accessible after osd_compat_spec_insert it
1244                  * will still contain NULL inode, so don't keep it in cache */
1245                 d_invalidate(dentry);
1246                 dput(dentry);
1247         }
1248
1249         RETURN(rc);
1250 }