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