Whamcloud - gitweb
LU-3409 llite: silence lockdep warning in ll_md_blocking_ast
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_compat.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_compat.c
37  *
38  * on-disk structure for managing /O
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  */
42
43 /* LUSTRE_VERSION_CODE */
44 #include <lustre_ver.h>
45 /* prerequisite for linux/xattr.h */
46 #include <linux/types.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/fs.h>
49 /* XATTR_{REPLACE,CREATE} */
50 #include <linux/xattr.h>
51
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  * OBD_FAIL_CHECK
55  */
56 #include <obd_support.h>
57
58 #include "osd_internal.h"
59 #include "osd_oi.h"
60
61 static void osd_push_ctxt(const struct osd_device *dev,
62                           struct lvfs_run_ctxt *newctxt,
63                           struct lvfs_run_ctxt *save)
64 {
65         OBD_SET_CTXT_MAGIC(newctxt);
66         newctxt->pwdmnt = dev->od_mnt;
67         newctxt->pwd = dev->od_mnt->mnt_root;
68         newctxt->fs = get_ds();
69
70         push_ctxt(save, newctxt, NULL);
71 }
72
73 static void osd_pop_ctxt(const struct osd_device *dev,
74                          struct lvfs_run_ctxt *new,
75                          struct lvfs_run_ctxt *save)
76 {
77         pop_ctxt(save, new, NULL);
78 }
79
80 /* utility to make a directory */
81 static struct dentry *simple_mkdir(struct dentry *dir, struct vfsmount *mnt,
82                                    const char *name, int mode, int fix)
83 {
84         struct dentry *dchild;
85         int err = 0;
86         ENTRY;
87
88         // ASSERT_KERNEL_CTXT("kernel doing mkdir outside kernel context\n");
89         CDEBUG(D_INODE, "creating directory %.*s\n", (int)strlen(name), name);
90         dchild = ll_lookup_one_len(name, dir, strlen(name));
91         if (IS_ERR(dchild))
92                 GOTO(out_up, dchild);
93
94         if (dchild->d_inode) {
95                 int old_mode = dchild->d_inode->i_mode;
96                 if (!S_ISDIR(old_mode)) {
97                         CERROR("found %s (%lu/%u) is mode %o\n", name,
98                                dchild->d_inode->i_ino,
99                                dchild->d_inode->i_generation, old_mode);
100                         GOTO(out_err, err = -ENOTDIR);
101                 }
102
103                 /* Fixup directory permissions if necessary */
104                 if (fix && (old_mode & S_IALLUGO) != (mode & S_IALLUGO)) {
105                         CDEBUG(D_CONFIG,
106                                "fixing permissions on %s from %o to %o\n",
107                                name, old_mode, mode);
108                         dchild->d_inode->i_mode = (mode & S_IALLUGO) |
109                                                   (old_mode & ~S_IALLUGO);
110                         mark_inode_dirty(dchild->d_inode);
111                 }
112                 GOTO(out_up, dchild);
113         }
114
115         err = ll_vfs_mkdir(dir->d_inode, dchild, mnt, mode);
116         if (err)
117                 GOTO(out_err, err);
118
119         RETURN(dchild);
120
121 out_err:
122         dput(dchild);
123         dchild = ERR_PTR(err);
124 out_up:
125         return dchild;
126 }
127
128 int osd_last_rcvd_subdir_count(struct osd_device *osd)
129 {
130         struct lr_server_data lsd;
131         struct dentry        *dlast;
132         loff_t                off;
133         int                   rc = 0;
134         int                   count = FILTER_SUBDIR_COUNT;
135
136         ENTRY;
137
138         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
139                                   strlen(LAST_RCVD));
140         if (IS_ERR(dlast))
141                 return PTR_ERR(dlast);
142         else if (dlast->d_inode == NULL)
143                 goto out;
144
145         off = 0;
146         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
147         if (rc == sizeof(lsd)) {
148                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
149                        "subdir count = %d\n", lsd.lsd_uuid,
150                        lsd.lsd_subdir_count);
151                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
152                         count = le16_to_cpu(lsd.lsd_subdir_count);
153         } else if (rc != 0) {
154                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
155                 if (rc > 0)
156                         rc = -EFAULT;
157                 dput(dlast);
158                 return rc;
159         }
160 out:
161         dput(dlast);
162         LASSERT(count > 0);
163         return count;
164 }
165
166 static const char remote_parent_dir[] = "REMOTE_PARENT_DIR";
167 static int osd_mdt_init(const struct lu_env *env, struct osd_device *dev)
168 {
169         struct lvfs_run_ctxt    new;
170         struct lvfs_run_ctxt    save;
171         struct dentry           *parent;
172         struct osd_mdobj_map    *omm;
173         struct dentry           *d;
174         struct osd_thread_info  *info = osd_oti_get(env);
175         struct lu_fid           *fid = &info->oti_fid;
176         int                     rc = 0;
177         ENTRY;
178
179         OBD_ALLOC_PTR(dev->od_mdt_map);
180         if (dev->od_mdt_map == NULL)
181                 RETURN(-ENOMEM);
182
183         omm = dev->od_mdt_map;
184
185         LASSERT(dev->od_fsops);
186
187         parent = osd_sb(dev)->s_root;
188         osd_push_ctxt(dev, &new, &save);
189
190         d = simple_mkdir(parent, dev->od_mnt, remote_parent_dir,
191                          0755, 1);
192         if (IS_ERR(d))
193                 GOTO(cleanup, rc = PTR_ERR(d));
194
195         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
196         omm->omm_remote_parent = d;
197
198         /* Set LMA for remote parent inode */
199         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
200         rc = osd_ea_fid_set(info, d->d_inode, fid, 0);
201         if (rc != 0)
202                 GOTO(cleanup, rc);
203 cleanup:
204         pop_ctxt(&save, &new, NULL);
205         if (rc) {
206                 if (omm->omm_remote_parent != NULL)
207                         dput(omm->omm_remote_parent);
208                 OBD_FREE_PTR(omm);
209                 dev->od_mdt_map = NULL;
210         }
211         RETURN(rc);
212 }
213
214 static void osd_mdt_fini(struct osd_device *osd)
215 {
216         struct osd_mdobj_map *omm = osd->od_mdt_map;
217
218         if (omm == NULL)
219                 return;
220
221         if (omm->omm_remote_parent)
222                 dput(omm->omm_remote_parent);
223
224         OBD_FREE_PTR(omm);
225         osd->od_ost_map = NULL;
226 }
227
228 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
229                              struct osd_object *obj, struct osd_thandle *oh)
230 {
231         struct osd_mdobj_map    *omm = osd->od_mdt_map;
232         struct osd_thread_info  *oti = osd_oti_get(env);
233         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
234         char                    *name = oti->oti_name;
235         struct dentry           *dentry;
236         struct dentry           *parent;
237         int                     rc;
238
239         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
240          * can identify this object quickly */
241         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
242         if (rc != 0)
243                 RETURN(rc);
244
245         lma->lma_incompat |= LMAI_REMOTE_PARENT;
246         lustre_lma_swab(lma);
247         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
248                              sizeof(*lma), XATTR_REPLACE);
249         if (rc != 0)
250                 RETURN(rc);
251
252         parent = omm->omm_remote_parent;
253         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
254         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
255                                            name, strlen(name));
256         mutex_lock(&parent->d_inode->i_mutex);
257         rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
258                                    NULL);
259         CDEBUG(D_INODE, "%s: add %s:%lu to remote parent %lu.\n", osd_name(osd),
260                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
261         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
262                  osd_name(osd), parent->d_inode->i_ino,
263                  parent->d_inode->i_nlink);
264         parent->d_inode->i_nlink++;
265         mark_inode_dirty(parent->d_inode);
266         mutex_unlock(&parent->d_inode->i_mutex);
267         RETURN(rc);
268 }
269
270 int osd_delete_from_remote_parent(const struct lu_env *env,
271                                   struct osd_device *osd,
272                                   struct osd_object *obj,
273                                   struct osd_thandle *oh)
274 {
275         struct osd_mdobj_map       *omm = osd->od_mdt_map;
276         struct osd_thread_info     *oti = osd_oti_get(env);
277         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
278         char                       *name = oti->oti_name;
279         struct dentry              *dentry;
280         struct dentry              *parent;
281         struct ldiskfs_dir_entry_2 *de;
282         struct buffer_head         *bh;
283         int                        rc;
284
285         /* Check lma to see whether it is remote object */
286         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
287         if (rc != 0)
288                 RETURN(rc);
289
290         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
291                 RETURN(0);
292
293         parent = omm->omm_remote_parent;
294         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
295         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
296                                            name, strlen(name));
297         mutex_lock(&parent->d_inode->i_mutex);
298         bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
299         if (bh == NULL) {
300                 mutex_unlock(&parent->d_inode->i_mutex);
301                 RETURN(-ENOENT);
302         }
303         CDEBUG(D_INODE, "%s: el %s:%lu to remote parent %lu.\n", osd_name(osd),
304                name, obj->oo_inode->i_ino, parent->d_inode->i_ino);
305         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
306         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
307                  osd_name(osd), parent->d_inode->i_ino,
308                  parent->d_inode->i_nlink);
309         parent->d_inode->i_nlink--;
310         mark_inode_dirty(parent->d_inode);
311         mutex_unlock(&parent->d_inode->i_mutex);
312         brelse(bh);
313
314         /* Get rid of REMOTE_PARENT flag from incompat */
315         lma->lma_incompat &= ~LMAI_REMOTE_PARENT;
316         lustre_lma_swab(lma);
317         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
318                              sizeof(*lma), XATTR_REPLACE);
319         RETURN(rc);
320 }
321
322 /*
323  * directory structure on legacy OST:
324  *
325  * O/<seq>/d0-31/<objid>
326  * O/<seq>/LAST_ID
327  * last_rcvd
328  * LAST_GROUP
329  * CONFIGS
330  *
331  */
332 static int osd_ost_init(struct osd_device *dev)
333 {
334         struct lvfs_run_ctxt  new;
335         struct lvfs_run_ctxt  save;
336         struct dentry        *rootd = osd_sb(dev)->s_root;
337         struct dentry        *d;
338         int                   rc;
339         ENTRY;
340
341         OBD_ALLOC_PTR(dev->od_ost_map);
342         if (dev->od_ost_map == NULL)
343                 RETURN(-ENOMEM);
344
345         /* to get subdir count from last_rcvd */
346         rc = osd_last_rcvd_subdir_count(dev);
347         if (rc < 0) {
348                 OBD_FREE_PTR(dev->od_ost_map);
349                 RETURN(rc);
350         }
351
352         dev->od_ost_map->om_subdir_count = rc;
353         rc = 0;
354
355         CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
356         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
357         sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
358
359         LASSERT(dev->od_fsops);
360         osd_push_ctxt(dev, &new, &save);
361
362         d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
363         if (IS_ERR(d))
364                 GOTO(cleanup, rc = PTR_ERR(d));
365
366         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
367         dev->od_ost_map->om_root = d;
368
369 cleanup:
370         osd_pop_ctxt(dev, &new, &save);
371         if (IS_ERR(d)) {
372                 OBD_FREE_PTR(dev->od_ost_map);
373                 RETURN(PTR_ERR(d));
374         }
375
376         RETURN(rc);
377 }
378
379 static void osd_seq_free(struct osd_obj_map *map,
380                          struct osd_obj_seq *osd_seq)
381 {
382         int j;
383
384         cfs_list_del_init(&osd_seq->oos_seq_list);
385
386         if (osd_seq->oos_dirs) {
387                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
388                         if (osd_seq->oos_dirs[j])
389                                 dput(osd_seq->oos_dirs[j]);
390                 }
391                 OBD_FREE(osd_seq->oos_dirs,
392                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
393         }
394
395         if (osd_seq->oos_root)
396                 dput(osd_seq->oos_root);
397
398         OBD_FREE_PTR(osd_seq);
399 }
400
401 static void osd_ost_fini(struct osd_device *osd)
402 {
403         struct osd_obj_seq    *osd_seq;
404         struct osd_obj_seq    *tmp;
405         struct osd_obj_map    *map = osd->od_ost_map;
406         ENTRY;
407
408         if (map == NULL)
409                 return;
410
411         write_lock(&map->om_seq_list_lock);
412         cfs_list_for_each_entry_safe(osd_seq, tmp,
413                                      &map->om_seq_list,
414                                      oos_seq_list) {
415                 osd_seq_free(map, osd_seq);
416         }
417         write_unlock(&map->om_seq_list_lock);
418         if (map->om_root)
419                 dput(map->om_root);
420         OBD_FREE_PTR(map);
421         osd->od_ost_map = NULL;
422         EXIT;
423 }
424
425 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
426 {
427         int rc;
428         ENTRY;
429
430         /* prepare structures for OST */
431         rc = osd_ost_init(dev);
432         if (rc)
433                 RETURN(rc);
434
435         /* prepare structures for MDS */
436         rc = osd_mdt_init(env, dev);
437
438         RETURN(rc);
439 }
440
441 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
442 {
443         struct osd_obj_seq *osd_seq;
444
445         cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
446                 if (osd_seq->oos_seq == seq)
447                         return osd_seq;
448         }
449         return NULL;
450 }
451
452 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
453 {
454         struct osd_obj_seq *osd_seq;
455
456         read_lock(&map->om_seq_list_lock);
457         osd_seq = osd_seq_find_locked(map, seq);
458         read_unlock(&map->om_seq_list_lock);
459         return osd_seq;
460 }
461
462 void osd_obj_map_fini(struct osd_device *dev)
463 {
464         osd_ost_fini(dev);
465         osd_mdt_fini(dev);
466 }
467
468 static int osd_obj_del_entry(struct osd_thread_info *info,
469                              struct osd_device *osd,
470                              struct dentry *dird, char *name,
471                              struct thandle *th)
472 {
473         struct ldiskfs_dir_entry_2 *de;
474         struct buffer_head         *bh;
475         struct osd_thandle         *oh;
476         struct dentry              *child;
477         struct inode               *dir = dird->d_inode;
478         int                         rc;
479         ENTRY;
480
481         oh = container_of(th, struct osd_thandle, ot_super);
482         LASSERT(oh->ot_handle != NULL);
483         LASSERT(oh->ot_handle->h_transaction != NULL);
484
485
486         child = &info->oti_child_dentry;
487         child->d_name.hash = 0;
488         child->d_name.name = name;
489         child->d_name.len = strlen(name);
490         child->d_parent = dird;
491         child->d_inode = NULL;
492
493         ll_vfs_dq_init(dir);
494         mutex_lock(&dir->i_mutex);
495         rc = -ENOENT;
496         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
497         if (bh) {
498                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
499                 brelse(bh);
500         }
501         mutex_unlock(&dir->i_mutex);
502
503         RETURN(rc);
504 }
505
506 int osd_obj_add_entry(struct osd_thread_info *info,
507                       struct osd_device *osd,
508                       struct dentry *dir, char *name,
509                       const struct osd_inode_id *id,
510                       struct thandle *th)
511 {
512         struct osd_thandle *oh;
513         struct dentry *child;
514         struct inode *inode;
515         int rc;
516
517         ENTRY;
518
519         oh = container_of(th, struct osd_thandle, ot_super);
520         LASSERT(oh->ot_handle != NULL);
521         LASSERT(oh->ot_handle->h_transaction != NULL);
522
523         inode = &info->oti_inode;
524         inode->i_sb = osd_sb(osd);
525         osd_id_to_inode(inode, id);
526         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
527
528         child = &info->oti_child_dentry;
529         child->d_name.hash = 0;
530         child->d_name.name = name;
531         child->d_name.len = strlen(name);
532         child->d_parent = dir;
533         child->d_inode = inode;
534
535         ll_vfs_dq_init(dir->d_inode);
536         mutex_lock(&dir->d_inode->i_mutex);
537         rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
538         mutex_unlock(&dir->d_inode->i_mutex);
539
540         RETURN(rc);
541 }
542
543 /**
544  * Use LPU64 for legacy OST sequences, but use LPX64i for new
545  * sequences names, so that the O/{seq}/dN/{oid} more closely
546  * follows the DFID/PFID format. This makes it easier to map from
547  * debug messages to objects in the future, and the legacy space
548  * of FID_SEQ_OST_MDT0 will be unused in the future.
549  **/
550 static inline void osd_seq_name(char *seq_name, obd_seq seq)
551 {
552         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
553                            fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
554                 fid_seq_is_idif(seq) ? 0 : seq);
555 }
556
557 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
558 {
559         sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
560                 fid_seq_is_mdt0(fid_seq(fid)) ||
561                 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
562 }
563
564 /* external locking is required */
565 static int osd_seq_load_locked(struct osd_device *osd,
566                                struct osd_obj_seq *osd_seq)
567 {
568         struct osd_obj_map  *map = osd->od_ost_map;
569         struct dentry       *seq_dir;
570         int                 rc = 0;
571         int                 i;
572         char                seq_name[32];
573         ENTRY;
574
575         if (osd_seq->oos_root != NULL)
576                 RETURN(0);
577
578         LASSERT(map);
579         LASSERT(map->om_root);
580
581         osd_seq_name(seq_name, osd_seq->oos_seq);
582
583         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
584         if (IS_ERR(seq_dir))
585                 GOTO(out_err, rc = PTR_ERR(seq_dir));
586         else if (seq_dir->d_inode == NULL)
587                 GOTO(out_put, rc = -EFAULT);
588
589         ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
590         osd_seq->oos_root = seq_dir;
591
592         LASSERT(osd_seq->oos_dirs == NULL);
593         OBD_ALLOC(osd_seq->oos_dirs,
594                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
595         if (osd_seq->oos_dirs == NULL)
596                 GOTO(out_put, rc = -ENOMEM);
597
598         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
599                 struct dentry   *dir;
600                 char         name[32];
601
602                 sprintf(name, "d%u", i);
603                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
604                                    0700, 1);
605                 if (IS_ERR(dir)) {
606                         rc = PTR_ERR(dir);
607                 } else if (dir->d_inode) {
608                         ldiskfs_set_inode_state(dir->d_inode,
609                                                 LDISKFS_STATE_LUSTRE_NO_OI);
610                         osd_seq->oos_dirs[i] = dir;
611                         rc = 0;
612                 } else {
613                         LBUG();
614                 }
615         }
616
617         if (rc != 0)
618                 osd_seq_free(map, osd_seq);
619 out_put:
620         if (rc != 0) {
621                 dput(seq_dir);
622                 osd_seq->oos_root = NULL;
623         }
624 out_err:
625         RETURN(rc);
626 }
627
628 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
629 {
630         struct osd_obj_map      *map;
631         struct osd_obj_seq      *osd_seq;
632         int                     rc = 0;
633         ENTRY;
634
635         map = osd->od_ost_map;
636         LASSERT(map);
637         LASSERT(map->om_root);
638
639         osd_seq = osd_seq_find(map, seq);
640         if (likely(osd_seq != NULL))
641                 RETURN(osd_seq);
642
643         /* Serializing init process */
644         down(&map->om_dir_init_sem);
645
646         /* Check whether the seq has been added */
647         read_lock(&map->om_seq_list_lock);
648         osd_seq = osd_seq_find_locked(map, seq);
649         if (osd_seq != NULL) {
650                 read_unlock(&map->om_seq_list_lock);
651                 GOTO(cleanup, rc = 0);
652         }
653         read_unlock(&map->om_seq_list_lock);
654
655         OBD_ALLOC_PTR(osd_seq);
656         if (osd_seq == NULL)
657                 GOTO(cleanup, rc = -ENOMEM);
658
659         CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
660         osd_seq->oos_seq = seq;
661         /* Init subdir count to be 32, but each seq can have
662          * different subdir count */
663         osd_seq->oos_subdir_count = map->om_subdir_count;
664         rc = osd_seq_load_locked(osd, osd_seq);
665         if (rc != 0)
666                 GOTO(cleanup, rc);
667
668         write_lock(&map->om_seq_list_lock);
669         cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
670         write_unlock(&map->om_seq_list_lock);
671
672 cleanup:
673         up(&map->om_dir_init_sem);
674         if (rc != 0) {
675                 if (osd_seq != NULL)
676                         OBD_FREE_PTR(osd_seq);
677                 RETURN(ERR_PTR(rc));
678         }
679
680         RETURN(osd_seq);
681 }
682
683 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
684                        const struct lu_fid *fid, struct osd_inode_id *id)
685 {
686         struct osd_obj_map              *map;
687         struct osd_obj_seq              *osd_seq;
688         struct dentry                   *d_seq;
689         struct dentry                   *child;
690         struct ost_id                   *ostid = &info->oti_ostid;
691         int                             dirn;
692         char                            name[32];
693         struct ldiskfs_dir_entry_2      *de;
694         struct buffer_head              *bh;
695         struct inode                    *dir;
696         struct inode                    *inode;
697         ENTRY;
698
699         /* on the very first lookup we find and open directories */
700
701         map = dev->od_ost_map;
702         LASSERT(map);
703         LASSERT(map->om_root);
704
705         fid_to_ostid(fid, ostid);
706         osd_seq = osd_seq_load(dev, ostid_seq(ostid));
707         if (IS_ERR(osd_seq))
708                 RETURN(PTR_ERR(osd_seq));
709
710         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
711         d_seq = osd_seq->oos_dirs[dirn];
712         LASSERT(d_seq);
713
714         osd_oid_name(name, fid, ostid_id(ostid));
715
716         child = &info->oti_child_dentry;
717         child->d_parent = d_seq;
718         child->d_name.hash = 0;
719         child->d_name.name = name;
720         /* XXX: we can use rc from sprintf() instead of strlen() */
721         child->d_name.len = strlen(name);
722
723         dir = d_seq->d_inode;
724         mutex_lock(&dir->i_mutex);
725         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
726         mutex_unlock(&dir->i_mutex);
727
728         if (bh == NULL)
729                 RETURN(-ENOENT);
730
731         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
732         brelse(bh);
733
734         inode = osd_iget(info, dev, id);
735         if (IS_ERR(inode))
736                 RETURN(PTR_ERR(inode));
737
738         iput(inode);
739         RETURN(0);
740 }
741
742 int osd_obj_map_insert(struct osd_thread_info *info,
743                        struct osd_device *osd,
744                        const struct lu_fid *fid,
745                        const struct osd_inode_id *id,
746                        struct thandle *th)
747 {
748         struct osd_obj_map      *map;
749         struct osd_obj_seq      *osd_seq;
750         struct dentry           *d;
751         struct ost_id           *ostid = &info->oti_ostid;
752         int                     dirn, rc = 0;
753         char                    name[32];
754         ENTRY;
755
756         map = osd->od_ost_map;
757         LASSERT(map);
758
759         /* map fid to seq:objid */
760         fid_to_ostid(fid, ostid);
761
762         osd_seq = osd_seq_load(osd, ostid_seq(ostid));
763         if (IS_ERR(osd_seq))
764                 RETURN(PTR_ERR(osd_seq));
765
766         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
767         d = osd_seq->oos_dirs[dirn];
768         LASSERT(d);
769
770         osd_oid_name(name, fid, ostid_id(ostid));
771         rc = osd_obj_add_entry(info, osd, d, name, id, th);
772
773         RETURN(rc);
774 }
775
776 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
777                        const struct lu_fid *fid, struct thandle *th)
778 {
779         struct osd_obj_map      *map;
780         struct osd_obj_seq      *osd_seq;
781         struct dentry           *d;
782         struct ost_id           *ostid = &info->oti_ostid;
783         int                     dirn, rc = 0;
784         char                    name[32];
785         ENTRY;
786
787         map = osd->od_ost_map;
788         LASSERT(map);
789
790         /* map fid to seq:objid */
791         fid_to_ostid(fid, ostid);
792
793         osd_seq = osd_seq_load(osd, ostid_seq(ostid));
794         if (IS_ERR(osd_seq))
795                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
796
797         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
798         d = osd_seq->oos_dirs[dirn];
799         LASSERT(d);
800
801         osd_oid_name(name, fid, ostid_id(ostid));
802         rc = osd_obj_del_entry(info, osd, d, name, th);
803 cleanup:
804         RETURN(rc);
805 }
806
807 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
808                         const struct lu_fid *fid,
809                         const struct osd_inode_id *id,
810                         struct thandle *th)
811 {
812         struct osd_obj_map      *map = osd->od_ost_map;
813         struct dentry           *root = osd_sb(osd)->s_root;
814         char                    *name;
815         int                     rc = 0;
816         ENTRY;
817
818         if (fid_is_last_id(fid)) {
819                 struct osd_obj_seq      *osd_seq;
820
821                 /* on creation of LAST_ID we create O/<seq> hierarchy */
822                 LASSERT(map);
823                 osd_seq = osd_seq_load(osd, fid_seq(fid));
824                 if (IS_ERR(osd_seq))
825                         RETURN(PTR_ERR(osd_seq));
826                 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
827                                        "LAST_ID", id, th);
828         } else {
829                 name = osd_lf_fid2name(fid);
830                 if (name == NULL)
831                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
832                 else if (name[0])
833                         rc = osd_obj_add_entry(info, osd, root, name, id, th);
834         }
835
836         RETURN(rc);
837 }
838
839 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
840                         const struct lu_fid *fid, struct osd_inode_id *id)
841 {
842         struct dentry   *root;
843         struct dentry   *dentry;
844         struct inode    *inode;
845         char            *name;
846         int             rc = -ENOENT;
847         ENTRY;
848
849         if (fid_is_last_id(fid)) {
850                 struct osd_obj_seq *osd_seq;
851
852                 osd_seq = osd_seq_load(osd, fid_seq(fid));
853                 if (IS_ERR(osd_seq))
854                         RETURN(PTR_ERR(osd_seq));
855                 root = osd_seq->oos_root;
856                 name = "LAST_ID";
857         } else {
858                 root = osd_sb(osd)->s_root;
859                 name = osd_lf_fid2name(fid);
860                 if (name == NULL || strlen(name) == 0)
861                         RETURN(-ENOENT);
862         }
863
864         dentry = ll_lookup_one_len(name, root, strlen(name));
865         if (!IS_ERR(dentry)) {
866                 inode = dentry->d_inode;
867                 if (inode) {
868                         if (is_bad_inode(inode)) {
869                                 rc = -EIO;
870                         } else {
871                                 osd_id_gen(id, inode->i_ino,
872                                            inode->i_generation);
873                                 rc = 0;
874                         }
875                 }
876                 /* if dentry is accessible after osd_compat_spec_insert it
877                  * will still contain NULL inode, so don't keep it in cache */
878                 d_invalidate(dentry);
879                 dput(dentry);
880         }
881
882         RETURN(rc);
883 }