Whamcloud - gitweb
LU-1187 osd: add agent/local inode for remote directory
[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
50 /*
51  * struct OBD_{ALLOC,FREE}*()
52  * OBD_FAIL_CHECK
53  */
54 #include <obd_support.h>
55 #include <lvfs.h>
56
57 #include "osd_internal.h"
58 #include "osd_oi.h"
59
60 static void osd_push_ctxt(const struct osd_device *dev,
61                           struct lvfs_run_ctxt *newctxt,
62                           struct lvfs_run_ctxt *save)
63 {
64         OBD_SET_CTXT_MAGIC(newctxt);
65         newctxt->pwdmnt = dev->od_mnt;
66         newctxt->pwd = dev->od_mnt->mnt_root;
67         newctxt->fs = get_ds();
68
69         push_ctxt(save, newctxt, NULL);
70 }
71
72 static void osd_pop_ctxt(const struct osd_device *dev,
73                          struct lvfs_run_ctxt *new,
74                          struct lvfs_run_ctxt *save)
75 {
76         pop_ctxt(save, new, NULL);
77 }
78
79 int osd_last_rcvd_subdir_count(struct osd_device *osd)
80 {
81         struct lr_server_data lsd;
82         struct dentry        *dlast;
83         loff_t                off;
84         int                   rc = 0;
85         int                   count = FILTER_SUBDIR_COUNT;
86
87         ENTRY;
88
89         dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
90                                   strlen(LAST_RCVD));
91         if (IS_ERR(dlast))
92                 return PTR_ERR(dlast);
93         else if (dlast->d_inode == NULL)
94                 goto out;
95
96         off = 0;
97         rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
98         if (rc == sizeof(lsd)) {
99                 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
100                        "subdir count = %d\n", lsd.lsd_uuid,
101                        lsd.lsd_subdir_count);
102                 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
103                         count = le16_to_cpu(lsd.lsd_subdir_count);
104         } else if (rc != 0) {
105                 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
106                 if (rc > 0)
107                         rc = -EFAULT;
108                 dput(dlast);
109                 return rc;
110         }
111 out:
112         dput(dlast);
113         LASSERT(count > 0);
114         return count;
115 }
116
117 /*
118  * directory structure on legacy MDT:
119  *
120  * REM_OBJ_DIR/ per mdt
121  * AGENT_OBJ_DIR/ per mdt
122  *
123  */
124 static const char remote_obj_dir[] = "REM_OBJ_DIR";
125 static const char agent_obj_dir[] = "AGENT_OBJ_DIR";
126 int osd_mdt_init(struct osd_device *dev)
127 {
128         struct lvfs_run_ctxt  new;
129         struct lvfs_run_ctxt  save;
130         struct dentry   *parent;
131         struct osd_mdobj_map *omm;
132         struct dentry   *d;
133         int                rc = 0;
134         ENTRY;
135
136         OBD_ALLOC_PTR(dev->od_mdt_map);
137         if (dev->od_mdt_map == NULL)
138                 RETURN(-ENOMEM);
139
140         omm = dev->od_mdt_map;
141
142         LASSERT(dev->od_fsops);
143
144         parent = osd_sb(dev)->s_root;
145         osd_push_ctxt(dev, &new, &save);
146
147         d = simple_mkdir(parent, dev->od_mnt, agent_obj_dir,
148                          0755, 1);
149         if (IS_ERR(d))
150                 GOTO(cleanup, rc = PTR_ERR(d));
151
152         omm->omm_agent_dentry = d;
153
154 cleanup:
155         pop_ctxt(&save, &new, NULL);
156         if (rc) {
157                 if (omm->omm_agent_dentry != NULL)
158                         dput(omm->omm_agent_dentry);
159                 OBD_FREE_PTR(omm);
160                 dev->od_mdt_map = NULL;
161         }
162         RETURN(rc);
163 }
164
165 static void osd_mdt_fini(struct osd_device *osd)
166 {
167         struct osd_mdobj_map *omm = osd->od_mdt_map;
168
169         if (omm == NULL)
170                 return;
171
172         if (omm->omm_agent_dentry)
173                 dput(omm->omm_agent_dentry);
174
175         OBD_FREE_PTR(omm);
176         osd->od_ost_map = NULL;
177 }
178
179 int osd_create_agent_inode(const struct lu_env *env, struct osd_device *osd,
180                            struct osd_object *obj, struct osd_thandle *oh)
181 {
182         struct osd_mdobj_map    *omm = osd->od_mdt_map;
183         struct osd_thread_info  *oti = osd_oti_get(env);
184         char                    *name_buf = oti->oti_name;
185         struct dentry           *agent;
186         struct dentry           *parent;
187         int                     rc;
188
189         parent = omm->omm_agent_dentry;
190         sprintf(name_buf, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
191         agent = osd_child_dentry_by_inode(env, parent->d_inode,
192                                           name_buf, strlen(name_buf));
193         mutex_lock(&parent->d_inode->i_mutex);
194         rc = osd_ldiskfs_add_entry(oh->ot_handle, agent, obj->oo_inode, NULL);
195         parent->d_inode->i_nlink++;
196         mark_inode_dirty(parent->d_inode);
197         mutex_unlock(&parent->d_inode->i_mutex);
198         if (rc != 0)
199                 CERROR("%s: "DFID" add agent error: rc = %d\n", osd_name(osd),
200                        PFID(lu_object_fid(&obj->oo_dt.do_lu)), rc);
201         RETURN(rc);
202 }
203
204 int osd_delete_agent_inode(const struct lu_env *env, struct osd_device *osd,
205                            struct osd_object *obj, struct osd_thandle *oh)
206 {
207         struct osd_mdobj_map       *omm = osd->od_mdt_map;
208         struct osd_thread_info     *oti = osd_oti_get(env);
209         char                       *name = oti->oti_name;
210         struct dentry              *agent;
211         struct dentry              *parent;
212         struct ldiskfs_dir_entry_2 *de;
213         struct buffer_head         *bh;
214         int                        rc;
215
216         parent = omm->omm_agent_dentry;
217         sprintf(name, DFID, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
218         agent = osd_child_dentry_by_inode(env, parent->d_inode,
219                                           name, strlen(name));
220         mutex_lock(&parent->d_inode->i_mutex);
221         bh = osd_ldiskfs_find_entry(parent->d_inode, agent, &de, NULL);
222         if (bh == NULL) {
223                 mutex_unlock(&parent->d_inode->i_mutex);
224                 RETURN(-ENOENT);
225         }
226         rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
227         parent->d_inode->i_nlink--;
228         mark_inode_dirty(parent->d_inode);
229         mutex_unlock(&parent->d_inode->i_mutex);
230         brelse(bh);
231         RETURN(rc);
232 }
233
234 /*
235  * directory structure on legacy OST:
236  *
237  * O/<seq>/d0-31/<objid>
238  * O/<seq>/LAST_ID
239  * last_rcvd
240  * LAST_GROUP
241  * CONFIGS
242  *
243  */
244 int osd_ost_init(struct osd_device *dev)
245 {
246         struct lvfs_run_ctxt  new;
247         struct lvfs_run_ctxt  save;
248         struct dentry        *rootd = osd_sb(dev)->s_root;
249         struct dentry        *d;
250         int                   rc;
251         ENTRY;
252
253         OBD_ALLOC_PTR(dev->od_ost_map);
254         if (dev->od_ost_map == NULL)
255                 RETURN(-ENOMEM);
256
257         /* to get subdir count from last_rcvd */
258         rc = osd_last_rcvd_subdir_count(dev);
259         if (rc < 0) {
260                 OBD_FREE_PTR(dev->od_ost_map);
261                 RETURN(rc);
262         }
263
264         dev->od_ost_map->om_subdir_count = rc;
265         rc = 0;
266
267         CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
268         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
269         sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
270
271         LASSERT(dev->od_fsops);
272         osd_push_ctxt(dev, &new, &save);
273
274         d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
275         if (IS_ERR(d))
276                 GOTO(cleanup, rc = PTR_ERR(d));
277
278         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
279         dev->od_ost_map->om_root = d;
280
281 cleanup:
282         osd_pop_ctxt(dev, &new, &save);
283         if (IS_ERR(d)) {
284                 OBD_FREE_PTR(dev->od_ost_map);
285                 RETURN(PTR_ERR(d));
286         }
287
288         RETURN(rc);
289 }
290
291 static void osd_seq_free(struct osd_obj_map *map,
292                          struct osd_obj_seq *osd_seq)
293 {
294         int j;
295
296         cfs_list_del_init(&osd_seq->oos_seq_list);
297
298         if (osd_seq->oos_dirs) {
299                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
300                         if (osd_seq->oos_dirs[j])
301                                 dput(osd_seq->oos_dirs[j]);
302                 }
303                 OBD_FREE(osd_seq->oos_dirs,
304                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
305         }
306
307         if (osd_seq->oos_root)
308                 dput(osd_seq->oos_root);
309
310         OBD_FREE_PTR(osd_seq);
311 }
312
313 static void osd_ost_fini(struct osd_device *osd)
314 {
315         struct osd_obj_seq    *osd_seq;
316         struct osd_obj_seq    *tmp;
317         struct osd_obj_map    *map = osd->od_ost_map;
318         ENTRY;
319
320         if (map == NULL)
321                 return;
322
323         write_lock(&map->om_seq_list_lock);
324         cfs_list_for_each_entry_safe(osd_seq, tmp,
325                                      &map->om_seq_list,
326                                      oos_seq_list) {
327                 osd_seq_free(map, osd_seq);
328         }
329         write_unlock(&map->om_seq_list_lock);
330         if (map->om_root)
331                 dput(map->om_root);
332         OBD_FREE_PTR(map);
333         osd->od_ost_map = NULL;
334         EXIT;
335 }
336
337 int osd_obj_map_init(struct osd_device *dev)
338 {
339         int rc;
340         ENTRY;
341
342         /* prepare structures for OST */
343         rc = osd_ost_init(dev);
344         if (rc)
345                 RETURN(rc);
346
347         /* prepare structures for MDS */
348         rc = osd_mdt_init(dev);
349
350         RETURN(rc);
351 }
352
353 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
354 {
355         struct osd_obj_seq *osd_seq;
356
357         cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
358                 if (osd_seq->oos_seq == seq)
359                         return osd_seq;
360         }
361         return NULL;
362 }
363
364 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
365 {
366         struct osd_obj_seq *osd_seq;
367
368         read_lock(&map->om_seq_list_lock);
369         osd_seq = osd_seq_find_locked(map, seq);
370         read_unlock(&map->om_seq_list_lock);
371         return osd_seq;
372 }
373
374 void osd_obj_map_fini(struct osd_device *dev)
375 {
376         osd_ost_fini(dev);
377         osd_mdt_fini(dev);
378 }
379
380 static int osd_obj_del_entry(struct osd_thread_info *info,
381                              struct osd_device *osd,
382                              struct dentry *dird, char *name,
383                              struct thandle *th)
384 {
385         struct ldiskfs_dir_entry_2 *de;
386         struct buffer_head         *bh;
387         struct osd_thandle         *oh;
388         struct dentry              *child;
389         struct inode               *dir = dird->d_inode;
390         int                         rc;
391         ENTRY;
392
393         oh = container_of(th, struct osd_thandle, ot_super);
394         LASSERT(oh->ot_handle != NULL);
395         LASSERT(oh->ot_handle->h_transaction != NULL);
396
397
398         child = &info->oti_child_dentry;
399         child->d_name.hash = 0;
400         child->d_name.name = name;
401         child->d_name.len = strlen(name);
402         child->d_parent = dird;
403         child->d_inode = NULL;
404
405         ll_vfs_dq_init(dir);
406         mutex_lock(&dir->i_mutex);
407         rc = -ENOENT;
408         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
409         if (bh) {
410                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
411                 brelse(bh);
412         }
413         mutex_unlock(&dir->i_mutex);
414
415         RETURN(rc);
416 }
417
418 int osd_obj_add_entry(struct osd_thread_info *info,
419                       struct osd_device *osd,
420                       struct dentry *dir, char *name,
421                       const struct osd_inode_id *id,
422                       struct thandle *th)
423 {
424         struct osd_thandle *oh;
425         struct dentry *child;
426         struct inode *inode;
427         int rc;
428
429         ENTRY;
430
431         oh = container_of(th, struct osd_thandle, ot_super);
432         LASSERT(oh->ot_handle != NULL);
433         LASSERT(oh->ot_handle->h_transaction != NULL);
434
435         inode = &info->oti_inode;
436         inode->i_sb = osd_sb(osd);
437         osd_id_to_inode(inode, id);
438         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
439
440         child = &info->oti_child_dentry;
441         child->d_name.hash = 0;
442         child->d_name.name = name;
443         child->d_name.len = strlen(name);
444         child->d_parent = dir;
445         child->d_inode = inode;
446
447         ll_vfs_dq_init(dir->d_inode);
448         mutex_lock(&dir->d_inode->i_mutex);
449         rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
450         mutex_unlock(&dir->d_inode->i_mutex);
451
452         RETURN(rc);
453 }
454
455 /**
456  * Use LPU64 for legacy OST sequences, but use LPX64i for new
457  * sequences names, so that the O/{seq}/dN/{oid} more closely
458  * follows the DFID/PFID format. This makes it easier to map from
459  * debug messages to objects in the future, and the legacy space
460  * of FID_SEQ_OST_MDT0 will be unused in the future.
461  **/
462 static inline void osd_seq_name(char *seq_name, obd_seq seq)
463 {
464         sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
465                            fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
466                 fid_seq_is_idif(seq) ? 0 : seq);
467 }
468
469 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
470 {
471         sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
472                 fid_seq_is_mdt0(fid_seq(fid)) ||
473                 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
474 }
475
476 /* external locking is required */
477 static int osd_seq_load_locked(struct osd_device *osd,
478                                struct osd_obj_seq *osd_seq)
479 {
480         struct osd_obj_map  *map = osd->od_ost_map;
481         struct dentry       *seq_dir;
482         int                 rc = 0;
483         int                 i;
484         char                seq_name[32];
485         ENTRY;
486
487         if (osd_seq->oos_root != NULL)
488                 RETURN(0);
489
490         LASSERT(map);
491         LASSERT(map->om_root);
492
493         osd_seq_name(seq_name, osd_seq->oos_seq);
494
495         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
496         if (IS_ERR(seq_dir))
497                 GOTO(out_err, rc = PTR_ERR(seq_dir));
498         else if (seq_dir->d_inode == NULL)
499                 GOTO(out_put, rc = -EFAULT);
500
501         ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
502         osd_seq->oos_root = seq_dir;
503
504         LASSERT(osd_seq->oos_dirs == NULL);
505         OBD_ALLOC(osd_seq->oos_dirs,
506                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
507         if (osd_seq->oos_dirs == NULL)
508                 GOTO(out_put, rc = -ENOMEM);
509
510         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
511                 struct dentry   *dir;
512                 char         name[32];
513
514                 sprintf(name, "d%u", i);
515                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
516                                    0700, 1);
517                 if (IS_ERR(dir)) {
518                         rc = PTR_ERR(dir);
519                 } else if (dir->d_inode) {
520                         ldiskfs_set_inode_state(dir->d_inode,
521                                                 LDISKFS_STATE_LUSTRE_NO_OI);
522                         osd_seq->oos_dirs[i] = dir;
523                         rc = 0;
524                 } else {
525                         LBUG();
526                 }
527         }
528
529         if (rc != 0)
530                 osd_seq_free(map, osd_seq);
531 out_put:
532         if (rc != 0) {
533                 dput(seq_dir);
534                 osd_seq->oos_root = NULL;
535         }
536 out_err:
537         RETURN(rc);
538 }
539
540 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
541 {
542         struct osd_obj_map      *map;
543         struct osd_obj_seq      *osd_seq;
544         int                     rc = 0;
545         ENTRY;
546
547         map = osd->od_ost_map;
548         LASSERT(map);
549         LASSERT(map->om_root);
550
551         osd_seq = osd_seq_find(map, seq);
552         if (likely(osd_seq != NULL))
553                 RETURN(osd_seq);
554
555         /* Serializing init process */
556         down(&map->om_dir_init_sem);
557
558         /* Check whether the seq has been added */
559         read_lock(&map->om_seq_list_lock);
560         osd_seq = osd_seq_find_locked(map, seq);
561         if (osd_seq != NULL) {
562                 read_unlock(&map->om_seq_list_lock);
563                 GOTO(cleanup, rc = 0);
564         }
565         read_unlock(&map->om_seq_list_lock);
566
567         OBD_ALLOC_PTR(osd_seq);
568         if (osd_seq == NULL)
569                 GOTO(cleanup, rc = -ENOMEM);
570
571         CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
572         osd_seq->oos_seq = seq;
573         /* Init subdir count to be 32, but each seq can have
574          * different subdir count */
575         osd_seq->oos_subdir_count = map->om_subdir_count;
576         rc = osd_seq_load_locked(osd, osd_seq);
577         if (rc != 0)
578                 GOTO(cleanup, rc);
579
580         write_lock(&map->om_seq_list_lock);
581         cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
582         write_unlock(&map->om_seq_list_lock);
583
584 cleanup:
585         up(&map->om_dir_init_sem);
586         if (rc != 0) {
587                 if (osd_seq != NULL)
588                         OBD_FREE_PTR(osd_seq);
589                 RETURN(ERR_PTR(rc));
590         }
591
592         RETURN(osd_seq);
593 }
594
595 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
596                        const struct lu_fid *fid, struct osd_inode_id *id)
597 {
598         struct osd_obj_map              *map;
599         struct osd_obj_seq              *osd_seq;
600         struct dentry                   *d_seq;
601         struct dentry                   *child;
602         struct ost_id                   *ostid = &info->oti_ostid;
603         int                             dirn;
604         char                            name[32];
605         struct ldiskfs_dir_entry_2      *de;
606         struct buffer_head              *bh;
607         struct inode                    *dir;
608         struct inode                    *inode;
609         ENTRY;
610
611         /* on the very first lookup we find and open directories */
612
613         map = dev->od_ost_map;
614         LASSERT(map);
615         LASSERT(map->om_root);
616
617         fid_ostid_pack(fid, ostid);
618         osd_seq = osd_seq_load(dev, ostid->oi_seq);
619         if (IS_ERR(osd_seq))
620                 RETURN(PTR_ERR(osd_seq));
621
622         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
623         d_seq = osd_seq->oos_dirs[dirn];
624         LASSERT(d_seq);
625
626         osd_oid_name(name, fid, ostid->oi_id);
627
628         child = &info->oti_child_dentry;
629         child->d_parent = d_seq;
630         child->d_name.hash = 0;
631         child->d_name.name = name;
632         /* XXX: we can use rc from sprintf() instead of strlen() */
633         child->d_name.len = strlen(name);
634
635         dir = d_seq->d_inode;
636         mutex_lock(&dir->i_mutex);
637         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
638         mutex_unlock(&dir->i_mutex);
639
640         if (bh == NULL)
641                 RETURN(-ENOENT);
642
643         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
644         brelse(bh);
645
646         inode = osd_iget(info, dev, id);
647         if (IS_ERR(inode))
648                 RETURN(PTR_ERR(inode));
649
650         iput(inode);
651         RETURN(0);
652 }
653
654 int osd_obj_map_insert(struct osd_thread_info *info,
655                        struct osd_device *osd,
656                        const struct lu_fid *fid,
657                        const struct osd_inode_id *id,
658                        struct thandle *th)
659 {
660         struct osd_obj_map      *map;
661         struct osd_obj_seq      *osd_seq;
662         struct dentry           *d;
663         struct ost_id           *ostid = &info->oti_ostid;
664         int                     dirn, rc = 0;
665         char                    name[32];
666         ENTRY;
667
668         map = osd->od_ost_map;
669         LASSERT(map);
670
671         /* map fid to seq:objid */
672         fid_ostid_pack(fid, ostid);
673
674         osd_seq = osd_seq_load(osd, ostid->oi_seq);
675         if (IS_ERR(osd_seq))
676                 RETURN(PTR_ERR(osd_seq));
677
678         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
679         d = osd_seq->oos_dirs[dirn];
680         LASSERT(d);
681
682         osd_oid_name(name, fid, ostid->oi_id);
683         rc = osd_obj_add_entry(info, osd, d, name, id, th);
684
685         RETURN(rc);
686 }
687
688 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
689                        const struct lu_fid *fid, struct thandle *th)
690 {
691         struct osd_obj_map      *map;
692         struct osd_obj_seq      *osd_seq;
693         struct dentry           *d;
694         struct ost_id           *ostid = &info->oti_ostid;
695         int                     dirn, rc = 0;
696         char                    name[32];
697         ENTRY;
698
699         map = osd->od_ost_map;
700         LASSERT(map);
701
702         /* map fid to seq:objid */
703         fid_ostid_pack(fid, ostid);
704
705         osd_seq = osd_seq_load(osd, ostid->oi_seq);
706         if (IS_ERR(osd_seq))
707                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
708
709         dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
710         d = osd_seq->oos_dirs[dirn];
711         LASSERT(d);
712
713         osd_oid_name(name, fid, ostid->oi_id);
714         rc = osd_obj_del_entry(info, osd, d, name, th);
715 cleanup:
716         RETURN(rc);
717 }
718
719 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
720                         const struct lu_fid *fid,
721                         const struct osd_inode_id *id,
722                         struct thandle *th)
723 {
724         struct osd_obj_map      *map = osd->od_ost_map;
725         struct dentry           *root = osd_sb(osd)->s_root;
726         char                    *name;
727         int                     rc = 0;
728         ENTRY;
729
730         if (fid_is_last_id(fid)) {
731                 struct osd_obj_seq      *osd_seq;
732
733                 /* on creation of LAST_ID we create O/<seq> hierarchy */
734                 LASSERT(map);
735                 osd_seq = osd_seq_load(osd, fid_seq(fid));
736                 if (IS_ERR(osd_seq))
737                         RETURN(PTR_ERR(osd_seq));
738                 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
739                                        "LAST_ID", id, th);
740         } else {
741                 name = osd_lf_fid2name(fid);
742                 if (name == NULL)
743                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
744                 else if (name[0])
745                         rc = osd_obj_add_entry(info, osd, root, name, id, th);
746         }
747
748         RETURN(rc);
749 }
750
751 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
752                         const struct lu_fid *fid, struct osd_inode_id *id)
753 {
754         struct dentry   *root;
755         struct dentry *dentry;
756         struct inode  *inode;
757         char          *name;
758         int            rc = -ENOENT;
759         ENTRY;
760
761         if (fid_is_last_id(fid)) {
762                 struct osd_obj_seq *osd_seq;
763
764                 osd_seq = osd_seq_load(osd, fid_seq(fid));
765                 if (IS_ERR(osd_seq))
766                         RETURN(PTR_ERR(osd_seq));
767                 root = osd_seq->oos_root;
768                 name = "LAST_ID";
769         } else {
770                 root = osd_sb(osd)->s_root;
771                 name = osd_lf_fid2name(fid);
772                 if (name == NULL || strlen(name) == 0)
773                         RETURN(-ENOENT);
774         }
775
776         dentry = ll_lookup_one_len(name, root, strlen(name));
777         if (!IS_ERR(dentry)) {
778                 inode = dentry->d_inode;
779                 if (inode) {
780                         if (is_bad_inode(inode)) {
781                                 rc = -EIO;
782                         } else {
783                                 osd_id_gen(id, inode->i_ino,
784                                            inode->i_generation);
785                                 rc = 0;
786                         }
787                 }
788                 /* if dentry is accessible after osd_compat_spec_insert it
789                  * will still contain NULL inode, so don't keep it in cache */
790                 d_invalidate(dentry);
791                 dput(dentry);
792         }
793
794         RETURN(rc);
795 }