Whamcloud - gitweb
LU-2646 osd: add special flag for agent inode.
[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, 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         omm->omm_remote_parent = d;
196
197         /* Set LMA for remote parent inode */
198         lu_local_obj_fid(fid, REMOTE_PARENT_DIR_OID);
199         rc = osd_ea_fid_set(info, d->d_inode, fid, 0);
200         if (rc != 0)
201                 GOTO(cleanup, rc);
202 cleanup:
203         pop_ctxt(&save, &new, NULL);
204         if (rc) {
205                 if (omm->omm_remote_parent != NULL)
206                         dput(omm->omm_remote_parent);
207                 OBD_FREE_PTR(omm);
208                 dev->od_mdt_map = NULL;
209         }
210         RETURN(rc);
211 }
212
213 static void osd_mdt_fini(struct osd_device *osd)
214 {
215         struct osd_mdobj_map *omm = osd->od_mdt_map;
216
217         if (omm == NULL)
218                 return;
219
220         if (omm->omm_remote_parent)
221                 dput(omm->omm_remote_parent);
222
223         OBD_FREE_PTR(omm);
224         osd->od_ost_map = NULL;
225 }
226
227 int osd_add_to_remote_parent(const struct lu_env *env, struct osd_device *osd,
228                              struct osd_object *obj, struct osd_thandle *oh)
229 {
230         struct osd_mdobj_map    *omm = osd->od_mdt_map;
231         struct osd_thread_info  *oti = osd_oti_get(env);
232         struct lustre_mdt_attrs *lma = &oti->oti_mdt_attrs;
233         char                    *name = oti->oti_name;
234         struct dentry           *dentry;
235         struct dentry           *parent;
236         int                     rc;
237
238         /* Set REMOTE_PARENT in lma, so other process like unlink or lfsck
239          * can identify this object quickly */
240         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
241         if (rc != 0)
242                 RETURN(rc);
243
244         lma->lma_incompat |= LMAI_REMOTE_PARENT;
245         lustre_lma_swab(lma);
246         rc = __osd_xattr_set(oti, obj->oo_inode, XATTR_NAME_LMA, lma,
247                              sizeof(*lma), XATTR_REPLACE);
248         if (rc != 0)
249                 RETURN(rc);
250
251         parent = omm->omm_remote_parent;
252         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
253         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
254                                            name, strlen(name));
255         mutex_lock(&parent->d_inode->i_mutex);
256         rc = osd_ldiskfs_add_entry(oh->ot_handle, dentry, obj->oo_inode,
257                                    NULL);
258         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
259                  osd_name(osd), parent->d_inode->i_ino,
260                  parent->d_inode->i_nlink);
261         parent->d_inode->i_nlink++;
262         mark_inode_dirty(parent->d_inode);
263         mutex_unlock(&parent->d_inode->i_mutex);
264         RETURN(rc);
265 }
266
267 int osd_delete_from_remote_parent(const struct lu_env *env,
268                                   struct osd_device *osd,
269                                   struct osd_object *obj,
270                                   struct osd_thandle *oh)
271 {
272         struct osd_mdobj_map       *omm = osd->od_mdt_map;
273         struct osd_thread_info     *oti = osd_oti_get(env);
274         struct lustre_mdt_attrs    *lma = &oti->oti_mdt_attrs;
275         char                       *name = oti->oti_name;
276         struct dentry              *dentry;
277         struct dentry              *parent;
278         struct ldiskfs_dir_entry_2 *de;
279         struct buffer_head         *bh;
280         int                        rc;
281
282         /* Check lma to see whether it is remote object */
283         rc = osd_get_lma(oti, obj->oo_inode, &oti->oti_obj_dentry, lma);
284         if (rc != 0)
285                 RETURN(rc);
286
287         if (likely(!(lma->lma_incompat & LMAI_REMOTE_PARENT)))
288                 RETURN(0);
289
290         parent = omm->omm_remote_parent;
291         sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
292         dentry = osd_child_dentry_by_inode(env, parent->d_inode,
293                                            name, strlen(name));
294         mutex_lock(&parent->d_inode->i_mutex);
295         bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
296         if (bh == NULL) {
297                 mutex_unlock(&parent->d_inode->i_mutex);
298                 RETURN(-ENOENT);
299         }
300         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
301         LASSERTF(parent->d_inode->i_nlink > 1, "%s: %lu nlink %d",
302                  osd_name(osd), parent->d_inode->i_ino,
303                  parent->d_inode->i_nlink);
304         parent->d_inode->i_nlink--;
305         mark_inode_dirty(parent->d_inode);
306         mutex_unlock(&parent->d_inode->i_mutex);
307         brelse(bh);
308         RETURN(rc);
309 }
310
311 /*
312  * directory structure on legacy OST:
313  *
314  * O/<seq>/d0-31/<objid>
315  * O/<seq>/LAST_ID
316  * last_rcvd
317  * LAST_GROUP
318  * CONFIGS
319  *
320  */
321 static int osd_ost_init(struct osd_device *dev)
322 {
323         struct lvfs_run_ctxt  new;
324         struct lvfs_run_ctxt  save;
325         struct dentry        *rootd = osd_sb(dev)->s_root;
326         struct dentry        *d;
327         int                   rc;
328         ENTRY;
329
330         OBD_ALLOC_PTR(dev->od_ost_map);
331         if (dev->od_ost_map == NULL)
332                 RETURN(-ENOMEM);
333
334         /* to get subdir count from last_rcvd */
335         rc = osd_last_rcvd_subdir_count(dev);
336         if (rc < 0) {
337                 OBD_FREE_PTR(dev->od_ost_map);
338                 RETURN(rc);
339         }
340
341         dev->od_ost_map->om_subdir_count = rc;
342         rc = 0;
343
344         CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
345         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
346         sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
347
348         LASSERT(dev->od_fsops);
349         osd_push_ctxt(dev, &new, &save);
350
351         d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
352         if (IS_ERR(d))
353                 GOTO(cleanup, rc = PTR_ERR(d));
354
355         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
356         dev->od_ost_map->om_root = d;
357
358 cleanup:
359         osd_pop_ctxt(dev, &new, &save);
360         if (IS_ERR(d)) {
361                 OBD_FREE_PTR(dev->od_ost_map);
362                 RETURN(PTR_ERR(d));
363         }
364
365         RETURN(rc);
366 }
367
368 static void osd_seq_free(struct osd_obj_map *map,
369                          struct osd_obj_seq *osd_seq)
370 {
371         int j;
372
373         cfs_list_del_init(&osd_seq->oos_seq_list);
374
375         if (osd_seq->oos_dirs) {
376                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
377                         if (osd_seq->oos_dirs[j])
378                                 dput(osd_seq->oos_dirs[j]);
379                 }
380                 OBD_FREE(osd_seq->oos_dirs,
381                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
382         }
383
384         if (osd_seq->oos_root)
385                 dput(osd_seq->oos_root);
386
387         OBD_FREE_PTR(osd_seq);
388 }
389
390 static void osd_ost_fini(struct osd_device *osd)
391 {
392         struct osd_obj_seq    *osd_seq;
393         struct osd_obj_seq    *tmp;
394         struct osd_obj_map    *map = osd->od_ost_map;
395         ENTRY;
396
397         if (map == NULL)
398                 return;
399
400         write_lock(&map->om_seq_list_lock);
401         cfs_list_for_each_entry_safe(osd_seq, tmp,
402                                      &map->om_seq_list,
403                                      oos_seq_list) {
404                 osd_seq_free(map, osd_seq);
405         }
406         write_unlock(&map->om_seq_list_lock);
407         if (map->om_root)
408                 dput(map->om_root);
409         OBD_FREE_PTR(map);
410         osd->od_ost_map = NULL;
411         EXIT;
412 }
413
414 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
415 {
416         int rc;
417         ENTRY;
418
419         /* prepare structures for OST */
420         rc = osd_ost_init(dev);
421         if (rc)
422                 RETURN(rc);
423
424         /* prepare structures for MDS */
425         rc = osd_mdt_init(env, dev);
426
427         RETURN(rc);
428 }
429
430 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
431 {
432         struct osd_obj_seq *osd_seq;
433
434         cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
435                 if (osd_seq->oos_seq == seq)
436                         return osd_seq;
437         }
438         return NULL;
439 }
440
441 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
442 {
443         struct osd_obj_seq *osd_seq;
444
445         read_lock(&map->om_seq_list_lock);
446         osd_seq = osd_seq_find_locked(map, seq);
447         read_unlock(&map->om_seq_list_lock);
448         return osd_seq;
449 }
450
451 void osd_obj_map_fini(struct osd_device *dev)
452 {
453         osd_ost_fini(dev);
454         osd_mdt_fini(dev);
455 }
456
457 static int osd_obj_del_entry(struct osd_thread_info *info,
458                              struct osd_device *osd,
459                              struct dentry *dird, char *name,
460                              struct thandle *th)
461 {
462         struct ldiskfs_dir_entry_2 *de;
463         struct buffer_head         *bh;
464         struct osd_thandle         *oh;
465         struct dentry              *child;
466         struct inode               *dir = dird->d_inode;
467         int                         rc;
468         ENTRY;
469
470         oh = container_of(th, struct osd_thandle, ot_super);
471         LASSERT(oh->ot_handle != NULL);
472         LASSERT(oh->ot_handle->h_transaction != NULL);
473
474
475         child = &info->oti_child_dentry;
476         child->d_name.hash = 0;
477         child->d_name.name = name;
478         child->d_name.len = strlen(name);
479         child->d_parent = dird;
480         child->d_inode = NULL;
481
482         ll_vfs_dq_init(dir);
483         mutex_lock(&dir->i_mutex);
484         rc = -ENOENT;
485         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
486         if (bh) {
487                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
488                 brelse(bh);
489         }
490         mutex_unlock(&dir->i_mutex);
491
492         RETURN(rc);
493 }
494
495 int osd_obj_add_entry(struct osd_thread_info *info,
496                       struct osd_device *osd,
497                       struct dentry *dir, char *name,
498                       const struct osd_inode_id *id,
499                       struct thandle *th)
500 {
501         struct osd_thandle *oh;
502         struct dentry *child;
503         struct inode *inode;
504         int rc;
505
506         ENTRY;
507
508         oh = container_of(th, struct osd_thandle, ot_super);
509         LASSERT(oh->ot_handle != NULL);
510         LASSERT(oh->ot_handle->h_transaction != NULL);
511
512         inode = &info->oti_inode;
513         inode->i_sb = osd_sb(osd);
514         osd_id_to_inode(inode, id);
515         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
516
517         child = &info->oti_child_dentry;
518         child->d_name.hash = 0;
519         child->d_name.name = name;
520         child->d_name.len = strlen(name);
521         child->d_parent = dir;
522         child->d_inode = inode;
523
524         ll_vfs_dq_init(dir->d_inode);
525         mutex_lock(&dir->d_inode->i_mutex);
526         rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
527         mutex_unlock(&dir->d_inode->i_mutex);
528
529         RETURN(rc);
530 }
531
532 /**
533  * Use LPU64 for legacy OST sequences, but use LPX64i for new
534  * sequences names, so that the O/{seq}/dN/{oid} more closely
535  * follows the DFID/PFID format. This makes it easier to map from
536  * debug messages to objects in the future, and the legacy space
537  * of FID_SEQ_OST_MDT0 will be unused in the future.
538  **/
539 static inline void osd_seq_name(char *seq_name, obd_seq seq)
540 {
541         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
542                            fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
543                 fid_seq_is_idif(seq) ? 0 : seq);
544 }
545
546 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
547 {
548         sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
549                 fid_seq_is_mdt0(fid_seq(fid)) ||
550                 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
551 }
552
553 /* external locking is required */
554 static int osd_seq_load_locked(struct osd_device *osd,
555                                struct osd_obj_seq *osd_seq)
556 {
557         struct osd_obj_map  *map = osd->od_ost_map;
558         struct dentry       *seq_dir;
559         int                 rc = 0;
560         int                 i;
561         char                seq_name[32];
562         ENTRY;
563
564         if (osd_seq->oos_root != NULL)
565                 RETURN(0);
566
567         LASSERT(map);
568         LASSERT(map->om_root);
569
570         osd_seq_name(seq_name, osd_seq->oos_seq);
571
572         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
573         if (IS_ERR(seq_dir))
574                 GOTO(out_err, rc = PTR_ERR(seq_dir));
575         else if (seq_dir->d_inode == NULL)
576                 GOTO(out_put, rc = -EFAULT);
577
578         ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
579         osd_seq->oos_root = seq_dir;
580
581         LASSERT(osd_seq->oos_dirs == NULL);
582         OBD_ALLOC(osd_seq->oos_dirs,
583                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
584         if (osd_seq->oos_dirs == NULL)
585                 GOTO(out_put, rc = -ENOMEM);
586
587         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
588                 struct dentry   *dir;
589                 char         name[32];
590
591                 sprintf(name, "d%u", i);
592                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
593                                    0700, 1);
594                 if (IS_ERR(dir)) {
595                         rc = PTR_ERR(dir);
596                 } else if (dir->d_inode) {
597                         ldiskfs_set_inode_state(dir->d_inode,
598                                                 LDISKFS_STATE_LUSTRE_NO_OI);
599                         osd_seq->oos_dirs[i] = dir;
600                         rc = 0;
601                 } else {
602                         LBUG();
603                 }
604         }
605
606         if (rc != 0)
607                 osd_seq_free(map, osd_seq);
608 out_put:
609         if (rc != 0) {
610                 dput(seq_dir);
611                 osd_seq->oos_root = NULL;
612         }
613 out_err:
614         RETURN(rc);
615 }
616
617 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
618 {
619         struct osd_obj_map      *map;
620         struct osd_obj_seq      *osd_seq;
621         int                     rc = 0;
622         ENTRY;
623
624         map = osd->od_ost_map;
625         LASSERT(map);
626         LASSERT(map->om_root);
627
628         osd_seq = osd_seq_find(map, seq);
629         if (likely(osd_seq != NULL))
630                 RETURN(osd_seq);
631
632         /* Serializing init process */
633         down(&map->om_dir_init_sem);
634
635         /* Check whether the seq has been added */
636         read_lock(&map->om_seq_list_lock);
637         osd_seq = osd_seq_find_locked(map, seq);
638         if (osd_seq != NULL) {
639                 read_unlock(&map->om_seq_list_lock);
640                 GOTO(cleanup, rc = 0);
641         }
642         read_unlock(&map->om_seq_list_lock);
643
644         OBD_ALLOC_PTR(osd_seq);
645         if (osd_seq == NULL)
646                 GOTO(cleanup, rc = -ENOMEM);
647
648         CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
649         osd_seq->oos_seq = seq;
650         /* Init subdir count to be 32, but each seq can have
651          * different subdir count */
652         osd_seq->oos_subdir_count = map->om_subdir_count;
653         rc = osd_seq_load_locked(osd, osd_seq);
654         if (rc != 0)
655                 GOTO(cleanup, rc);
656
657         write_lock(&map->om_seq_list_lock);
658         cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
659         write_unlock(&map->om_seq_list_lock);
660
661 cleanup:
662         up(&map->om_dir_init_sem);
663         if (rc != 0) {
664                 if (osd_seq != NULL)
665                         OBD_FREE_PTR(osd_seq);
666                 RETURN(ERR_PTR(rc));
667         }
668
669         RETURN(osd_seq);
670 }
671
672 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
673                        const struct lu_fid *fid, struct osd_inode_id *id)
674 {
675         struct osd_obj_map              *map;
676         struct osd_obj_seq              *osd_seq;
677         struct dentry                   *d_seq;
678         struct dentry                   *child;
679         struct ost_id                   *ostid = &info->oti_ostid;
680         int                             dirn;
681         char                            name[32];
682         struct ldiskfs_dir_entry_2      *de;
683         struct buffer_head              *bh;
684         struct inode                    *dir;
685         struct inode                    *inode;
686         ENTRY;
687
688         /* on the very first lookup we find and open directories */
689
690         map = dev->od_ost_map;
691         LASSERT(map);
692         LASSERT(map->om_root);
693
694         fid_ostid_pack(fid, ostid);
695         osd_seq = osd_seq_load(dev, ostid->oi_seq);
696         if (IS_ERR(osd_seq))
697                 RETURN(PTR_ERR(osd_seq));
698
699         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
700         d_seq = osd_seq->oos_dirs[dirn];
701         LASSERT(d_seq);
702
703         osd_oid_name(name, fid, ostid->oi_id);
704
705         child = &info->oti_child_dentry;
706         child->d_parent = d_seq;
707         child->d_name.hash = 0;
708         child->d_name.name = name;
709         /* XXX: we can use rc from sprintf() instead of strlen() */
710         child->d_name.len = strlen(name);
711
712         dir = d_seq->d_inode;
713         mutex_lock(&dir->i_mutex);
714         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
715         mutex_unlock(&dir->i_mutex);
716
717         if (bh == NULL)
718                 RETURN(-ENOENT);
719
720         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
721         brelse(bh);
722
723         inode = osd_iget(info, dev, id);
724         if (IS_ERR(inode))
725                 RETURN(PTR_ERR(inode));
726
727         iput(inode);
728         RETURN(0);
729 }
730
731 int osd_obj_map_insert(struct osd_thread_info *info,
732                        struct osd_device *osd,
733                        const struct lu_fid *fid,
734                        const struct osd_inode_id *id,
735                        struct thandle *th)
736 {
737         struct osd_obj_map      *map;
738         struct osd_obj_seq      *osd_seq;
739         struct dentry           *d;
740         struct ost_id           *ostid = &info->oti_ostid;
741         int                     dirn, rc = 0;
742         char                    name[32];
743         ENTRY;
744
745         map = osd->od_ost_map;
746         LASSERT(map);
747
748         /* map fid to seq:objid */
749         fid_ostid_pack(fid, ostid);
750
751         osd_seq = osd_seq_load(osd, ostid->oi_seq);
752         if (IS_ERR(osd_seq))
753                 RETURN(PTR_ERR(osd_seq));
754
755         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
756         d = osd_seq->oos_dirs[dirn];
757         LASSERT(d);
758
759         osd_oid_name(name, fid, ostid->oi_id);
760         rc = osd_obj_add_entry(info, osd, d, name, id, th);
761
762         RETURN(rc);
763 }
764
765 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
766                        const struct lu_fid *fid, struct thandle *th)
767 {
768         struct osd_obj_map      *map;
769         struct osd_obj_seq      *osd_seq;
770         struct dentry           *d;
771         struct ost_id           *ostid = &info->oti_ostid;
772         int                     dirn, rc = 0;
773         char                    name[32];
774         ENTRY;
775
776         map = osd->od_ost_map;
777         LASSERT(map);
778
779         /* map fid to seq:objid */
780         fid_ostid_pack(fid, ostid);
781
782         osd_seq = osd_seq_load(osd, ostid->oi_seq);
783         if (IS_ERR(osd_seq))
784                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
785
786         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
787         d = osd_seq->oos_dirs[dirn];
788         LASSERT(d);
789
790         osd_oid_name(name, fid, ostid->oi_id);
791         rc = osd_obj_del_entry(info, osd, d, name, th);
792 cleanup:
793         RETURN(rc);
794 }
795
796 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
797                         const struct lu_fid *fid,
798                         const struct osd_inode_id *id,
799                         struct thandle *th)
800 {
801         struct osd_obj_map      *map = osd->od_ost_map;
802         struct dentry           *root = osd_sb(osd)->s_root;
803         char                    *name;
804         int                     rc = 0;
805         ENTRY;
806
807         if (fid_is_last_id(fid)) {
808                 struct osd_obj_seq      *osd_seq;
809
810                 /* on creation of LAST_ID we create O/<seq> hierarchy */
811                 LASSERT(map);
812                 osd_seq = osd_seq_load(osd, fid_seq(fid));
813                 if (IS_ERR(osd_seq))
814                         RETURN(PTR_ERR(osd_seq));
815                 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
816                                        "LAST_ID", id, th);
817         } else {
818                 name = osd_lf_fid2name(fid);
819                 if (name == NULL)
820                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
821                 else if (name[0])
822                         rc = osd_obj_add_entry(info, osd, root, name, id, th);
823         }
824
825         RETURN(rc);
826 }
827
828 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
829                         const struct lu_fid *fid, struct osd_inode_id *id)
830 {
831         struct dentry   *root;
832         struct dentry   *dentry;
833         struct inode    *inode;
834         char            *name;
835         int             rc = -ENOENT;
836         ENTRY;
837
838         if (fid_is_last_id(fid)) {
839                 struct osd_obj_seq *osd_seq;
840
841                 osd_seq = osd_seq_load(osd, fid_seq(fid));
842                 if (IS_ERR(osd_seq))
843                         RETURN(PTR_ERR(osd_seq));
844                 root = osd_seq->oos_root;
845                 name = "LAST_ID";
846         } else {
847                 root = osd_sb(osd)->s_root;
848                 name = osd_lf_fid2name(fid);
849                 if (name == NULL || strlen(name) == 0)
850                         RETURN(-ENOENT);
851         }
852
853         dentry = ll_lookup_one_len(name, root, strlen(name));
854         if (!IS_ERR(dentry)) {
855                 inode = dentry->d_inode;
856                 if (inode) {
857                         if (is_bad_inode(inode)) {
858                                 rc = -EIO;
859                         } else {
860                                 osd_id_gen(id, inode->i_ino,
861                                            inode->i_generation);
862                                 rc = 0;
863                         }
864                 }
865                 /* if dentry is accessible after osd_compat_spec_insert it
866                  * will still contain NULL inode, so don't keep it in cache */
867                 d_invalidate(dentry);
868                 dput(dentry);
869         }
870
871         RETURN(rc);
872 }