sys.c 15 KB

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