Whamcloud - gitweb
LU-3420 scrub: trigger OI scrub properly
[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 int osd_lookup_in_remote_parent(struct osd_thread_info *oti,
323                                 struct osd_device *osd,
324                                 const struct lu_fid *fid,
325                                 struct osd_inode_id *id)
326 {
327         struct osd_mdobj_map        *omm = osd->od_mdt_map;
328         char                        *name = oti->oti_name;
329         struct dentry               *parent;
330         struct dentry               *dentry;
331         struct ldiskfs_dir_entry_2 *de;
332         struct buffer_head         *bh;
333         int                         rc;
334         ENTRY;
335
336         parent = omm->omm_remote_parent;
337         sprintf(name, DFID_NOBRACE, PFID(fid));
338         dentry = osd_child_dentry_by_inode(oti->oti_env, parent->d_inode,
339                                            name, strlen(name));
340         mutex_lock(&parent->d_inode->i_mutex);
341         bh = osd_ldiskfs_find_entry(parent->d_inode, dentry, &de, NULL);
342         if (bh == NULL) {
343                 rc = -ENOENT;
344         } else {
345                 rc = 0;
346                 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
347                 brelse(bh);
348         }
349         mutex_unlock(&parent->d_inode->i_mutex);
350         if (rc == 0)
351                 osd_add_oi_cache(oti, osd, id, fid);
352         RETURN(rc);
353 }
354
355 /*
356  * directory structure on legacy OST:
357  *
358  * O/<seq>/d0-31/<objid>
359  * O/<seq>/LAST_ID
360  * last_rcvd
361  * LAST_GROUP
362  * CONFIGS
363  *
364  */
365 static int osd_ost_init(struct osd_device *dev)
366 {
367         struct lvfs_run_ctxt  new;
368         struct lvfs_run_ctxt  save;
369         struct dentry        *rootd = osd_sb(dev)->s_root;
370         struct dentry        *d;
371         int                   rc;
372         ENTRY;
373
374         OBD_ALLOC_PTR(dev->od_ost_map);
375         if (dev->od_ost_map == NULL)
376                 RETURN(-ENOMEM);
377
378         /* to get subdir count from last_rcvd */
379         rc = osd_last_rcvd_subdir_count(dev);
380         if (rc < 0) {
381                 OBD_FREE_PTR(dev->od_ost_map);
382                 RETURN(rc);
383         }
384
385         dev->od_ost_map->om_subdir_count = rc;
386         rc = 0;
387
388         CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
389         rwlock_init(&dev->od_ost_map->om_seq_list_lock);
390         sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
391
392         LASSERT(dev->od_fsops);
393         osd_push_ctxt(dev, &new, &save);
394
395         d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
396         if (IS_ERR(d))
397                 GOTO(cleanup, rc = PTR_ERR(d));
398
399         ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
400         dev->od_ost_map->om_root = d;
401
402 cleanup:
403         osd_pop_ctxt(dev, &new, &save);
404         if (IS_ERR(d)) {
405                 OBD_FREE_PTR(dev->od_ost_map);
406                 RETURN(PTR_ERR(d));
407         }
408
409         RETURN(rc);
410 }
411
412 static void osd_seq_free(struct osd_obj_map *map,
413                          struct osd_obj_seq *osd_seq)
414 {
415         int j;
416
417         cfs_list_del_init(&osd_seq->oos_seq_list);
418
419         if (osd_seq->oos_dirs) {
420                 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
421                         if (osd_seq->oos_dirs[j])
422                                 dput(osd_seq->oos_dirs[j]);
423                 }
424                 OBD_FREE(osd_seq->oos_dirs,
425                          sizeof(struct dentry *) * osd_seq->oos_subdir_count);
426         }
427
428         if (osd_seq->oos_root)
429                 dput(osd_seq->oos_root);
430
431         OBD_FREE_PTR(osd_seq);
432 }
433
434 static void osd_ost_fini(struct osd_device *osd)
435 {
436         struct osd_obj_seq    *osd_seq;
437         struct osd_obj_seq    *tmp;
438         struct osd_obj_map    *map = osd->od_ost_map;
439         ENTRY;
440
441         if (map == NULL)
442                 return;
443
444         write_lock(&map->om_seq_list_lock);
445         cfs_list_for_each_entry_safe(osd_seq, tmp,
446                                      &map->om_seq_list,
447                                      oos_seq_list) {
448                 osd_seq_free(map, osd_seq);
449         }
450         write_unlock(&map->om_seq_list_lock);
451         if (map->om_root)
452                 dput(map->om_root);
453         OBD_FREE_PTR(map);
454         osd->od_ost_map = NULL;
455         EXIT;
456 }
457
458 int osd_obj_map_init(const struct lu_env *env, struct osd_device *dev)
459 {
460         int rc;
461         ENTRY;
462
463         /* prepare structures for OST */
464         rc = osd_ost_init(dev);
465         if (rc)
466                 RETURN(rc);
467
468         /* prepare structures for MDS */
469         rc = osd_mdt_init(env, dev);
470
471         RETURN(rc);
472 }
473
474 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
475 {
476         struct osd_obj_seq *osd_seq;
477
478         cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
479                 if (osd_seq->oos_seq == seq)
480                         return osd_seq;
481         }
482         return NULL;
483 }
484
485 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
486 {
487         struct osd_obj_seq *osd_seq;
488
489         read_lock(&map->om_seq_list_lock);
490         osd_seq = osd_seq_find_locked(map, seq);
491         read_unlock(&map->om_seq_list_lock);
492         return osd_seq;
493 }
494
495 void osd_obj_map_fini(struct osd_device *dev)
496 {
497         osd_ost_fini(dev);
498         osd_mdt_fini(dev);
499 }
500
501 static int osd_obj_del_entry(struct osd_thread_info *info,
502                              struct osd_device *osd,
503                              struct dentry *dird, char *name,
504                              struct thandle *th)
505 {
506         struct ldiskfs_dir_entry_2 *de;
507         struct buffer_head         *bh;
508         struct osd_thandle         *oh;
509         struct dentry              *child;
510         struct inode               *dir = dird->d_inode;
511         int                         rc;
512         ENTRY;
513
514         oh = container_of(th, struct osd_thandle, ot_super);
515         LASSERT(oh->ot_handle != NULL);
516         LASSERT(oh->ot_handle->h_transaction != NULL);
517
518
519         child = &info->oti_child_dentry;
520         child->d_name.hash = 0;
521         child->d_name.name = name;
522         child->d_name.len = strlen(name);
523         child->d_parent = dird;
524         child->d_inode = NULL;
525
526         ll_vfs_dq_init(dir);
527         mutex_lock(&dir->i_mutex);
528         rc = -ENOENT;
529         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
530         if (bh) {
531                 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
532                 brelse(bh);
533         }
534         mutex_unlock(&dir->i_mutex);
535
536         RETURN(rc);
537 }
538
539 int osd_obj_add_entry(struct osd_thread_info *info,
540                       struct osd_device *osd,
541                       struct dentry *dir, char *name,
542                       const struct osd_inode_id *id,
543                       struct thandle *th)
544 {
545         struct osd_thandle *oh;
546         struct dentry *child;
547         struct inode *inode;
548         int rc;
549
550         ENTRY;
551
552         oh = container_of(th, struct osd_thandle, ot_super);
553         LASSERT(oh->ot_handle != NULL);
554         LASSERT(oh->ot_handle->h_transaction != NULL);
555
556         inode = &info->oti_inode;
557         inode->i_sb = osd_sb(osd);
558         osd_id_to_inode(inode, id);
559         inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
560
561         child = &info->oti_child_dentry;
562         child->d_name.hash = 0;
563         child->d_name.name = name;
564         child->d_name.len = strlen(name);
565         child->d_parent = dir;
566         child->d_inode = inode;
567
568         ll_vfs_dq_init(dir->d_inode);
569         mutex_lock(&dir->d_inode->i_mutex);
570         rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
571         mutex_unlock(&dir->d_inode->i_mutex);
572
573         RETURN(rc);
574 }
575
576 /**
577  * Use LPU64 for legacy OST sequences, but use LPX64i for new
578  * sequences names, so that the O/{seq}/dN/{oid} more closely
579  * follows the DFID/PFID format. This makes it easier to map from
580  * debug messages to objects in the future, and the legacy space
581  * of FID_SEQ_OST_MDT0 will be unused in the future.
582  **/
583 static inline void osd_seq_name(char *seq_name, size_t name_size, obd_seq seq)
584 {
585         snprintf(seq_name, name_size,
586                  (fid_seq_is_rsvd(seq) ||
587                   fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
588                  fid_seq_is_idif(seq) ? 0 : seq);
589 }
590
591 static inline void osd_oid_name(char *name, size_t name_size,
592                                 const struct lu_fid *fid, obd_id id)
593 {
594         snprintf(name, name_size,
595                  (fid_seq_is_rsvd(fid_seq(fid)) ||
596                   fid_seq_is_mdt0(fid_seq(fid)) ||
597                   fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
598 }
599
600 /* external locking is required */
601 static int osd_seq_load_locked(struct osd_device *osd,
602                                struct osd_obj_seq *osd_seq)
603 {
604         struct osd_obj_map  *map = osd->od_ost_map;
605         struct dentry       *seq_dir;
606         int                 rc = 0;
607         int                 i;
608         char                dir_name[32];
609         ENTRY;
610
611         if (osd_seq->oos_root != NULL)
612                 RETURN(0);
613
614         LASSERT(map);
615         LASSERT(map->om_root);
616
617         osd_seq_name(dir_name, sizeof(dir_name), osd_seq->oos_seq);
618
619         seq_dir = simple_mkdir(map->om_root, osd->od_mnt, dir_name, 0755, 1);
620         if (IS_ERR(seq_dir))
621                 GOTO(out_err, rc = PTR_ERR(seq_dir));
622         else if (seq_dir->d_inode == NULL)
623                 GOTO(out_put, rc = -EFAULT);
624
625         ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
626         osd_seq->oos_root = seq_dir;
627
628         LASSERT(osd_seq->oos_dirs == NULL);
629         OBD_ALLOC(osd_seq->oos_dirs,
630                   sizeof(seq_dir) * osd_seq->oos_subdir_count);
631         if (osd_seq->oos_dirs == NULL)
632                 GOTO(out_put, rc = -ENOMEM);
633
634         for (i = 0; i < osd_seq->oos_subdir_count; i++) {
635                 struct dentry   *dir;
636
637                 snprintf(dir_name, sizeof(dir_name), "d%u", i);
638                 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, dir_name,
639                                    0700, 1);
640                 if (IS_ERR(dir)) {
641                         GOTO(out_free, rc = PTR_ERR(dir));
642                 } else if (dir->d_inode == NULL) {
643                         dput(dir);
644                         GOTO(out_free, rc = -EFAULT);
645                 }
646
647                 ldiskfs_set_inode_state(dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
648                 osd_seq->oos_dirs[i] = dir;
649         }
650
651         if (rc != 0) {
652 out_free:
653                 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
654                         if (osd_seq->oos_dirs[i] != NULL)
655                                 dput(osd_seq->oos_dirs[i]);
656                 }
657                 OBD_FREE(osd_seq->oos_dirs,
658                          sizeof(seq_dir) * osd_seq->oos_subdir_count);
659 out_put:
660                 dput(seq_dir);
661                 osd_seq->oos_root = NULL;
662         }
663 out_err:
664         RETURN(rc);
665 }
666
667 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
668 {
669         struct osd_obj_map      *map;
670         struct osd_obj_seq      *osd_seq;
671         int                     rc = 0;
672         ENTRY;
673
674         map = osd->od_ost_map;
675         LASSERT(map);
676         LASSERT(map->om_root);
677
678         osd_seq = osd_seq_find(map, seq);
679         if (likely(osd_seq != NULL))
680                 RETURN(osd_seq);
681
682         /* Serializing init process */
683         down(&map->om_dir_init_sem);
684
685         /* Check whether the seq has been added */
686         read_lock(&map->om_seq_list_lock);
687         osd_seq = osd_seq_find_locked(map, seq);
688         if (osd_seq != NULL) {
689                 read_unlock(&map->om_seq_list_lock);
690                 GOTO(cleanup, rc = 0);
691         }
692         read_unlock(&map->om_seq_list_lock);
693
694         OBD_ALLOC_PTR(osd_seq);
695         if (osd_seq == NULL)
696                 GOTO(cleanup, rc = -ENOMEM);
697
698         CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
699         osd_seq->oos_seq = seq;
700         /* Init subdir count to be 32, but each seq can have
701          * different subdir count */
702         osd_seq->oos_subdir_count = map->om_subdir_count;
703         rc = osd_seq_load_locked(osd, osd_seq);
704         if (rc != 0)
705                 GOTO(cleanup, rc);
706
707         write_lock(&map->om_seq_list_lock);
708         cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
709         write_unlock(&map->om_seq_list_lock);
710
711 cleanup:
712         up(&map->om_dir_init_sem);
713         if (rc != 0) {
714                 if (osd_seq != NULL)
715                         OBD_FREE_PTR(osd_seq);
716                 RETURN(ERR_PTR(rc));
717         }
718
719         RETURN(osd_seq);
720 }
721
722 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
723                        const struct lu_fid *fid, struct osd_inode_id *id)
724 {
725         struct osd_obj_map              *map;
726         struct osd_obj_seq              *osd_seq;
727         struct dentry                   *d_seq;
728         struct dentry                   *child;
729         struct ost_id                   *ostid = &info->oti_ostid;
730         int                             dirn;
731         char                            name[32];
732         struct ldiskfs_dir_entry_2      *de;
733         struct buffer_head              *bh;
734         struct inode                    *dir;
735         struct inode                    *inode;
736         ENTRY;
737
738         /* on the very first lookup we find and open directories */
739
740         map = dev->od_ost_map;
741         LASSERT(map);
742         LASSERT(map->om_root);
743
744         fid_to_ostid(fid, ostid);
745         osd_seq = osd_seq_load(dev, ostid_seq(ostid));
746         if (IS_ERR(osd_seq))
747                 RETURN(PTR_ERR(osd_seq));
748
749         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
750         d_seq = osd_seq->oos_dirs[dirn];
751         LASSERT(d_seq);
752
753         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
754
755         child = &info->oti_child_dentry;
756         child->d_parent = d_seq;
757         child->d_name.hash = 0;
758         child->d_name.name = name;
759         /* XXX: we can use rc from sprintf() instead of strlen() */
760         child->d_name.len = strlen(name);
761
762         dir = d_seq->d_inode;
763         mutex_lock(&dir->i_mutex);
764         bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
765         mutex_unlock(&dir->i_mutex);
766
767         if (bh == NULL)
768                 RETURN(-ENOENT);
769
770         osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
771         brelse(bh);
772
773         inode = osd_iget(info, dev, id);
774         if (IS_ERR(inode))
775                 RETURN(PTR_ERR(inode));
776
777         iput(inode);
778         RETURN(0);
779 }
780
781 int osd_obj_map_insert(struct osd_thread_info *info,
782                        struct osd_device *osd,
783                        const struct lu_fid *fid,
784                        const struct osd_inode_id *id,
785                        struct thandle *th)
786 {
787         struct osd_obj_map      *map;
788         struct osd_obj_seq      *osd_seq;
789         struct dentry           *d;
790         struct ost_id           *ostid = &info->oti_ostid;
791         int                     dirn, rc = 0;
792         char                    name[32];
793         ENTRY;
794
795         map = osd->od_ost_map;
796         LASSERT(map);
797
798         /* map fid to seq:objid */
799         fid_to_ostid(fid, ostid);
800
801         osd_seq = osd_seq_load(osd, ostid_seq(ostid));
802         if (IS_ERR(osd_seq))
803                 RETURN(PTR_ERR(osd_seq));
804
805         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
806         d = osd_seq->oos_dirs[dirn];
807         LASSERT(d);
808
809         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
810         rc = osd_obj_add_entry(info, osd, d, name, id, th);
811
812         RETURN(rc);
813 }
814
815 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
816                        const struct lu_fid *fid, struct thandle *th)
817 {
818         struct osd_obj_map      *map;
819         struct osd_obj_seq      *osd_seq;
820         struct dentry           *d;
821         struct ost_id           *ostid = &info->oti_ostid;
822         int                     dirn, rc = 0;
823         char                    name[32];
824         ENTRY;
825
826         map = osd->od_ost_map;
827         LASSERT(map);
828
829         /* map fid to seq:objid */
830         fid_to_ostid(fid, ostid);
831
832         osd_seq = osd_seq_load(osd, ostid_seq(ostid));
833         if (IS_ERR(osd_seq))
834                 GOTO(cleanup, rc = PTR_ERR(osd_seq));
835
836         dirn = ostid_id(ostid) & (osd_seq->oos_subdir_count - 1);
837         d = osd_seq->oos_dirs[dirn];
838         LASSERT(d);
839
840         osd_oid_name(name, sizeof(name), fid, ostid_id(ostid));
841         rc = osd_obj_del_entry(info, osd, d, name, th);
842 cleanup:
843         RETURN(rc);
844 }
845
846 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
847                         const struct lu_fid *fid,
848                         const struct osd_inode_id *id,
849                         struct thandle *th)
850 {
851         struct osd_obj_map      *map = osd->od_ost_map;
852         struct dentry           *root = osd_sb(osd)->s_root;
853         char                    *name;
854         int                     rc = 0;
855         ENTRY;
856
857         if (fid_is_last_id(fid)) {
858                 struct osd_obj_seq      *osd_seq;
859
860                 /* on creation of LAST_ID we create O/<seq> hierarchy */
861                 LASSERT(map);
862                 osd_seq = osd_seq_load(osd, fid_seq(fid));
863                 if (IS_ERR(osd_seq))
864                         RETURN(PTR_ERR(osd_seq));
865                 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
866                                        "LAST_ID", id, th);
867         } else {
868                 name = osd_lf_fid2name(fid);
869                 if (name == NULL)
870                         CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
871                 else if (name[0])
872                         rc = osd_obj_add_entry(info, osd, root, name, id, th);
873         }
874
875         RETURN(rc);
876 }
877
878 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
879                         const struct lu_fid *fid, struct osd_inode_id *id)
880 {
881         struct dentry   *root;
882         struct dentry   *dentry;
883         struct inode    *inode;
884         char            *name;
885         int             rc = -ENOENT;
886         ENTRY;
887
888         if (fid_is_last_id(fid)) {
889                 struct osd_obj_seq *osd_seq;
890
891                 osd_seq = osd_seq_load(osd, fid_seq(fid));
892                 if (IS_ERR(osd_seq))
893                         RETURN(PTR_ERR(osd_seq));
894                 root = osd_seq->oos_root;
895                 name = "LAST_ID";
896         } else {
897                 root = osd_sb(osd)->s_root;
898                 name = osd_lf_fid2name(fid);
899                 if (name == NULL || strlen(name) == 0)
900                         RETURN(-ENOENT);
901         }
902
903         dentry = ll_lookup_one_len(name, root, strlen(name));
904         if (!IS_ERR(dentry)) {
905                 inode = dentry->d_inode;
906                 if (inode) {
907                         if (is_bad_inode(inode)) {
908                                 rc = -EIO;
909                         } else {
910                                 osd_id_gen(id, inode->i_ino,
911                                            inode->i_generation);
912                                 rc = 0;
913                         }
914                 }
915                 /* if dentry is accessible after osd_compat_spec_insert it
916                  * will still contain NULL inode, so don't keep it in cache */
917                 d_invalidate(dentry);
918                 dput(dentry);
919         }
920
921         RETURN(rc);
922 }