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