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"
59 struct osd_compat_objid_seq {
60 /* protects on-fly initialization */
61 cfs_semaphore_t dir_init_sem;
62 /* file storing last created objid */
63 struct osd_inode_id last_id;
64 struct dentry *groot; /* O/<seq> */
65 struct dentry **dirs; /* O/<seq>/d0-dXX */
68 #define MAX_OBJID_GROUP (FID_SEQ_ECHO + 1)
70 struct osd_compat_objid {
73 struct osd_inode_id last_rcvd_id;
74 struct osd_inode_id last_seq_id;
75 struct osd_compat_objid_seq groups[MAX_OBJID_GROUP];
78 static void osd_push_ctxt(const struct osd_device *dev,
79 struct lvfs_run_ctxt *newctxt,
80 struct lvfs_run_ctxt *save)
82 OBD_SET_CTXT_MAGIC(newctxt);
83 newctxt->pwdmnt = dev->od_mnt;
84 newctxt->pwd = dev->od_mnt->mnt_root;
85 newctxt->fs = get_ds();
87 push_ctxt(save, newctxt, NULL);
90 void osd_compat_seq_fini(struct osd_device *osd, int seq)
92 struct osd_compat_objid_seq *grp;
93 struct osd_compat_objid *map = osd->od_ost_map;
98 grp = &map->groups[seq];
99 if (grp->groot ==NULL)
103 for (i = 0; i < map->subdir_count; i++) {
104 if (grp->dirs[i] == NULL)
109 OBD_FREE(grp->dirs, sizeof(struct dentry *) * map->subdir_count);
114 int osd_compat_seq_init(struct osd_device *osd, int seq)
116 struct osd_compat_objid_seq *grp;
117 struct osd_compat_objid *map;
124 map = osd->od_ost_map;
127 grp = &map->groups[seq];
129 if (grp->groot != NULL)
132 cfs_down(&grp->dir_init_sem);
134 sprintf(name, "%d", seq);
135 d = simple_mkdir(map->root, osd->od_mnt, name, 0755, 1);
139 } else if (d->d_inode == NULL) {
145 LASSERT(grp->dirs == NULL);
146 OBD_ALLOC(grp->dirs, sizeof(d) * map->subdir_count);
147 if (grp->dirs == NULL) {
149 GOTO(out, rc = -ENOMEM);
153 for (i = 0; i < map->subdir_count; i++) {
154 sprintf(name, "d%d", i);
155 d = simple_mkdir(grp->groot, osd->od_mnt, name, 0755, 1);
159 } else if (d->d_inode == NULL) {
169 osd_compat_seq_fini(osd, seq);
171 cfs_up(&grp->dir_init_sem);
175 int osd_last_rcvd_subdir_count(struct osd_device *osd)
177 struct lr_server_data lsd;
178 struct dentry *dlast;
185 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
188 return PTR_ERR(dlast);
189 else if (dlast->d_inode == NULL)
193 rc = osd_ldiskfs_read(dlast->d_inode, &lsd, sizeof(lsd), &off);
194 if (rc == sizeof(lsd)) {
195 CDEBUG(D_INFO, "read last_rcvd header, uuid = %s, "
196 "subdir count = %d\n", lsd.lsd_uuid,
197 lsd.lsd_subdir_count);
198 count = le16_to_cpu(lsd.lsd_subdir_count);
199 } else if (rc != 0) {
200 CERROR("Can't read last_rcvd file, rc = %d\n", rc);
205 count = FILTER_SUBDIR_COUNT;
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 /* to get subdir count from last_rcvd */
255 rc = osd_last_rcvd_subdir_count(dev);
259 dev->od_ost_map->subdir_count = rc;
262 OBD_ALLOC_PTR(dev->od_ost_map);
263 if (dev->od_ost_map == NULL)
266 LASSERT(dev->od_fsops);
267 osd_push_ctxt(dev, &new, &save);
269 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
270 pop_ctxt(&save, &new, NULL);
272 OBD_FREE_PTR(dev->od_ost_map);
276 dev->od_ost_map->root = d;
278 /* Initialize all groups */
279 for (i = 0; i < MAX_OBJID_GROUP; i++) {
280 cfs_sema_init(&dev->od_ost_map->groups[i].dir_init_sem, 1);
281 rc = osd_compat_seq_init(dev, i);
283 osd_compat_fini(dev);
291 int osd_compat_del_entry(struct osd_thread_info *info, struct osd_device *osd,
292 struct dentry *dird, char *name, struct thandle *th)
294 struct ldiskfs_dir_entry_2 *de;
295 struct buffer_head *bh;
296 struct osd_thandle *oh;
297 struct dentry *child;
298 struct inode *dir = dird->d_inode;
303 oh = container_of(th, struct osd_thandle, ot_super);
304 LASSERT(oh->ot_handle != NULL);
305 LASSERT(oh->ot_handle->h_transaction != NULL);
308 child = &info->oti_child_dentry;
309 child->d_name.hash = 0;
310 child->d_name.name = name;
311 child->d_name.len = strlen(name);
312 child->d_parent = dird;
313 child->d_inode = NULL;
315 LOCK_INODE_MUTEX(dir);
317 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
319 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
322 UNLOCK_INODE_MUTEX(dir);
327 int osd_compat_add_entry(struct osd_thread_info *info, struct osd_device *osd,
328 struct dentry *dir, char *name,
329 const struct osd_inode_id *id, struct thandle *th)
331 struct osd_thandle *oh;
332 struct dentry *child;
338 oh = container_of(th, struct osd_thandle, ot_super);
339 LASSERT(oh->ot_handle != NULL);
340 LASSERT(oh->ot_handle->h_transaction != NULL);
342 inode = &info->oti_inode;
343 inode->i_sb = osd_sb(osd);
344 inode->i_ino = id->oii_ino;
345 inode->i_generation = id->oii_gen;
347 child = &info->oti_child_dentry;
348 child->d_name.hash = 0;
349 child->d_name.name = name;
350 child->d_name.len = strlen(name);
351 child->d_parent = dir;
352 child->d_inode = inode;
354 LOCK_INODE_MUTEX(dir->d_inode);
355 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
356 UNLOCK_INODE_MUTEX(dir->d_inode);
361 int osd_compat_objid_lookup(struct osd_thread_info *info,
362 struct osd_device *dev, const struct lu_fid *fid,
363 struct osd_inode_id *id)
365 struct osd_compat_objid *map;
367 struct dentry *d_seq;
368 struct ost_id *ostid = &info->oti_ostid;
372 struct ldiskfs_dir_entry_2 *de;
373 struct buffer_head *bh;
377 /* on the very first lookup we find and open directories */
379 map = dev->od_ost_map;
383 fid_ostid_pack(fid, ostid);
384 LASSERT(ostid->oi_seq < MAX_OBJID_GROUP);
385 LASSERT(map->subdir_count > 0);
386 LASSERT(map->groups[ostid->oi_seq].groot);
388 dirn = ostid->oi_id & (map->subdir_count - 1);
389 d = map->groups[ostid->oi_seq].dirs[dirn];
392 sprintf(name, "%llu", ostid->oi_id);
393 d_seq = &info->oti_child_dentry;
395 d_seq->d_name.hash = 0;
396 d_seq->d_name.name = name;
397 /* XXX: we can use rc from sprintf() instead of strlen() */
398 d_seq->d_name.len = strlen(name);
401 LOCK_INODE_MUTEX(dir);
402 bh = osd_ldiskfs_find_entry(dir, d_seq, &de, NULL);
403 UNLOCK_INODE_MUTEX(dir);
409 id->oii_ino = le32_to_cpu(de->inode);
412 id->oii_gen = OSD_OII_NOGEN;
413 inode = osd_iget(info, dev, id);
416 GOTO(cleanup, rc = PTR_ERR(inode));
418 id->oii_gen = inode->i_generation;
426 int osd_compat_objid_insert(struct osd_thread_info *info,
427 struct osd_device *osd,
428 const struct lu_fid *fid,
429 const struct osd_inode_id *id,
432 struct osd_compat_objid *map;
434 struct ost_id *ostid = &info->oti_ostid;
439 map = osd->od_ost_map;
442 LASSERT(map->subdir_count > 0);
443 LASSERT(map->groups[ostid->oi_seq].groot);
445 /* map fid to group:objid */
446 fid_ostid_pack(fid, ostid);
447 dirn = ostid->oi_id & (map->subdir_count - 1);
448 d = map->groups[ostid->oi_seq].dirs[dirn];
451 sprintf(name, "%llu", ostid->oi_id);
452 rc = osd_compat_add_entry(info, osd, d, name, id, th);
457 int osd_compat_objid_delete(struct osd_thread_info *info,
458 struct osd_device *osd,
459 const struct lu_fid *fid, struct thandle *th)
461 struct osd_compat_objid *map;
463 struct ost_id *ostid = &info->oti_ostid;
468 map = osd->od_ost_map;
471 LASSERT(map->subdir_count > 0);
472 LASSERT(map->groups[ostid->oi_seq].groot);
474 /* map fid to group:objid */
475 fid_ostid_pack(fid, ostid);
476 dirn = ostid->oi_id & (map->subdir_count - 1);
477 d = map->groups[ostid->oi_seq].dirs[dirn];
480 sprintf(name, "%llu", ostid->oi_id);
481 rc = osd_compat_del_entry(info, osd, d, name, th);
491 static const struct named_oid oids[] = {
492 { FLD_INDEX_OID, "" /* "fld" */ },
493 { FID_SEQ_CTL_OID, "" /* "seq_ctl" */ },
494 { FID_SEQ_SRV_OID, "" /* "seq_srv" */ },
495 { MDD_ROOT_INDEX_OID, "" /* "ROOT" */ },
496 { MDD_ORPHAN_OID, "" /* "PENDING" */ },
497 { MDD_LOV_OBJ_OID, "" /* LOV_OBJID */ },
498 { MDD_CAPA_KEYS_OID, "" /* CAPA_KEYS */ },
499 { MDT_LAST_RECV_OID, LAST_RCVD },
500 { OFD_LAST_RECV_OID, "" /* LAST_RCVD */ },
501 { OFD_LAST_GROUP_OID, "" /* "LAST_GROUP" */ },
502 { LLOG_CATALOGS_OID, "" /* "CATALOGS" */ },
503 { MGS_CONFIGS_OID, "" /* MOUNT_CONFIGS_DIR */ },
504 { OFD_HEALTH_CHECK_OID, "" /* HEALTH_CHECK */ },
508 static char *oid2name(const unsigned long oid)
512 while (oids[i].oid) {
513 if (oids[i].oid == oid)
520 int osd_compat_spec_insert(struct osd_thread_info *info,
521 struct osd_device *osd, const struct lu_fid *fid,
522 const struct osd_inode_id *id, struct thandle *th)
524 struct osd_compat_objid *map = osd->od_ost_map;
525 struct dentry *root = osd_sb(osd)->s_root;
531 if (fid_oid(fid) >= OFD_GROUP0_LAST_OID &&
532 fid_oid(fid) < OFD_GROUP4K_LAST_OID) {
533 /* on creation of LAST_ID we create O/<group> hierarchy */
535 seq = fid_oid(fid) - OFD_GROUP0_LAST_OID;
536 LASSERT(seq < MAX_OBJID_GROUP);
537 LASSERT(map->groups[seq].groot);
539 name = oid2name(fid_oid(fid));
541 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
543 rc = osd_compat_add_entry(info, osd, root, name, id,
550 int osd_compat_spec_lookup(struct osd_thread_info *info,
551 struct osd_device *osd, const struct lu_fid *fid,
552 struct osd_inode_id *id)
554 struct dentry *dentry;
560 name = oid2name(fid_oid(fid));
561 if (name == NULL || strlen(name) == 0)
564 dentry = ll_lookup_one_len(name, osd_sb(osd)->s_root, strlen(name));
565 if (!IS_ERR(dentry)) {
566 if (dentry->d_inode) {
567 if (is_bad_inode(dentry->d_inode)) {
570 id->oii_ino = dentry->d_inode->i_ino;
571 id->oii_gen = dentry->d_inode->i_generation;