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 ldiskfs_set_inode_state(d->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
162 dev->od_ost_map->om_root = d;
165 osd_pop_ctxt(dev, &new, &save);
167 OBD_FREE_PTR(dev->od_ost_map);
174 static void osd_seq_free(struct osd_obj_map *map,
175 struct osd_obj_seq *osd_seq)
179 cfs_list_del_init(&osd_seq->oos_seq_list);
181 if (osd_seq->oos_dirs) {
182 for (j = 0; j < osd_seq->oos_subdir_count; j++) {
183 if (osd_seq->oos_dirs[j])
184 dput(osd_seq->oos_dirs[j]);
186 OBD_FREE(osd_seq->oos_dirs,
187 sizeof(struct dentry *) * osd_seq->oos_subdir_count);
190 if (osd_seq->oos_root)
191 dput(osd_seq->oos_root);
193 OBD_FREE_PTR(osd_seq);
196 int osd_obj_map_init(struct osd_device *dev)
201 /* prepare structures for OST */
202 rc = osd_ost_init(dev);
204 /* prepare structures for MDS */
209 struct osd_obj_seq *osd_seq_find_locked(struct osd_obj_map *map, obd_seq seq)
211 struct osd_obj_seq *osd_seq;
213 cfs_list_for_each_entry(osd_seq, &map->om_seq_list, oos_seq_list) {
214 if (osd_seq->oos_seq == seq)
220 struct osd_obj_seq *osd_seq_find(struct osd_obj_map *map, obd_seq seq)
222 struct osd_obj_seq *osd_seq;
224 read_lock(&map->om_seq_list_lock);
225 osd_seq = osd_seq_find_locked(map, seq);
226 read_unlock(&map->om_seq_list_lock);
230 void osd_obj_map_fini(struct osd_device *dev)
232 struct osd_obj_seq *osd_seq;
233 struct osd_obj_seq *tmp;
234 struct osd_obj_map *map = dev->od_ost_map;
237 map = dev->od_ost_map;
241 write_lock(&dev->od_ost_map->om_seq_list_lock);
242 cfs_list_for_each_entry_safe(osd_seq, tmp,
243 &dev->od_ost_map->om_seq_list,
245 osd_seq_free(map, osd_seq);
247 write_unlock(&dev->od_ost_map->om_seq_list_lock);
250 OBD_FREE_PTR(dev->od_ost_map);
251 dev->od_ost_map = NULL;
255 static int osd_obj_del_entry(struct osd_thread_info *info,
256 struct osd_device *osd,
257 struct dentry *dird, char *name,
260 struct ldiskfs_dir_entry_2 *de;
261 struct buffer_head *bh;
262 struct osd_thandle *oh;
263 struct dentry *child;
264 struct inode *dir = dird->d_inode;
269 oh = container_of(th, struct osd_thandle, ot_super);
270 LASSERT(oh->ot_handle != NULL);
271 LASSERT(oh->ot_handle->h_transaction != NULL);
274 child = &info->oti_child_dentry;
275 child->d_name.hash = 0;
276 child->d_name.name = name;
277 child->d_name.len = strlen(name);
278 child->d_parent = dird;
279 child->d_inode = NULL;
282 mutex_lock(&dir->i_mutex);
284 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
286 rc = ldiskfs_delete_entry(oh->ot_handle, dir, de, bh);
289 mutex_unlock(&dir->i_mutex);
294 int osd_obj_add_entry(struct osd_thread_info *info,
295 struct osd_device *osd,
296 struct dentry *dir, char *name,
297 const struct osd_inode_id *id,
300 struct osd_thandle *oh;
301 struct dentry *child;
307 oh = container_of(th, struct osd_thandle, ot_super);
308 LASSERT(oh->ot_handle != NULL);
309 LASSERT(oh->ot_handle->h_transaction != NULL);
311 inode = &info->oti_inode;
312 inode->i_sb = osd_sb(osd);
313 osd_id_to_inode(inode, id);
314 inode->i_mode = S_IFREG; /* for type in ldiskfs dir entry */
316 child = &info->oti_child_dentry;
317 child->d_name.hash = 0;
318 child->d_name.name = name;
319 child->d_name.len = strlen(name);
320 child->d_parent = dir;
321 child->d_inode = inode;
323 ll_vfs_dq_init(dir->d_inode);
324 mutex_lock(&dir->d_inode->i_mutex);
325 rc = osd_ldiskfs_add_entry(oh->ot_handle, child, inode, NULL);
326 mutex_unlock(&dir->d_inode->i_mutex);
332 * Use LPU64 for legacy OST sequences, but use LPX64i for new
333 * sequences names, so that the O/{seq}/dN/{oid} more closely
334 * follows the DFID/PFID format. This makes it easier to map from
335 * debug messages to objects in the future, and the legacy space
336 * of FID_SEQ_OST_MDT0 will be unused in the future.
338 static inline void osd_seq_name(char *seq_name, obd_seq seq)
340 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
341 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
342 fid_seq_is_idif(seq) ? 0 : seq);
345 static inline void osd_oid_name(char *name, const struct lu_fid *fid, obd_id id)
347 sprintf(name, (fid_seq_is_rsvd(fid_seq(fid)) ||
348 fid_seq_is_mdt0(fid_seq(fid)) ||
349 fid_seq_is_idif(fid_seq(fid))) ? LPU64 : LPX64i, id);
352 /* external locking is required */
353 static int osd_seq_load_locked(struct osd_device *osd,
354 struct osd_obj_seq *osd_seq)
356 struct osd_obj_map *map = osd->od_ost_map;
357 struct dentry *seq_dir;
363 if (osd_seq->oos_root != NULL)
367 LASSERT(map->om_root);
369 osd_seq_name(seq_name, osd_seq->oos_seq);
371 seq_dir = simple_mkdir(map->om_root, osd->od_mnt, seq_name, 0755, 1);
373 GOTO(out_err, rc = PTR_ERR(seq_dir));
374 else if (seq_dir->d_inode == NULL)
375 GOTO(out_put, rc = -EFAULT);
377 ldiskfs_set_inode_state(seq_dir->d_inode, LDISKFS_STATE_LUSTRE_NO_OI);
378 osd_seq->oos_root = seq_dir;
380 LASSERT(osd_seq->oos_dirs == NULL);
381 OBD_ALLOC(osd_seq->oos_dirs,
382 sizeof(seq_dir) * osd_seq->oos_subdir_count);
383 if (osd_seq->oos_dirs == NULL)
384 GOTO(out_put, rc = -ENOMEM);
386 for (i = 0; i < osd_seq->oos_subdir_count; i++) {
390 sprintf(name, "d%u", i);
391 dir = simple_mkdir(osd_seq->oos_root, osd->od_mnt, name,
395 } else if (dir->d_inode) {
396 ldiskfs_set_inode_state(dir->d_inode,
397 LDISKFS_STATE_LUSTRE_NO_OI);
398 osd_seq->oos_dirs[i] = dir;
406 osd_seq_free(map, osd_seq);
410 osd_seq->oos_root = NULL;
416 struct osd_obj_seq *osd_seq_load(struct osd_device *osd, obd_seq seq)
418 struct osd_obj_map *map;
419 struct osd_obj_seq *osd_seq;
423 map = osd->od_ost_map;
425 LASSERT(map->om_root);
427 osd_seq = osd_seq_find(map, seq);
428 if (likely(osd_seq != NULL))
431 /* Serializing init process */
432 down(&map->om_dir_init_sem);
434 /* Check whether the seq has been added */
435 read_lock(&map->om_seq_list_lock);
436 osd_seq = osd_seq_find_locked(map, seq);
437 if (osd_seq != NULL) {
438 read_unlock(&map->om_seq_list_lock);
439 GOTO(cleanup, rc = 0);
441 read_unlock(&map->om_seq_list_lock);
443 OBD_ALLOC_PTR(osd_seq);
445 GOTO(cleanup, rc = -ENOMEM);
447 CFS_INIT_LIST_HEAD(&osd_seq->oos_seq_list);
448 osd_seq->oos_seq = seq;
449 /* Init subdir count to be 32, but each seq can have
450 * different subdir count */
451 osd_seq->oos_subdir_count = map->om_subdir_count;
452 rc = osd_seq_load_locked(osd, osd_seq);
456 write_lock(&map->om_seq_list_lock);
457 cfs_list_add(&osd_seq->oos_seq_list, &map->om_seq_list);
458 write_unlock(&map->om_seq_list_lock);
461 up(&map->om_dir_init_sem);
464 OBD_FREE_PTR(osd_seq);
471 int osd_obj_map_lookup(struct osd_thread_info *info, struct osd_device *dev,
472 const struct lu_fid *fid, struct osd_inode_id *id)
474 struct osd_obj_map *map;
475 struct osd_obj_seq *osd_seq;
476 struct dentry *d_seq;
477 struct dentry *child;
478 struct ost_id *ostid = &info->oti_ostid;
481 struct ldiskfs_dir_entry_2 *de;
482 struct buffer_head *bh;
487 /* on the very first lookup we find and open directories */
489 map = dev->od_ost_map;
491 LASSERT(map->om_root);
493 fid_ostid_pack(fid, ostid);
494 osd_seq = osd_seq_load(dev, ostid->oi_seq);
496 RETURN(PTR_ERR(osd_seq));
498 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
499 d_seq = osd_seq->oos_dirs[dirn];
502 osd_oid_name(name, fid, ostid->oi_id);
504 child = &info->oti_child_dentry;
505 child->d_parent = d_seq;
506 child->d_name.hash = 0;
507 child->d_name.name = name;
508 /* XXX: we can use rc from sprintf() instead of strlen() */
509 child->d_name.len = strlen(name);
511 dir = d_seq->d_inode;
512 mutex_lock(&dir->i_mutex);
513 bh = osd_ldiskfs_find_entry(dir, child, &de, NULL);
514 mutex_unlock(&dir->i_mutex);
519 osd_id_gen(id, le32_to_cpu(de->inode), OSD_OII_NOGEN);
522 inode = osd_iget(info, dev, id);
524 RETURN(PTR_ERR(inode));
530 int osd_obj_map_insert(struct osd_thread_info *info,
531 struct osd_device *osd,
532 const struct lu_fid *fid,
533 const struct osd_inode_id *id,
536 struct osd_obj_map *map;
537 struct osd_obj_seq *osd_seq;
539 struct ost_id *ostid = &info->oti_ostid;
544 map = osd->od_ost_map;
547 /* map fid to seq:objid */
548 fid_ostid_pack(fid, ostid);
550 osd_seq = osd_seq_load(osd, ostid->oi_seq);
552 RETURN(PTR_ERR(osd_seq));
554 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
555 d = osd_seq->oos_dirs[dirn];
558 osd_oid_name(name, fid, ostid->oi_id);
559 rc = osd_obj_add_entry(info, osd, d, name, id, th);
564 int osd_obj_map_delete(struct osd_thread_info *info, struct osd_device *osd,
565 const struct lu_fid *fid, struct thandle *th)
567 struct osd_obj_map *map;
568 struct osd_obj_seq *osd_seq;
570 struct ost_id *ostid = &info->oti_ostid;
575 map = osd->od_ost_map;
578 /* map fid to seq:objid */
579 fid_ostid_pack(fid, ostid);
581 osd_seq = osd_seq_load(osd, ostid->oi_seq);
583 GOTO(cleanup, rc = PTR_ERR(osd_seq));
585 dirn = ostid->oi_id & (osd_seq->oos_subdir_count - 1);
586 d = osd_seq->oos_dirs[dirn];
589 osd_oid_name(name, fid, ostid->oi_id);
590 rc = osd_obj_del_entry(info, osd, d, name, th);
595 int osd_obj_spec_insert(struct osd_thread_info *info, struct osd_device *osd,
596 const struct lu_fid *fid,
597 const struct osd_inode_id *id,
600 struct osd_obj_map *map = osd->od_ost_map;
601 struct dentry *root = osd_sb(osd)->s_root;
606 if (fid_is_last_id(fid)) {
607 struct osd_obj_seq *osd_seq;
609 /* on creation of LAST_ID we create O/<seq> hierarchy */
611 osd_seq = osd_seq_load(osd, fid_seq(fid));
613 RETURN(PTR_ERR(osd_seq));
614 rc = osd_obj_add_entry(info, osd, osd_seq->oos_root,
617 name = osd_lf_fid2name(fid);
619 CWARN("UNKNOWN COMPAT FID "DFID"\n", PFID(fid));
621 rc = osd_obj_add_entry(info, osd, root, name, id, th);
627 int osd_obj_spec_lookup(struct osd_thread_info *info, struct osd_device *osd,
628 const struct lu_fid *fid, struct osd_inode_id *id)
631 struct dentry *dentry;
637 if (fid_is_last_id(fid)) {
638 struct osd_obj_seq *osd_seq;
640 osd_seq = osd_seq_load(osd, fid_seq(fid));
642 RETURN(PTR_ERR(osd_seq));
643 root = osd_seq->oos_root;
646 root = osd_sb(osd)->s_root;
647 name = osd_lf_fid2name(fid);
648 if (name == NULL || strlen(name) == 0)
652 dentry = ll_lookup_one_len(name, root, strlen(name));
653 if (!IS_ERR(dentry)) {
654 inode = dentry->d_inode;
656 if (is_bad_inode(inode)) {
659 osd_id_gen(id, inode->i_ino,
660 inode->i_generation);
664 /* if dentry is accessible after osd_compat_spec_insert it
665 * will still contain NULL inode, so don't keep it in cache */
666 d_invalidate(dentry);