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