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_add_to_agent(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 = oti->oti_name;
185 struct dentry *agent;
186 struct dentry *parent;
189 parent = omm->omm_agent_dentry;
190 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
191 agent = osd_child_dentry_by_inode(env, parent->d_inode,
193 mutex_lock(&parent->d_inode->i_mutex);
194 rc = osd_ldiskfs_add_entry(oh->ot_handle, agent, obj->oo_inode, NULL);
195 LASSERTF(parent->d_inode->i_nlink > 1, "%s: agent inode nlink %d",
196 osd_name(osd), parent->d_inode->i_nlink);
197 parent->d_inode->i_nlink++;
198 mark_inode_dirty(parent->d_inode);
199 mutex_unlock(&parent->d_inode->i_mutex);
203 int osd_delete_from_agent(const struct lu_env *env, struct osd_device *osd,
204 struct osd_object *obj, struct osd_thandle *oh)
206 struct osd_mdobj_map *omm = osd->od_mdt_map;
207 struct osd_thread_info *oti = osd_oti_get(env);
208 char *name = oti->oti_name;
209 struct dentry *agent;
210 struct dentry *parent;
211 struct ldiskfs_dir_entry_2 *de;
212 struct buffer_head *bh;
215 parent = omm->omm_agent_dentry;
216 sprintf(name, DFID_NOBRACE, PFID(lu_object_fid(&obj->oo_dt.do_lu)));
217 agent = osd_child_dentry_by_inode(env, parent->d_inode,
219 mutex_lock(&parent->d_inode->i_mutex);
220 bh = osd_ldiskfs_find_entry(parent->d_inode, agent, &de, NULL);
222 mutex_unlock(&parent->d_inode->i_mutex);
225 rc = ldiskfs_delete_entry(oh->ot_handle, parent->d_inode, de, bh);
226 LASSERTF(parent->d_inode->i_nlink > 1, "%s: agent inode nlink %d",
227 osd_name(osd), parent->d_inode->i_nlink);
228 parent->d_inode->i_nlink--;
229 mark_inode_dirty(parent->d_inode);
230 mutex_unlock(&parent->d_inode->i_mutex);
236 * directory structure on legacy OST:
238 * O/<seq>/d0-31/<objid>
245 int osd_ost_init(struct osd_device *dev)
247 struct lvfs_run_ctxt new;
248 struct lvfs_run_ctxt save;
249 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->om_subdir_count = rc;
268 CFS_INIT_LIST_HEAD(&dev->od_ost_map->om_seq_list);
269 rwlock_init(&dev->od_ost_map->om_seq_list_lock);
270 sema_init(&dev->od_ost_map->om_dir_init_sem, 1);
272 LASSERT(dev->od_fsops);
273 osd_push_ctxt(dev, &new, &save);
275 d = simple_mkdir(rootd, dev->od_mnt, "O", 0755, 1);
277 GOTO(cleanup, rc = PTR_ERR(d));
279 ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
280 dev->od_ost_map->om_root = d;
283 osd_pop_ctxt(dev, &new, &save);
285 OBD_FREE_PTR(dev->od_ost_map);
292 static void osd_seq_free(struct osd_obj_map *map,
293 struct osd_obj_seq *osd_seq)
297 cfs_list_del_init(&osd_seq->oos_seq_list);
299 if (osd_seq->oos_dirs) {
300 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
301 if (osd_seq->oos_dirs[j])
302 dput(osd_seq->oos_dirs[j]);
304 OBD_FREE(osd_seq->oos_dirs,
305 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
308 if (osd_seq->oos_root)
309 dput(osd_seq->oos_root);
311 OBD_FREE_PTR(osd_seq);
314 static void osd_ost_fini(struct osd_device *osd)
316 struct osd_obj_seq *osd_seq;
317 struct osd_obj_seq *tmp;
318 struct osd_obj_map *map = osd->od_ost_map;
324 write_lock(&map->om_seq_list_lock);
325 cfs_list_for_each_entry_safe(osd_seq, tmp,
328 osd_seq_free(map, osd_seq);
330 write_unlock(&map->om_seq_list_lock);
334 osd->od_ost_map = NULL;
338 int osd_obj_map_init(struct osd_device *dev)
343 /* prepare structures for OST */
344 rc = osd_ost_init(dev);
348 /* prepare structures for MDS */
349 rc = osd_mdt_init(dev);
354 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
356 struct osd_obj_seq *osd_seq;
358 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
359 if (osd_seq->oos_seq == seq)
365 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
367 struct osd_obj_seq *osd_seq;
369 read_lock(&map->om_seq_list_lock);
370 osd_seq = osd_seq_find_locked(map, seq);
371 read_unlock(&map->om_seq_list_lock);
375 void osd_obj_map_fini(struct osd_device *dev)
381 static int osd_obj_del_entry(struct osd_thread_info *info,
382 struct osd_device *osd,
383 struct dentry *dird, char *name,
386 struct ldiskfs_dir_entry_2 *de;
387 struct buffer_head *bh;
388 struct osd_thandle *oh;
389 struct dentry *child;
390 struct inode *dir = dird->d_inode;
394 oh = container_of(th, struct osd_thandle, ot_super);
395 LASSERT(oh->ot_handle != NULL);
396 LASSERT(oh->ot_handle->h_transaction != NULL);
399 child = &info->oti_child_dentry;
400 child->d_name.hash = 0;
401 child->d_name.name = name;
402 child->d_name.len = strlen(name);
403 child->d_parent = dird;
404 child->d_inode = NULL;
407 mutex_lock(&dir->i_mutex);
409 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
411 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
414 mutex_unlock(&dir->i_mutex);
419 int osd_obj_add_entry(struct osd_thread_info *info,
420 struct osd_device *osd,
421 struct dentry *dir, char *name,
422 const struct osd_inode_id *id,
425 struct osd_thandle *oh;
426 struct dentry *child;
432 oh = container_of(th, struct osd_thandle, ot_super);
433 LASSERT(oh->ot_handle != NULL);
434 LASSERT(oh->ot_handle->h_transaction != NULL);
436 inode = &info->oti_inode;
437 inode->i_sb = osd_sb(osd);
438 osd_id_to_inode(inode, id);
439 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
441 child = &info->oti_child_dentry;
442 child->d_name.hash = 0;
443 child->d_name.name = name;
444 child->d_name.len = strlen(name);
445 child->d_parent = dir;
446 child->d_inode = inode;
448 ll_vfs_dq_init(dir->d_inode);
449 mutex_lock(&dir->d_inode->i_mutex);
450 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
451 mutex_unlock(&dir->d_inode->i_mutex);
457 * Use LPU64 for legacy OST sequences, but use LPX64i for new
458 * sequences names, so that the O/{seq}/dN/{oid} more closely
459 * follows the DFID/PFID format. This makes it easier to map from
460 * debug messages to objects in the future, and the legacy space
461 * of FID_SEQ_OST_MDT0 will be unused in the future.
463 static inline void osd_seq_name(char *seq_name, obd_seq seq)
465 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
466 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
467 fid_seq_is_idif(seq) ? 0 : seq);
470 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
472 sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
473 fid_seq_is_mdt0(fid_seq(fid)) ||
474 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
477 /* external locking is required */
478 static int osd_seq_load_locked(struct osd_device *osd,
479 struct osd_obj_seq *osd_seq)
481 struct osd_obj_map *map = osd->od_ost_map;
482 struct dentry *seq_dir;
488 if (osd_seq->oos_root != NULL)
492 LASSERT(map->om_root);
494 osd_seq_name(seq_name, osd_seq->oos_seq);
496 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
498 GOTO(out_err, rc = PTR_ERR(seq_dir));
499 else if (seq_dir->d_inode == NULL)
500 GOTO(out_put, rc = -EFAULT);
502 ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
503 osd_seq->oos_root = seq_dir;
505 LASSERT(osd_seq->oos_dirs == NULL);
506 OBD_ALLOC(osd_seq->oos_dirs,
507 sizeof(seq_dir) * osd_seq->oos_subdir_count);
508 if (osd_seq->oos_dirs == NULL)
509 GOTO(out_put, rc = -ENOMEM);
511 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
515 sprintf(name, "d%u", i);
516 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
520 } else if (dir->d_inode) {
521 ldiskfs_set_inode_state(dir->d_inode,
522 LDISKFS_STATE_LUSTRE_NO_OI);
523 osd_seq->oos_dirs[i] = dir;
531 osd_seq_free(map, osd_seq);
535 osd_seq->oos_root = NULL;
541 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
543 struct osd_obj_map *map;
544 struct osd_obj_seq *osd_seq;
548 map = osd->od_ost_map;
550 LASSERT(map->om_root);
552 osd_seq = osd_seq_find(map, seq);
553 if (likely(osd_seq != NULL))
556 /* Serializing init process */
557 down(&map->om_dir_init_sem);
559 /* Check whether the seq has been added */
560 read_lock(&map->om_seq_list_lock);
561 osd_seq = osd_seq_find_locked(map, seq);
562 if (osd_seq != NULL) {
563 read_unlock(&map->om_seq_list_lock);
564 GOTO(cleanup, rc = 0);
566 read_unlock(&map->om_seq_list_lock);
568 OBD_ALLOC_PTR(osd_seq);
570 GOTO(cleanup, rc = -ENOMEM);
572 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
573 osd_seq->oos_seq = seq;
574 /* Init subdir count to be 32, but each seq can have
575 * different subdir count */
576 osd_seq->oos_subdir_count = map->om_subdir_count;
577 rc = osd_seq_load_locked(osd, osd_seq);
581 write_lock(&map->om_seq_list_lock);
582 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
583 write_unlock(&map->om_seq_list_lock);
586 up(&map->om_dir_init_sem);
589 OBD_FREE_PTR(osd_seq);
596 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
597 const struct lu_fid *fid, struct osd_inode_id *id)
599 struct osd_obj_map *map;
600 struct osd_obj_seq *osd_seq;
601 struct dentry *d_seq;
602 struct dentry *child;
603 struct ost_id *ostid = &info->oti_ostid;
606 struct ldiskfs_dir_entry_2 *de;
607 struct buffer_head *bh;
612 /* on the very first lookup we find and open directories */
614 map = dev->od_ost_map;
616 LASSERT(map->om_root);
618 fid_ostid_pack(fid, ostid);
619 osd_seq = osd_seq_load(dev, ostid->oi_seq);
621 RETURN(PTR_ERR(osd_seq));
623 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
624 d_seq = osd_seq->oos_dirs[dirn];
627 osd_oid_name(name, fid, ostid->oi_id);
629 child = &info->oti_child_dentry;
630 child->d_parent = d_seq;
631 child->d_name.hash = 0;
632 child->d_name.name = name;
633 /* XXX: we can use rc from sprintf() instead of strlen() */
634 child->d_name.len = strlen(name);
636 dir = d_seq->d_inode;
637 mutex_lock(&dir->i_mutex);
638 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
639 mutex_unlock(&dir->i_mutex);
644 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
647 inode = osd_iget(info, dev, id);
649 RETURN(PTR_ERR(inode));
655 int osd_obj_map_insert(struct osd_thread_info *info,
656 struct osd_device *osd,
657 const struct lu_fid *fid,
658 const struct osd_inode_id *id,
661 struct osd_obj_map *map;
662 struct osd_obj_seq *osd_seq;
664 struct ost_id *ostid = &info->oti_ostid;
669 map = osd->od_ost_map;
672 /* map fid to seq:objid */
673 fid_ostid_pack(fid, ostid);
675 osd_seq = osd_seq_load(osd, ostid->oi_seq);
677 RETURN(PTR_ERR(osd_seq));
679 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
680 d = osd_seq->oos_dirs[dirn];
683 osd_oid_name(name, fid, ostid->oi_id);
684 rc = osd_obj_add_entry(info, osd, d, name, id, th);
689 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
690 const struct lu_fid *fid, struct thandle *th)
692 struct osd_obj_map *map;
693 struct osd_obj_seq *osd_seq;
695 struct ost_id *ostid = &info->oti_ostid;
700 map = osd->od_ost_map;
703 /* map fid to seq:objid */
704 fid_ostid_pack(fid, ostid);
706 osd_seq = osd_seq_load(osd, ostid->oi_seq);
708 GOTO(cleanup, rc = PTR_ERR(osd_seq));
710 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
711 d = osd_seq->oos_dirs[dirn];
714 osd_oid_name(name, fid, ostid->oi_id);
715 rc = osd_obj_del_entry(info, osd, d, name, th);
720 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
721 const struct lu_fid *fid,
722 const struct osd_inode_id *id,
725 struct osd_obj_map *map = osd->od_ost_map;
726 struct dentry *root = osd_sb(osd)->s_root;
731 if (fid_is_last_id(fid)) {
732 struct osd_obj_seq *osd_seq;
734 /* on creation of LAST_ID we create O/<seq> hierarchy */
736 osd_seq = osd_seq_load(osd, fid_seq(fid));
738 RETURN(PTR_ERR(osd_seq));
739 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
742 name = osd_lf_fid2name(fid);
744 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
746 rc = osd_obj_add_entry(info, osd, root, name, id, th);
752 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
753 const struct lu_fid *fid, struct osd_inode_id *id)
756 struct dentry *dentry;
762 if (fid_is_last_id(fid)) {
763 struct osd_obj_seq *osd_seq;
765 osd_seq = osd_seq_load(osd, fid_seq(fid));
767 RETURN(PTR_ERR(osd_seq));
768 root = osd_seq->oos_root;
771 root = osd_sb(osd)->s_root;
772 name = osd_lf_fid2name(fid);
773 if (name == NULL || strlen(name) == 0)
777 dentry = ll_lookup_one_len(name, root, strlen(name));
778 if (!IS_ERR(dentry)) {
779 inode = dentry->d_inode;
781 if (is_bad_inode(inode)) {
784 osd_id_gen(id, inode->i_ino,
785 inode->i_generation);
789 /* if dentry is accessible after osd_compat_spec_insert it
790 * will still contain NULL inode, so don't keep it in cache */
791 d_invalidate(dentry);