1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (MDS) filesystem interface code
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 # define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
31 #include <linux/module.h>
32 #include <linux/kmod.h>
33 #include <linux/version.h>
34 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
35 #include <linux/mount.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_fsfilt.h>
42 #include <libcfs/list.h>
44 #include <linux/lustre_smfs.h>
45 #include "mds_internal.h"
47 /* This limit is arbitrary, but for now we fit it in 1 page (32k clients) */
48 #define MDS_MAX_CLIENTS (PAGE_SIZE * 8)
50 #define LAST_RCVD "last_rcvd"
51 #define LOV_OBJID "lov_objid"
52 #define LAST_FID "last_fid"
53 #define VIRT_FID "virt_fid"
54 #define CAPA_KEYS "capa_key"
57 struct hlist_node fm_hash;
58 struct lustre_id fm_id;
61 int mds_fidmap_init(struct obd_device *obd, int size)
63 struct mds_obd *mds = &obd->u.mds;
64 struct hlist_head *head;
68 LASSERT(size > sizeof(sizeof(struct hlist_head)));
69 mds->mds_fidmap_size = size / sizeof(struct hlist_head);
71 CWARN("allocating %lu fid mapping entries\n",
72 (unsigned long)mds->mds_fidmap_size);
74 OBD_ALLOC(mds->mds_fidmap_table, size);
75 if (!mds->mds_fidmap_table)
78 i = mds->mds_fidmap_size;
79 head = mds->mds_fidmap_table;
81 INIT_HLIST_HEAD(head);
89 int mds_fidmap_cleanup(struct obd_device *obd)
91 struct hlist_node *node = NULL, *tmp = NULL;
92 struct mds_obd *mds = &obd->u.mds;
93 struct fidmap_entry *entry;
94 struct hlist_head *head;
98 spin_lock(&mds->mds_fidmap_lock);
99 for (i = 0, head = mds->mds_fidmap_table;
100 i < mds->mds_fidmap_size; i++, head++) {
101 hlist_for_each_safe(node, tmp, head) {
102 entry = hlist_entry(node, struct fidmap_entry, fm_hash);
103 hlist_del_init(&entry->fm_hash);
104 OBD_FREE(entry, sizeof(*entry));
107 spin_unlock(&mds->mds_fidmap_lock);
108 OBD_FREE(mds->mds_fidmap_table, mds->mds_fidmap_size *
109 sizeof(struct hlist_head));
113 static inline unsigned long
114 const hashfn(struct obd_device *obd, __u64 fid)
116 struct mds_obd *mds = &obd->u.mds;
117 return (unsigned long)(fid & (mds->mds_fidmap_size - 1));
120 static struct fidmap_entry *
121 __mds_fidmap_find(struct obd_device *obd, __u64 fid)
123 struct fidmap_entry *entry = NULL;
124 struct mds_obd *mds = &obd->u.mds;
125 struct hlist_node *node = NULL;
126 struct hlist_head *head;
129 head = mds->mds_fidmap_table + hashfn(obd, fid);
130 hlist_for_each(node, head) {
131 entry = hlist_entry(node, struct fidmap_entry, fm_hash);
132 if (id_fid(&entry->fm_id) == fid)
138 struct fidmap_entry *
139 mds_fidmap_find(struct obd_device *obd, __u64 fid)
141 struct mds_obd *mds = &obd->u.mds;
142 struct fidmap_entry *entry;
145 spin_lock(&mds->mds_fidmap_lock);
146 entry = __mds_fidmap_find(obd, fid);
147 spin_unlock(&mds->mds_fidmap_lock);
152 static void __mds_fidmap_insert(struct obd_device *obd,
153 struct fidmap_entry *entry)
155 struct mds_obd *mds = &obd->u.mds;
156 struct hlist_head *head;
160 idx = hashfn(obd, id_fid(&entry->fm_id));
161 head = mds->mds_fidmap_table + idx;
162 hlist_add_head(&entry->fm_hash, head);
167 void mds_fidmap_insert(struct obd_device *obd,
168 struct fidmap_entry *entry)
170 struct mds_obd *mds = &obd->u.mds;
173 spin_lock(&mds->mds_fidmap_lock);
174 __mds_fidmap_insert(obd, entry);
175 spin_unlock(&mds->mds_fidmap_lock);
180 static void __mds_fidmap_remove(struct obd_device *obd,
181 struct fidmap_entry *entry)
184 hlist_del_init(&entry->fm_hash);
188 void mds_fidmap_remove(struct obd_device *obd,
189 struct fidmap_entry *entry)
191 struct mds_obd *mds = &obd->u.mds;
194 spin_lock(&mds->mds_fidmap_lock);
195 __mds_fidmap_remove(obd, entry);
196 spin_unlock(&mds->mds_fidmap_lock);
201 /* creates new mapping remote fid -> local inode store cookie. Both are saved in
202 * lustre_id for better usability, as all mds function use lustre_id as input
204 int mds_fidmap_add(struct obd_device *obd,
205 struct lustre_id *id)
207 struct mds_obd *mds = &obd->u.mds;
208 struct fidmap_entry *entry;
211 OBD_ALLOC(entry, sizeof(*entry));
217 spin_lock(&mds->mds_fidmap_lock);
218 if (!__mds_fidmap_find(obd, id_fid(id))) {
219 __mds_fidmap_insert(obd, entry);
220 spin_unlock(&mds->mds_fidmap_lock);
221 CDEBUG(D_INODE, "added mapping to "DLID4"\n",
225 spin_unlock(&mds->mds_fidmap_lock);
226 OBD_FREE(entry, sizeof(*entry));
231 /* removes mapping using fid component from passed @id */
232 void mds_fidmap_del(struct obd_device *obd,
233 struct lustre_id *id)
235 struct mds_obd *mds = &obd->u.mds;
236 struct fidmap_entry *entry;
239 spin_lock(&mds->mds_fidmap_lock);
240 entry = __mds_fidmap_find(obd, id_fid(id));
242 __mds_fidmap_remove(obd, entry);
243 spin_unlock(&mds->mds_fidmap_lock);
244 OBD_FREE(entry, sizeof(*entry));
245 CDEBUG(D_INODE, "removed mapping to "DLID4"\n",
249 spin_unlock(&mds->mds_fidmap_lock);
254 struct lustre_id *mds_fidmap_lookup(struct obd_device *obd,
255 struct lustre_id *id)
257 struct mds_obd *mds = &obd->u.mds;
258 struct fidmap_entry *entry;
261 spin_lock(&mds->mds_fidmap_lock);
262 entry = __mds_fidmap_find(obd, id_fid(id));
263 spin_unlock(&mds->mds_fidmap_lock);
268 RETURN(&entry->fm_id);
271 /* Add client data to the MDS. We use a bitmap to locate a free space
272 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
273 * Otherwise, we have just read the data from the last_rcvd file and
274 * we know its offset.
276 int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
277 struct mds_export_data *med, int cl_idx)
279 unsigned long *bitmap = mds->mds_client_bitmap;
280 int new_client = (cl_idx == -1);
283 LASSERT(bitmap != NULL);
285 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
286 if (!strcmp((char *)med->med_mcd->mcd_uuid, (char *)obd->obd_uuid.uuid))
289 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
290 * there's no need for extra complication here
293 cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
295 if (cl_idx >= MDS_MAX_CLIENTS) {
296 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
299 if (test_and_set_bit(cl_idx, bitmap)) {
300 cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
305 if (test_and_set_bit(cl_idx, bitmap)) {
306 CERROR("MDS client %d: bit already set in bitmap!!\n",
312 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
313 cl_idx, med->med_mcd->mcd_uuid);
315 med->med_idx = cl_idx;
316 med->med_off = le32_to_cpu(mds->mds_server_data->msd_client_start) +
317 (cl_idx * le16_to_cpu(mds->mds_server_data->msd_client_size));
320 struct file *file = mds->mds_rcvd_filp;
321 struct lvfs_run_ctxt saved;
322 loff_t off = med->med_off;
325 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
326 rc = fsfilt_write_record(obd, file, med->med_mcd,
327 sizeof(*med->med_mcd), &off, 1);
328 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
332 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
333 med->med_idx, med->med_off,
334 (unsigned int)sizeof(*med->med_mcd));
339 int mds_client_free(struct obd_export *exp, int clear_client)
341 struct mds_export_data *med = &exp->exp_mds_data;
342 struct mds_obd *mds = &exp->exp_obd->u.mds;
343 unsigned long *bitmap = mds->mds_client_bitmap;
344 struct obd_device *obd = exp->exp_obd;
345 struct mds_client_data zero_mcd;
346 struct lvfs_run_ctxt saved;
352 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
353 if (!strcmp((char *)med->med_mcd->mcd_uuid, (char *)obd->obd_uuid.uuid))
354 GOTO(free_and_out, 0);
356 CDEBUG(D_INFO, "freeing client at idx %u (%lld)with UUID '%s'\n",
357 med->med_idx, med->med_off, med->med_mcd->mcd_uuid);
361 /* Clear the bit _after_ zeroing out the client so we don't
362 race with mds_client_add and zero out new clients.*/
363 if (!test_bit(med->med_idx, bitmap)) {
364 CERROR("MDS client %u: bit already clear in bitmap!!\n",
370 memset(&zero_mcd, 0, sizeof zero_mcd);
371 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
372 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
373 sizeof(zero_mcd), &med->med_off, 1);
374 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
376 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
377 "zeroing out client %s idx %u in %s rc %d\n",
378 med->med_mcd->mcd_uuid, med->med_idx, LAST_RCVD, rc);
381 if (!test_and_clear_bit(med->med_idx, bitmap)) {
382 CERROR("MDS client %u: bit already clear in bitmap!!\n",
388 /* Make sure the server's last_transno is up to date. Do this
389 * after the client is freed so we know all the client's
390 * transactions have been committed. */
391 mds_update_server_data(exp->exp_obd, 1);
394 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
399 static int mds_server_free_data(struct mds_obd *mds)
401 OBD_FREE(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
402 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
403 mds->mds_server_data = NULL;
408 static int mds_read_last_fid(struct obd_device *obd, struct file *file)
412 struct mds_obd *mds = &obd->u.mds;
413 unsigned long last_fid_size = file->f_dentry->d_inode->i_size;
416 if (last_fid_size == 0) {
417 CWARN("%s: initializing new %s\n", obd->obd_name,
418 file->f_dentry->d_name.name);
421 * as fid is used for forming res_id for locking, it should not
422 * be zero. This will keep us out of lots possible problems,
425 mds_set_last_fid(obd, 0);
429 rc = fsfilt_read_record(obd, file, &lastfid,
430 sizeof(lastfid), &off);
432 CERROR("error reading MDS %s: rc = %d\n",
433 file->f_dentry->d_name.name, rc);
438 * make sure, that fid is up-to-date.
440 mds_set_last_fid(obd, lastfid);
443 CDEBUG(D_INODE, "%s: server last_fid: "LPU64"\n",
444 obd->obd_name, mds->mds_last_fid);
446 rc = mds_update_last_fid(obd, NULL, 1);
450 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
452 unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
453 struct mds_obd *mds = &obd->u.mds;
454 struct mds_server_data *msd = NULL;
455 struct mds_client_data *mcd = NULL;
461 /* ensure padding in the struct is the correct size */
462 LASSERT(offsetof(struct mds_server_data, msd_padding) +
463 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
464 LASSERT(offsetof(struct mds_client_data, mcd_padding) +
465 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
467 OBD_ALLOC_WAIT(msd, sizeof(*msd));
471 OBD_ALLOC_WAIT(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
472 if (!mds->mds_client_bitmap) {
473 OBD_FREE(msd, sizeof(*msd));
477 mds->mds_server_data = msd;
479 if (last_rcvd_size == 0) {
480 CWARN("%s: initializing new %s\n", obd->obd_name,
481 file->f_dentry->d_name.name);
483 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
484 msd->msd_last_transno = 0;
485 mount_count = msd->msd_mount_count = 0;
486 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
487 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
488 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
489 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
491 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
493 CERROR("error reading MDS %s: rc = %d\n",
494 file->f_dentry->d_name.name, rc);
497 if (strcmp((char *)msd->msd_uuid, (char *)obd->obd_uuid.uuid)) {
498 CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
499 obd->obd_uuid.uuid, msd->msd_uuid);
500 GOTO(err_msd, rc = -EINVAL);
502 mount_count = le64_to_cpu(msd->msd_mount_count);
504 if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
505 CERROR("unsupported incompat feature %x\n",
506 le32_to_cpu(msd->msd_feature_incompat) &
508 GOTO(err_msd, rc = -EINVAL);
510 /* XXX updating existing b_devel fs only, can be removed in future */
511 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
512 if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
513 CERROR("unsupported read-only feature %x\n",
514 le32_to_cpu(msd->msd_feature_rocompat) &
516 /* Do something like remount filesystem read-only */
517 GOTO(err_msd, rc = -EINVAL);
520 mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
522 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
523 obd->obd_name, mds->mds_last_transno);
524 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
525 obd->obd_name, mount_count + 1);
526 CDEBUG(D_INODE, "%s: server data size: %u\n",
527 obd->obd_name, le32_to_cpu(msd->msd_server_size));
528 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
529 obd->obd_name, le32_to_cpu(msd->msd_client_start));
530 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
531 obd->obd_name, le32_to_cpu(msd->msd_client_size));
532 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
533 obd->obd_name, last_rcvd_size);
534 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
535 last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
536 (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
537 le16_to_cpu(msd->msd_client_size));
539 /* When we do a clean MDS shutdown, we save the last_transno into
540 * the header. If we find clients with higher last_transno values
541 * then those clients may need recovery done. */
542 for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
543 off < last_rcvd_size; cl_idx++) {
545 struct obd_export *exp;
546 struct mds_export_data *med;
549 OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
551 GOTO(err_client, rc = -ENOMEM);
554 /* Don't assume off is incremented properly by
555 * fsfilt_read_record(), in case sizeof(*mcd)
556 * isn't the same as msd->msd_client_size. */
557 off = le32_to_cpu(msd->msd_client_start) +
558 cl_idx * le16_to_cpu(msd->msd_client_size);
559 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
561 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
562 file->f_dentry->d_name.name, cl_idx, off, rc);
563 break; /* read error shouldn't cause startup to fail */
566 if (mcd->mcd_uuid[0] == '\0') {
567 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
572 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
573 le64_to_cpu(mcd->mcd_last_close_transno) ?
574 le64_to_cpu(mcd->mcd_last_transno) :
575 le64_to_cpu(mcd->mcd_last_close_transno);
577 /* These exports are cleaned up by mds_disconnect(), so they
578 * need to be set up like real exports as mds_connect() does.
580 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
581 " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
582 last_transno, le64_to_cpu(msd->msd_last_transno),
585 exp = class_new_export(obd);
587 GOTO(err_client, rc = -ENOMEM);
589 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
590 sizeof exp->exp_client_uuid.uuid);
591 med = &exp->exp_mds_data;
593 mds_client_add(obd, mds, med, cl_idx);
594 /* create helper if export init gets more complex */
595 INIT_LIST_HEAD(&med->med_open_head);
596 spin_lock_init(&med->med_open_lock);
599 exp->exp_connected = 0;
600 exp->exp_req_replay_needed = 1;
601 obd->obd_recoverable_clients++;
602 obd->obd_max_recoverable_clients++;
604 /* track clients to separate req replay
605 * from lock replay. bug 6063 */
606 atomic_inc(&obd->obd_req_replay_clients);
607 exp->exp_req_replay_needed = 1;
608 atomic_inc(&obd->obd_lock_replay_clients);
609 exp->exp_lock_replay_needed = 1;
611 class_export_put(exp);
613 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
614 cl_idx, last_transno);
616 if (last_transno > mds->mds_last_transno)
617 mds->mds_last_transno = last_transno;
620 OBD_FREE(mcd, sizeof(*mcd));
621 obd->obd_last_committed = mds->mds_last_transno;
622 if (obd->obd_recoverable_clients) {
623 CWARN("RECOVERY: service %s, %d recoverable clients, "
624 "last_transno "LPU64"\n", obd->obd_name,
625 obd->obd_recoverable_clients, mds->mds_last_transno);
626 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
627 target_start_recovery_thread(obd, mds_handle);
628 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
631 mds->mds_mount_count = mount_count + 1;
632 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
634 /* save it, so mount count and last_transno is current */
635 rc = mds_update_server_data(obd, 1);
637 GOTO(err_client, rc);
642 class_disconnect_exports(obd, 0);
644 mds_server_free_data(mds);
649 * sets up root inode lustre_id. It tries to read it first from root inode and
650 * if it is not there, new rootid is allocated and saved there.
652 int mds_fs_setup_rootid(struct obd_device *obd)
657 struct dentry *dentry;
658 struct mds_obd *mds = &obd->u.mds;
661 /* getting root directory and setup its fid. */
662 dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
663 if (IS_ERR(dentry)) {
664 CERROR("Can't find ROOT by "DLID4", err = %d\n",
665 OLID4(&mds->mds_rootid), (int)PTR_ERR(dentry));
666 RETURN(PTR_ERR(dentry));
669 inode = dentry->d_inode;
670 LASSERT(dentry->d_inode);
672 rc = mds_pack_inode2id(obd, &mds->mds_rootid, inode, 1);
673 if (rc && rc != -ENODATA)
674 GOTO(out_dentry, rc);
678 GOTO(out_dentry, rc);
680 /* rootid is filled by mds_read_inode_sid(), so we do not need
681 * to allocate it and update. */
682 LASSERT(id_group(&mds->mds_rootid) == mds->mds_num);
683 mds_set_last_fid(obd, id_fid(&mds->mds_rootid));
685 rc = mds_fidmap_add(obd, &mds->mds_rootid);
689 GOTO(out_dentry, rc);
692 /* allocating new one, as it is not found in root inode. */
693 handle = fsfilt_start(obd, inode,
694 FSFILT_OP_SETATTR, NULL);
696 if (IS_ERR(handle)) {
697 rc = PTR_ERR(handle);
698 CERROR("fsfilt_start() failed, rc = %d\n", rc);
699 GOTO(out_dentry, rc);
702 mds_inode2id(obd, &mds->mds_rootid, inode, mds_alloc_fid(obd));
703 rc = mds_update_inode_ids(obd, inode, handle, &mds->mds_rootid, NULL);
705 CERROR("mds_update_inode_ids() failed, rc = %d\n", rc);
706 GOTO(out_dentry, rc);
709 rc = mds_fidmap_add(obd, &mds->mds_rootid);
711 GOTO(out_dentry, rc);
715 rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
717 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
723 CWARN("%s: rootid: "DLID4"\n", obd->obd_name,
724 OLID4(&mds->mds_rootid));
728 static int mds_update_virtid_fid(struct obd_device *obd,
729 void *handle, int force_sync)
731 struct mds_obd *mds = &obd->u.mds;
732 struct file *filp = mds->mds_virtid_filp;
733 struct lvfs_run_ctxt saved;
738 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
739 rc = fsfilt_write_record(obd, filp, &mds->mds_virtid_fid,
740 sizeof(mds->mds_virtid_fid),
743 CERROR("error writing MDS virtid_fid #"LPU64
744 ", err = %d\n", mds->mds_virtid_fid, rc);
747 CDEBUG(D_SUPER, "wrote virtid fid #"LPU64" at idx "
748 "%llu: err = %d\n", mds->mds_virtid_fid,
750 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
755 static int mds_read_virtid_fid(struct obd_device *obd, struct file *file)
759 struct mds_obd *mds = &obd->u.mds;
760 unsigned long virtid_fid_size = file->f_dentry->d_inode->i_size;
763 if (virtid_fid_size == 0) {
764 mds->mds_virtid_fid = mds_alloc_fid(obd);
766 rc = fsfilt_read_record(obd, file, &mds->mds_virtid_fid,
767 sizeof(mds->mds_virtid_fid), &off);
769 CERROR("error reading MDS %s: rc = %d\n",
770 file->f_dentry->d_name.name, rc);
774 rc = mds_update_virtid_fid(obd, NULL, 1);
780 * initializes lustre_id for virtual id directory, it is needed sometimes, as it
781 * is possible that it will be the parent for object an operations is going to
784 int mds_fs_setup_virtid(struct obd_device *obd)
788 struct lustre_id sid;
789 struct mds_obd *mds = &obd->u.mds;
790 struct inode *inode = mds->mds_id_dir->d_inode;
793 handle = fsfilt_start(obd, inode,
794 FSFILT_OP_SETATTR, NULL);
796 if (IS_ERR(handle)) {
797 rc = PTR_ERR(handle);
798 CERROR("fsfilt_start() failed, rc = %d\n", rc);
802 id_group(&sid) = mds->mds_num;
803 id_fid(&sid) = mds->mds_virtid_fid;
805 id_ino(&sid) = inode->i_ino;
806 id_gen(&sid) = inode->i_generation;
807 id_type(&sid) = (S_IFMT & inode->i_mode);
809 rc = mds_update_inode_ids(obd, inode, handle, &sid, NULL);
812 CERROR("mds_update_inode_ids() failed, rc = %d\n", rc);
816 rc = mds_fidmap_add(obd, &sid);
822 rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
824 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
831 #define MDS_FIDMAP_SIZE (2*PAGE_SIZE)
833 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
835 struct mds_obd *mds = &obd->u.mds;
836 struct lvfs_run_ctxt saved;
837 struct dentry *dentry;
842 rc = cleanup_group_info();
846 mds->mds_vfsmnt = mnt;
847 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
849 fsfilt_setup(obd, mds->mds_sb);
851 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
852 obd->obd_lvfs_ctxt.pwdmnt = mnt;
853 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
854 obd->obd_lvfs_ctxt.fs = get_ds();
855 obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
857 /* setup the directory tree */
858 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
859 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
860 if (IS_ERR(dentry)) {
861 rc = PTR_ERR(dentry);
862 CERROR("cannot create ROOT directory: rc = %d\n", rc);
866 mdc_pack_id(&mds->mds_rootid, dentry->d_inode->i_ino,
867 dentry->d_inode->i_generation, S_IFDIR, 0, 0);
871 dentry = lookup_one_len("__iopen__", current->fs->pwd,
872 strlen("__iopen__"));
873 if (IS_ERR(dentry)) {
874 rc = PTR_ERR(dentry);
875 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
878 mds->mds_id_de = dentry;
879 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
881 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
885 dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
886 if (IS_ERR(dentry)) {
887 rc = PTR_ERR(dentry);
888 CERROR("cannot create PENDING directory: rc = %d\n", rc);
891 mds->mds_pending_dir = dentry;
893 dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
894 if (IS_ERR(dentry)) {
895 rc = PTR_ERR(dentry);
896 CERROR("cannot create LOGS directory: rc = %d\n", rc);
897 GOTO(err_pending, rc);
899 mds->mds_logs_dir = dentry;
901 dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
902 if (IS_ERR(dentry)) {
903 rc = PTR_ERR(dentry);
904 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
907 mds->mds_objects_dir = dentry;
909 dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
910 if (IS_ERR(dentry)) {
911 rc = PTR_ERR(dentry);
912 CERROR("cannot create FIDS directory: rc = %d\n", rc);
913 GOTO(err_objects, rc);
915 mds->mds_id_dir = dentry;
917 dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
918 if (IS_ERR(dentry)) {
919 rc = PTR_ERR(dentry);
920 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
921 GOTO(err_id_dir, rc);
923 mds->mds_unnamed_dir = dentry;
925 /* open and test the last rcvd file */
926 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
929 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
930 GOTO(err_unnamed, rc = PTR_ERR(file));
932 mds->mds_rcvd_filp = file;
934 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
935 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
936 file->f_dentry->d_inode->i_mode);
937 GOTO(err_last_rcvd, rc = -ENOENT);
940 rc = mds_read_last_rcvd(obd, file);
942 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
943 GOTO(err_last_rcvd, rc);
946 /* open and test last fid file */
947 file = filp_open(LAST_FID, O_RDWR | O_CREAT, 0644);
950 CERROR("cannot open/create %s file: rc = %d\n",
952 GOTO(err_client, rc = PTR_ERR(file));
954 mds->mds_fid_filp = file;
955 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
956 CERROR("%s is not a regular file!: mode = %o\n",
957 LAST_FID, file->f_dentry->d_inode->i_mode);
958 GOTO(err_last_fid, rc = -ENOENT);
961 rc = mds_read_last_fid(obd, file);
963 CERROR("cannot read %s: rc = %d\n", LAST_FID, rc);
964 GOTO(err_last_fid, rc);
967 /* open and test virtid fid file */
968 file = filp_open(VIRT_FID, O_RDWR | O_CREAT, 0644);
971 CERROR("cannot open/create %s file: rc = %d\n",
973 GOTO(err_last_fid, rc = PTR_ERR(file));
975 mds->mds_virtid_filp = file;
976 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
977 CERROR("%s is not a regular file!: mode = %o\n",
978 VIRT_FID, file->f_dentry->d_inode->i_mode);
979 GOTO(err_virtid_fid, rc = -ENOENT);
982 rc = mds_read_virtid_fid(obd, file);
984 CERROR("cannot read %s: rc = %d\n", VIRT_FID, rc);
985 GOTO(err_virtid_fid, rc);
988 /* open and test the lov objid file */
989 file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
992 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
993 GOTO(err_virtid_fid, rc = PTR_ERR(file));
995 mds->mds_dt_objid_filp = file;
996 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
997 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
998 file->f_dentry->d_inode->i_mode);
999 GOTO(err_lov_objid, rc = -ENOENT);
1002 /* open and test capa keyid file */
1003 file = filp_open(CAPA_KEYS, O_RDWR | O_CREAT, 0644);
1006 CERROR("cannot open/create %s file: rc = %d\n",
1008 GOTO(err_lov_objid, rc = PTR_ERR(file));
1010 mds->mds_capa_keys_filp = file;
1011 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1012 CERROR("%s is not a regular file!: mode = %o\n",
1013 CAPA_KEYS, file->f_dentry->d_inode->i_mode);
1014 GOTO(err_capa_keyid, rc = -ENOENT);
1017 rc = mds_read_capa_key(obd, file);
1019 CERROR("cannot read %s: rc = %d\n", CAPA_KEYS, rc);
1020 GOTO(err_capa_keyid, rc);
1023 /* reint fidext thumb by last fid after root and virt are initialized */
1024 mds->mds_fidext_thumb = mds->mds_last_fid;
1026 rc = mds_fidmap_init(obd, MDS_FIDMAP_SIZE);
1028 CERROR("cannot init fid mapping tables, err %d\n", rc);
1029 GOTO(err_capa_keyid, rc);
1033 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1037 if (mds->mds_capa_keys_filp && filp_close(mds->mds_capa_keys_filp, 0))
1038 CERROR("can't close %s after error\n", CAPA_KEYS);
1040 if (mds->mds_dt_objid_filp && filp_close(mds->mds_dt_objid_filp, 0))
1041 CERROR("can't close %s after error\n", LOV_OBJID);
1043 if (mds->mds_virtid_filp && filp_close(mds->mds_virtid_filp, 0))
1044 CERROR("can't close %s after error\n", VIRT_FID);
1046 if (mds->mds_fid_filp && filp_close(mds->mds_fid_filp, 0))
1047 CERROR("can't close %s after error\n", LAST_FID);
1049 class_disconnect_exports(obd, 0);
1051 if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
1052 CERROR("can't close %s after error\n", LAST_RCVD);
1054 dput(mds->mds_unnamed_dir);
1056 dput(mds->mds_id_dir);
1058 dput(mds->mds_objects_dir);
1060 dput(mds->mds_logs_dir);
1062 dput(mds->mds_pending_dir);
1064 dput(mds->mds_id_de);
1068 static int mds_fs_post_cleanup(struct obd_device *obd)
1071 rc = fsfilt_post_cleanup(obd);
1075 int mds_fs_cleanup(struct obd_device *obd, int flags)
1077 struct mds_obd *mds = &obd->u.mds;
1078 struct lvfs_run_ctxt saved;
1081 if (flags & OBD_OPT_FAILOVER)
1082 CERROR("%s: shutting down for failover; client state will"
1083 " be preserved.\n", obd->obd_name);
1085 class_disconnect_exports(obd, flags); /* cleans up client info too */
1086 target_cleanup_recovery(obd);
1087 mds_server_free_data(mds);
1088 mds_fidmap_cleanup(obd);
1090 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1091 if (mds->mds_virtid_filp) {
1092 rc = filp_close(mds->mds_virtid_filp, 0);
1093 mds->mds_virtid_filp = NULL;
1095 CERROR("%s file won't close, rc = %d\n", VIRT_FID, rc);
1097 if (mds->mds_fid_filp) {
1098 rc = filp_close(mds->mds_fid_filp, 0);
1099 mds->mds_fid_filp = NULL;
1101 CERROR("%s file won't close, rc = %d\n", LAST_FID, rc);
1103 if (mds->mds_rcvd_filp) {
1104 rc = filp_close(mds->mds_rcvd_filp, 0);
1105 mds->mds_rcvd_filp = NULL;
1107 CERROR("%s file won't close, rc = %d\n", LAST_RCVD, rc);
1109 if (mds->mds_dt_objid_filp) {
1110 rc = filp_close(mds->mds_dt_objid_filp, 0);
1111 mds->mds_dt_objid_filp = NULL;
1113 CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
1115 if (mds->mds_capa_keys_filp) {
1116 rc = filp_close(mds->mds_capa_keys_filp, 0);
1117 mds->mds_capa_keys_filp = NULL;
1119 CERROR("%s file won't close, rc=%d\n", CAPA_KEYS, rc);
1121 if (mds->mds_unnamed_dir != NULL) {
1122 l_dput(mds->mds_unnamed_dir);
1123 mds->mds_unnamed_dir = NULL;
1125 if (mds->mds_id_dir != NULL) {
1126 l_dput(mds->mds_id_dir);
1127 mds->mds_id_dir = NULL;
1129 if (mds->mds_objects_dir != NULL) {
1130 l_dput(mds->mds_objects_dir);
1131 mds->mds_objects_dir = NULL;
1133 if (mds->mds_logs_dir) {
1134 l_dput(mds->mds_logs_dir);
1135 mds->mds_logs_dir = NULL;
1137 if (mds->mds_pending_dir) {
1138 l_dput(mds->mds_pending_dir);
1139 mds->mds_pending_dir = NULL;
1141 rc = mds_fs_post_cleanup(obd);
1143 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1144 shrink_dcache_parent(mds->mds_id_de);
1145 dput(mds->mds_id_de);
1150 /* Creates an object with the same name as its id. Because this is not at all
1151 * performance sensitive, it is accomplished by creating a file, checking the
1152 * id, and renaming it. */
1153 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
1154 void *acl, int acl_size,
1155 struct lov_stripe_md **ea, struct obd_trans_info *oti)
1157 struct mds_obd *mds = &exp->exp_obd->u.mds;
1158 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
1160 struct dentry *dchild;
1161 struct lvfs_run_ctxt saved;
1162 char idname[LL_ID_NAMELEN];
1163 int rc = 0, err, idlen;
1167 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
1168 down(&parent_inode->i_sem);
1170 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
1171 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
1173 GOTO(out_pop, rc = PTR_ERR(dchild));
1175 if (dchild->d_inode == NULL) {
1176 struct dentry_params dp;
1177 struct inode *inode;
1179 CWARN("creating log with ID "LPU64"\n", oa->o_id);
1181 dchild->d_fsdata = (void *) &dp;
1183 dp.p_inum = oa->o_id;
1184 rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
1185 if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
1186 dchild->d_fsdata = NULL;
1188 CDEBUG(D_INODE, "err during create: %d\n", rc);
1192 inode = dchild->d_inode;
1193 LASSERT(inode->i_ino == oa->o_id);
1194 inode->i_generation = oa->o_generation;
1195 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
1196 inode->i_ino, inode->i_generation);
1197 mark_inode_dirty(inode);
1199 CWARN("it should be here!\n");
1204 sprintf(idname, "OBJECTS/%u.%u", ll_insecure_random_int(), current->pid);
1205 filp = filp_open(idname, O_CREAT | O_EXCL, 0644);
1208 if (rc == -EEXIST) {
1209 CERROR("impossible object name collision %s\n",
1213 CERROR("error creating tmp object %s: rc %d\n",
1218 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
1220 oa->o_id = filp->f_dentry->d_inode->i_ino;
1221 oa->o_generation = filp->f_dentry->d_inode->i_generation;
1222 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
1224 CWARN("created log anonymous "LPU64"/%u\n",
1225 oa->o_id, oa->o_generation);
1227 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
1228 if (IS_ERR(dchild)) {
1229 CERROR("getting neg dentry for obj rename: %d\n", rc);
1230 GOTO(out_close, rc = PTR_ERR(dchild));
1232 if (dchild->d_inode != NULL) {
1233 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
1234 oa->o_id, oa->o_generation);
1238 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
1239 FSFILT_OP_RENAME, NULL);
1241 GOTO(out_dput, rc = PTR_ERR(handle));
1244 rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
1245 mds->mds_objects_dir->d_inode, dchild);
1248 CERROR("error renaming new object "LPU64":%u: rc %d\n",
1249 oa->o_id, oa->o_generation, rc);
1251 err = fsfilt_commit(exp->exp_obd, mds->mds_sb,
1252 mds->mds_objects_dir->d_inode, handle, 0);
1254 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1255 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
1261 err = filp_close(filp, 0);
1263 CERROR("closing tmpfile %s: rc %d\n", idname, rc);
1268 up(&parent_inode->i_sem);
1269 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
1273 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
1274 struct lov_stripe_md *ea, struct obd_trans_info *oti)
1276 struct mds_obd *mds = &exp->exp_obd->u.mds;
1277 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
1278 struct obd_device *obd = exp->exp_obd;
1279 struct lvfs_run_ctxt saved;
1280 char idname[LL_ID_NAMELEN];
1283 int err, idlen, rc = 0;
1286 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1288 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
1290 down(&parent_inode->i_sem);
1291 de = lookup_one_len(idname, mds->mds_objects_dir, idlen);
1292 if (IS_ERR(de) || de->d_inode == NULL) {
1293 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
1294 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
1295 oa->o_id, idname, rc);
1298 /* Stripe count is 1 here since this is some MDS specific stuff
1299 that is unlinked, not spanned across multiple OSTs */
1300 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
1301 FSFILT_OP_UNLINK, oti, 1);
1304 GOTO(out_dput, rc = PTR_ERR(handle));
1306 rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
1308 CERROR("error destroying object "LPU64":%u: rc %d\n",
1309 oa->o_id, oa->o_generation, rc);
1311 err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode,
1312 handle, exp->exp_sync);
1318 up(&parent_inode->i_sem);
1319 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);