4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osd/osd_compat.c
38 * on-disk compatibility stuff for OST
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
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 */
51 * struct OBD_{ALLOC,FREE}*()
54 #include <obd_support.h>
57 #include "osd_internal.h"
60 struct osd_compat_objid_seq {
61 /* protects on-fly initialization */
62 cfs_semaphore_t dir_init_sem;
63 /* file storing last created objid */
64 struct osd_inode_id last_id;
65 struct dentry *groot; /* O/<seq> */
66 struct dentry **dirs; /* O/<seq>/d0-dXX */
69 #define MAX_OBJID_GROUP (FID_SEQ_ECHO + 1)
71 struct osd_compat_objid {
74 struct osd_inode_id last_rcvd_id;
75 struct osd_inode_id last_seq_id;
76 struct osd_compat_objid_seq groups[MAX_OBJID_GROUP];
79 static void osd_push_ctxt(const struct osd_device *dev,
80 struct lvfs_run_ctxt *newctxt,
81 struct lvfs_run_ctxt *save)
83 OBD_SET_CTXT_MAGIC(newctxt);
84 newctxt->pwdmnt = dev->od_mnt;
85 newctxt->pwd = dev->od_mnt->mnt_root;
86 newctxt->fs = get_ds();
88 push_ctxt(save, newctxt, NULL);
91 void osd_compat_seq_fini(struct osd_device *osd, int seq)
93 struct osd_compat_objid_seq *grp;
94 struct osd_compat_objid *map = osd->od_ost_map;
99 grp = &map->groups[seq];
100 if (grp->groot ==NULL)
104 for (i = 0; i < map->subdir_count; i++) {
105 if (grp->dirs[i] == NULL)
110 OBD_FREE(grp->dirs, sizeof(struct dentry *) * map->subdir_count);
115 int osd_compat_seq_init(struct osd_device *osd, int seq)
117 struct osd_compat_objid_seq *grp;
118 struct osd_compat_objid *map;
125 map = osd->od_ost_map;
128 grp = &map->groups[seq];
130 if (grp->groot != NULL)
133 cfs_down(&grp->dir_init_sem);
135 sprintf(name, "%d", seq);
136 d = simple_mkdir(map->root, osd->od_mnt, name, 0755, 1);
140 } else if (d->d_inode == NULL) {
146 LASSERT(grp->dirs == NULL);
147 OBD_ALLOC(grp->dirs, sizeof(d) * map->subdir_count);
148 if (grp->dirs == NULL) {
150 GOTO(out, rc = -ENOMEM);
154 for (i = 0; i < map->subdir_count; i++) {
155 sprintf(name, "d%d", i);
156 d = simple_mkdir(grp->groot, osd->od_mnt, name, 0755, 1);
160 } else if (d->d_inode == NULL) {
170 osd_compat_seq_fini(osd, seq);
172 cfs_up(&grp->dir_init_sem);
176 int osd_last_rcvd_subdir_count(struct osd_device *osd)
178 struct lr_server_data lsd;
179 struct dentry *dlast;
182 int count = FILTER_SUBDIR_COUNT;
186 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
189 return PTR_ERR(dlast);
190 else if (dlast->d_inode == NULL)
194 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
195 if (rc == sizeof(lsd)) {
196 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
197 "subdir count = %d\n", lsd.lsd_uuid,
198 lsd.lsd_subdir_count);
199 if (le16_to_cpu(lsd.lsd_subdir_count) > 0)
200 count = le16_to_cpu(lsd.lsd_subdir_count);
201 } else if (rc != 0) {
202 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
214 void osd_compat_fini(struct osd_device *dev)
220 if (dev->od_ost_map == NULL)
223 for (i = 0; i < MAX_OBJID_GROUP; i++)
224 osd_compat_seq_fini(dev, i);
226 dput(dev->od_ost_map->root);
227 OBD_FREE_PTR(dev->od_ost_map);
228 dev->od_ost_map = NULL;
234 * directory structure on legacy OST:
236 * O/<seq>/d0-31/<objid>
243 int osd_compat_init(struct osd_device *dev)
245 struct lvfs_run_ctxt new;
246 struct lvfs_run_ctxt save;
247 struct dentry *rootd = osd_sb(dev)->s_root;
254 OBD_ALLOC_PTR(dev->od_ost_map);
255 if (dev->od_ost_map == NULL)
258 /* to get subdir count from last_rcvd */
259 rc = osd_last_rcvd_subdir_count(dev);
261 OBD_FREE_PTR(dev->od_ost_map);
265 dev->od_ost_map->subdir_count = rc;
268 LASSERT(dev->od_fsops);
269 osd_push_ctxt(dev, &new, &save);
271 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
272 pop_ctxt(&save, &new, NULL);
274 OBD_FREE_PTR(dev->od_ost_map);
278 dev->od_ost_map->root = d;
280 /* Initialize all groups */
281 for (i = 0; i < MAX_OBJID_GROUP; i++) {
282 cfs_sema_init(&dev->od_ost_map->groups[i].dir_init_sem, 1);
283 rc = osd_compat_seq_init(dev, i);
285 osd_compat_fini(dev);
293 int osd_compat_del_entry(struct osd_thread_info *info, struct osd_device *osd,
294 struct dentry *dird, char *name, struct thandle *th)
296 struct ldiskfs_dir_entry_2 *de;
297 struct buffer_head *bh;
298 struct osd_thandle *oh;
299 struct dentry *child;
300 struct inode *dir = dird->d_inode;
305 oh = container_of(th, struct osd_thandle, ot_super);
306 LASSERT(oh->ot_handle != NULL);
307 LASSERT(oh->ot_handle->h_transaction != NULL);
310 child = &info->oti_child_dentry;
311 child->d_name.hash = 0;
312 child->d_name.name = name;
313 child->d_name.len = strlen(name);
314 child->d_parent = dird;
315 child->d_inode = NULL;
317 mutex_lock(&dir->i_mutex);
319 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
321 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
324 mutex_unlock(&dir->i_mutex);
329 int osd_compat_add_entry(struct osd_thread_info *info, struct osd_device *osd,
330 struct dentry *dir, char *name,
331 const struct osd_inode_id *id, struct thandle *th)
333 struct osd_thandle *oh;
334 struct dentry *child;
340 oh = container_of(th, struct osd_thandle, ot_super);
341 LASSERT(oh->ot_handle != NULL);
342 LASSERT(oh->ot_handle->h_transaction != NULL);
344 inode = &info->oti_inode;
345 inode->i_sb = osd_sb(osd);
346 osd_id_to_inode(inode, id);
348 child = &info->oti_child_dentry;
349 child->d_name.hash = 0;
350 child->d_name.name = name;
351 child->d_name.len = strlen(name);
352 child->d_parent = dir;
353 child->d_inode = inode;
355 mutex_lock(&dir->d_inode->i_mutex);
356 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
357 mutex_unlock(&dir->d_inode->i_mutex);
362 int osd_compat_objid_lookup(struct osd_thread_info *info,
363 struct osd_device *dev, const struct lu_fid *fid,
364 struct osd_inode_id *id)
366 struct osd_compat_objid *map;
368 struct dentry *d_seq;
369 struct ost_id *ostid = &info->oti_ostid;
372 struct ldiskfs_dir_entry_2 *de;
373 struct buffer_head *bh;
378 /* on the very first lookup we find and open directories */
380 map = dev->od_ost_map;
384 fid_ostid_pack(fid, ostid);
385 LASSERT(ostid->oi_seq < MAX_OBJID_GROUP);
386 LASSERT(map->subdir_count > 0);
387 LASSERT(map->groups[ostid->oi_seq].groot);
389 dirn = ostid->oi_id & (map->subdir_count - 1);
390 d = map->groups[ostid->oi_seq].dirs[dirn];
393 sprintf(name, "%llu", ostid->oi_id);
394 d_seq = &info->oti_child_dentry;
396 d_seq->d_name.hash = 0;
397 d_seq->d_name.name = name;
398 /* XXX: we can use rc from sprintf() instead of strlen() */
399 d_seq->d_name.len = strlen(name);
402 mutex_lock(&dir->i_mutex);
403 bh = osd_ldiskfs_find_entry(dir, d_seq, &de, NULL);
404 mutex_unlock(&dir->i_mutex);
409 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
412 inode = osd_iget(info, dev, id);
414 RETURN(PTR_ERR(inode));
420 int osd_compat_objid_insert(struct osd_thread_info *info,
421 struct osd_device *osd,
422 const struct lu_fid *fid,
423 const struct osd_inode_id *id,
426 struct osd_compat_objid *map;
428 struct ost_id *ostid = &info->oti_ostid;
433 map = osd->od_ost_map;
436 LASSERT(map->subdir_count > 0);
437 LASSERT(map->groups[ostid->oi_seq].groot);
439 /* map fid to group:objid */
440 fid_ostid_pack(fid, ostid);
441 dirn = ostid->oi_id & (map->subdir_count - 1);
442 d = map->groups[ostid->oi_seq].dirs[dirn];
445 sprintf(name, "%llu", ostid->oi_id);
446 rc = osd_compat_add_entry(info, osd, d, name, id, th);
451 int osd_compat_objid_delete(struct osd_thread_info *info,
452 struct osd_device *osd,
453 const struct lu_fid *fid, struct thandle *th)
455 struct osd_compat_objid *map;
457 struct ost_id *ostid = &info->oti_ostid;
462 map = osd->od_ost_map;
465 LASSERT(map->subdir_count > 0);
466 LASSERT(map->groups[ostid->oi_seq].groot);
468 /* map fid to group:objid */
469 fid_ostid_pack(fid, ostid);
470 dirn = ostid->oi_id & (map->subdir_count - 1);
471 d = map->groups[ostid->oi_seq].dirs[dirn];
474 sprintf(name, "%llu", ostid->oi_id);
475 rc = osd_compat_del_entry(info, osd, d, name, th);
485 static const struct named_oid oids[] = {
486 { FLD_INDEX_OID, "" /* "fld" */ },
487 { FID_SEQ_CTL_OID, "" /* "seq_ctl" */ },
488 { FID_SEQ_SRV_OID, "" /* "seq_srv" */ },
489 { MDD_ROOT_INDEX_OID, "" /* "ROOT" */ },
490 { MDD_ORPHAN_OID, "" /* "PENDING" */ },
491 { MDD_LOV_OBJ_OID, "" /* LOV_OBJID */ },
492 { MDD_CAPA_KEYS_OID, "" /* CAPA_KEYS */ },
493 { MDT_LAST_RECV_OID, LAST_RCVD },
494 { LFSCK_BOOKMARK_OID, "" /* "lfsck_bookmark" */ },
495 { OTABLE_IT_OID, "" /* "otable iterator" */},
496 { OFD_LAST_RECV_OID, "" /* LAST_RCVD */ },
497 { OFD_LAST_GROUP_OID, "LAST_GROUP" },
498 { LLOG_CATALOGS_OID, "" /* "CATALOGS" */ },
499 { MGS_CONFIGS_OID, "" /* MOUNT_CONFIGS_DIR */ },
500 { OFD_HEALTH_CHECK_OID, HEALTH_CHECK },
504 static char *oid2name(const unsigned long oid)
508 while (oids[i].oid) {
509 if (oids[i].oid == oid)
516 int osd_compat_spec_insert(struct osd_thread_info *info,
517 struct osd_device *osd, const struct lu_fid *fid,
518 const struct osd_inode_id *id, struct thandle *th)
520 struct osd_compat_objid *map = osd->od_ost_map;
521 struct dentry *root = osd_sb(osd)->s_root;
527 if (fid_oid(fid) >= OFD_GROUP0_LAST_OID &&
528 fid_oid(fid) < OFD_GROUP4K_LAST_OID) {
529 /* on creation of LAST_ID we create O/<group> hierarchy */
531 seq = fid_oid(fid) - OFD_GROUP0_LAST_OID;
532 LASSERT(seq < MAX_OBJID_GROUP);
533 LASSERT(map->groups[seq].groot);
535 name = oid2name(fid_oid(fid));
537 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
539 rc = osd_compat_add_entry(info, osd, root, name, id,
546 int osd_compat_spec_lookup(struct osd_thread_info *info,
547 struct osd_device *osd, const struct lu_fid *fid,
548 struct osd_inode_id *id)
550 struct dentry *dentry;
556 name = oid2name(fid_oid(fid));
557 if (name == NULL || strlen(name) == 0)
560 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
561 if (!IS_ERR(dentry)) {
562 inode = dentry->d_inode;
564 if (is_bad_inode(inode)) {
567 osd_id_gen(id, inode->i_ino,
568 inode->i_generation);