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 the Lustre file system, http://www.lustre.org
11 * Lustre is a trademark of Cluster File Systems, Inc.
13 * You may have signed or agreed to another license before downloading
14 * this software. If so, you are bound by the terms and conditions
15 * of that agreement, and the following does not apply to you. See the
16 * LICENSE file included with this distribution for more information.
18 * If you did not agree to a different license, then this copy of Lustre
19 * is open source software; you can redistribute it and/or modify it
20 * under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * In either case, Lustre is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * license text for more details.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/module.h>
35 #include <linux/kmod.h>
36 #include <linux/version.h>
37 #include <linux/sched.h>
38 #include <lustre_quota.h>
39 #include <linux/mount.h>
40 #include <lustre_mds.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_lib.h>
44 #include <lustre_fsfilt.h>
45 #include <lustre_disk.h>
46 #include <libcfs/list.h>
48 #include "mds_internal.h"
51 static int mds_export_stats_init(struct obd_device *obd,
52 struct obd_export *exp,
55 int rc, num_stats, newnid;
57 rc = lprocfs_exp_setup(exp, localdata, &newnid);
62 struct nid_stat *tmp = exp->exp_nid_stats;
64 num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
66 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
67 LPROCFS_STATS_FLAG_NOPERCPU);
68 if (tmp->nid_stats == NULL)
71 lprocfs_init_ops_stats(LPROC_MDS_LAST, tmp->nid_stats);
72 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
76 mds_stats_counter_init(tmp->nid_stats);
81 /* Add client data to the MDS. We use a bitmap to locate a free space
82 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
83 * Otherwise, we have just read the data from the last_rcvd file and
86 * It should not be possible to fail adding an existing client - otherwise
87 * mds_init_server_data() callsite needs to be fixed.
89 int mds_client_add(struct obd_device *obd, struct obd_export *exp,
90 int cl_idx, void *localdata)
92 struct mds_obd *mds = &obd->u.mds;
93 struct mds_export_data *med = &exp->exp_mds_data;
94 unsigned long *bitmap = mds->mds_client_bitmap;
95 int new_client = (cl_idx == -1);
98 LASSERT(bitmap != NULL);
99 LASSERTF(cl_idx > -2, "%d\n", cl_idx);
101 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
102 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
105 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
106 * there's no need for extra complication here
109 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
111 if (cl_idx >= LR_MAX_CLIENTS ||
112 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
113 CERROR("no room for %u client - fix LR_MAX_CLIENTS\n",
117 if (test_and_set_bit(cl_idx, bitmap)) {
118 cl_idx = find_next_zero_bit(bitmap, LR_MAX_CLIENTS,
123 if (test_and_set_bit(cl_idx, bitmap)) {
124 CERROR("MDS client %d: bit already set in bitmap!!\n",
130 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
131 cl_idx, med->med_mcd->mcd_uuid);
133 med->med_lr_idx = cl_idx;
134 med->med_lr_off = le32_to_cpu(mds->mds_server_data->lsd_client_start) +
135 (cl_idx * le16_to_cpu(mds->mds_server_data->lsd_client_size));
136 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
137 mds_export_stats_init(obd, exp, localdata);
140 struct lvfs_run_ctxt saved;
141 loff_t off = med->med_lr_off;
142 struct file *file = mds->mds_rcvd_filp;
146 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
147 handle = fsfilt_start(obd, file->f_dentry->d_inode,
148 FSFILT_OP_SETATTR, NULL);
149 if (IS_ERR(handle)) {
150 rc = PTR_ERR(handle);
151 CERROR("unable to start transaction: rc %d\n", rc);
153 rc = fsfilt_add_journal_cb(obd, 0, handle,
154 target_client_add_cb, exp);
156 spin_lock(&exp->exp_lock);
157 exp->exp_need_sync = 1;
158 spin_unlock(&exp->exp_lock);
160 rc = fsfilt_write_record(obd, file, med->med_mcd,
161 sizeof(*med->med_mcd),
162 &off, rc /* sync if no cb */);
163 fsfilt_commit(obd, file->f_dentry->d_inode, handle, 0);
166 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
170 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
171 med->med_lr_idx, med->med_lr_off,
172 (unsigned int)sizeof(*med->med_mcd));
177 int mds_client_free(struct obd_export *exp)
179 struct mds_export_data *med = &exp->exp_mds_data;
180 struct mds_obd *mds = &exp->exp_obd->u.mds;
181 struct obd_device *obd = exp->exp_obd;
182 struct mds_client_data zero_mcd;
183 struct lvfs_run_ctxt saved;
191 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
192 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
195 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
196 med->med_lr_idx, med->med_lr_off, med->med_mcd->mcd_uuid);
198 LASSERT(mds->mds_client_bitmap != NULL);
200 lprocfs_exp_cleanup(exp);
202 off = med->med_lr_off;
204 /* Don't clear med_lr_idx here as it is likely also unset. At worst
205 * we leak a client slot that will be cleaned on the next recovery. */
207 CERROR("%s: client idx %d has offset %lld\n",
208 obd->obd_name, med->med_lr_idx, off);
209 GOTO(free, rc = -EINVAL);
212 /* Clear the bit _after_ zeroing out the client so we don't
213 race with mds_client_add and zero out new clients.*/
214 if (!test_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
215 CERROR("MDS client %u: bit already clear in bitmap!!\n",
220 if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
221 memset(&zero_mcd, 0, sizeof zero_mcd);
222 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
223 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
224 sizeof(zero_mcd), &off,
225 (!exp->exp_libclient ||
226 exp->exp_need_sync));
227 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
229 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
230 "zeroing out client %s idx %u in %s rc %d\n",
231 med->med_mcd->mcd_uuid, med->med_lr_idx, LAST_RCVD, rc);
234 if (!test_and_clear_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
235 CERROR("MDS client %u: bit already clear in bitmap!!\n",
241 /* Make sure the server's last_transno is up to date. Do this
242 * after the client is freed so we know all the client's
243 * transactions have been committed. */
244 mds_update_server_data(exp->exp_obd, 0);
248 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
254 static int mds_server_free_data(struct mds_obd *mds)
256 OBD_FREE(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
257 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
258 mds->mds_server_data = NULL;
263 static int mds_init_server_data(struct obd_device *obd, struct file *file)
265 struct mds_obd *mds = &obd->u.mds;
266 struct lr_server_data *lsd;
267 struct mds_client_data *mcd = NULL;
269 unsigned long last_rcvd_size = i_size_read(file->f_dentry->d_inode);
274 /* ensure padding in the struct is the correct size */
275 LASSERT(offsetof(struct lr_server_data, lsd_padding) +
276 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
277 LASSERT(offsetof(struct mds_client_data, mcd_padding) +
278 sizeof(mcd->mcd_padding) == LR_CLIENT_SIZE);
280 OBD_ALLOC_WAIT(lsd, sizeof(*lsd));
284 OBD_ALLOC_WAIT(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
285 if (!mds->mds_client_bitmap) {
286 OBD_FREE(lsd, sizeof(*lsd));
290 mds->mds_server_data = lsd;
292 if (last_rcvd_size == 0) {
293 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
295 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(lsd->lsd_uuid));
296 lsd->lsd_last_transno = 0;
297 mount_count = lsd->lsd_mount_count = 0;
298 lsd->lsd_server_size = cpu_to_le32(LR_SERVER_SIZE);
299 lsd->lsd_client_start = cpu_to_le32(LR_CLIENT_START);
300 lsd->lsd_client_size = cpu_to_le16(LR_CLIENT_SIZE);
301 lsd->lsd_feature_rocompat = cpu_to_le32(OBD_ROCOMPAT_LOVOBJID);
302 lsd->lsd_feature_incompat = cpu_to_le32(OBD_INCOMPAT_MDT);
304 rc = fsfilt_read_record(obd, file, lsd, sizeof(*lsd), &off);
306 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
309 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
310 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
311 "using the wrong disk %s. Were the "
312 "/dev/ assignments rearranged?\n",
313 obd->obd_uuid.uuid, lsd->lsd_uuid);
314 GOTO(err_msd, rc = -EINVAL);
316 mount_count = le64_to_cpu(lsd->lsd_mount_count);
319 if (lsd->lsd_feature_incompat & ~cpu_to_le32(MDT_INCOMPAT_SUPP)) {
320 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
321 obd->obd_name, le32_to_cpu(lsd->lsd_feature_incompat) &
323 GOTO(err_msd, rc = -EINVAL);
325 if (lsd->lsd_feature_rocompat & ~cpu_to_le32(MDT_ROCOMPAT_SUPP)) {
326 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
327 obd->obd_name, le32_to_cpu(lsd->lsd_feature_rocompat) &
329 /* Do something like remount filesystem read-only */
330 GOTO(err_msd, rc = -EINVAL);
333 lsd->lsd_feature_compat = cpu_to_le32(OBD_COMPAT_MDT);
335 mds->mds_last_transno = le64_to_cpu(lsd->lsd_last_transno);
337 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
338 obd->obd_name, mds->mds_last_transno);
339 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
340 obd->obd_name, mount_count + 1);
341 CDEBUG(D_INODE, "%s: server data size: %u\n",
342 obd->obd_name, le32_to_cpu(lsd->lsd_server_size));
343 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
344 obd->obd_name, le32_to_cpu(lsd->lsd_client_start));
345 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
346 obd->obd_name, le32_to_cpu(lsd->lsd_client_size));
347 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
348 obd->obd_name, last_rcvd_size);
349 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
350 last_rcvd_size <= le32_to_cpu(lsd->lsd_client_start) ? 0 :
351 (last_rcvd_size - le32_to_cpu(lsd->lsd_client_start)) /
352 le16_to_cpu(lsd->lsd_client_size));
354 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
355 !lsd->lsd_client_size) {
356 CERROR("Bad last_rcvd contents!\n");
357 GOTO(err_msd, rc = -EINVAL);
360 /* When we do a clean MDS shutdown, we save the last_transno into
361 * the header. If we find clients with higher last_transno values
362 * then those clients may need recovery done. */
363 for (cl_idx = 0, off = le32_to_cpu(lsd->lsd_client_start);
364 off < last_rcvd_size; cl_idx++) {
366 struct obd_export *exp;
367 struct mds_export_data *med;
370 OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
372 GOTO(err_client, rc = -ENOMEM);
375 /* Don't assume off is incremented properly by
376 * fsfilt_read_record(), in case sizeof(*mcd)
377 * isn't the same as lsd->lsd_client_size. */
378 off = le32_to_cpu(lsd->lsd_client_start) +
379 cl_idx * le16_to_cpu(lsd->lsd_client_size);
380 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
382 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
383 LAST_RCVD, cl_idx, off, rc);
384 break; /* read error shouldn't cause startup to fail */
387 if (mcd->mcd_uuid[0] == '\0') {
388 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
393 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
394 le64_to_cpu(mcd->mcd_last_close_transno) ?
395 le64_to_cpu(mcd->mcd_last_transno) :
396 le64_to_cpu(mcd->mcd_last_close_transno);
398 /* These exports are cleaned up by mds_disconnect(), so they
399 * need to be set up like real exports as mds_connect() does.
401 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
402 " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
403 last_transno, le64_to_cpu(lsd->lsd_last_transno),
404 le64_to_cpu(mcd->mcd_last_xid));
406 exp = class_new_export(obd, (struct obd_uuid *)mcd->mcd_uuid);
408 if (PTR_ERR(exp) == -EALREADY) {
409 /* export already exists, zero out this one */
410 mcd->mcd_uuid[0] = '\0';
412 GOTO(err_client, rc = PTR_ERR(exp));
415 med = &exp->exp_mds_data;
417 rc = mds_client_add(obd, exp, cl_idx, NULL);
418 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
421 spin_lock(&exp->exp_lock);
422 exp->exp_req_replay_needed = 1;
423 exp->exp_connecting = 0;
424 spin_unlock(&exp->exp_lock);
425 obd->obd_max_recoverable_clients++;
426 class_export_put(exp);
428 /* Need to check last_rcvd even for duplicated exports. */
429 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
430 cl_idx, last_transno);
432 if (last_transno > mds->mds_last_transno)
433 mds->mds_last_transno = last_transno;
437 OBD_FREE(mcd, sizeof(*mcd));
439 obd->obd_last_committed = mds->mds_last_transno;
441 if (obd->obd_max_recoverable_clients) {
442 /* shouldn't happen in b_new_cmd */
444 CWARN("RECOVERY: service %s, %d recoverable clients, "
445 "last_transno "LPU64"\n", obd->obd_name,
446 obd->obd_max_recoverable_clients, mds->mds_last_transno);
447 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
448 obd->obd_recovering = 1;
449 obd->obd_recovery_start = cfs_time_current_sec();
450 /* Only used for lprocfs_status */
451 obd->obd_recovery_end = obd->obd_recovery_start +
452 OBD_RECOVERY_TIMEOUT;
455 mds->mds_mount_count = mount_count + 1;
456 lsd->lsd_mount_count = lsd->lsd_compat14 =
457 cpu_to_le64(mds->mds_mount_count);
459 /* save it, so mount count and last_transno is current */
460 rc = mds_update_server_data(obd, 1);
462 GOTO(err_client, rc);
467 class_disconnect_exports(obd);
469 mds_server_free_data(mds);
473 void mds_init_ctxt(struct obd_device *obd, struct vfsmount *mnt)
475 struct mds_obd *mds = &obd->u.mds;
477 mds->mds_vfsmnt = mnt;
478 /* why not mnt->mnt_sb instead of mnt->mnt_root->d_inode->i_sb? */
479 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
481 fsfilt_setup(obd, obd->u.obt.obt_sb);
483 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
484 obd->obd_lvfs_ctxt.pwdmnt = mnt;
485 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
486 obd->obd_lvfs_ctxt.fs = get_ds();
487 obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
491 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
493 struct mds_obd *mds = &obd->u.mds;
494 struct lvfs_run_ctxt saved;
495 struct dentry *dentry;
500 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
503 rc = cleanup_group_info();
507 mds_init_ctxt(obd, mnt);
509 /* setup the directory tree */
510 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
511 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
512 if (IS_ERR(dentry)) {
513 rc = PTR_ERR(dentry);
514 CERROR("cannot create ROOT directory: rc = %d\n", rc);
518 mds->mds_rootfid.id = dentry->d_inode->i_ino;
519 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
520 mds->mds_rootfid.f_type = S_IFDIR;
524 dentry = lookup_one_len("__iopen__", current->fs->pwd,
525 strlen("__iopen__"));
526 if (IS_ERR(dentry)) {
527 rc = PTR_ERR(dentry);
528 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
532 mds->mds_fid_de = dentry;
533 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
535 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
539 dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
540 if (IS_ERR(dentry)) {
541 rc = PTR_ERR(dentry);
542 CERROR("cannot create PENDING directory: rc = %d\n", rc);
545 mds->mds_pending_dir = dentry;
548 dentry = simple_mkdir(current->fs->pwd, MDT_LOGS_DIR, 0777, 1);
549 if (IS_ERR(dentry)) {
550 rc = PTR_ERR(dentry);
551 CERROR("cannot create %s directory: rc = %d\n",
553 GOTO(err_pending, rc);
555 mds->mds_logs_dir = dentry;
558 dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
559 if (IS_ERR(dentry)) {
560 rc = PTR_ERR(dentry);
561 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
564 mds->mds_objects_dir = dentry;
566 /* open and test the last rcvd file */
567 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
570 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
571 GOTO(err_objects, rc = PTR_ERR(file));
573 mds->mds_rcvd_filp = file;
574 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
575 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
576 file->f_dentry->d_inode->i_mode);
577 GOTO(err_last_rcvd, rc = -ENOENT);
580 rc = mds_init_server_data(obd, file);
582 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
583 GOTO(err_last_rcvd, rc);
586 /* open and test the lov objd file */
587 file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
590 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
591 GOTO(err_client, rc = PTR_ERR(file));
593 mds->mds_lov_objid_filp = file;
594 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
595 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
596 file->f_dentry->d_inode->i_mode);
597 GOTO(err_lov_objid, rc = -ENOENT);
600 /* open and test the check io file junk */
601 file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
604 CERROR("cannot open/create %s file: rc = %d\n", HEALTH_CHECK,
606 GOTO(err_lov_objid, rc = PTR_ERR(file));
608 mds->mds_health_check_filp = file;
609 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
610 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
611 file->f_dentry->d_inode->i_mode);
612 GOTO(err_health_check, rc = -ENOENT);
614 rc = lvfs_check_io_health(obd, file);
616 GOTO(err_health_check, rc);
618 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
623 if (mds->mds_health_check_filp &&
624 filp_close(mds->mds_health_check_filp, 0))
625 CERROR("can't close %s after error\n", HEALTH_CHECK);
627 if (mds->mds_lov_objid_filp &&
628 filp_close((struct file *)mds->mds_lov_objid_filp, 0))
629 CERROR("can't close %s after error\n", LOV_OBJID);
631 class_disconnect_exports(obd);
633 if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
634 CERROR("can't close %s after error\n", LAST_RCVD);
636 dput(mds->mds_objects_dir);
638 dput(mds->mds_logs_dir);
640 dput(mds->mds_pending_dir);
642 dput(mds->mds_fid_de);
646 int mds_fs_cleanup(struct obd_device *obd)
648 struct mds_obd *mds = &obd->u.mds;
649 struct lvfs_run_ctxt saved;
653 LCONSOLE_WARN("%s: shutting down for failover; client state "
654 "will be preserved.\n", obd->obd_name);
656 class_disconnect_exports(obd); /* cleans up client info too */
657 mds_server_free_data(mds);
659 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
660 if (mds->mds_rcvd_filp) {
661 rc = filp_close(mds->mds_rcvd_filp, 0);
662 mds->mds_rcvd_filp = NULL;
664 CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
666 if (mds->mds_lov_objid_filp) {
667 rc = filp_close((struct file *)mds->mds_lov_objid_filp, 0);
668 mds->mds_lov_objid_filp = NULL;
670 CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
672 if (mds->mds_health_check_filp) {
673 rc = filp_close(mds->mds_health_check_filp, 0);
674 mds->mds_health_check_filp = NULL;
676 CERROR("%s file won't close, rc=%d\n", HEALTH_CHECK,
679 if (mds->mds_objects_dir != NULL) {
680 l_dput(mds->mds_objects_dir);
681 mds->mds_objects_dir = NULL;
683 if (mds->mds_logs_dir) {
684 l_dput(mds->mds_logs_dir);
685 mds->mds_logs_dir = NULL;
687 if (mds->mds_pending_dir) {
688 l_dput(mds->mds_pending_dir);
689 mds->mds_pending_dir = NULL;
692 lquota_fs_cleanup(mds_quota_interface_ref, obd);
694 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
695 shrink_dcache_parent(mds->mds_fid_de);
696 dput(mds->mds_fid_de);
697 LL_DQUOT_OFF(obd->u.obt.obt_sb);
702 /* Creates an object with the same name as its fid. Because this is not at all
703 * performance sensitive, it is accomplished by creating a file, checking the
704 * fid, and renaming it. */
705 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
706 struct lov_stripe_md **ea, struct obd_trans_info *oti)
708 struct mds_obd *mds = &exp->exp_obd->u.mds;
709 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
710 unsigned int tmpname = ll_rand();
712 struct dentry *new_child;
713 struct lvfs_run_ctxt saved;
714 char fidname[LL_FID_NAMELEN];
716 struct lvfs_ucred ucred = { 0 };
717 int rc = 0, err, namelen;
720 /* the owner of object file should always be root */
721 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
723 if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
724 strlen(MDD_OBD_NAME))) {
728 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
730 sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
731 filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
735 CERROR("impossible object name collision %u\n",
739 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
743 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
745 oa->o_id = filp->f_dentry->d_inode->i_ino;
746 oa->o_generation = filp->f_dentry->d_inode->i_generation;
747 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
749 LOCK_INODE_MUTEX(parent_inode);
750 new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
752 if (IS_ERR(new_child)) {
753 CERROR("getting neg dentry for obj rename: %d\n", rc);
754 GOTO(out_close, rc = PTR_ERR(new_child));
756 if (new_child->d_inode != NULL) {
757 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
758 oa->o_id, oa->o_generation);
762 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
763 FSFILT_OP_RENAME, NULL);
765 GOTO(out_dput, rc = PTR_ERR(handle));
768 rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
769 mds->mds_objects_dir->d_inode, new_child);
772 CERROR("error renaming new object "LPU64":%u: rc %d\n",
773 oa->o_id, oa->o_generation, rc);
775 err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
778 oa->o_gr = FILTER_GROUP_MDS0 + mds->mds_id;
779 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
785 UNLOCK_INODE_MUTEX(parent_inode);
786 err = filp_close(filp, 0);
788 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
793 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
797 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
798 struct lov_stripe_md *ea, struct obd_trans_info *oti,
799 struct obd_export *md_exp)
801 struct mds_obd *mds = &exp->exp_obd->u.mds;
802 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
803 struct obd_device *obd = exp->exp_obd;
804 struct lvfs_run_ctxt saved;
805 struct lvfs_ucred ucred = { 0 };
806 char fidname[LL_FID_NAMELEN];
807 struct inode *inode = NULL;
810 int err, namelen, rc = 0;
813 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
814 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
816 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
818 LOCK_INODE_MUTEX(parent_inode);
819 de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
823 CERROR("error looking up object "LPU64" %s: rc %d\n",
824 oa->o_id, fidname, rc);
827 if (de->d_inode == NULL) {
828 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
829 oa->o_id, fidname, rc);
830 GOTO(out_dput, rc = -ENOENT);
833 /* Stripe count is 1 here since this is some MDS specific stuff
834 that is unlinked, not spanned across multiple OSTs */
835 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
836 FSFILT_OP_UNLINK, oti, 1);
839 GOTO(out_dput, rc = PTR_ERR(handle));
841 /* take a reference to protect inode from truncation within
842 vfs_unlink() context. bug 10409 */
844 atomic_inc(&inode->i_count);
845 rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
847 CERROR("error destroying object "LPU64":%u: rc %d\n",
848 oa->o_id, oa->o_generation, rc);
850 err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
856 UNLOCK_INODE_MUTEX(parent_inode);
861 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);