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,
53 lnet_nid_t client_nid)
55 int rc, num_stats, newnid;
56 rc = lprocfs_exp_setup(exp, client_nid, &newnid);
60 if (client_nid && newnid) {
61 struct nid_stat *tmp = exp->exp_nid_stats;
63 num_stats = (sizeof(*obd->obd_type->typ_dt_ops) / sizeof(void *)) +
65 tmp->nid_stats = lprocfs_alloc_stats(num_stats,
66 LPROCFS_STATS_FLAG_NOPERCPU);
67 if (tmp->nid_stats == NULL)
70 lprocfs_init_ops_stats(LPROC_MDS_LAST, tmp->nid_stats);
71 rc = lprocfs_register_stats(tmp->nid_proc, "stats",
75 mds_stats_counter_init(tmp->nid_stats);
80 /* Add client data to the MDS. We use a bitmap to locate a free space
81 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
82 * Otherwise, we have just read the data from the last_rcvd file and
85 * It should not be possible to fail adding an existing client - otherwise
86 * mds_init_server_data() callsite needs to be fixed.
88 int mds_client_add(struct obd_device *obd, struct obd_export *exp,
89 int cl_idx, lnet_nid_t client_nid)
91 struct mds_obd *mds = &obd->u.mds;
92 struct mds_export_data *med = &exp->exp_mds_data;
93 unsigned long *bitmap = mds->mds_client_bitmap;
94 int new_client = (cl_idx == -1);
97 LASSERT(bitmap != NULL);
98 LASSERTF(cl_idx > -2, "%d\n", cl_idx);
100 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
101 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
104 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
105 * there's no need for extra complication here
108 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
110 if (cl_idx >= LR_MAX_CLIENTS ||
111 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
112 CERROR("no room for %u client - fix LR_MAX_CLIENTS\n",
116 if (test_and_set_bit(cl_idx, bitmap)) {
117 cl_idx = find_next_zero_bit(bitmap, LR_MAX_CLIENTS,
122 if (test_and_set_bit(cl_idx, bitmap)) {
123 CERROR("MDS client %d: bit already set in bitmap!!\n",
129 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
130 cl_idx, med->med_mcd->mcd_uuid);
132 med->med_lr_idx = cl_idx;
133 med->med_lr_off = le32_to_cpu(mds->mds_server_data->lsd_client_start) +
134 (cl_idx * le16_to_cpu(mds->mds_server_data->lsd_client_size));
135 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
136 mds_export_stats_init(obd, exp, client_nid);
139 struct lvfs_run_ctxt saved;
140 loff_t off = med->med_lr_off;
141 struct file *file = mds->mds_rcvd_filp;
145 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
146 handle = fsfilt_start(obd, file->f_dentry->d_inode,
147 FSFILT_OP_SETATTR, NULL);
148 if (IS_ERR(handle)) {
149 rc = PTR_ERR(handle);
150 CERROR("unable to start transaction: rc %d\n", rc);
152 rc = fsfilt_add_journal_cb(obd, 0, handle,
153 target_client_add_cb, exp);
155 spin_lock(&exp->exp_lock);
156 exp->exp_need_sync = 1;
157 spin_unlock(&exp->exp_lock);
159 rc = fsfilt_write_record(obd, file, med->med_mcd,
160 sizeof(*med->med_mcd),
161 &off, rc /* sync if no cb */);
162 fsfilt_commit(obd, file->f_dentry->d_inode, handle, 0);
165 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
169 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
170 med->med_lr_idx, med->med_lr_off,
171 (unsigned int)sizeof(*med->med_mcd));
176 int mds_client_free(struct obd_export *exp)
178 struct mds_export_data *med = &exp->exp_mds_data;
179 struct mds_obd *mds = &exp->exp_obd->u.mds;
180 struct obd_device *obd = exp->exp_obd;
181 struct mds_client_data zero_mcd;
182 struct lvfs_run_ctxt saved;
190 /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
191 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
194 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
195 med->med_lr_idx, med->med_lr_off, med->med_mcd->mcd_uuid);
197 LASSERT(mds->mds_client_bitmap != NULL);
199 lprocfs_exp_cleanup(exp);
201 off = med->med_lr_off;
203 /* Don't clear med_lr_idx here as it is likely also unset. At worst
204 * we leak a client slot that will be cleaned on the next recovery. */
206 CERROR("%s: client idx %d has offset %lld\n",
207 obd->obd_name, med->med_lr_idx, off);
208 GOTO(free, rc = -EINVAL);
211 /* Clear the bit _after_ zeroing out the client so we don't
212 race with mds_client_add and zero out new clients.*/
213 if (!test_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
214 CERROR("MDS client %u: bit already clear in bitmap!!\n",
219 if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
220 memset(&zero_mcd, 0, sizeof zero_mcd);
221 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
222 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
223 sizeof(zero_mcd), &off,
224 (!exp->exp_libclient ||
225 exp->exp_need_sync));
226 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
228 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
229 "zeroing out client %s idx %u in %s rc %d\n",
230 med->med_mcd->mcd_uuid, med->med_lr_idx, LAST_RCVD, rc);
233 if (!test_and_clear_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
234 CERROR("MDS client %u: bit already clear in bitmap!!\n",
240 /* Make sure the server's last_transno is up to date. Do this
241 * after the client is freed so we know all the client's
242 * transactions have been committed. */
243 mds_update_server_data(exp->exp_obd, 0);
247 OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
253 static int mds_server_free_data(struct mds_obd *mds)
255 OBD_FREE(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
256 OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
257 mds->mds_server_data = NULL;
262 static int mds_init_server_data(struct obd_device *obd, struct file *file)
264 struct mds_obd *mds = &obd->u.mds;
265 struct lr_server_data *lsd;
266 struct mds_client_data *mcd = NULL;
268 unsigned long last_rcvd_size = i_size_read(file->f_dentry->d_inode);
273 /* ensure padding in the struct is the correct size */
274 LASSERT(offsetof(struct lr_server_data, lsd_padding) +
275 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
276 LASSERT(offsetof(struct mds_client_data, mcd_padding) +
277 sizeof(mcd->mcd_padding) == LR_CLIENT_SIZE);
279 OBD_ALLOC_WAIT(lsd, sizeof(*lsd));
283 OBD_ALLOC_WAIT(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
284 if (!mds->mds_client_bitmap) {
285 OBD_FREE(lsd, sizeof(*lsd));
289 mds->mds_server_data = lsd;
291 if (last_rcvd_size == 0) {
292 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
294 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(lsd->lsd_uuid));
295 lsd->lsd_last_transno = 0;
296 mount_count = lsd->lsd_mount_count = 0;
297 lsd->lsd_server_size = cpu_to_le32(LR_SERVER_SIZE);
298 lsd->lsd_client_start = cpu_to_le32(LR_CLIENT_START);
299 lsd->lsd_client_size = cpu_to_le16(LR_CLIENT_SIZE);
300 lsd->lsd_feature_rocompat = cpu_to_le32(OBD_ROCOMPAT_LOVOBJID);
301 lsd->lsd_feature_incompat = cpu_to_le32(OBD_INCOMPAT_MDT);
303 rc = fsfilt_read_record(obd, file, lsd, sizeof(*lsd), &off);
305 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
308 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
309 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
310 "using the wrong disk %s. Were the "
311 "/dev/ assignments rearranged?\n",
312 obd->obd_uuid.uuid, lsd->lsd_uuid);
313 GOTO(err_msd, rc = -EINVAL);
315 mount_count = le64_to_cpu(lsd->lsd_mount_count);
318 if (lsd->lsd_feature_incompat & ~cpu_to_le32(MDT_INCOMPAT_SUPP)) {
319 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
320 obd->obd_name, le32_to_cpu(lsd->lsd_feature_incompat) &
322 GOTO(err_msd, rc = -EINVAL);
324 if (lsd->lsd_feature_rocompat & ~cpu_to_le32(MDT_ROCOMPAT_SUPP)) {
325 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
326 obd->obd_name, le32_to_cpu(lsd->lsd_feature_rocompat) &
328 /* Do something like remount filesystem read-only */
329 GOTO(err_msd, rc = -EINVAL);
332 lsd->lsd_feature_compat = cpu_to_le32(OBD_COMPAT_MDT);
334 mds->mds_last_transno = le64_to_cpu(lsd->lsd_last_transno);
336 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
337 obd->obd_name, mds->mds_last_transno);
338 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
339 obd->obd_name, mount_count + 1);
340 CDEBUG(D_INODE, "%s: server data size: %u\n",
341 obd->obd_name, le32_to_cpu(lsd->lsd_server_size));
342 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
343 obd->obd_name, le32_to_cpu(lsd->lsd_client_start));
344 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
345 obd->obd_name, le32_to_cpu(lsd->lsd_client_size));
346 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
347 obd->obd_name, last_rcvd_size);
348 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
349 last_rcvd_size <= le32_to_cpu(lsd->lsd_client_start) ? 0 :
350 (last_rcvd_size - le32_to_cpu(lsd->lsd_client_start)) /
351 le16_to_cpu(lsd->lsd_client_size));
353 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
354 !lsd->lsd_client_size) {
355 CERROR("Bad last_rcvd contents!\n");
356 GOTO(err_msd, rc = -EINVAL);
359 /* When we do a clean MDS shutdown, we save the last_transno into
360 * the header. If we find clients with higher last_transno values
361 * then those clients may need recovery done. */
362 for (cl_idx = 0, off = le32_to_cpu(lsd->lsd_client_start);
363 off < last_rcvd_size; cl_idx++) {
365 struct obd_export *exp;
366 struct mds_export_data *med;
369 OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
371 GOTO(err_client, rc = -ENOMEM);
374 /* Don't assume off is incremented properly by
375 * fsfilt_read_record(), in case sizeof(*mcd)
376 * isn't the same as lsd->lsd_client_size. */
377 off = le32_to_cpu(lsd->lsd_client_start) +
378 cl_idx * le16_to_cpu(lsd->lsd_client_size);
379 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
381 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
382 LAST_RCVD, cl_idx, off, rc);
383 break; /* read error shouldn't cause startup to fail */
386 if (mcd->mcd_uuid[0] == '\0') {
387 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
392 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
393 le64_to_cpu(mcd->mcd_last_close_transno) ?
394 le64_to_cpu(mcd->mcd_last_transno) :
395 le64_to_cpu(mcd->mcd_last_close_transno);
397 /* These exports are cleaned up by mds_disconnect(), so they
398 * need to be set up like real exports as mds_connect() does.
400 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
401 " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
402 last_transno, le64_to_cpu(lsd->lsd_last_transno),
403 le64_to_cpu(mcd->mcd_last_xid));
405 exp = class_new_export(obd, (struct obd_uuid *)mcd->mcd_uuid);
407 if (PTR_ERR(exp) == -EALREADY) {
408 /* export already exists, zero out this one */
409 mcd->mcd_uuid[0] = '\0';
411 GOTO(err_client, rc = PTR_ERR(exp));
414 med = &exp->exp_mds_data;
416 rc = mds_client_add(obd, exp, cl_idx, 0);
417 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
420 spin_lock(&exp->exp_lock);
421 exp->exp_req_replay_needed = 1;
422 exp->exp_connecting = 0;
423 spin_unlock(&exp->exp_lock);
424 obd->obd_max_recoverable_clients++;
425 class_export_put(exp);
427 /* Need to check last_rcvd even for duplicated exports. */
428 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
429 cl_idx, last_transno);
431 if (last_transno > mds->mds_last_transno)
432 mds->mds_last_transno = last_transno;
436 OBD_FREE(mcd, sizeof(*mcd));
438 obd->obd_last_committed = mds->mds_last_transno;
440 if (obd->obd_max_recoverable_clients) {
441 /* shouldn't happen in b_new_cmd */
443 CWARN("RECOVERY: service %s, %d recoverable clients, "
444 "last_transno "LPU64"\n", obd->obd_name,
445 obd->obd_max_recoverable_clients, mds->mds_last_transno);
446 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
447 obd->obd_recovering = 1;
448 obd->obd_recovery_start = CURRENT_SECONDS;
449 /* Only used for lprocfs_status */
450 obd->obd_recovery_end = obd->obd_recovery_start +
451 OBD_RECOVERY_TIMEOUT;
454 mds->mds_mount_count = mount_count + 1;
455 lsd->lsd_mount_count = lsd->lsd_compat14 =
456 cpu_to_le64(mds->mds_mount_count);
458 /* save it, so mount count and last_transno is current */
459 rc = mds_update_server_data(obd, 1);
461 GOTO(err_client, rc);
466 class_disconnect_exports(obd);
468 mds_server_free_data(mds);
472 void mds_init_ctxt(struct obd_device *obd, struct vfsmount *mnt)
474 struct mds_obd *mds = &obd->u.mds;
476 mds->mds_vfsmnt = mnt;
477 /* why not mnt->mnt_sb instead of mnt->mnt_root->d_inode->i_sb? */
478 obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
480 fsfilt_setup(obd, obd->u.obt.obt_sb);
482 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
483 obd->obd_lvfs_ctxt.pwdmnt = mnt;
484 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
485 obd->obd_lvfs_ctxt.fs = get_ds();
486 obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
490 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
492 struct mds_obd *mds = &obd->u.mds;
493 struct lvfs_run_ctxt saved;
494 struct dentry *dentry;
499 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
502 rc = cleanup_group_info();
506 mds_init_ctxt(obd, mnt);
508 /* setup the directory tree */
509 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
510 dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
511 if (IS_ERR(dentry)) {
512 rc = PTR_ERR(dentry);
513 CERROR("cannot create ROOT directory: rc = %d\n", rc);
517 mds->mds_rootfid.id = dentry->d_inode->i_ino;
518 mds->mds_rootfid.generation = dentry->d_inode->i_generation;
519 mds->mds_rootfid.f_type = S_IFDIR;
523 dentry = lookup_one_len("__iopen__", current->fs->pwd,
524 strlen("__iopen__"));
525 if (IS_ERR(dentry)) {
526 rc = PTR_ERR(dentry);
527 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
531 mds->mds_fid_de = dentry;
532 if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
534 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
538 dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
539 if (IS_ERR(dentry)) {
540 rc = PTR_ERR(dentry);
541 CERROR("cannot create PENDING directory: rc = %d\n", rc);
544 mds->mds_pending_dir = dentry;
547 dentry = simple_mkdir(current->fs->pwd, MDT_LOGS_DIR, 0777, 1);
548 if (IS_ERR(dentry)) {
549 rc = PTR_ERR(dentry);
550 CERROR("cannot create %s directory: rc = %d\n",
552 GOTO(err_pending, rc);
554 mds->mds_logs_dir = dentry;
557 dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
558 if (IS_ERR(dentry)) {
559 rc = PTR_ERR(dentry);
560 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
563 mds->mds_objects_dir = dentry;
565 /* open and test the last rcvd file */
566 file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
569 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
570 GOTO(err_objects, rc = PTR_ERR(file));
572 mds->mds_rcvd_filp = file;
573 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
574 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
575 file->f_dentry->d_inode->i_mode);
576 GOTO(err_last_rcvd, rc = -ENOENT);
579 rc = mds_init_server_data(obd, file);
581 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
582 GOTO(err_last_rcvd, rc);
585 /* open and test the lov objd file */
586 file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
589 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
590 GOTO(err_client, rc = PTR_ERR(file));
592 mds->mds_lov_objid_filp = file;
593 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
594 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
595 file->f_dentry->d_inode->i_mode);
596 GOTO(err_lov_objid, rc = -ENOENT);
599 /* open and test the check io file junk */
600 file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
603 CERROR("cannot open/create %s file: rc = %d\n", HEALTH_CHECK,
605 GOTO(err_lov_objid, rc = PTR_ERR(file));
607 mds->mds_health_check_filp = file;
608 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
609 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
610 file->f_dentry->d_inode->i_mode);
611 GOTO(err_health_check, rc = -ENOENT);
613 rc = lvfs_check_io_health(obd, file);
615 GOTO(err_health_check, rc);
617 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
622 if (mds->mds_health_check_filp &&
623 filp_close(mds->mds_health_check_filp, 0))
624 CERROR("can't close %s after error\n", HEALTH_CHECK);
626 if (mds->mds_lov_objid_filp &&
627 filp_close((struct file *)mds->mds_lov_objid_filp, 0))
628 CERROR("can't close %s after error\n", LOV_OBJID);
630 class_disconnect_exports(obd);
632 if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
633 CERROR("can't close %s after error\n", LAST_RCVD);
635 dput(mds->mds_objects_dir);
637 dput(mds->mds_logs_dir);
639 dput(mds->mds_pending_dir);
641 dput(mds->mds_fid_de);
645 int mds_fs_cleanup(struct obd_device *obd)
647 struct mds_obd *mds = &obd->u.mds;
648 struct lvfs_run_ctxt saved;
652 LCONSOLE_WARN("%s: shutting down for failover; client state "
653 "will be preserved.\n", obd->obd_name);
655 class_disconnect_exports(obd); /* cleans up client info too */
656 mds_server_free_data(mds);
658 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
659 if (mds->mds_rcvd_filp) {
660 rc = filp_close(mds->mds_rcvd_filp, 0);
661 mds->mds_rcvd_filp = NULL;
663 CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
665 if (mds->mds_lov_objid_filp) {
666 rc = filp_close((struct file *)mds->mds_lov_objid_filp, 0);
667 mds->mds_lov_objid_filp = NULL;
669 CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
671 if (mds->mds_health_check_filp) {
672 rc = filp_close(mds->mds_health_check_filp, 0);
673 mds->mds_health_check_filp = NULL;
675 CERROR("%s file won't close, rc=%d\n", HEALTH_CHECK,
678 if (mds->mds_objects_dir != NULL) {
679 l_dput(mds->mds_objects_dir);
680 mds->mds_objects_dir = NULL;
682 if (mds->mds_logs_dir) {
683 l_dput(mds->mds_logs_dir);
684 mds->mds_logs_dir = NULL;
686 if (mds->mds_pending_dir) {
687 l_dput(mds->mds_pending_dir);
688 mds->mds_pending_dir = NULL;
691 lquota_fs_cleanup(mds_quota_interface_ref, obd);
693 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
694 shrink_dcache_parent(mds->mds_fid_de);
695 dput(mds->mds_fid_de);
696 LL_DQUOT_OFF(obd->u.obt.obt_sb);
701 /* Creates an object with the same name as its fid. Because this is not at all
702 * performance sensitive, it is accomplished by creating a file, checking the
703 * fid, and renaming it. */
704 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
705 struct lov_stripe_md **ea, struct obd_trans_info *oti)
707 struct mds_obd *mds = &exp->exp_obd->u.mds;
708 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
709 unsigned int tmpname = ll_rand();
711 struct dentry *new_child;
712 struct lvfs_run_ctxt saved;
713 char fidname[LL_FID_NAMELEN];
715 struct lvfs_ucred ucred = { 0 };
716 int rc = 0, err, namelen;
719 /* the owner of object file should always be root */
720 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
722 if (strncmp(exp->exp_obd->obd_name, MDD_OBD_NAME,
723 strlen(MDD_OBD_NAME))) {
727 push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
729 sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
730 filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
734 CERROR("impossible object name collision %u\n",
738 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
742 LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
744 oa->o_id = filp->f_dentry->d_inode->i_ino;
745 oa->o_generation = filp->f_dentry->d_inode->i_generation;
746 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
748 LOCK_INODE_MUTEX(parent_inode);
749 new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
751 if (IS_ERR(new_child)) {
752 CERROR("getting neg dentry for obj rename: %d\n", rc);
753 GOTO(out_close, rc = PTR_ERR(new_child));
755 if (new_child->d_inode != NULL) {
756 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
757 oa->o_id, oa->o_generation);
761 handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
762 FSFILT_OP_RENAME, NULL);
764 GOTO(out_dput, rc = PTR_ERR(handle));
767 rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
768 mds->mds_objects_dir->d_inode, new_child);
771 CERROR("error renaming new object "LPU64":%u: rc %d\n",
772 oa->o_id, oa->o_generation, rc);
774 err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
777 oa->o_gr = FILTER_GROUP_MDS0 + mds->mds_id;
778 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLGROUP;
784 UNLOCK_INODE_MUTEX(parent_inode);
785 err = filp_close(filp, 0);
787 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
792 pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
796 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
797 struct lov_stripe_md *ea, struct obd_trans_info *oti,
798 struct obd_export *md_exp)
800 struct mds_obd *mds = &exp->exp_obd->u.mds;
801 struct inode *parent_inode = mds->mds_objects_dir->d_inode;
802 struct obd_device *obd = exp->exp_obd;
803 struct lvfs_run_ctxt saved;
804 struct lvfs_ucred ucred = { 0 };
805 char fidname[LL_FID_NAMELEN];
806 struct inode *inode = NULL;
809 int err, namelen, rc = 0;
812 cap_raise(ucred.luc_cap, CAP_SYS_RESOURCE);
813 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
815 namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
817 LOCK_INODE_MUTEX(parent_inode);
818 de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
822 CERROR("error looking up object "LPU64" %s: rc %d\n",
823 oa->o_id, fidname, rc);
826 if (de->d_inode == NULL) {
827 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
828 oa->o_id, fidname, rc);
829 GOTO(out_dput, rc = -ENOENT);
832 /* Stripe count is 1 here since this is some MDS specific stuff
833 that is unlinked, not spanned across multiple OSTs */
834 handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
835 FSFILT_OP_UNLINK, oti, 1);
838 GOTO(out_dput, rc = PTR_ERR(handle));
840 /* take a reference to protect inode from truncation within
841 vfs_unlink() context. bug 10409 */
843 atomic_inc(&inode->i_count);
844 rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
846 CERROR("error destroying object "LPU64":%u: rc %d\n",
847 oa->o_id, oa->o_generation, rc);
849 err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
855 UNLOCK_INODE_MUTEX(parent_inode);
860 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);