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) 2012, Intel Corporation.
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 structure for managing /O
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 static void osd_push_ctxt(const struct osd_device *dev,
61 struct lvfs_run_ctxt *newctxt,
62 struct lvfs_run_ctxt *save)
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();
69 push_ctxt(save, newctxt, NULL);
72 static void osd_pop_ctxt(const struct osd_device *dev,
73 struct lvfs_run_ctxt *new,
74 struct lvfs_run_ctxt *save)
76 pop_ctxt(save, new, NULL);
79 int osd_last_rcvd_subdir_count(struct osd_device *osd)
81 struct lr_server_data lsd;
85 int count = FILTER_SUBDIR_COUNT;
89 dlast = ll_lookup_one_len(LAST_RCVD, osd_sb(osd)->s_root,
92 return PTR_ERR(dlast);
93 else if (dlast->d_inode == NULL)
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);
118 * directory structure on legacy OST:
120 * O/<seq>/d0-31/<objid>
127 int osd_ost_init(struct osd_device *dev)
129 struct lvfs_run_ctxt new;
130 struct lvfs_run_ctxt save;
131 struct dentry *rootd = osd_sb(dev)->s_root;
136 OBD_ALLOC_PTR(dev->od_ost_map);
137 if (dev->od_ost_map == NULL)
140 /* to get subdir count from last_rcvd */
141 rc = osd_last_rcvd_subdir_count(dev);
143 OBD_FREE_PTR(dev->od_ost_map);
147 dev->od_ost_map->om_subdir_count = rc;
150 CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
151 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
152 sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
154 LASSERT(dev->od_fsops);
155 osd_push_ctxt(dev, &new, &save);
157 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
159 GOTO(cleanup, rc = PTR_ERR(d));
161 dev->od_ost_map->om_root = d;
164 osd_pop_ctxt(dev, &new, &save);
166 OBD_FREE_PTR(dev->od_ost_map);
173 static void osd_seq_free(struct osd_obj_map *map,
174 struct osd_obj_seq *osd_seq)
178 cfs_list_del_init(&osd_seq->oos_seq_list);
180 if (osd_seq->oos_dirs) {
181 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
182 if (osd_seq->oos_dirs[j])
183 dput(osd_seq->oos_dirs[j]);
185 OBD_FREE(osd_seq->oos_dirs,
186 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
189 if (osd_seq->oos_root)
190 dput(osd_seq->oos_root);
192 OBD_FREE_PTR(osd_seq);
195 int osd_obj_map_init(struct osd_device *dev)
200 /* prepare structures for OST */
201 rc = osd_ost_init(dev);
203 /* prepare structures for MDS */
208 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
210 struct osd_obj_seq *osd_seq;
212 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
213 if (osd_seq->oos_seq == seq)
219 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
221 struct osd_obj_seq *osd_seq;
223 read_lock(&map->om_seq_list_lock);
224 osd_seq = osd_seq_find_locked(map, seq);
225 read_unlock(&map->om_seq_list_lock);
229 void osd_obj_map_fini(struct osd_device *dev)
231 struct osd_obj_seq *osd_seq;
232 struct osd_obj_seq *tmp;
233 struct osd_obj_map *map = dev->od_ost_map;
236 map = dev->od_ost_map;
240 write_lock(&dev->od_ost_map->om_seq_list_lock);
241 cfs_list_for_each_entry_safe(osd_seq, tmp,
242 &dev->od_ost_map->om_seq_list,
244 osd_seq_free(map, osd_seq);
246 write_unlock(&dev->od_ost_map->om_seq_list_lock);
249 OBD_FREE_PTR(dev->od_ost_map);
250 dev->od_ost_map = NULL;
254 static int osd_obj_del_entry(struct osd_thread_info *info,
255 struct osd_device *osd,
256 struct dentry *dird, char *name,
259 struct ldiskfs_dir_entry_2 *de;
260 struct buffer_head *bh;
261 struct osd_thandle *oh;
262 struct dentry *child;
263 struct inode *dir = dird->d_inode;
268 oh = container_of(th, struct osd_thandle, ot_super);
269 LASSERT(oh->ot_handle != NULL);
270 LASSERT(oh->ot_handle->h_transaction != NULL);
273 child = &info->oti_child_dentry;
274 child->d_name.hash = 0;
275 child->d_name.name = name;
276 child->d_name.len = strlen(name);
277 child->d_parent = dird;
278 child->d_inode = NULL;
281 mutex_lock(&dir->i_mutex);
283 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
285 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
288 mutex_unlock(&dir->i_mutex);
293 int osd_obj_add_entry(struct osd_thread_info *info,
294 struct osd_device *osd,
295 struct dentry *dir, char *name,
296 const struct osd_inode_id *id,
299 struct osd_thandle *oh;
300 struct dentry *child;
306 oh = container_of(th, struct osd_thandle, ot_super);
307 LASSERT(oh->ot_handle != NULL);
308 LASSERT(oh->ot_handle->h_transaction != NULL);
310 inode = &info->oti_inode;
311 inode->i_sb = osd_sb(osd);
312 osd_id_to_inode(inode, id);
313 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
315 child = &info->oti_child_dentry;
316 child->d_name.hash = 0;
317 child->d_name.name = name;
318 child->d_name.len = strlen(name);
319 child->d_parent = dir;
320 child->d_inode = inode;
322 ll_vfs_dq_init(dir->d_inode);
323 mutex_lock(&dir->d_inode->i_mutex);
324 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
325 mutex_unlock(&dir->d_inode->i_mutex);
331 * Use LPU64 for legacy OST sequences, but use LPX64i for new
332 * sequences names, so that the O/{seq}/dN/{oid} more closely
333 * follows the DFID/PFID format. This makes it easier to map from
334 * debug messages to objects in the future, and the legacy space
335 * of FID_SEQ_OST_MDT0 will be unused in the future.
337 static inline void osd_seq_name(char *seq_name, obd_seq seq)
339 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
340 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
341 fid_seq_is_idif(seq) ? 0 : seq);
344 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
346 sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
347 fid_seq_is_mdt0(fid_seq(fid)) ||
348 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
351 /* external locking is required */
352 static int osd_seq_load_locked(struct osd_device *osd,
353 struct osd_obj_seq *osd_seq)
355 struct osd_obj_map *map = osd->od_ost_map;
356 struct dentry *seq_dir;
362 if (osd_seq->oos_root != NULL)
366 LASSERT(map->om_root);
368 osd_seq_name(seq_name, osd_seq->oos_seq);
370 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
372 GOTO(out_err, rc = PTR_ERR(seq_dir));
373 else if (seq_dir->d_inode == NULL)
374 GOTO(out_put, rc = -EFAULT);
376 osd_seq->oos_root = seq_dir;
378 LASSERT(osd_seq->oos_dirs == NULL);
379 OBD_ALLOC(osd_seq->oos_dirs,
380 sizeof(seq_dir) * osd_seq->oos_subdir_count);
381 if (osd_seq->oos_dirs == NULL)
382 GOTO(out_put, rc = -ENOMEM);
384 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
388 sprintf(name, "d%u", i);
389 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
393 } else if (dir->d_inode) {
394 osd_seq->oos_dirs[i] = dir;
402 osd_seq_free(map, osd_seq);
406 osd_seq->oos_root = NULL;
412 static struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
414 struct osd_obj_map *map;
415 struct osd_obj_seq *osd_seq;
419 map = osd->od_ost_map;
421 LASSERT(map->om_root);
423 osd_seq = osd_seq_find(map, seq);
424 if (likely(osd_seq != NULL))
427 /* Serializing init process */
428 down(&map->om_dir_init_sem);
430 /* Check whether the seq has been added */
431 read_lock(&map->om_seq_list_lock);
432 osd_seq = osd_seq_find_locked(map, seq);
433 if (osd_seq != NULL) {
434 read_unlock(&map->om_seq_list_lock);
435 GOTO(cleanup, rc = 0);
437 read_unlock(&map->om_seq_list_lock);
439 OBD_ALLOC_PTR(osd_seq);
441 GOTO(cleanup, rc = -ENOMEM);
443 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
444 osd_seq->oos_seq = seq;
445 /* Init subdir count to be 32, but each seq can have
446 * different subdir count */
447 osd_seq->oos_subdir_count = map->om_subdir_count;
448 rc = osd_seq_load_locked(osd, osd_seq);
452 write_lock(&map->om_seq_list_lock);
453 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
454 write_unlock(&map->om_seq_list_lock);
457 up(&map->om_dir_init_sem);
460 OBD_FREE_PTR(osd_seq);
467 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
468 const struct lu_fid *fid, struct osd_inode_id *id)
470 struct osd_obj_map *map;
471 struct osd_obj_seq *osd_seq;
472 struct dentry *d_seq;
473 struct dentry *child;
474 struct ost_id *ostid = &info->oti_ostid;
477 struct ldiskfs_dir_entry_2 *de;
478 struct buffer_head *bh;
483 /* on the very first lookup we find and open directories */
485 map = dev->od_ost_map;
487 LASSERT(map->om_root);
489 fid_ostid_pack(fid, ostid);
490 osd_seq = osd_seq_load(dev, ostid->oi_seq);
492 RETURN(PTR_ERR(osd_seq));
494 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
495 d_seq = osd_seq->oos_dirs[dirn];
498 osd_oid_name(name, fid, ostid->oi_id);
500 child = &info->oti_child_dentry;
501 child->d_parent = d_seq;
502 child->d_name.hash = 0;
503 child->d_name.name = name;
504 /* XXX: we can use rc from sprintf() instead of strlen() */
505 child->d_name.len = strlen(name);
507 dir = d_seq->d_inode;
508 mutex_lock(&dir->i_mutex);
509 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
510 mutex_unlock(&dir->i_mutex);
515 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
518 inode = osd_iget(info, dev, id);
520 RETURN(PTR_ERR(inode));
526 int osd_obj_map_insert(struct osd_thread_info *info,
527 struct osd_device *osd,
528 const struct lu_fid *fid,
529 const struct osd_inode_id *id,
532 struct osd_obj_map *map;
533 struct osd_obj_seq *osd_seq;
535 struct ost_id *ostid = &info->oti_ostid;
540 map = osd->od_ost_map;
543 /* map fid to seq:objid */
544 fid_ostid_pack(fid, ostid);
546 osd_seq = osd_seq_load(osd, ostid->oi_seq);
548 RETURN(PTR_ERR(osd_seq));
550 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
551 d = osd_seq->oos_dirs[dirn];
554 osd_oid_name(name, fid, ostid->oi_id);
555 rc = osd_obj_add_entry(info, osd, d, name, id, th);
560 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
561 const struct lu_fid *fid, struct thandle *th)
563 struct osd_obj_map *map;
564 struct osd_obj_seq *osd_seq;
566 struct ost_id *ostid = &info->oti_ostid;
571 map = osd->od_ost_map;
574 /* map fid to seq:objid */
575 fid_ostid_pack(fid, ostid);
577 osd_seq = osd_seq_load(osd, ostid->oi_seq);
579 GOTO(cleanup, rc = PTR_ERR(osd_seq));
581 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
582 d = osd_seq->oos_dirs[dirn];
585 osd_oid_name(name, fid, ostid->oi_id);
586 rc = osd_obj_del_entry(info, osd, d, name, th);
596 static const struct named_oid oids[] = {
597 { FLD_INDEX_OID, "fld" },
598 { FID_SEQ_CTL_OID, "seq_ctl" },
599 { FID_SEQ_SRV_OID, "seq_srv" },
600 { MDD_ROOT_INDEX_OID, "" /* "ROOT" */ },
601 { MDD_ORPHAN_OID, "" /* "PENDING" */ },
602 { MDD_LOV_OBJ_OID, LOV_OBJID },
603 { MDD_CAPA_KEYS_OID, "" /* CAPA_KEYS */ },
604 { MDT_LAST_RECV_OID, LAST_RCVD },
605 { LFSCK_BOOKMARK_OID, "" /* "lfsck_bookmark" */ },
606 { OTABLE_IT_OID, "" /* "otable iterator" */},
607 { OFD_LAST_RECV_OID, LAST_RCVD },
608 { OFD_LAST_GROUP_OID, "LAST_GROUP" },
609 { LLOG_CATALOGS_OID, "CATALOGS" },
610 { MGS_CONFIGS_OID, "" /* MOUNT_CONFIGS_DIR */ },
611 { OFD_HEALTH_CHECK_OID, HEALTH_CHECK },
612 { MDD_LOV_OBJ_OSEQ, LOV_OBJSEQ },
616 static char *oid2name(const unsigned long oid)
620 while (oids[i].oid) {
621 if (oids[i].oid == oid)
628 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
629 const struct lu_fid *fid,
630 const struct osd_inode_id *id,
633 struct osd_obj_map *map = osd->od_ost_map;
634 struct dentry *root = osd_sb(osd)->s_root;
639 if (fid_is_last_id(fid)) {
640 struct osd_obj_seq *osd_seq;
642 /* on creation of LAST_ID we create O/<seq> hierarchy */
644 osd_seq = osd_seq_load(osd, fid_seq(fid));
646 RETURN(PTR_ERR(osd_seq));
647 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
650 name = oid2name(fid_oid(fid));
652 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
654 rc = osd_obj_add_entry(info, osd, root, name, id, th);
660 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
661 const struct lu_fid *fid, struct osd_inode_id *id)
664 struct dentry *dentry;
670 if (fid_is_last_id(fid)) {
671 struct osd_obj_seq *osd_seq;
673 osd_seq = osd_seq_load(osd, fid_seq(fid));
675 RETURN(PTR_ERR(osd_seq));
676 root = osd_seq->oos_root;
679 root = osd_sb(osd)->s_root;
680 name = oid2name(fid_oid(fid));
681 if (name == NULL || strlen(name) == 0)
685 dentry = ll_lookup_one_len(name, root, strlen(name));
686 if (!IS_ERR(dentry)) {
687 inode = dentry->d_inode;
689 if (is_bad_inode(inode)) {
692 osd_id_gen(id, inode->i_ino,
693 inode->i_generation);
697 /* if dentry is accessible after osd_compat_spec_insert it
698 * will still contain NULL inode, so don't keep it in cache */
699 d_invalidate(dentry);