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 MDT:
120 * REM_OBJ_DIR/ per mdt
121 * AGENT_OBJ_DIR/ per mdt
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)
128 struct lvfs_run_ctxt new;
129 struct lvfs_run_ctxt save;
130 struct dentry *parent;
131 struct osd_mdobj_map *omm;
136 OBD_ALLOC_PTR(dev->od_mdt_map);
137 if (dev->od_mdt_map == NULL)
140 omm = dev->od_mdt_map;
142 LASSERT(dev->od_fsops);
144 parent = osd_sb(dev)->s_root;
145 osd_push_ctxt(dev, &new, &save);
147 d = simple_mkdir(parent, dev->od_mnt, agent_obj_dir,
150 GOTO(cleanup, rc = PTR_ERR(d));
152 omm->omm_agent_dentry = d;
155 pop_ctxt(&save, &new, NULL);
157 if (omm->omm_agent_dentry != NULL)
158 dput(omm->omm_agent_dentry);
160 dev->od_mdt_map = NULL;
165 static void osd_mdt_fini(struct osd_device *osd)
167 struct osd_mdobj_map *omm = osd->od_mdt_map;
172 if (omm->omm_agent_dentry)
173 dput(omm->omm_agent_dentry);
176 osd->od_ost_map = NULL;
179 int osd_create_agent_inode(const struct lu_env *env, struct osd_device *osd,
180 struct osd_object *obj, struct osd_thandle *oh)
182 struct osd_mdobj_map *omm = osd->od_mdt_map;
183 struct osd_thread_info *oti = osd_oti_get(env);
184 char *name_buf = oti->oti_name;
185 struct dentry *agent;
186 struct dentry *parent;
189 parent = omm->omm_agent_dentry;
190 sprintf(name_buf, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
191 agent = osd_child_dentry_by_inode(env, parent->d_inode,
192 name_buf, strlen(name_buf));
193 mutex_lock(&parent->d_inode->i_mutex);
194 rc = osd_ldiskfs_add_entry(oh->ot_handle, agent, obj->oo_inode, NULL);
195 parent->d_inode->i_nlink++;
196 mark_inode_dirty(parent->d_inode);
197 mutex_unlock(&parent->d_inode->i_mutex);
199 CERROR("%s: "DFID" add agent error: rc = %d\n", osd_name(osd),
200 PFID(lu_object_fid(&obj->oo_dt.do_lu)), rc);
204 int osd_delete_agent_inode(const struct lu_env *env, struct osd_device *osd,
205 struct osd_object *obj, struct osd_thandle *oh)
207 struct osd_mdobj_map *omm = osd->od_mdt_map;
208 struct osd_thread_info *oti = osd_oti_get(env);
209 char *name = oti->oti_name;
210 struct dentry *agent;
211 struct dentry *parent;
212 struct ldiskfs_dir_entry_2 *de;
213 struct buffer_head *bh;
216 parent = omm->omm_agent_dentry;
217 sprintf(name, DFID, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
218 agent = osd_child_dentry_by_inode(env, parent->d_inode,
220 mutex_lock(&parent->d_inode->i_mutex);
221 bh = osd_ldiskfs_find_entry(parent->d_inode, agent, &de, NULL);
223 mutex_unlock(&parent->d_inode->i_mutex);
226 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
227 parent->d_inode->i_nlink--;
228 mark_inode_dirty(parent->d_inode);
229 mutex_unlock(&parent->d_inode->i_mutex);
235 * directory structure on legacy OST:
237 * O/<seq>/d0-31/<objid>
244 int osd_ost_init(struct osd_device *dev)
246 struct lvfs_run_ctxt new;
247 struct lvfs_run_ctxt save;
248 struct dentry *rootd = osd_sb(dev)->s_root;
253 OBD_ALLOC_PTR(dev->od_ost_map);
254 if (dev->od_ost_map == NULL)
257 /* to get subdir count from last_rcvd */
258 rc = osd_last_rcvd_subdir_count(dev);
260 OBD_FREE_PTR(dev->od_ost_map);
264 dev->od_ost_map->om_subdir_count = rc;
267 CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
268 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
269 sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
271 LASSERT(dev->od_fsops);
272 osd_push_ctxt(dev, &new, &save);
274 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
276 GOTO(cleanup, rc = PTR_ERR(d));
278 ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
279 dev->od_ost_map->om_root = d;
282 osd_pop_ctxt(dev, &new, &save);
284 OBD_FREE_PTR(dev->od_ost_map);
291 static void osd_seq_free(struct osd_obj_map *map,
292 struct osd_obj_seq *osd_seq)
296 cfs_list_del_init(&osd_seq->oos_seq_list);
298 if (osd_seq->oos_dirs) {
299 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
300 if (osd_seq->oos_dirs[j])
301 dput(osd_seq->oos_dirs[j]);
303 OBD_FREE(osd_seq->oos_dirs,
304 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
307 if (osd_seq->oos_root)
308 dput(osd_seq->oos_root);
310 OBD_FREE_PTR(osd_seq);
313 static void osd_ost_fini(struct osd_device *osd)
315 struct osd_obj_seq *osd_seq;
316 struct osd_obj_seq *tmp;
317 struct osd_obj_map *map = osd->od_ost_map;
323 write_lock(&map->om_seq_list_lock);
324 cfs_list_for_each_entry_safe(osd_seq, tmp,
327 osd_seq_free(map, osd_seq);
329 write_unlock(&map->om_seq_list_lock);
333 osd->od_ost_map = NULL;
337 int osd_obj_map_init(struct osd_device *dev)
342 /* prepare structures for OST */
343 rc = osd_ost_init(dev);
347 /* prepare structures for MDS */
348 rc = osd_mdt_init(dev);
353 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
355 struct osd_obj_seq *osd_seq;
357 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
358 if (osd_seq->oos_seq == seq)
364 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
366 struct osd_obj_seq *osd_seq;
368 read_lock(&map->om_seq_list_lock);
369 osd_seq = osd_seq_find_locked(map, seq);
370 read_unlock(&map->om_seq_list_lock);
374 void osd_obj_map_fini(struct osd_device *dev)
380 static int osd_obj_del_entry(struct osd_thread_info *info,
381 struct osd_device *osd,
382 struct dentry *dird, char *name,
385 struct ldiskfs_dir_entry_2 *de;
386 struct buffer_head *bh;
387 struct osd_thandle *oh;
388 struct dentry *child;
389 struct inode *dir = dird->d_inode;
393 oh = container_of(th, struct osd_thandle, ot_super);
394 LASSERT(oh->ot_handle != NULL);
395 LASSERT(oh->ot_handle->h_transaction != NULL);
398 child = &info->oti_child_dentry;
399 child->d_name.hash = 0;
400 child->d_name.name = name;
401 child->d_name.len = strlen(name);
402 child->d_parent = dird;
403 child->d_inode = NULL;
406 mutex_lock(&dir->i_mutex);
408 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
410 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
413 mutex_unlock(&dir->i_mutex);
418 int osd_obj_add_entry(struct osd_thread_info *info,
419 struct osd_device *osd,
420 struct dentry *dir, char *name,
421 const struct osd_inode_id *id,
424 struct osd_thandle *oh;
425 struct dentry *child;
431 oh = container_of(th, struct osd_thandle, ot_super);
432 LASSERT(oh->ot_handle != NULL);
433 LASSERT(oh->ot_handle->h_transaction != NULL);
435 inode = &info->oti_inode;
436 inode->i_sb = osd_sb(osd);
437 osd_id_to_inode(inode, id);
438 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
440 child = &info->oti_child_dentry;
441 child->d_name.hash = 0;
442 child->d_name.name = name;
443 child->d_name.len = strlen(name);
444 child->d_parent = dir;
445 child->d_inode = inode;
447 ll_vfs_dq_init(dir->d_inode);
448 mutex_lock(&dir->d_inode->i_mutex);
449 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
450 mutex_unlock(&dir->d_inode->i_mutex);
456 * Use LPU64 for legacy OST sequences, but use LPX64i for new
457 * sequences names, so that the O/{seq}/dN/{oid} more closely
458 * follows the DFID/PFID format. This makes it easier to map from
459 * debug messages to objects in the future, and the legacy space
460 * of FID_SEQ_OST_MDT0 will be unused in the future.
462 static inline void osd_seq_name(char *seq_name, obd_seq seq)
464 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
465 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
466 fid_seq_is_idif(seq) ? 0 : seq);
469 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
471 sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
472 fid_seq_is_mdt0(fid_seq(fid)) ||
473 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
476 /* external locking is required */
477 static int osd_seq_load_locked(struct osd_device *osd,
478 struct osd_obj_seq *osd_seq)
480 struct osd_obj_map *map = osd->od_ost_map;
481 struct dentry *seq_dir;
487 if (osd_seq->oos_root != NULL)
491 LASSERT(map->om_root);
493 osd_seq_name(seq_name, osd_seq->oos_seq);
495 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
497 GOTO(out_err, rc = PTR_ERR(seq_dir));
498 else if (seq_dir->d_inode == NULL)
499 GOTO(out_put, rc = -EFAULT);
501 ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
502 osd_seq->oos_root = seq_dir;
504 LASSERT(osd_seq->oos_dirs == NULL);
505 OBD_ALLOC(osd_seq->oos_dirs,
506 sizeof(seq_dir) * osd_seq->oos_subdir_count);
507 if (osd_seq->oos_dirs == NULL)
508 GOTO(out_put, rc = -ENOMEM);
510 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
514 sprintf(name, "d%u", i);
515 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
519 } else if (dir->d_inode) {
520 ldiskfs_set_inode_state(dir->d_inode,
521 LDISKFS_STATE_LUSTRE_NO_OI);
522 osd_seq->oos_dirs[i] = dir;
530 osd_seq_free(map, osd_seq);
534 osd_seq->oos_root = NULL;
540 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
542 struct osd_obj_map *map;
543 struct osd_obj_seq *osd_seq;
547 map = osd->od_ost_map;
549 LASSERT(map->om_root);
551 osd_seq = osd_seq_find(map, seq);
552 if (likely(osd_seq != NULL))
555 /* Serializing init process */
556 down(&map->om_dir_init_sem);
558 /* Check whether the seq has been added */
559 read_lock(&map->om_seq_list_lock);
560 osd_seq = osd_seq_find_locked(map, seq);
561 if (osd_seq != NULL) {
562 read_unlock(&map->om_seq_list_lock);
563 GOTO(cleanup, rc = 0);
565 read_unlock(&map->om_seq_list_lock);
567 OBD_ALLOC_PTR(osd_seq);
569 GOTO(cleanup, rc = -ENOMEM);
571 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
572 osd_seq->oos_seq = seq;
573 /* Init subdir count to be 32, but each seq can have
574 * different subdir count */
575 osd_seq->oos_subdir_count = map->om_subdir_count;
576 rc = osd_seq_load_locked(osd, osd_seq);
580 write_lock(&map->om_seq_list_lock);
581 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
582 write_unlock(&map->om_seq_list_lock);
585 up(&map->om_dir_init_sem);
588 OBD_FREE_PTR(osd_seq);
595 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
596 const struct lu_fid *fid, struct osd_inode_id *id)
598 struct osd_obj_map *map;
599 struct osd_obj_seq *osd_seq;
600 struct dentry *d_seq;
601 struct dentry *child;
602 struct ost_id *ostid = &info->oti_ostid;
605 struct ldiskfs_dir_entry_2 *de;
606 struct buffer_head *bh;
611 /* on the very first lookup we find and open directories */
613 map = dev->od_ost_map;
615 LASSERT(map->om_root);
617 fid_ostid_pack(fid, ostid);
618 osd_seq = osd_seq_load(dev, ostid->oi_seq);
620 RETURN(PTR_ERR(osd_seq));
622 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
623 d_seq = osd_seq->oos_dirs[dirn];
626 osd_oid_name(name, fid, ostid->oi_id);
628 child = &info->oti_child_dentry;
629 child->d_parent = d_seq;
630 child->d_name.hash = 0;
631 child->d_name.name = name;
632 /* XXX: we can use rc from sprintf() instead of strlen() */
633 child->d_name.len = strlen(name);
635 dir = d_seq->d_inode;
636 mutex_lock(&dir->i_mutex);
637 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
638 mutex_unlock(&dir->i_mutex);
643 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
646 inode = osd_iget(info, dev, id);
648 RETURN(PTR_ERR(inode));
654 int osd_obj_map_insert(struct osd_thread_info *info,
655 struct osd_device *osd,
656 const struct lu_fid *fid,
657 const struct osd_inode_id *id,
660 struct osd_obj_map *map;
661 struct osd_obj_seq *osd_seq;
663 struct ost_id *ostid = &info->oti_ostid;
668 map = osd->od_ost_map;
671 /* map fid to seq:objid */
672 fid_ostid_pack(fid, ostid);
674 osd_seq = osd_seq_load(osd, ostid->oi_seq);
676 RETURN(PTR_ERR(osd_seq));
678 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
679 d = osd_seq->oos_dirs[dirn];
682 osd_oid_name(name, fid, ostid->oi_id);
683 rc = osd_obj_add_entry(info, osd, d, name, id, th);
688 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
689 const struct lu_fid *fid, struct thandle *th)
691 struct osd_obj_map *map;
692 struct osd_obj_seq *osd_seq;
694 struct ost_id *ostid = &info->oti_ostid;
699 map = osd->od_ost_map;
702 /* map fid to seq:objid */
703 fid_ostid_pack(fid, ostid);
705 osd_seq = osd_seq_load(osd, ostid->oi_seq);
707 GOTO(cleanup, rc = PTR_ERR(osd_seq));
709 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
710 d = osd_seq->oos_dirs[dirn];
713 osd_oid_name(name, fid, ostid->oi_id);
714 rc = osd_obj_del_entry(info, osd, d, name, th);
719 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
720 const struct lu_fid *fid,
721 const struct osd_inode_id *id,
724 struct osd_obj_map *map = osd->od_ost_map;
725 struct dentry *root = osd_sb(osd)->s_root;
730 if (fid_is_last_id(fid)) {
731 struct osd_obj_seq *osd_seq;
733 /* on creation of LAST_ID we create O/<seq> hierarchy */
735 osd_seq = osd_seq_load(osd, fid_seq(fid));
737 RETURN(PTR_ERR(osd_seq));
738 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
741 name = osd_lf_fid2name(fid);
743 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
745 rc = osd_obj_add_entry(info, osd, root, name, id, th);
751 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
752 const struct lu_fid *fid, struct osd_inode_id *id)
755 struct dentry *dentry;
761 if (fid_is_last_id(fid)) {
762 struct osd_obj_seq *osd_seq;
764 osd_seq = osd_seq_load(osd, fid_seq(fid));
766 RETURN(PTR_ERR(osd_seq));
767 root = osd_seq->oos_root;
770 root = osd_sb(osd)->s_root;
771 name = osd_lf_fid2name(fid);
772 if (name == NULL || strlen(name) == 0)
776 dentry = ll_lookup_one_len(name, root, strlen(name));
777 if (!IS_ERR(dentry)) {
778 inode = dentry->d_inode;
780 if (is_bad_inode(inode)) {
783 osd_id_gen(id, inode->i_ino,
784 inode->i_generation);
788 /* if dentry is accessible after osd_compat_spec_insert it
789 * will still contain NULL inode, so don't keep it in cache */
790 d_invalidate(dentry);