add hlist_bl_lock/unlock helpers
[linux-2.6.git] / fs / gfs2 / sys.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/module.h>
15 #include <linux/kobject.h>
16 #include <asm/uaccess.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/genhd.h>
19
20 #include "gfs2.h"
21 #include "incore.h"
22 #include "sys.h"
23 #include "super.h"
24 #include "glock.h"
25 #include "quota.h"
26 #include "util.h"
27 #include "glops.h"
28 #include "recovery.h"
29
30 struct gfs2_attr {
31         struct attribute attr;
32         ssize_t (*show)(struct gfs2_sbd *, char *);
33         ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
34 };
35
36 static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
37                               char *buf)
38 {
39         struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
40         struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
41         return a->show ? a->show(sdp, buf) : 0;
42 }
43
44 static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
45                                const char *buf, size_t len)
46 {
47         struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
48         struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
49         return a->store ? a->store(sdp, buf, len) : len;
50 }
51
52 static const struct sysfs_ops gfs2_attr_ops = {
53         .show  = gfs2_attr_show,
54         .store = gfs2_attr_store,
55 };
56
57
58 static struct kset *gfs2_kset;
59
60 static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
61 {
62         return snprintf(buf, PAGE_SIZE, "%u:%u\n",
63                         MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
64 }
65
66 static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
67 {
68         return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
69 }
70
71 static int gfs2_uuid_valid(const u8 *uuid)
72 {
73         int i;
74
75         for (i = 0; i < 16; i++) {
76                 if (uuid[i])
77                         return 1;
78         }
79         return 0;
80 }
81
82 static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
83 {
84         const u8 *uuid = sdp->sd_sb.sb_uuid;
85         buf[0] = '\0';
86         if (!gfs2_uuid_valid(uuid))
87                 return 0;
88         return snprintf(buf, PAGE_SIZE, "%pUB\n", uuid);
89 }
90
91 static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
92 {
93         unsigned int count;
94
95         mutex_lock(&sdp->sd_freeze_lock);
96         count = sdp->sd_freeze_count;
97         mutex_unlock(&sdp->sd_freeze_lock);
98
99         return snprintf(buf, PAGE_SIZE, "%u\n", count);
100 }
101
102 static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
103 {
104         ssize_t ret = len;
105         int error = 0;
106         int n = simple_strtol(buf, NULL, 0);
107
108         if (!capable(CAP_SYS_ADMIN))
109                 return -EACCES;
110
111         switch (n) {
112         case 0:
113                 gfs2_unfreeze_fs(sdp);
114                 break;
115         case 1:
116                 error = gfs2_freeze_fs(sdp);
117                 break;
118         default:
119                 ret = -EINVAL;
120         }
121
122         if (error)
123                 fs_warn(sdp, "freeze %d error %d", n, error);
124
125         return ret;
126 }
127
128 static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
129 {
130         unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
131         return snprintf(buf, PAGE_SIZE, "%u\n", b);
132 }
133
134 static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
135 {
136         if (!capable(CAP_SYS_ADMIN))
137                 return -EACCES;
138
139         if (simple_strtol(buf, NULL, 0) != 1)
140                 return -EINVAL;
141
142         gfs2_lm_withdraw(sdp,
143                 "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
144                 sdp->sd_fsname);
145         return len;
146 }
147
148 static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
149                                  size_t len)
150 {
151         if (!capable(CAP_SYS_ADMIN))
152                 return -EACCES;
153
154         if (simple_strtol(buf, NULL, 0) != 1)
155                 return -EINVAL;
156
157         gfs2_statfs_sync(sdp->sd_vfs, 0);
158         return len;
159 }
160
161 static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
162                                 size_t len)
163 {
164         if (!capable(CAP_SYS_ADMIN))
165                 return -EACCES;
166
167         if (simple_strtol(buf, NULL, 0) != 1)
168                 return -EINVAL;
169
170         gfs2_quota_sync(sdp->sd_vfs, 0, 1);
171         return len;
172 }
173
174 static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
175                                         size_t len)
176 {
177         int error;
178         u32 id;
179
180         if (!capable(CAP_SYS_ADMIN))
181                 return -EACCES;
182
183         id = simple_strtoul(buf, NULL, 0);
184
185         error = gfs2_quota_refresh(sdp, 1, id);
186         return error ? error : len;
187 }
188
189 static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
190                                          size_t len)
191 {
192         int error;
193         u32 id;
194
195         if (!capable(CAP_SYS_ADMIN))
196                 return -EACCES;
197
198         id = simple_strtoul(buf, NULL, 0);
199
200         error = gfs2_quota_refresh(sdp, 0, id);
201         return error ? error : len;
202 }
203
204 static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
205 {
206         struct gfs2_glock *gl;
207         const struct gfs2_glock_operations *glops;
208         unsigned int glmode;
209         unsigned int gltype;
210         unsigned long long glnum;
211         char mode[16];
212         int rv;
213
214         if (!capable(CAP_SYS_ADMIN))
215                 return -EACCES;
216
217         rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
218                     mode);
219         if (rv != 3)
220                 return -EINVAL;
221
222         if (strcmp(mode, "EX") == 0)
223                 glmode = LM_ST_UNLOCKED;
224         else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
225                 glmode = LM_ST_DEFERRED;
226         else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
227                 glmode = LM_ST_SHARED;
228         else
229                 return -EINVAL;
230
231         if (gltype > LM_TYPE_JOURNAL)
232                 return -EINVAL;
233         if (gltype == LM_TYPE_NONDISK && glnum == GFS2_TRANS_LOCK)
234                 glops = &gfs2_trans_glops;
235         else
236                 glops = gfs2_glops_list[gltype];
237         if (glops == NULL)
238                 return -EINVAL;
239         if (!test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
240                 fs_info(sdp, "demote interface used\n");
241         rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
242         if (rv)
243                 return rv;
244         gfs2_glock_cb(gl, glmode);
245         gfs2_glock_put(gl);
246         return len;
247 }
248
249
250 #define GFS2_ATTR(name, mode, show, store) \
251 static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
252
253 GFS2_ATTR(id,                  0444, id_show,       NULL);
254 GFS2_ATTR(fsname,              0444, fsname_show,   NULL);
255 GFS2_ATTR(uuid,                0444, uuid_show,     NULL);
256 GFS2_ATTR(freeze,              0644, freeze_show,   freeze_store);
257 GFS2_ATTR(withdraw,            0644, withdraw_show, withdraw_store);
258 GFS2_ATTR(statfs_sync,         0200, NULL,          statfs_sync_store);
259 GFS2_ATTR(quota_sync,          0200, NULL,          quota_sync_store);
260 GFS2_ATTR(quota_refresh_user,  0200, NULL,          quota_refresh_user_store);
261 GFS2_ATTR(quota_refresh_group, 0200, NULL,          quota_refresh_group_store);
262 GFS2_ATTR(demote_rq,           0200, NULL,          demote_rq_store);
263
264 static struct attribute *gfs2_attrs[] = {
265         &gfs2_attr_id.attr,
266         &gfs2_attr_fsname.attr,
267         &gfs2_attr_uuid.attr,
268         &gfs2_attr_freeze.attr,
269         &gfs2_attr_withdraw.attr,
270         &gfs2_attr_statfs_sync.attr,
271         &gfs2_attr_quota_sync.attr,
272         &gfs2_attr_quota_refresh_user.attr,
273         &gfs2_attr_quota_refresh_group.attr,
274         &gfs2_attr_demote_rq.attr,
275         NULL,
276 };
277
278 static struct kobj_type gfs2_ktype = {
279         .default_attrs = gfs2_attrs,
280         .sysfs_ops     = &gfs2_attr_ops,
281 };
282
283
284 /*
285  * lock_module. Originally from lock_dlm
286  */
287
288 static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
289 {
290         const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
291         return sprintf(buf, "%s\n", ops->lm_proto_name);
292 }
293
294 static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
295 {
296         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
297         ssize_t ret;
298         int val = 0;
299
300         if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))
301                 val = 1;
302         ret = sprintf(buf, "%d\n", val);
303         return ret;
304 }
305
306 static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
307 {
308         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
309         ssize_t ret = len;
310         int val;
311
312         val = simple_strtol(buf, NULL, 0);
313
314         if (val == 1)
315                 set_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
316         else if (val == 0) {
317                 clear_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
318                 smp_mb__after_clear_bit();
319                 gfs2_glock_thaw(sdp);
320         } else {
321                 ret = -EINVAL;
322         }
323         return ret;
324 }
325
326 static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
327 {
328         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
329         return sprintf(buf, "%d\n", ls->ls_first);
330 }
331
332 static ssize_t lkfirst_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
333 {
334         unsigned first;
335         int rv;
336
337         rv = sscanf(buf, "%u", &first);
338         if (rv != 1 || first > 1)
339                 return -EINVAL;
340         spin_lock(&sdp->sd_jindex_spin);
341         rv = -EBUSY;
342         if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
343                 goto out;
344         rv = -EINVAL;
345         if (sdp->sd_args.ar_spectator)
346                 goto out;
347         if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
348                 goto out;
349         sdp->sd_lockstruct.ls_first = first;
350         rv = 0;
351 out:
352         spin_unlock(&sdp->sd_jindex_spin);
353         return rv ? rv : len;
354 }
355
356 static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
357 {
358         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
359         return sprintf(buf, "%d\n", ls->ls_first_done);
360 }
361
362 static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
363 {
364         unsigned jid;
365         struct gfs2_jdesc *jd;
366         int rv;
367
368         rv = sscanf(buf, "%u", &jid);
369         if (rv != 1)
370                 return -EINVAL;
371
372         rv = -ESHUTDOWN;
373         spin_lock(&sdp->sd_jindex_spin);
374         if (test_bit(SDF_NORECOVERY, &sdp->sd_flags))
375                 goto out;
376         rv = -EBUSY;
377         if (sdp->sd_jdesc->jd_jid == jid)
378                 goto out;
379         rv = -ENOENT;
380         list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
381                 if (jd->jd_jid != jid)
382                         continue;
383                 rv = gfs2_recover_journal(jd, false);
384                 break;
385         }
386 out:
387         spin_unlock(&sdp->sd_jindex_spin);
388         return rv ? rv : len;
389 }
390
391 static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
392 {
393         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
394         return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
395 }
396
397 static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
398 {
399         struct lm_lockstruct *ls = &sdp->sd_lockstruct;
400         return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
401 }
402
403 static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
404 {
405         return sprintf(buf, "%d\n", sdp->sd_lockstruct.ls_jid);
406 }
407
408 static ssize_t jid_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
409 {
410         int jid;
411         int rv;
412
413         rv = sscanf(buf, "%d", &jid);
414         if (rv != 1)
415                 return -EINVAL;
416
417         spin_lock(&sdp->sd_jindex_spin);
418         rv = -EINVAL;
419         if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
420                 goto out;
421         rv = -EBUSY;
422         if (test_bit(SDF_NOJOURNALID, &sdp->sd_flags) == 0)
423                 goto out;
424         rv = 0;
425         if (sdp->sd_args.ar_spectator && jid > 0)
426                 rv = jid = -EINVAL;
427         sdp->sd_lockstruct.ls_jid = jid;
428         clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
429         smp_mb__after_clear_bit();
430         wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
431 out:
432         spin_unlock(&sdp->sd_jindex_spin);
433         return rv ? rv : len;
434 }
435
436 #define GDLM_ATTR(_name,_mode,_show,_store) \
437 static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
438
439 GDLM_ATTR(proto_name,           0444, proto_name_show,          NULL);
440 GDLM_ATTR(block,                0644, block_show,               block_store);
441 GDLM_ATTR(withdraw,             0644, withdraw_show,            withdraw_store);
442 GDLM_ATTR(jid,                  0644, jid_show,                 jid_store);
443 GDLM_ATTR(first,                0644, lkfirst_show,             lkfirst_store);
444 GDLM_ATTR(first_done,           0444, first_done_show,          NULL);
445 GDLM_ATTR(recover,              0600, NULL,                     recover_store);
446 GDLM_ATTR(recover_done,         0444, recover_done_show,        NULL);
447 GDLM_ATTR(recover_status,       0444, recover_status_show,      NULL);
448
449 static struct attribute *lock_module_attrs[] = {
450         &gdlm_attr_proto_name.attr,
451         &gdlm_attr_block.attr,
452         &gdlm_attr_withdraw.attr,
453         &gdlm_attr_jid.attr,
454         &gdlm_attr_first.attr,
455         &gdlm_attr_first_done.attr,
456         &gdlm_attr_recover.attr,
457         &gdlm_attr_recover_done.attr,
458         &gdlm_attr_recover_status.attr,
459         NULL,
460 };
461
462 /*
463  * get and set struct gfs2_tune fields
464  */
465
466 static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
467 {
468         return snprintf(buf, PAGE_SIZE, "%u %u\n",
469                         sdp->sd_tune.gt_quota_scale_num,
470                         sdp->sd_tune.gt_quota_scale_den);
471 }
472
473 static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
474                                  size_t len)
475 {
476         struct gfs2_tune *gt = &sdp->sd_tune;
477         unsigned int x, y;
478
479         if (!capable(CAP_SYS_ADMIN))
480                 return -EACCES;
481
482         if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
483                 return -EINVAL;
484
485         spin_lock(&gt->gt_spin);
486         gt->gt_quota_scale_num = x;
487         gt->gt_quota_scale_den = y;
488         spin_unlock(&gt->gt_spin);
489         return len;
490 }
491
492 static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
493                         int check_zero, const char *buf, size_t len)
494 {
495         struct gfs2_tune *gt = &sdp->sd_tune;
496         unsigned int x;
497
498         if (!capable(CAP_SYS_ADMIN))
499                 return -EACCES;
500
501         x = simple_strtoul(buf, NULL, 0);
502
503         if (check_zero && !x)
504                 return -EINVAL;
505
506         spin_lock(&gt->gt_spin);
507         *field = x;
508         spin_unlock(&gt->gt_spin);
509         return len;
510 }
511
512 #define TUNE_ATTR_3(name, show, store)                                        \
513 static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
514
515 #define TUNE_ATTR_2(name, store)                                              \
516 static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf)                   \
517 {                                                                             \
518         return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name);      \
519 }                                                                             \
520 TUNE_ATTR_3(name, name##_show, store)
521
522 #define TUNE_ATTR(name, check_zero)                                           \
523 static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
524 {                                                                             \
525         return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len);  \
526 }                                                                             \
527 TUNE_ATTR_2(name, name##_store)
528
529 TUNE_ATTR(quota_warn_period, 0);
530 TUNE_ATTR(quota_quantum, 0);
531 TUNE_ATTR(max_readahead, 0);
532 TUNE_ATTR(complain_secs, 0);
533 TUNE_ATTR(statfs_slow, 0);
534 TUNE_ATTR(new_files_jdata, 0);
535 TUNE_ATTR(quota_simul_sync, 1);
536 TUNE_ATTR(statfs_quantum, 1);
537 TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
538
539 static struct attribute *tune_attrs[] = {
540         &tune_attr_quota_warn_period.attr,
541         &tune_attr_quota_quantum.attr,
542         &tune_attr_max_readahead.attr,
543         &tune_attr_complain_secs.attr,
544         &tune_attr_statfs_slow.attr,
545         &tune_attr_quota_simul_sync.attr,
546         &tune_attr_statfs_quantum.attr,
547         &tune_attr_quota_scale.attr,
548         &tune_attr_new_files_jdata.attr,
549         NULL,
550 };
551
552 static struct attribute_group tune_group = {
553         .name = "tune",
554         .attrs = tune_attrs,
555 };
556
557 static struct attribute_group lock_module_group = {
558         .name = "lock_module",
559         .attrs = lock_module_attrs,
560 };
561
562 int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
563 {
564         struct super_block *sb = sdp->sd_vfs;
565         int error;
566         char ro[20];
567         char spectator[20];
568         char *envp[] = { ro, spectator, NULL };
569
570         sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
571         sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
572
573         sdp->sd_kobj.kset = gfs2_kset;
574         error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
575                                      "%s", sdp->sd_table_name);
576         if (error)
577                 goto fail;
578
579         error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
580         if (error)
581                 goto fail_reg;
582
583         error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
584         if (error)
585                 goto fail_tune;
586
587         error = sysfs_create_link(&sdp->sd_kobj,
588                                   &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
589                                   "device");
590         if (error)
591                 goto fail_lock_module;
592
593         kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
594         return 0;
595
596 fail_lock_module:
597         sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
598 fail_tune:
599         sysfs_remove_group(&sdp->sd_kobj, &tune_group);
600 fail_reg:
601         kobject_put(&sdp->sd_kobj);
602 fail:
603         fs_err(sdp, "error %d adding sysfs files", error);
604         return error;
605 }
606
607 void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
608 {
609         sysfs_remove_link(&sdp->sd_kobj, "device");
610         sysfs_remove_group(&sdp->sd_kobj, &tune_group);
611         sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
612         kobject_put(&sdp->sd_kobj);
613 }
614
615 static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
616                        struct kobj_uevent_env *env)
617 {
618         struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
619         const u8 *uuid = sdp->sd_sb.sb_uuid;
620
621         add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
622         add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
623         if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags))
624                 add_uevent_var(env, "JOURNALID=%d", sdp->sd_lockstruct.ls_jid);
625         if (gfs2_uuid_valid(uuid))
626                 add_uevent_var(env, "UUID=%pUB", uuid);
627         return 0;
628 }
629
630 static const struct kset_uevent_ops gfs2_uevent_ops = {
631         .uevent = gfs2_uevent,
632 };
633
634 int gfs2_sys_init(void)
635 {
636         gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
637         if (!gfs2_kset)
638                 return -ENOMEM;
639         return 0;
640 }
641
642 void gfs2_sys_uninit(void)
643 {
644         kset_unregister(gfs2_kset);
645 }
646