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"
55 /* Add client data to the MDS. We use a bitmap to locate a free space
56 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
57 * Otherwise, we have just read the data from the last_rcvd file and
60 int mds_client_add(struct obd_device *obd, struct mds_obd *mds,
61 struct mds_export_data *med, int cl_idx)
63 unsigned long *bitmap = mds->mds_client_bitmap;
64 int new_client = (cl_idx == -1);
67 LASSERT(bitmap != NULL);
69 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
70 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
73 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
74 * there's no need for extra complication here
77 cl_idx = find_first_zero_bit(bitmap, MDS_MAX_CLIENTS);
79 if (cl_idx >= MDS_MAX_CLIENTS) {
80 CERROR("no room for clients - fix MDS_MAX_CLIENTS\n");
83 if (test_and_set_bit(cl_idx, bitmap)) {
84 cl_idx = find_next_zero_bit(bitmap, MDS_MAX_CLIENTS,
89 if (test_and_set_bit(cl_idx, bitmap)) {
90 CERROR("MDS client %d: bit already set in bitmap!!\n",
96 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
97 cl_idx, med->med_mcd->mcd_uuid);
99 med->med_idx = cl_idx;
100 med->med_off = le32_to_cpu(mds->mds_server_data->msd_client_start) +
101 (cl_idx * le16_to_cpu(mds->mds_server_data->msd_client_size));
104 struct file *file = mds->mds_rcvd_filp;
105 struct lvfs_run_ctxt saved;
106 loff_t off = med->med_off;
109 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
110 rc = fsfilt_write_record(obd, file, med->med_mcd,
111 sizeof(*med->med_mcd), &off, 1);
112 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
116 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
117 med->med_idx, med->med_off,
118 (unsigned int)sizeof(*med->med_mcd));
123 int mds_client_free(struct obd_export *exp, int clear_client)
125 struct mds_export_data *med = &exp->exp_mds_data;
126 struct mds_obd *mds = &exp->exp_obd->u.mds;
127 unsigned long *bitmap = mds->mds_client_bitmap;
128 struct obd_device *obd = exp->exp_obd;
129 struct mds_client_data zero_mcd;
130 struct lvfs_run_ctxt saved;
136 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
137 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
138 GOTO(free_and_out, 0);
140 CDEBUG(D_INFO, "freeing client at idx %u (%lld)with UUID '%s'\n",
141 med->med_idx, med->med_off, med->med_mcd->mcd_uuid);
145 /* Clear the bit _after_ zeroing out the client so we don't
146 race with mds_client_add and zero out new clients.*/
147 if (!test_bit(med->med_idx, bitmap)) {
148 CERROR("MDS client %u: bit already clear in bitmap!!\n",
154 memset(&zero_mcd, 0, sizeof zero_mcd);
155 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
156 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
157 sizeof(zero_mcd), &med->med_off, 1);
158 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
160 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
161 "zeroing out client %s idx %u in %s rc %d\n",
162 med->med_mcd->mcd_uuid, med->med_idx, LAST_RCVD, rc);
165 if (!test_and_clear_bit(med->med_idx, bitmap)) {
166 CERROR("MDS client %u: bit already clear in bitmap!!\n",
172 /* Make sure the server's last_transno is up to date. Do this
173 * after the client is freed so we know all the client's
174 * transactions have been committed. */
175 mds_update_server_data(exp->exp_obd, 1);
178 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
183 static int mds_server_free_data(struct mds_obd *mds)
185 OBD_FREE(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
186 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
187 mds->mds_server_data = NULL;
192 static int mds_read_last_fid(struct obd_device *obd, struct file *file)
196 struct mds_obd *mds = &obd->u.mds;
197 unsigned long last_fid_size = file->f_dentry->d_inode->i_size;
200 if (last_fid_size == 0) {
201 CWARN("%s: initializing new %s\n", obd->obd_name,
202 file->f_dentry->d_name.name);
205 * as fid is used for forming res_id for locking, it should not
206 * be zero. This will keep us out of lots possible problems,
209 mds_set_last_fid(obd, 0);
213 rc = fsfilt_read_record(obd, file, &lastfid,
214 sizeof(lastfid), &off);
216 CERROR("error reading MDS %s: rc = %d\n",
217 file->f_dentry->d_name.name, rc);
222 * make sure, that fid is up-to-date.
224 mds_set_last_fid(obd, lastfid);
227 CDEBUG(D_INODE, "%s: server last_fid: "LPU64"\n",
228 obd->obd_name, mds->mds_last_fid);
230 rc = mds_update_last_fid(obd, NULL, 1);
234 static int mds_read_last_rcvd(struct obd_device *obd, struct file *file)
236 unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
237 struct mds_obd *mds = &obd->u.mds;
238 struct mds_server_data *msd = NULL;
239 struct mds_client_data *mcd = NULL;
245 /* ensure padding in the struct is the correct size */
246 LASSERT(offsetof(struct mds_server_data, msd_padding) +
247 sizeof(msd->msd_padding) == MDS_LR_SERVER_SIZE);
248 LASSERT(offsetof(struct mds_client_data, mcd_padding) +
249 sizeof(mcd->mcd_padding) == MDS_LR_CLIENT_SIZE);
251 OBD_ALLOC_WAIT(msd, sizeof(*msd));
255 OBD_ALLOC_WAIT(mds->mds_client_bitmap, MDS_MAX_CLIENTS / 8);
256 if (!mds->mds_client_bitmap) {
257 OBD_FREE(msd, sizeof(*msd));
261 mds->mds_server_data = msd;
263 if (last_rcvd_size == 0) {
264 CWARN("%s: initializing new %s\n", obd->obd_name,
265 file->f_dentry->d_name.name);
267 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,sizeof(msd->msd_uuid));
268 msd->msd_last_transno = 0;
269 mount_count = msd->msd_mount_count = 0;
270 msd->msd_server_size = cpu_to_le32(MDS_LR_SERVER_SIZE);
271 msd->msd_client_start = cpu_to_le32(MDS_LR_CLIENT_START);
272 msd->msd_client_size = cpu_to_le16(MDS_LR_CLIENT_SIZE);
273 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
275 rc = fsfilt_read_record(obd, file, msd, sizeof(*msd), &off);
277 CERROR("error reading MDS %s: rc = %d\n",
278 file->f_dentry->d_name.name, rc);
281 if (strcmp(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
282 CERROR("OBD UUID %s does not match last_rcvd UUID %s\n",
283 obd->obd_uuid.uuid, msd->msd_uuid);
284 GOTO(err_msd, rc = -EINVAL);
286 mount_count = le64_to_cpu(msd->msd_mount_count);
288 if (msd->msd_feature_incompat & ~cpu_to_le32(MDS_INCOMPAT_SUPP)) {
289 CERROR("unsupported incompat feature %x\n",
290 le32_to_cpu(msd->msd_feature_incompat) &
292 GOTO(err_msd, rc = -EINVAL);
294 /* XXX updating existing b_devel fs only, can be removed in future */
295 msd->msd_feature_rocompat = cpu_to_le32(MDS_ROCOMPAT_LOVOBJID);
296 if (msd->msd_feature_rocompat & ~cpu_to_le32(MDS_ROCOMPAT_SUPP)) {
297 CERROR("unsupported read-only feature %x\n",
298 le32_to_cpu(msd->msd_feature_rocompat) &
300 /* Do something like remount filesystem read-only */
301 GOTO(err_msd, rc = -EINVAL);
304 mds->mds_last_transno = le64_to_cpu(msd->msd_last_transno);
306 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
307 obd->obd_name, mds->mds_last_transno);
308 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
309 obd->obd_name, mount_count + 1);
310 CDEBUG(D_INODE, "%s: server data size: %u\n",
311 obd->obd_name, le32_to_cpu(msd->msd_server_size));
312 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
313 obd->obd_name, le32_to_cpu(msd->msd_client_start));
314 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
315 obd->obd_name, le32_to_cpu(msd->msd_client_size));
316 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
317 obd->obd_name, last_rcvd_size);
318 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
319 last_rcvd_size <= le32_to_cpu(msd->msd_client_start) ? 0 :
320 (last_rcvd_size - le32_to_cpu(msd->msd_client_start)) /
321 le16_to_cpu(msd->msd_client_size));
323 /* When we do a clean MDS shutdown, we save the last_transno into
324 * the header. If we find clients with higher last_transno values
325 * then those clients may need recovery done. */
326 for (cl_idx = 0, off = le32_to_cpu(msd->msd_client_start);
327 off < last_rcvd_size; cl_idx++) {
329 struct obd_export *exp;
330 struct mds_export_data *med;
333 OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
335 GOTO(err_client, rc = -ENOMEM);
338 /* Don't assume off is incremented properly by
339 * fsfilt_read_record(), in case sizeof(*mcd)
340 * isn't the same as msd->msd_client_size. */
341 off = le32_to_cpu(msd->msd_client_start) +
342 cl_idx * le16_to_cpu(msd->msd_client_size);
343 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
345 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
346 file->f_dentry->d_name.name, cl_idx, off, rc);
347 break; /* read error shouldn't cause startup to fail */
350 if (mcd->mcd_uuid[0] == '\0') {
351 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
356 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
357 le64_to_cpu(mcd->mcd_last_close_transno) ?
358 le64_to_cpu(mcd->mcd_last_transno) :
359 le64_to_cpu(mcd->mcd_last_close_transno);
361 /* These exports are cleaned up by mds_disconnect(), so they
362 * need to be set up like real exports as mds_connect() does.
364 CDEBUG(D_HA|D_WARNING,"RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
365 " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
366 last_transno, le64_to_cpu(msd->msd_last_transno),
369 exp = class_new_export(obd);
371 GOTO(err_client, rc = -ENOMEM);
373 memcpy(&exp->exp_client_uuid.uuid, mcd->mcd_uuid,
374 sizeof exp->exp_client_uuid.uuid);
375 med = &exp->exp_mds_data;
377 mds_client_add(obd, mds, med, cl_idx);
378 /* create helper if export init gets more complex */
379 INIT_LIST_HEAD(&med->med_open_head);
380 spin_lock_init(&med->med_open_lock);
383 exp->exp_connected = 0;
384 exp->exp_req_replay_needed = 1;
385 obd->obd_recoverable_clients++;
386 obd->obd_max_recoverable_clients++;
388 /* track clients to separate req replay
389 * from lock replay. bug 6063 */
390 atomic_inc(&obd->obd_req_replay_clients);
391 exp->exp_req_replay_needed = 1;
392 atomic_inc(&obd->obd_lock_replay_clients);
393 exp->exp_lock_replay_needed = 1;
395 class_export_put(exp);
397 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
398 cl_idx, last_transno);
400 if (last_transno > mds->mds_last_transno)
401 mds->mds_last_transno = last_transno;
404 OBD_FREE(mcd, sizeof(*mcd));
405 obd->obd_last_committed = mds->mds_last_transno;
406 if (obd->obd_recoverable_clients) {
407 CWARN("RECOVERY: service %s, %d recoverable clients, "
408 "last_transno "LPU64"\n", obd->obd_name,
409 obd->obd_recoverable_clients, mds->mds_last_transno);
410 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
411 target_start_recovery_thread(obd, mds_handle);
412 obd->obd_recovery_start = LTIME_S(CURRENT_TIME);
415 mds->mds_mount_count = mount_count + 1;
416 msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
418 /* save it, so mount count and last_transno is current */
419 rc = mds_update_server_data(obd, 1);
421 GOTO(err_client, rc);
426 class_disconnect_exports(obd, 0);
428 mds_server_free_data(mds);
432 static int mds_fs_post_setup(struct obd_device *obd)
434 struct mds_obd *mds = &obd->u.mds;
435 struct dentry *dentry;
439 dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
440 if (IS_ERR(dentry)) {
441 CERROR("Can't find ROOT, err = %d\n",
442 (int)PTR_ERR(dentry));
443 RETURN(PTR_ERR(dentry));
446 rc = fsfilt_post_setup(obd, dentry);
450 LASSERT(dentry->d_inode != NULL);
452 fsfilt_set_fs_flags(obd, dentry->d_inode,
453 SM_DO_REC | SM_DO_COW);
455 fsfilt_set_fs_flags(obd, mds->mds_pending_dir->d_inode,
456 SM_DO_REC | SM_DO_COW);
458 fsfilt_set_mds_flags(obd, mds->mds_sb);
466 * sets up root inode lustre_id. It tries to read it first from root inode and
467 * if it is not there, new rootid is allocated and saved there.
469 int mds_fs_setup_rootid(struct obd_device *obd)
474 struct dentry *dentry;
475 struct mds_obd *mds = &obd->u.mds;
478 /* getting root directory and setup its fid. */
479 dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
480 if (IS_ERR(dentry)) {
481 CERROR("Can't find ROOT by "DLID4", err = %d\n",
482 OLID4(&mds->mds_rootid), (int)PTR_ERR(dentry));
483 RETURN(PTR_ERR(dentry));
486 inode = dentry->d_inode;
487 LASSERT(dentry->d_inode);
489 rc = mds_pack_inode2id(obd, &mds->mds_rootid, inode, 1);
492 GOTO(out_dentry, rc);
495 * rootid is filled by mds_read_inode_sid(), so we do not need
496 * to allocate it and update. The only thing we need to check is
499 LASSERT(id_group(&mds->mds_rootid) == mds->mds_num);
500 mds_set_last_fid(obd, id_fid(&mds->mds_rootid));
501 GOTO(out_dentry, rc);
504 /* allocating new one, as it is not found in root inode. */
505 handle = fsfilt_start(obd, inode,
506 FSFILT_OP_SETATTR, NULL);
508 if (IS_ERR(handle)) {
509 rc = PTR_ERR(handle);
510 CERROR("fsfilt_start() failed, rc = %d\n", rc);
511 GOTO(out_dentry, rc);
515 rc = mds_alloc_inode_sid(obd, inode, handle, &mds->mds_rootid);
519 CERROR("mds_alloc_inode_sid() failed, rc = %d\n",
521 GOTO(out_dentry, rc);
524 rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
526 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
532 CWARN("%s: rootid: "DLID4"\n", obd->obd_name,
533 OLID4(&mds->mds_rootid));
537 static int mds_update_virtid_fid(struct obd_device *obd,
538 void *handle, int force_sync)
540 struct mds_obd *mds = &obd->u.mds;
541 struct file *filp = mds->mds_virtid_filp;
542 struct lvfs_run_ctxt saved;
547 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
548 rc = fsfilt_write_record(obd, filp, &mds->mds_virtid_fid,
549 sizeof(mds->mds_virtid_fid),
552 CERROR("error writing MDS virtid_fid #"LPU64
553 ", err = %d\n", mds->mds_virtid_fid, rc);
556 CDEBUG(D_SUPER, "wrote virtid fid #"LPU64" at idx "
557 "%llu: err = %d\n", mds->mds_virtid_fid,
559 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
564 static int mds_read_virtid_fid(struct obd_device *obd,
569 struct mds_obd *mds = &obd->u.mds;
570 unsigned long virtid_fid_size = file->f_dentry->d_inode->i_size;
573 if (virtid_fid_size == 0) {
574 mds->mds_virtid_fid = mds_alloc_fid(obd);
576 rc = fsfilt_read_record(obd, file, &mds->mds_virtid_fid,
577 sizeof(mds->mds_virtid_fid), &off);
579 CERROR("error reading MDS %s: rc = %d\n",
580 file->f_dentry->d_name.name, rc);
584 rc = mds_update_virtid_fid(obd, NULL, 1);
590 * initializes lustre_id for virtual id directory, it is needed sometimes, as it
591 * is possible that it will be the parent for object an operations is going to
594 int mds_fs_setup_virtid(struct obd_device *obd)
598 struct lustre_id sid;
599 struct mds_obd *mds = &obd->u.mds;
600 struct inode *inode = mds->mds_id_dir->d_inode;
603 handle = fsfilt_start(obd, inode,
604 FSFILT_OP_SETATTR, NULL);
606 if (IS_ERR(handle)) {
607 rc = PTR_ERR(handle);
608 CERROR("fsfilt_start() failed, rc = %d\n", rc);
612 id_group(&sid) = mds->mds_num;
613 id_fid(&sid) = mds->mds_virtid_fid;
615 id_ino(&sid) = inode->i_ino;
616 id_gen(&sid) = inode->i_generation;
617 id_type(&sid) = (S_IFMT & inode->i_mode);
620 rc = mds_update_inode_sid(obd, inode, handle, &sid);
624 CERROR("mds_update_inode_sid() failed, rc = %d\n",
629 rc = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
631 CERROR("fsfilt_commit() failed, rc = %d\n", rc);
638 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
640 struct mds_obd *mds = &obd->u.mds;
641 struct lvfs_run_ctxt saved;
642 struct dentry *dentry;
647 rc = cleanup_group_info();
651 mds->mds_vfsmnt = mnt;
652 mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
654 fsfilt_setup(obd, mds->mds_sb);
656 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
657 obd->obd_lvfs_ctxt.pwdmnt = mnt;
658 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
659 obd->obd_lvfs_ctxt.fs = get_ds();
660 obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
662 /* setup the directory tree */
663 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
664 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
665 if (IS_ERR(dentry)) {
666 rc = PTR_ERR(dentry);
667 CERROR("cannot create ROOT directory: rc = %d\n", rc);
671 mdc_pack_id(&mds->mds_rootid, dentry->d_inode->i_ino,
672 dentry->d_inode->i_generation, S_IFDIR, 0, 0);
676 dentry = lookup_one_len("__iopen__", current->fs->pwd,
677 strlen("__iopen__"));
678 if (IS_ERR(dentry)) {
679 rc = PTR_ERR(dentry);
680 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
683 mds->mds_id_de = dentry;
684 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
686 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
690 dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
691 if (IS_ERR(dentry)) {
692 rc = PTR_ERR(dentry);
693 CERROR("cannot create PENDING directory: rc = %d\n", rc);
696 mds->mds_pending_dir = dentry;
698 dentry = simple_mkdir(current->fs->pwd, "LOGS", 0777, 1);
699 if (IS_ERR(dentry)) {
700 rc = PTR_ERR(dentry);
701 CERROR("cannot create LOGS directory: rc = %d\n", rc);
702 GOTO(err_pending, rc);
704 mds->mds_logs_dir = dentry;
706 dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
707 if (IS_ERR(dentry)) {
708 rc = PTR_ERR(dentry);
709 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
712 mds->mds_objects_dir = dentry;
714 dentry = simple_mkdir(current->fs->pwd, "FIDS", 0777, 1);
715 if (IS_ERR(dentry)) {
716 rc = PTR_ERR(dentry);
717 CERROR("cannot create FIDS directory: rc = %d\n", rc);
718 GOTO(err_objects, rc);
720 mds->mds_id_dir = dentry;
722 dentry = simple_mkdir(current->fs->pwd, "UNNAMED", 0777, 1);
723 if (IS_ERR(dentry)) {
724 rc = PTR_ERR(dentry);
725 CERROR("cannot create UNNAMED directory: rc = %d\n", rc);
726 GOTO(err_unnamed, rc);
728 mds->mds_unnamed_dir = dentry;
730 /* open and test the last rcvd file */
731 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
734 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
735 GOTO(err_id_dir, rc = PTR_ERR(file));
737 mds->mds_rcvd_filp = file;
739 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
740 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
741 file->f_dentry->d_inode->i_mode);
742 GOTO(err_last_rcvd, rc = -ENOENT);
745 rc = mds_read_last_rcvd(obd, file);
747 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
748 GOTO(err_last_rcvd, rc);
751 /* open and test last fid file */
752 file = filp_open(LAST_FID, O_RDWR | O_CREAT, 0644);
755 CERROR("cannot open/create %s file: rc = %d\n",
757 GOTO(err_client, rc = PTR_ERR(file));
759 mds->mds_fid_filp = file;
760 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
761 CERROR("%s is not a regular file!: mode = %o\n",
762 LAST_FID, file->f_dentry->d_inode->i_mode);
763 GOTO(err_last_fid, rc = -ENOENT);
766 rc = mds_read_last_fid(obd, file);
768 CERROR("cannot read %s: rc = %d\n", LAST_FID, rc);
769 GOTO(err_last_fid, rc);
772 /* open and test virtid fid file */
773 file = filp_open(VIRT_FID, O_RDWR | O_CREAT, 0644);
776 CERROR("cannot open/create %s file: rc = %d\n",
778 GOTO(err_last_fid, rc = PTR_ERR(file));
780 mds->mds_virtid_filp = file;
781 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
782 CERROR("%s is not a regular file!: mode = %o\n",
783 VIRT_FID, file->f_dentry->d_inode->i_mode);
784 GOTO(err_virtid_fid, rc = -ENOENT);
787 rc = mds_read_virtid_fid(obd, file);
789 CERROR("cannot read %s: rc = %d\n", VIRT_FID, rc);
790 GOTO(err_virtid_fid, rc);
793 /* open and test the lov objid file */
794 file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
797 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
798 GOTO(err_last_fid, rc = PTR_ERR(file));
800 mds->mds_dt_objid_filp = file;
801 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
802 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
803 file->f_dentry->d_inode->i_mode);
804 GOTO(err_lov_objid, rc = -ENOENT);
808 rc = mds_fs_post_setup(obd);
810 CERROR("can not post setup fsfilt\n");
812 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
816 if (mds->mds_dt_objid_filp && filp_close(mds->mds_dt_objid_filp, 0))
817 CERROR("can't close %s after error\n", LOV_OBJID);
819 if (mds->mds_virtid_filp && filp_close(mds->mds_virtid_filp, 0))
820 CERROR("can't close %s after error\n", VIRT_FID);
822 if (mds->mds_fid_filp && filp_close(mds->mds_fid_filp, 0))
823 CERROR("can't close %s after error\n", LAST_FID);
825 class_disconnect_exports(obd, 0);
827 if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
828 CERROR("can't close %s after error\n", LAST_RCVD);
830 dput(mds->mds_unnamed_dir);
832 dput(mds->mds_id_dir);
834 dput(mds->mds_objects_dir);
836 dput(mds->mds_logs_dir);
838 dput(mds->mds_pending_dir);
840 dput(mds->mds_id_de);
844 static int mds_fs_post_cleanup(struct obd_device *obd)
847 rc = fsfilt_post_cleanup(obd);
851 int mds_fs_cleanup(struct obd_device *obd, int flags)
853 struct mds_obd *mds = &obd->u.mds;
854 struct lvfs_run_ctxt saved;
857 if (flags & OBD_OPT_FAILOVER)
858 CERROR("%s: shutting down for failover; client state will"
859 " be preserved.\n", obd->obd_name);
861 class_disconnect_exports(obd, flags); /* cleans up client info too */
862 target_cleanup_recovery(obd);
863 mds_server_free_data(mds);
865 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
866 if (mds->mds_virtid_filp) {
867 rc = filp_close(mds->mds_virtid_filp, 0);
868 mds->mds_virtid_filp = NULL;
870 CERROR("%s file won't close, rc = %d\n", VIRT_FID, rc);
872 if (mds->mds_fid_filp) {
873 rc = filp_close(mds->mds_fid_filp, 0);
874 mds->mds_fid_filp = NULL;
876 CERROR("%s file won't close, rc = %d\n", LAST_FID, rc);
878 if (mds->mds_rcvd_filp) {
879 rc = filp_close(mds->mds_rcvd_filp, 0);
880 mds->mds_rcvd_filp = NULL;
882 CERROR("%s file won't close, rc = %d\n", LAST_RCVD, rc);
884 if (mds->mds_dt_objid_filp) {
885 rc = filp_close(mds->mds_dt_objid_filp, 0);
886 mds->mds_dt_objid_filp = NULL;
888 CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
890 if (mds->mds_unnamed_dir != NULL) {
891 l_dput(mds->mds_unnamed_dir);
892 mds->mds_unnamed_dir = NULL;
894 if (mds->mds_id_dir != NULL) {
895 l_dput(mds->mds_id_dir);
896 mds->mds_id_dir = NULL;
898 if (mds->mds_objects_dir != NULL) {
899 l_dput(mds->mds_objects_dir);
900 mds->mds_objects_dir = NULL;
902 if (mds->mds_logs_dir) {
903 l_dput(mds->mds_logs_dir);
904 mds->mds_logs_dir = NULL;
906 if (mds->mds_pending_dir) {
907 l_dput(mds->mds_pending_dir);
908 mds->mds_pending_dir = NULL;
910 rc = mds_fs_post_cleanup(obd);
912 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
913 shrink_dcache_parent(mds->mds_id_de);
914 dput(mds->mds_id_de);
919 /* Creates an object with the same name as its id. Because this is not at all
920 * performance sensitive, it is accomplished by creating a file, checking the
921 * id, and renaming it. */
922 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
923 void *acl, int acl_size,
924 struct lov_stripe_md **ea, struct obd_trans_info *oti)
926 struct mds_obd *mds = &exp->exp_obd->u.mds;
927 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
929 struct dentry *dchild;
930 struct lvfs_run_ctxt saved;
931 char idname[LL_ID_NAMELEN];
932 int rc = 0, err, idlen;
936 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
937 down(&parent_inode->i_sem);
939 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
940 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
942 GOTO(out_pop, rc = PTR_ERR(dchild));
944 if (dchild->d_inode == NULL) {
945 struct dentry_params dp;
948 CWARN("creating log with ID "LPU64"\n", oa->o_id);
950 dchild->d_fsdata = (void *) &dp;
952 dp.p_inum = oa->o_id;
953 rc = ll_vfs_create(parent_inode, dchild, S_IFREG, NULL);
954 if (dchild->d_fsdata == (void *)(unsigned long)oa->o_id)
955 dchild->d_fsdata = NULL;
957 CDEBUG(D_INODE, "err during create: %d\n", rc);
961 inode = dchild->d_inode;
962 LASSERT(inode->i_ino == oa->o_id);
963 inode->i_generation = oa->o_generation;
964 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
965 inode->i_ino, inode->i_generation);
966 mark_inode_dirty(inode);
968 CWARN("it should be here!\n");
973 sprintf(idname, "OBJECTS/%u.%u", ll_insecure_random_int(), current->pid);
974 filp = filp_open(idname, O_CREAT | O_EXCL, 0644);
978 CERROR("impossible object name collision %s\n",
982 CERROR("error creating tmp object %s: rc %d\n",
987 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
989 oa->o_id = filp->f_dentry->d_inode->i_ino;
990 oa->o_generation = filp->f_dentry->d_inode->i_generation;
991 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
993 CWARN("created log anonymous "LPU64"/%u\n",
994 oa->o_id, oa->o_generation);
996 dchild = lookup_one_len(idname, mds->mds_objects_dir, idlen);
997 if (IS_ERR(dchild)) {
998 CERROR("getting neg dentry for obj rename: %d\n", rc);
999 GOTO(out_close, rc = PTR_ERR(dchild));
1001 if (dchild->d_inode != NULL) {
1002 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
1003 oa->o_id, oa->o_generation);
1007 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
1008 FSFILT_OP_RENAME, NULL);
1010 GOTO(out_dput, rc = PTR_ERR(handle));
1013 rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
1014 mds->mds_objects_dir->d_inode, dchild);
1017 CERROR("error renaming new object "LPU64":%u: rc %d\n",
1018 oa->o_id, oa->o_generation, rc);
1020 err = fsfilt_commit(exp->exp_obd, mds->mds_sb,
1021 mds->mds_objects_dir->d_inode, handle, 0);
1023 oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1024 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
1030 err = filp_close(filp, 0);
1032 CERROR("closing tmpfile %s: rc %d\n", idname, rc);
1037 up(&parent_inode->i_sem);
1038 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, NULL);
1042 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
1043 struct lov_stripe_md *ea, struct obd_trans_info *oti)
1045 struct mds_obd *mds = &exp->exp_obd->u.mds;
1046 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
1047 struct obd_device *obd = exp->exp_obd;
1048 struct lvfs_run_ctxt saved;
1049 char idname[LL_ID_NAMELEN];
1052 int err, idlen, rc = 0;
1055 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1057 idlen = ll_id2str(idname, oa->o_id, oa->o_generation);
1059 down(&parent_inode->i_sem);
1060 de = lookup_one_len(idname, mds->mds_objects_dir, idlen);
1061 if (IS_ERR(de) || de->d_inode == NULL) {
1062 rc = IS_ERR(de) ? PTR_ERR(de) : -ENOENT;
1063 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
1064 oa->o_id, idname, rc);
1067 /* Stripe count is 1 here since this is some MDS specific stuff
1068 that is unlinked, not spanned across multiple OSTs */
1069 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
1070 FSFILT_OP_UNLINK, oti, 1);
1073 GOTO(out_dput, rc = PTR_ERR(handle));
1075 rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
1077 CERROR("error destroying object "LPU64":%u: rc %d\n",
1078 oa->o_id, oa->o_generation, rc);
1080 err = fsfilt_commit(obd, mds->mds_sb, mds->mds_objects_dir->d_inode,
1081 handle, exp->exp_sync);
1087 up(&parent_inode->i_sem);
1088 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);