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