sys.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. #include <linux/sched.h>
  10. #include <linux/slab.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. #include "gfs2.h"
  20. #include "incore.h"
  21. #include "sys.h"
  22. #include "super.h"
  23. #include "glock.h"
  24. #include "quota.h"
  25. #include "util.h"
  26. #include "glops.h"
  27. struct gfs2_attr {
  28. struct attribute attr;
  29. ssize_t (*show)(struct gfs2_sbd *, char *);
  30. ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
  31. };
  32. static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
  33. char *buf)
  34. {
  35. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  36. struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  37. return a->show ? a->show(sdp, buf) : 0;
  38. }
  39. static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
  40. const char *buf, size_t len)
  41. {
  42. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  43. struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  44. return a->store ? a->store(sdp, buf, len) : len;
  45. }
  46. static const struct sysfs_ops gfs2_attr_ops = {
  47. .show = gfs2_attr_show,
  48. .store = gfs2_attr_store,
  49. };
  50. static struct kset *gfs2_kset;
  51. static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
  52. {
  53. return snprintf(buf, PAGE_SIZE, "%u:%u\n",
  54. MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
  55. }
  56. static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
  57. {
  58. return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
  59. }
  60. static int gfs2_uuid_valid(const u8 *uuid)
  61. {
  62. int i;
  63. for (i = 0; i < 16; i++) {
  64. if (uuid[i])
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. static ssize_t uuid_show(struct gfs2_sbd *sdp, char *buf)
  70. {
  71. const u8 *uuid = sdp->sd_sb.sb_uuid;
  72. buf[0] = '\0';
  73. if (!gfs2_uuid_valid(uuid))
  74. return 0;
  75. return snprintf(buf, PAGE_SIZE, "%pUB\n", uuid);
  76. }
  77. static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
  78. {
  79. unsigned int count;
  80. mutex_lock(&sdp->sd_freeze_lock);
  81. count = sdp->sd_freeze_count;
  82. mutex_unlock(&sdp->sd_freeze_lock);
  83. return snprintf(buf, PAGE_SIZE, "%u\n", count);
  84. }
  85. static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  86. {
  87. ssize_t ret = len;
  88. int error = 0;
  89. int n = simple_strtol(buf, NULL, 0);
  90. if (!capable(CAP_SYS_ADMIN))
  91. return -EACCES;
  92. switch (n) {
  93. case 0:
  94. gfs2_unfreeze_fs(sdp);
  95. break;
  96. case 1:
  97. error = gfs2_freeze_fs(sdp);
  98. break;
  99. default:
  100. ret = -EINVAL;
  101. }
  102. if (error)
  103. fs_warn(sdp, "freeze %d error %d", n, error);
  104. return ret;
  105. }
  106. static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
  107. {
  108. unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
  109. return snprintf(buf, PAGE_SIZE, "%u\n", b);
  110. }
  111. static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  112. {
  113. if (!capable(CAP_SYS_ADMIN))
  114. return -EACCES;
  115. if (simple_strtol(buf, NULL, 0) != 1)
  116. return -EINVAL;
  117. gfs2_lm_withdraw(sdp,
  118. "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
  119. sdp->sd_fsname);
  120. return len;
  121. }
  122. static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
  123. size_t len)
  124. {
  125. if (!capable(CAP_SYS_ADMIN))
  126. return -EACCES;
  127. if (simple_strtol(buf, NULL, 0) != 1)
  128. return -EINVAL;
  129. gfs2_statfs_sync(sdp->sd_vfs, 0);
  130. return len;
  131. }
  132. static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
  133. size_t len)
  134. {
  135. if (!capable(CAP_SYS_ADMIN))
  136. return -EACCES;
  137. if (simple_strtol(buf, NULL, 0) != 1)
  138. return -EINVAL;
  139. gfs2_quota_sync(sdp->sd_vfs, 0, 1);
  140. return len;
  141. }
  142. static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
  143. size_t len)
  144. {
  145. int error;
  146. u32 id;
  147. if (!capable(CAP_SYS_ADMIN))
  148. return -EACCES;
  149. id = simple_strtoul(buf, NULL, 0);
  150. error = gfs2_quota_refresh(sdp, 1, id);
  151. return error ? error : len;
  152. }
  153. static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
  154. size_t len)
  155. {
  156. int error;
  157. u32 id;
  158. if (!capable(CAP_SYS_ADMIN))
  159. return -EACCES;
  160. id = simple_strtoul(buf, NULL, 0);
  161. error = gfs2_quota_refresh(sdp, 0, id);
  162. return error ? error : len;
  163. }
  164. static ssize_t demote_rq_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  165. {
  166. struct gfs2_glock *gl;
  167. const struct gfs2_glock_operations *glops;
  168. unsigned int glmode;
  169. unsigned int gltype;
  170. unsigned long long glnum;
  171. char mode[16];
  172. int rv;
  173. if (!capable(CAP_SYS_ADMIN))
  174. return -EACCES;
  175. rv = sscanf(buf, "%u:%llu %15s", &gltype, &glnum,
  176. mode);
  177. if (rv != 3)
  178. return -EINVAL;
  179. if (strcmp(mode, "EX") == 0)
  180. glmode = LM_ST_UNLOCKED;
  181. else if ((strcmp(mode, "CW") == 0) || (strcmp(mode, "DF") == 0))
  182. glmode = LM_ST_DEFERRED;
  183. else if ((strcmp(mode, "PR") == 0) || (strcmp(mode, "SH") == 0))
  184. glmode = LM_ST_SHARED;
  185. else
  186. return -EINVAL;
  187. if (gltype > LM_TYPE_JOURNAL)
  188. return -EINVAL;
  189. glops = gfs2_glops_list[gltype];
  190. if (glops == NULL)
  191. return -EINVAL;
  192. if (test_and_set_bit(SDF_DEMOTE, &sdp->sd_flags))
  193. fs_info(sdp, "demote interface used\n");
  194. rv = gfs2_glock_get(sdp, glnum, glops, 0, &gl);
  195. if (rv)
  196. return rv;
  197. gfs2_glock_cb(gl, glmode);
  198. gfs2_glock_put(gl);
  199. return len;
  200. }
  201. #define GFS2_ATTR(name, mode, show, store) \
  202. static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
  203. GFS2_ATTR(id, 0444, id_show, NULL);
  204. GFS2_ATTR(fsname, 0444, fsname_show, NULL);
  205. GFS2_ATTR(uuid, 0444, uuid_show, NULL);
  206. GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
  207. GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
  208. GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
  209. GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
  210. GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
  211. GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
  212. GFS2_ATTR(demote_rq, 0200, NULL, demote_rq_store);
  213. static struct attribute *gfs2_attrs[] = {
  214. &gfs2_attr_id.attr,
  215. &gfs2_attr_fsname.attr,
  216. &gfs2_attr_uuid.attr,
  217. &gfs2_attr_freeze.attr,
  218. &gfs2_attr_withdraw.attr,
  219. &gfs2_attr_statfs_sync.attr,
  220. &gfs2_attr_quota_sync.attr,
  221. &gfs2_attr_quota_refresh_user.attr,
  222. &gfs2_attr_quota_refresh_group.attr,
  223. &gfs2_attr_demote_rq.attr,
  224. NULL,
  225. };
  226. static struct kobj_type gfs2_ktype = {
  227. .default_attrs = gfs2_attrs,
  228. .sysfs_ops = &gfs2_attr_ops,
  229. };
  230. /*
  231. * lock_module. Originally from lock_dlm
  232. */
  233. static ssize_t proto_name_show(struct gfs2_sbd *sdp, char *buf)
  234. {
  235. const struct lm_lockops *ops = sdp->sd_lockstruct.ls_ops;
  236. return sprintf(buf, "%s\n", ops->lm_proto_name);
  237. }
  238. static ssize_t block_show(struct gfs2_sbd *sdp, char *buf)
  239. {
  240. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  241. ssize_t ret;
  242. int val = 0;
  243. if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_flags))
  244. val = 1;
  245. ret = sprintf(buf, "%d\n", val);
  246. return ret;
  247. }
  248. static ssize_t block_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  249. {
  250. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  251. ssize_t ret = len;
  252. int val;
  253. val = simple_strtol(buf, NULL, 0);
  254. if (val == 1)
  255. set_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
  256. else if (val == 0) {
  257. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_flags);
  258. smp_mb__after_clear_bit();
  259. gfs2_glock_thaw(sdp);
  260. } else {
  261. ret = -EINVAL;
  262. }
  263. return ret;
  264. }
  265. static ssize_t lkfirst_show(struct gfs2_sbd *sdp, char *buf)
  266. {
  267. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  268. return sprintf(buf, "%d\n", ls->ls_first);
  269. }
  270. static ssize_t first_done_show(struct gfs2_sbd *sdp, char *buf)
  271. {
  272. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  273. return sprintf(buf, "%d\n", ls->ls_first_done);
  274. }
  275. static ssize_t recover_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  276. {
  277. unsigned jid;
  278. struct gfs2_jdesc *jd;
  279. int rv;
  280. rv = sscanf(buf, "%u", &jid);
  281. if (rv != 1)
  282. return -EINVAL;
  283. rv = -ESHUTDOWN;
  284. spin_lock(&sdp->sd_jindex_spin);
  285. if (test_bit(SDF_NORECOVERY, &sdp->sd_flags))
  286. goto out;
  287. rv = -EBUSY;
  288. if (sdp->sd_jdesc->jd_jid == jid)
  289. goto out;
  290. rv = -ENOENT;
  291. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  292. if (jd->jd_jid != jid)
  293. continue;
  294. rv = slow_work_enqueue(&jd->jd_work);
  295. break;
  296. }
  297. out:
  298. spin_unlock(&sdp->sd_jindex_spin);
  299. return rv ? rv : len;
  300. }
  301. static ssize_t recover_done_show(struct gfs2_sbd *sdp, char *buf)
  302. {
  303. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  304. return sprintf(buf, "%d\n", ls->ls_recover_jid_done);
  305. }
  306. static ssize_t recover_status_show(struct gfs2_sbd *sdp, char *buf)
  307. {
  308. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  309. return sprintf(buf, "%d\n", ls->ls_recover_jid_status);
  310. }
  311. static ssize_t jid_show(struct gfs2_sbd *sdp, char *buf)
  312. {
  313. return sprintf(buf, "%u\n", sdp->sd_lockstruct.ls_jid);
  314. }
  315. #define GDLM_ATTR(_name,_mode,_show,_store) \
  316. static struct gfs2_attr gdlm_attr_##_name = __ATTR(_name,_mode,_show,_store)
  317. GDLM_ATTR(proto_name, 0444, proto_name_show, NULL);
  318. GDLM_ATTR(block, 0644, block_show, block_store);
  319. GDLM_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
  320. GDLM_ATTR(jid, 0444, jid_show, NULL);
  321. GDLM_ATTR(first, 0444, lkfirst_show, NULL);
  322. GDLM_ATTR(first_done, 0444, first_done_show, NULL);
  323. GDLM_ATTR(recover, 0600, NULL, recover_store);
  324. GDLM_ATTR(recover_done, 0444, recover_done_show, NULL);
  325. GDLM_ATTR(recover_status, 0444, recover_status_show, NULL);
  326. static struct attribute *lock_module_attrs[] = {
  327. &gdlm_attr_proto_name.attr,
  328. &gdlm_attr_block.attr,
  329. &gdlm_attr_withdraw.attr,
  330. &gdlm_attr_jid.attr,
  331. &gdlm_attr_first.attr,
  332. &gdlm_attr_first_done.attr,
  333. &gdlm_attr_recover.attr,
  334. &gdlm_attr_recover_done.attr,
  335. &gdlm_attr_recover_status.attr,
  336. NULL,
  337. };
  338. /*
  339. * get and set struct gfs2_tune fields
  340. */
  341. static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
  342. {
  343. return snprintf(buf, PAGE_SIZE, "%u %u\n",
  344. sdp->sd_tune.gt_quota_scale_num,
  345. sdp->sd_tune.gt_quota_scale_den);
  346. }
  347. static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
  348. size_t len)
  349. {
  350. struct gfs2_tune *gt = &sdp->sd_tune;
  351. unsigned int x, y;
  352. if (!capable(CAP_SYS_ADMIN))
  353. return -EACCES;
  354. if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
  355. return -EINVAL;
  356. spin_lock(&gt->gt_spin);
  357. gt->gt_quota_scale_num = x;
  358. gt->gt_quota_scale_den = y;
  359. spin_unlock(&gt->gt_spin);
  360. return len;
  361. }
  362. static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
  363. int check_zero, const char *buf, size_t len)
  364. {
  365. struct gfs2_tune *gt = &sdp->sd_tune;
  366. unsigned int x;
  367. if (!capable(CAP_SYS_ADMIN))
  368. return -EACCES;
  369. x = simple_strtoul(buf, NULL, 0);
  370. if (check_zero && !x)
  371. return -EINVAL;
  372. spin_lock(&gt->gt_spin);
  373. *field = x;
  374. spin_unlock(&gt->gt_spin);
  375. return len;
  376. }
  377. #define TUNE_ATTR_3(name, show, store) \
  378. static struct gfs2_attr tune_attr_##name = __ATTR(name, 0644, show, store)
  379. #define TUNE_ATTR_2(name, store) \
  380. static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
  381. { \
  382. return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
  383. } \
  384. TUNE_ATTR_3(name, name##_show, store)
  385. #define TUNE_ATTR(name, check_zero) \
  386. static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
  387. { \
  388. return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
  389. } \
  390. TUNE_ATTR_2(name, name##_store)
  391. TUNE_ATTR(quota_warn_period, 0);
  392. TUNE_ATTR(quota_quantum, 0);
  393. TUNE_ATTR(max_readahead, 0);
  394. TUNE_ATTR(complain_secs, 0);
  395. TUNE_ATTR(statfs_slow, 0);
  396. TUNE_ATTR(new_files_jdata, 0);
  397. TUNE_ATTR(quota_simul_sync, 1);
  398. TUNE_ATTR(statfs_quantum, 1);
  399. TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
  400. static struct attribute *tune_attrs[] = {
  401. &tune_attr_quota_warn_period.attr,
  402. &tune_attr_quota_quantum.attr,
  403. &tune_attr_max_readahead.attr,
  404. &tune_attr_complain_secs.attr,
  405. &tune_attr_statfs_slow.attr,
  406. &tune_attr_quota_simul_sync.attr,
  407. &tune_attr_statfs_quantum.attr,
  408. &tune_attr_quota_scale.attr,
  409. &tune_attr_new_files_jdata.attr,
  410. NULL,
  411. };
  412. static struct attribute_group tune_group = {
  413. .name = "tune",
  414. .attrs = tune_attrs,
  415. };
  416. static struct attribute_group lock_module_group = {
  417. .name = "lock_module",
  418. .attrs = lock_module_attrs,
  419. };
  420. int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
  421. {
  422. struct super_block *sb = sdp->sd_vfs;
  423. int error;
  424. char ro[20];
  425. char spectator[20];
  426. char *envp[] = { ro, spectator, NULL };
  427. sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
  428. sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
  429. sdp->sd_kobj.kset = gfs2_kset;
  430. error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
  431. "%s", sdp->sd_table_name);
  432. if (error)
  433. goto fail;
  434. error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
  435. if (error)
  436. goto fail_reg;
  437. error = sysfs_create_group(&sdp->sd_kobj, &lock_module_group);
  438. if (error)
  439. goto fail_tune;
  440. error = sysfs_create_link(&sdp->sd_kobj,
  441. &disk_to_dev(sb->s_bdev->bd_disk)->kobj,
  442. "device");
  443. if (error)
  444. goto fail_lock_module;
  445. kobject_uevent_env(&sdp->sd_kobj, KOBJ_ADD, envp);
  446. return 0;
  447. fail_lock_module:
  448. sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
  449. fail_tune:
  450. sysfs_remove_group(&sdp->sd_kobj, &tune_group);
  451. fail_reg:
  452. kobject_put(&sdp->sd_kobj);
  453. fail:
  454. fs_err(sdp, "error %d adding sysfs files", error);
  455. return error;
  456. }
  457. void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
  458. {
  459. sysfs_remove_link(&sdp->sd_kobj, "device");
  460. sysfs_remove_group(&sdp->sd_kobj, &tune_group);
  461. sysfs_remove_group(&sdp->sd_kobj, &lock_module_group);
  462. kobject_put(&sdp->sd_kobj);
  463. }
  464. static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
  465. struct kobj_uevent_env *env)
  466. {
  467. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  468. const u8 *uuid = sdp->sd_sb.sb_uuid;
  469. add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
  470. add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
  471. if (!sdp->sd_args.ar_spectator)
  472. add_uevent_var(env, "JOURNALID=%u", sdp->sd_lockstruct.ls_jid);
  473. if (gfs2_uuid_valid(uuid))
  474. add_uevent_var(env, "UUID=%pUB", uuid);
  475. return 0;
  476. }
  477. static const struct kset_uevent_ops gfs2_uevent_ops = {
  478. .uevent = gfs2_uevent,
  479. };
  480. int gfs2_sys_init(void)
  481. {
  482. gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
  483. if (!gfs2_kset)
  484. return -ENOMEM;
  485. return 0;
  486. }
  487. void gfs2_sys_uninit(void)
  488. {
  489. kset_unregister(gfs2_kset);
  490. }