Whamcloud - gitweb
3f65d73b2040b6a2d477f398616174b3a7a99345
[fs/lustre-release.git] / lustre / mds / mds_fs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  mds/mds_fs.c
5  *  Lustre Metadata Server (MDS) filesystem interface code
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
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.
17  *
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.
22  *
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.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
40 #include <linux/mount.h>
41 #endif
42 #include <lustre_mds.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_lib.h>
46 #include <lustre_fsfilt.h>
47 #include <lustre_disk.h>
48 #include <libcfs/list.h>
49
50 #include "mds_internal.h"
51
52
53 static int mds_export_stats_init(struct obd_device *obd, struct obd_export *exp)
54 {
55         int rc, num_stats;
56
57         rc = lprocfs_exp_setup(exp);
58         if (rc) 
59                 return rc;
60         num_stats = (sizeof(*obd->obd_type->typ_ops) / sizeof(void *)) +
61                      LPROC_MDS_LAST - 1;
62         exp->exp_ops_stats = lprocfs_alloc_stats(num_stats);
63         if (exp->exp_ops_stats == NULL)
64                 return -ENOMEM;
65         lprocfs_init_ops_stats(LPROC_MDS_LAST, exp->exp_ops_stats);
66         mds_stats_counter_init(exp->exp_ops_stats);
67         lprocfs_register_stats(exp->exp_proc, "stats", exp->exp_ops_stats);
68         return 0;
69 }
70
71 /* Add client data to the MDS.  We use a bitmap to locate a free space
72  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
73  * Otherwise, we have just read the data from the last_rcvd file and
74  * we know its offset.
75  *
76  * It should not be possible to fail adding an existing client - otherwise
77  * mds_init_server_data() callsite needs to be fixed.
78  */
79 int mds_client_add(struct obd_device *obd, struct obd_export *exp,
80                    int cl_idx)
81 {
82         struct mds_obd *mds = &obd->u.mds;
83         struct mds_export_data *med = &exp->exp_mds_data;
84         unsigned long *bitmap = mds->mds_client_bitmap;
85         int new_client = (cl_idx == -1);
86         int rc;
87         ENTRY;
88
89         LASSERT(bitmap != NULL);
90         LASSERTF(cl_idx > -2, "%d\n", cl_idx);
91
92         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
93         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
94                 RETURN(0);
95
96         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
97          * there's no need for extra complication here
98          */
99         if (new_client) {
100                 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
101         repeat:
102                 if (cl_idx >= LR_MAX_CLIENTS ||
103                     OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_CLIENT_ADD)) {
104                         CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
105                                cl_idx);
106                         return -EOVERFLOW;
107                 }
108                 if (test_and_set_bit(cl_idx, bitmap)) {
109                         cl_idx = find_next_zero_bit(bitmap, LR_MAX_CLIENTS,
110                                                     cl_idx);
111                         goto repeat;
112                 }
113         } else {
114                 if (test_and_set_bit(cl_idx, bitmap)) {
115                         CERROR("MDS client %d: bit already set in bitmap!!\n",
116                                cl_idx);
117                         LBUG();
118                 }
119         }
120
121         CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
122                cl_idx, med->med_mcd->mcd_uuid);
123
124         med->med_lr_idx = cl_idx;
125         med->med_lr_off = le32_to_cpu(mds->mds_server_data->lsd_client_start) +
126                 (cl_idx * le16_to_cpu(mds->mds_server_data->lsd_client_size));
127         LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
128         mds_export_stats_init(obd, exp);
129
130         if (new_client) {
131                 struct lvfs_run_ctxt saved;
132                 loff_t off = med->med_lr_off;
133                 struct file *file = mds->mds_rcvd_filp;
134                 void *handle;
135
136                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
137                 handle = fsfilt_start(obd, file->f_dentry->d_inode,
138                                       FSFILT_OP_SETATTR, NULL);
139                 if (IS_ERR(handle)) {
140                         rc = PTR_ERR(handle);
141                         CERROR("unable to start transaction: rc %d\n", rc);
142                 } else {
143                         rc = fsfilt_add_journal_cb(obd, 0, handle,
144                                                    target_client_add_cb, exp);
145                         if (rc == 0)
146                                 exp->exp_need_sync = 1;
147                         rc = fsfilt_write_record(obd, file, med->med_mcd,
148                                                  sizeof(*med->med_mcd),
149                                                  &off, rc /* sync if no cb */);
150                         fsfilt_commit(obd, file->f_dentry->d_inode, handle, 0);
151                 }
152
153                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
154
155                 if (rc)
156                         return rc;
157                 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
158                        med->med_lr_idx, med->med_lr_off,
159                        (unsigned int)sizeof(*med->med_mcd));
160         }
161         return 0;
162 }
163
164 int mds_client_free(struct obd_export *exp)
165 {
166         struct mds_export_data *med = &exp->exp_mds_data;
167         struct mds_obd *mds = &exp->exp_obd->u.mds;
168         struct obd_device *obd = exp->exp_obd;
169         struct mds_client_data zero_mcd;
170         struct lvfs_run_ctxt saved;
171         int rc;
172         loff_t off;
173         ENTRY;
174
175         if (!med->med_mcd)
176                 RETURN(0);
177
178         /* XXX if mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
179         if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
180                 GOTO(free, 0);
181
182         CDEBUG(D_INFO, "freeing client at idx %u, offset %lld with UUID '%s'\n",
183                med->med_lr_idx, med->med_lr_off, med->med_mcd->mcd_uuid);
184
185         LASSERT(mds->mds_client_bitmap != NULL);
186
187         lprocfs_exp_cleanup(exp);
188
189         off = med->med_lr_off;
190
191         /* Don't clear med_lr_idx here as it is likely also unset.  At worst
192          * we leak a client slot that will be cleaned on the next recovery. */
193         if (off <= 0) {
194                 CERROR("%s: client idx %d has offset %lld\n",
195                         obd->obd_name, med->med_lr_idx, off);
196                 GOTO(free, rc = -EINVAL);
197         }
198
199         /* Clear the bit _after_ zeroing out the client so we don't
200            race with mds_client_add and zero out new clients.*/
201         if (!test_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
202                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
203                        med->med_lr_idx);
204                 LBUG();
205         }
206
207         if (!(exp->exp_flags & OBD_OPT_FAILOVER)) {
208                 memset(&zero_mcd, 0, sizeof zero_mcd);
209                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
210                 rc = fsfilt_write_record(obd, mds->mds_rcvd_filp, &zero_mcd,
211                                          sizeof(zero_mcd), &off,
212                                          !exp->exp_libclient);
213                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
214
215                 CDEBUG(rc == 0 ? D_INFO : D_ERROR,
216                        "zeroing out client %s idx %u in %s rc %d\n",
217                        med->med_mcd->mcd_uuid, med->med_lr_idx, LAST_RCVD, rc);
218         }
219
220         if (!test_and_clear_bit(med->med_lr_idx, mds->mds_client_bitmap)) {
221                 CERROR("MDS client %u: bit already clear in bitmap!!\n",
222                        med->med_lr_idx);
223                 LBUG();
224         }
225
226
227         /* Make sure the server's last_transno is up to date. Do this
228          * after the client is freed so we know all the client's
229          * transactions have been committed. */
230         mds_update_server_data(exp->exp_obd, 0);
231
232         EXIT;
233  free:
234         OBD_FREE(med->med_mcd, sizeof(*med->med_mcd));
235         med->med_mcd = NULL;
236
237         return 0;
238 }
239
240 static int mds_server_free_data(struct mds_obd *mds)
241 {
242         OBD_FREE(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
243         OBD_FREE(mds->mds_server_data, sizeof(*mds->mds_server_data));
244         mds->mds_server_data = NULL;
245
246         return 0;
247 }
248
249 static int mds_init_server_data(struct obd_device *obd, struct file *file)
250 {
251         struct mds_obd *mds = &obd->u.mds;
252         struct lr_server_data *lsd;
253         struct mds_client_data *mcd = NULL;
254         loff_t off = 0;
255         unsigned long last_rcvd_size = file->f_dentry->d_inode->i_size;
256         __u64 mount_count;
257         int cl_idx, rc = 0;
258         ENTRY;
259
260         /* ensure padding in the struct is the correct size */
261         LASSERT(offsetof(struct lr_server_data, lsd_padding) +
262                 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
263         LASSERT(offsetof(struct mds_client_data, mcd_padding) +
264                 sizeof(mcd->mcd_padding) == LR_CLIENT_SIZE);
265
266         OBD_ALLOC_WAIT(lsd, sizeof(*lsd));
267         if (!lsd)
268                 RETURN(-ENOMEM);
269
270         OBD_ALLOC_WAIT(mds->mds_client_bitmap, LR_MAX_CLIENTS / 8);
271         if (!mds->mds_client_bitmap) {
272                 OBD_FREE(lsd, sizeof(*lsd));
273                 RETURN(-ENOMEM);
274         }
275
276         mds->mds_server_data = lsd;
277
278         if (last_rcvd_size == 0) {
279                 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
280
281                 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,sizeof(lsd->lsd_uuid));
282                 lsd->lsd_last_transno = 0;
283                 mount_count = lsd->lsd_mount_count = 0;
284                 lsd->lsd_server_size = cpu_to_le32(LR_SERVER_SIZE);
285                 lsd->lsd_client_start = cpu_to_le32(LR_CLIENT_START);
286                 lsd->lsd_client_size = cpu_to_le16(LR_CLIENT_SIZE);
287                 lsd->lsd_feature_rocompat = cpu_to_le32(OBD_ROCOMPAT_LOVOBJID);
288                 lsd->lsd_feature_incompat = cpu_to_le32(OBD_INCOMPAT_MDT);
289         } else {
290                 rc = fsfilt_read_record(obd, file, lsd, sizeof(*lsd), &off);
291                 if (rc) {
292                         CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
293                         GOTO(err_msd, rc);
294                 }
295                 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
296                         LCONSOLE_ERROR("Trying to start OBD %s using the wrong"
297                                        " disk %s. Were the /dev/ assignments "
298                                        "rearranged?\n",
299                                        obd->obd_uuid.uuid, lsd->lsd_uuid);
300                         GOTO(err_msd, rc = -EINVAL);
301                 }
302                 /* COMPAT_146 */
303                 /* Assume old last_rcvd format unless I_C_LR is set */
304                 if (!(lsd->lsd_feature_incompat & 
305                       cpu_to_le32(OBD_INCOMPAT_COMMON_LR)))
306                         lsd->lsd_mount_count = lsd->lsd_compat14;
307                 /* end COMPAT_146 */
308                 mount_count = le64_to_cpu(lsd->lsd_mount_count);
309         }
310
311         if (lsd->lsd_feature_incompat & ~cpu_to_le32(MDT_INCOMPAT_SUPP)) {
312                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
313                        obd->obd_name, le32_to_cpu(lsd->lsd_feature_incompat) &
314                        ~MDT_INCOMPAT_SUPP);
315                 GOTO(err_msd, rc = -EINVAL);
316         }
317         if (lsd->lsd_feature_rocompat & ~cpu_to_le32(MDT_ROCOMPAT_SUPP)) {
318                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
319                        obd->obd_name, le32_to_cpu(lsd->lsd_feature_rocompat) &
320                        ~MDT_ROCOMPAT_SUPP);
321                 /* Do something like remount filesystem read-only */
322                 GOTO(err_msd, rc = -EINVAL);
323         }
324
325         lsd->lsd_feature_compat = cpu_to_le32(OBD_COMPAT_MDT);
326         
327         mds->mds_last_transno = le64_to_cpu(lsd->lsd_last_transno);
328
329         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
330                obd->obd_name, mds->mds_last_transno);
331         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
332                obd->obd_name, mount_count + 1);
333         CDEBUG(D_INODE, "%s: server data size: %u\n",
334                obd->obd_name, le32_to_cpu(lsd->lsd_server_size));
335         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
336                obd->obd_name, le32_to_cpu(lsd->lsd_client_start));
337         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
338                obd->obd_name, le32_to_cpu(lsd->lsd_client_size));
339         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
340                obd->obd_name, last_rcvd_size);
341         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
342                last_rcvd_size <= le32_to_cpu(lsd->lsd_client_start) ? 0 :
343                (last_rcvd_size - le32_to_cpu(lsd->lsd_client_start)) /
344                 le16_to_cpu(lsd->lsd_client_size));
345
346         if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
347             !lsd->lsd_client_size) {
348                 CERROR("Bad last_rcvd contents!\n");
349                 GOTO(err_msd, rc = -EINVAL);
350         }
351
352         /* When we do a clean MDS shutdown, we save the last_transno into
353          * the header.  If we find clients with higher last_transno values
354          * then those clients may need recovery done. */
355         for (cl_idx = 0, off = le32_to_cpu(lsd->lsd_client_start);
356              off < last_rcvd_size; cl_idx++) {
357                 __u64 last_transno;
358                 struct obd_export *exp;
359                 struct mds_export_data *med;
360
361                 if (!mcd) {
362                         OBD_ALLOC_WAIT(mcd, sizeof(*mcd));
363                         if (!mcd)
364                                 GOTO(err_client, rc = -ENOMEM);
365                 }
366
367                 /* Don't assume off is incremented properly by
368                  * fsfilt_read_record(), in case sizeof(*mcd)
369                  * isn't the same as lsd->lsd_client_size.  */
370                 off = le32_to_cpu(lsd->lsd_client_start) +
371                         cl_idx * le16_to_cpu(lsd->lsd_client_size);
372                 rc = fsfilt_read_record(obd, file, mcd, sizeof(*mcd), &off);
373                 if (rc) {
374                         CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
375                                LAST_RCVD, cl_idx, off, rc);
376                         break; /* read error shouldn't cause startup to fail */
377                 }
378
379                 if (mcd->mcd_uuid[0] == '\0') {
380                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
381                                cl_idx);
382                         continue;
383                 }
384
385                 last_transno = le64_to_cpu(mcd->mcd_last_transno) >
386                                le64_to_cpu(mcd->mcd_last_close_transno) ?
387                                le64_to_cpu(mcd->mcd_last_transno) :
388                                le64_to_cpu(mcd->mcd_last_close_transno);
389
390                 /* These exports are cleaned up by mds_disconnect(), so they
391                  * need to be set up like real exports as mds_connect() does.
392                  */
393                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
394                        " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
395                        last_transno, le64_to_cpu(lsd->lsd_last_transno),
396                        le64_to_cpu(mcd->mcd_last_xid));
397
398                 exp = class_new_export(obd, (struct obd_uuid *)mcd->mcd_uuid);
399                 if (IS_ERR(exp))
400                         GOTO(err_client, rc = PTR_ERR(exp));
401
402                 med = &exp->exp_mds_data;
403                 med->med_mcd = mcd;
404                 rc = mds_client_add(obd, exp, cl_idx);
405                 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
406
407
408                 mcd = NULL;
409                 exp->exp_replay_needed = 1;
410                 exp->exp_connecting = 0;
411                 obd->obd_recoverable_clients++;
412                 obd->obd_max_recoverable_clients++;
413                 class_export_put(exp);
414
415                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
416                        cl_idx, last_transno);
417
418                 if (last_transno > mds->mds_last_transno)
419                         mds->mds_last_transno = last_transno;
420         }
421
422         if (mcd)
423                 OBD_FREE(mcd, sizeof(*mcd));
424
425         obd->obd_last_committed = mds->mds_last_transno;
426
427         if (obd->obd_recoverable_clients) {
428                 CWARN("RECOVERY: service %s, %d recoverable clients, "
429                       "last_transno "LPU64"\n", obd->obd_name,
430                       obd->obd_recoverable_clients, mds->mds_last_transno);
431                 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
432                 obd->obd_recovering = 1;
433                 obd->obd_recovery_start = CURRENT_SECONDS;
434                 /* Only used for lprocfs_status */
435                 obd->obd_recovery_end = obd->obd_recovery_start +
436                         OBD_RECOVERY_TIMEOUT;
437         }
438
439         mds->mds_mount_count = mount_count + 1;
440         lsd->lsd_mount_count = lsd->lsd_compat14 = 
441                 cpu_to_le64(mds->mds_mount_count);
442
443         /* save it, so mount count and last_transno is current */
444         rc = mds_update_server_data(obd, 1);
445         if (rc)
446                 GOTO(err_client, rc);
447
448         RETURN(0);
449
450 err_client:
451         class_disconnect_exports(obd);
452 err_msd:
453         mds_server_free_data(mds);
454         RETURN(rc);
455 }
456
457 int mds_fs_setup(struct obd_device *obd, struct vfsmount *mnt)
458 {
459         struct mds_obd *mds = &obd->u.mds;
460         struct lvfs_run_ctxt saved;
461         struct dentry *dentry;
462         struct file *file;
463         int rc;
464         ENTRY;
465
466         OBD_FAIL_RETURN(OBD_FAIL_MDS_FS_SETUP, -ENOENT);
467
468         rc = cleanup_group_info();
469         if (rc)
470                 RETURN(rc);
471
472         mds->mds_vfsmnt = mnt;
473         /* why not mnt->mnt_sb instead of mnt->mnt_root->d_inode->i_sb? */
474         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
475
476         fsfilt_setup(obd, obd->u.obt.obt_sb);
477
478         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
479         obd->obd_lvfs_ctxt.pwdmnt = mnt;
480         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
481         obd->obd_lvfs_ctxt.fs = get_ds();
482         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
483
484         /* setup the directory tree */
485         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
486         dentry = simple_mkdir(current->fs->pwd, "ROOT", 0755, 0);
487         if (IS_ERR(dentry)) {
488                 rc = PTR_ERR(dentry);
489                 CERROR("cannot create ROOT directory: rc = %d\n", rc);
490                 GOTO(err_pop, rc);
491         }
492
493         mds->mds_rootfid.id = dentry->d_inode->i_ino;
494         mds->mds_rootfid.generation = dentry->d_inode->i_generation;
495         mds->mds_rootfid.f_type = S_IFDIR;
496
497         dput(dentry);
498
499         dentry = lookup_one_len("__iopen__", current->fs->pwd,
500                                 strlen("__iopen__"));
501         if (IS_ERR(dentry)) {
502                 rc = PTR_ERR(dentry);
503                 CERROR("cannot lookup __iopen__ directory: rc = %d\n", rc);
504                 GOTO(err_pop, rc);
505         }
506
507         mds->mds_fid_de = dentry;
508         if (!dentry->d_inode || is_bad_inode(dentry->d_inode)) {
509                 rc = -ENOENT;
510                 CERROR("__iopen__ directory has no inode? rc = %d\n", rc);
511                 GOTO(err_fid, rc);
512         }
513
514         dentry = simple_mkdir(current->fs->pwd, "PENDING", 0777, 1);
515         if (IS_ERR(dentry)) {
516                 rc = PTR_ERR(dentry);
517                 CERROR("cannot create PENDING directory: rc = %d\n", rc);
518                 GOTO(err_fid, rc);
519         }
520         mds->mds_pending_dir = dentry;
521
522         /* COMPAT_146 */
523         dentry = simple_mkdir(current->fs->pwd, MDT_LOGS_DIR, 0777, 1);
524         if (IS_ERR(dentry)) {
525                 rc = PTR_ERR(dentry);
526                 CERROR("cannot create %s directory: rc = %d\n",
527                        MDT_LOGS_DIR, rc);
528                 GOTO(err_pending, rc);
529         }
530         mds->mds_logs_dir = dentry;
531         /* end COMPAT_146 */
532
533         dentry = simple_mkdir(current->fs->pwd, "OBJECTS", 0777, 1);
534         if (IS_ERR(dentry)) {
535                 rc = PTR_ERR(dentry);
536                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
537                 GOTO(err_logs, rc);
538         }
539         mds->mds_objects_dir = dentry;
540
541         /* open and test the last rcvd file */
542         file = filp_open(LAST_RCVD, O_RDWR | O_CREAT, 0644);
543         if (IS_ERR(file)) {
544                 rc = PTR_ERR(file);
545                 CERROR("cannot open/create %s file: rc = %d\n", LAST_RCVD, rc);
546                 GOTO(err_objects, rc = PTR_ERR(file));
547         }
548         mds->mds_rcvd_filp = file;
549         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
550                 CERROR("%s is not a regular file!: mode = %o\n", LAST_RCVD,
551                        file->f_dentry->d_inode->i_mode);
552                 GOTO(err_last_rcvd, rc = -ENOENT);
553         }
554
555         rc = mds_init_server_data(obd, file);
556         if (rc) {
557                 CERROR("cannot read %s: rc = %d\n", LAST_RCVD, rc);
558                 GOTO(err_last_rcvd, rc);
559         }
560
561         /* open and test the lov objd file */
562         file = filp_open(LOV_OBJID, O_RDWR | O_CREAT, 0644);
563         if (IS_ERR(file)) {
564                 rc = PTR_ERR(file);
565                 CERROR("cannot open/create %s file: rc = %d\n", LOV_OBJID, rc);
566                 GOTO(err_client, rc = PTR_ERR(file));
567         }
568         mds->mds_lov_objid_filp = file;
569         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
570                 CERROR("%s is not a regular file!: mode = %o\n", LOV_OBJID,
571                        file->f_dentry->d_inode->i_mode);
572                 GOTO(err_lov_objid, rc = -ENOENT);
573         }
574
575         /* open and test the check io file junk */
576         file = filp_open(HEALTH_CHECK, O_RDWR | O_CREAT, 0644);
577         if (IS_ERR(file)) {
578                 rc = PTR_ERR(file);
579                 CERROR("cannot open/create %s file: rc = %d\n", HEALTH_CHECK, rc);
580                 GOTO(err_lov_objid, rc = PTR_ERR(file));
581         }
582         mds->mds_health_check_filp = file;
583         if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
584                 CERROR("%s is not a regular file!: mode = %o\n", HEALTH_CHECK,
585                        file->f_dentry->d_inode->i_mode);
586                 GOTO(err_health_check, rc = -ENOENT);
587         }
588         rc = lvfs_check_io_health(obd, file);
589         if (rc)
590                 GOTO(err_health_check, rc);
591 err_pop:
592         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
593
594         return rc;
595
596 err_health_check:
597         if (mds->mds_health_check_filp && 
598             filp_close(mds->mds_health_check_filp, 0))
599                 CERROR("can't close %s after error\n", HEALTH_CHECK);
600 err_lov_objid:
601         if (mds->mds_lov_objid_filp && filp_close(mds->mds_lov_objid_filp, 0))
602                 CERROR("can't close %s after error\n", LOV_OBJID);
603 err_client:
604         class_disconnect_exports(obd);
605 err_last_rcvd:
606         if (mds->mds_rcvd_filp && filp_close(mds->mds_rcvd_filp, 0))
607                 CERROR("can't close %s after error\n", LAST_RCVD);
608 err_objects:
609         dput(mds->mds_objects_dir);
610 err_logs:
611         dput(mds->mds_logs_dir);
612 err_pending:
613         dput(mds->mds_pending_dir);
614 err_fid:
615         dput(mds->mds_fid_de);
616         goto err_pop;
617 }
618
619
620 int mds_fs_cleanup(struct obd_device *obd)
621 {
622         struct mds_obd *mds = &obd->u.mds;
623         struct lvfs_run_ctxt saved;
624         int rc = 0;
625
626         if (obd->obd_fail)
627                 LCONSOLE_WARN("%s: shutting down for failover; client state "
628                               "will be preserved.\n", obd->obd_name);
629
630         class_disconnect_exports(obd); /* cleans up client info too */
631         mds_server_free_data(mds);
632
633         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
634         if (mds->mds_rcvd_filp) {
635                 rc = filp_close(mds->mds_rcvd_filp, 0);
636                 mds->mds_rcvd_filp = NULL;
637                 if (rc)
638                         CERROR("%s file won't close, rc=%d\n", LAST_RCVD, rc);
639         }
640         if (mds->mds_lov_objid_filp) {
641                 rc = filp_close(mds->mds_lov_objid_filp, 0);
642                 mds->mds_lov_objid_filp = NULL;
643                 if (rc)
644                         CERROR("%s file won't close, rc=%d\n", LOV_OBJID, rc);
645         }
646         if (mds->mds_health_check_filp) {
647                 rc = filp_close(mds->mds_health_check_filp, 0);
648                 mds->mds_health_check_filp = NULL;
649                 if (rc)
650                         CERROR("%s file won't close, rc=%d\n", HEALTH_CHECK, rc);
651         }
652         if (mds->mds_objects_dir != NULL) {
653                 l_dput(mds->mds_objects_dir);
654                 mds->mds_objects_dir = NULL;
655         }
656         if (mds->mds_logs_dir) {
657                 l_dput(mds->mds_logs_dir);
658                 mds->mds_logs_dir = NULL;
659         }
660         if (mds->mds_pending_dir) {
661                 l_dput(mds->mds_pending_dir);
662                 mds->mds_pending_dir = NULL;
663         }
664
665         lquota_fs_cleanup(mds_quota_interface_ref, obd);
666
667         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
668         shrink_dcache_parent(mds->mds_fid_de);
669         dput(mds->mds_fid_de);
670         LL_DQUOT_OFF(obd->u.obt.obt_sb);
671
672         return rc;
673 }
674
675 /* Creates an object with the same name as its fid.  Because this is not at all
676  * performance sensitive, it is accomplished by creating a file, checking the
677  * fid, and renaming it. */
678 int mds_obd_create(struct obd_export *exp, struct obdo *oa,
679                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
680 {
681         struct mds_obd *mds = &exp->exp_obd->u.mds;
682         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
683         unsigned int tmpname = ll_rand();
684         struct file *filp;
685         struct dentry *new_child;
686         struct lvfs_run_ctxt saved;
687         char fidname[LL_FID_NAMELEN];
688         void *handle;
689         struct lvfs_ucred ucred = { 0 };
690         int rc = 0, err, namelen;
691         ENTRY;
692
693         /* the owner of object file should always be root */
694         ucred.luc_cap = current->cap_effective | CAP_SYS_RESOURCE;
695
696         push_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
697
698         sprintf(fidname, "OBJECTS/%u.%u", tmpname, current->pid);
699         filp = filp_open(fidname, O_CREAT | O_EXCL, 0666);
700         if (IS_ERR(filp)) {
701                 rc = PTR_ERR(filp);
702                 if (rc == -EEXIST) {
703                         CERROR("impossible object name collision %u\n",
704                                tmpname);
705                         LBUG();
706                 }
707                 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
708                 GOTO(out_pop, rc);
709         }
710
711         LASSERT(mds->mds_objects_dir == filp->f_dentry->d_parent);
712
713         oa->o_id = filp->f_dentry->d_inode->i_ino;
714         oa->o_generation = filp->f_dentry->d_inode->i_generation;
715         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
716
717         LOCK_INODE_MUTEX(parent_inode);
718         new_child = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
719
720         if (IS_ERR(new_child)) {
721                 CERROR("getting neg dentry for obj rename: %d\n", rc);
722                 GOTO(out_close, rc = PTR_ERR(new_child));
723         }
724         if (new_child->d_inode != NULL) {
725                 CERROR("impossible non-negative obj dentry " LPU64":%u!\n",
726                        oa->o_id, oa->o_generation);
727                 LBUG();
728         }
729
730         handle = fsfilt_start(exp->exp_obd, mds->mds_objects_dir->d_inode,
731                               FSFILT_OP_RENAME, NULL);
732         if (IS_ERR(handle))
733                 GOTO(out_dput, rc = PTR_ERR(handle));
734
735         lock_kernel();
736         rc = vfs_rename(mds->mds_objects_dir->d_inode, filp->f_dentry,
737                         mds->mds_objects_dir->d_inode, new_child);
738         unlock_kernel();
739         if (rc)
740                 CERROR("error renaming new object "LPU64":%u: rc %d\n",
741                        oa->o_id, oa->o_generation, rc);
742
743         err = fsfilt_commit(exp->exp_obd, mds->mds_objects_dir->d_inode,
744                             handle, 0);
745         if (!err)
746                 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
747         else if (!rc)
748                 rc = err;
749 out_dput:
750         dput(new_child);
751 out_close:
752         UNLOCK_INODE_MUTEX(parent_inode);
753         err = filp_close(filp, 0);
754         if (err) {
755                 CERROR("closing tmpfile %u: rc %d\n", tmpname, rc);
756                 if (!rc)
757                         rc = err;
758         }
759 out_pop:
760         pop_ctxt(&saved, &exp->exp_obd->obd_lvfs_ctxt, &ucred);
761         RETURN(rc);
762 }
763
764 int mds_obd_destroy(struct obd_export *exp, struct obdo *oa,
765                     struct lov_stripe_md *ea, struct obd_trans_info *oti,
766                     struct obd_export *md_exp)
767 {
768         struct mds_obd *mds = &exp->exp_obd->u.mds;
769         struct inode *parent_inode = mds->mds_objects_dir->d_inode;
770         struct obd_device *obd = exp->exp_obd;
771         struct lvfs_run_ctxt saved;
772         struct lvfs_ucred ucred = { 0 };
773         char fidname[LL_FID_NAMELEN];
774         struct inode *inode = NULL;
775         struct dentry *de;
776         void *handle;
777         int err, namelen, rc = 0;
778         ENTRY;
779
780         ucred.luc_cap = current->cap_effective | CAP_SYS_RESOURCE;
781         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
782
783         namelen = ll_fid2str(fidname, oa->o_id, oa->o_generation);
784
785         LOCK_INODE_MUTEX(parent_inode);
786         de = lookup_one_len(fidname, mds->mds_objects_dir, namelen);
787         if (IS_ERR(de)) {
788                 rc = IS_ERR(de);
789                 de = NULL;
790                 CERROR("error looking up object "LPU64" %s: rc %d\n",
791                        oa->o_id, fidname, rc);
792                 GOTO(out_dput, rc);
793         }
794         if (de->d_inode == NULL) {
795                 CERROR("destroying non-existent object "LPU64" %s: rc %d\n",
796                        oa->o_id, fidname, rc);
797                 GOTO(out_dput, rc = -ENOENT);
798         }
799
800         /* Stripe count is 1 here since this is some MDS specific stuff
801            that is unlinked, not spanned across multiple OSTs */
802         handle = fsfilt_start_log(obd, mds->mds_objects_dir->d_inode,
803                                   FSFILT_OP_UNLINK, oti, 1);
804
805         if (IS_ERR(handle))
806                 GOTO(out_dput, rc = PTR_ERR(handle));
807
808         /* take a reference to protect inode from truncation within
809            vfs_unlink() context. bug 10409 */
810         inode = de->d_inode;
811         atomic_inc(&inode->i_count);
812         rc = vfs_unlink(mds->mds_objects_dir->d_inode, de);
813         if (rc)
814                 CERROR("error destroying object "LPU64":%u: rc %d\n",
815                        oa->o_id, oa->o_generation, rc);
816
817         err = fsfilt_commit(obd, mds->mds_objects_dir->d_inode, handle, 0);
818         if (err && !rc)
819                 rc = err;
820 out_dput:
821         if (de != NULL)
822                 l_dput(de);
823         UNLOCK_INODE_MUTEX(parent_inode);
824
825         if (inode)
826                 iput(inode);
827
828         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &ucred);
829         RETURN(rc);
830 }