PageRenderTime 245ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/sys.c

https://github.com/kipill-nn/Kernel-for-Mega
C | 487 lines | 386 code | 84 blank | 17 comment | 26 complexity | 5618d018a69a6c38e2beb89c42084d07 MD5 | raw file
  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 <linux/gfs2_ondisk.h>
  17. #include <linux/lm_interface.h>
  18. #include <asm/uaccess.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. static ssize_t id_show(struct gfs2_sbd *sdp, char *buf)
  27. {
  28. return snprintf(buf, PAGE_SIZE, "%u:%u\n",
  29. MAJOR(sdp->sd_vfs->s_dev), MINOR(sdp->sd_vfs->s_dev));
  30. }
  31. static ssize_t fsname_show(struct gfs2_sbd *sdp, char *buf)
  32. {
  33. return snprintf(buf, PAGE_SIZE, "%s\n", sdp->sd_fsname);
  34. }
  35. static ssize_t freeze_show(struct gfs2_sbd *sdp, char *buf)
  36. {
  37. unsigned int count;
  38. mutex_lock(&sdp->sd_freeze_lock);
  39. count = sdp->sd_freeze_count;
  40. mutex_unlock(&sdp->sd_freeze_lock);
  41. return snprintf(buf, PAGE_SIZE, "%u\n", count);
  42. }
  43. static ssize_t freeze_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  44. {
  45. ssize_t ret = len;
  46. int error = 0;
  47. int n = simple_strtol(buf, NULL, 0);
  48. if (!capable(CAP_SYS_ADMIN))
  49. return -EACCES;
  50. switch (n) {
  51. case 0:
  52. gfs2_unfreeze_fs(sdp);
  53. break;
  54. case 1:
  55. error = gfs2_freeze_fs(sdp);
  56. break;
  57. default:
  58. ret = -EINVAL;
  59. }
  60. if (error)
  61. fs_warn(sdp, "freeze %d error %d", n, error);
  62. return ret;
  63. }
  64. static ssize_t withdraw_show(struct gfs2_sbd *sdp, char *buf)
  65. {
  66. unsigned int b = test_bit(SDF_SHUTDOWN, &sdp->sd_flags);
  67. return snprintf(buf, PAGE_SIZE, "%u\n", b);
  68. }
  69. static ssize_t withdraw_store(struct gfs2_sbd *sdp, const char *buf, size_t len)
  70. {
  71. if (!capable(CAP_SYS_ADMIN))
  72. return -EACCES;
  73. if (simple_strtol(buf, NULL, 0) != 1)
  74. return -EINVAL;
  75. gfs2_lm_withdraw(sdp,
  76. "GFS2: fsid=%s: withdrawing from cluster at user's request\n",
  77. sdp->sd_fsname);
  78. return len;
  79. }
  80. static ssize_t statfs_sync_store(struct gfs2_sbd *sdp, const char *buf,
  81. size_t len)
  82. {
  83. if (!capable(CAP_SYS_ADMIN))
  84. return -EACCES;
  85. if (simple_strtol(buf, NULL, 0) != 1)
  86. return -EINVAL;
  87. gfs2_statfs_sync(sdp);
  88. return len;
  89. }
  90. static ssize_t quota_sync_store(struct gfs2_sbd *sdp, const char *buf,
  91. size_t len)
  92. {
  93. if (!capable(CAP_SYS_ADMIN))
  94. return -EACCES;
  95. if (simple_strtol(buf, NULL, 0) != 1)
  96. return -EINVAL;
  97. gfs2_quota_sync(sdp);
  98. return len;
  99. }
  100. static ssize_t quota_refresh_user_store(struct gfs2_sbd *sdp, const char *buf,
  101. size_t len)
  102. {
  103. u32 id;
  104. if (!capable(CAP_SYS_ADMIN))
  105. return -EACCES;
  106. id = simple_strtoul(buf, NULL, 0);
  107. gfs2_quota_refresh(sdp, 1, id);
  108. return len;
  109. }
  110. static ssize_t quota_refresh_group_store(struct gfs2_sbd *sdp, const char *buf,
  111. size_t len)
  112. {
  113. u32 id;
  114. if (!capable(CAP_SYS_ADMIN))
  115. return -EACCES;
  116. id = simple_strtoul(buf, NULL, 0);
  117. gfs2_quota_refresh(sdp, 0, id);
  118. return len;
  119. }
  120. struct gfs2_attr {
  121. struct attribute attr;
  122. ssize_t (*show)(struct gfs2_sbd *, char *);
  123. ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
  124. };
  125. #define GFS2_ATTR(name, mode, show, store) \
  126. static struct gfs2_attr gfs2_attr_##name = __ATTR(name, mode, show, store)
  127. GFS2_ATTR(id, 0444, id_show, NULL);
  128. GFS2_ATTR(fsname, 0444, fsname_show, NULL);
  129. GFS2_ATTR(freeze, 0644, freeze_show, freeze_store);
  130. GFS2_ATTR(withdraw, 0644, withdraw_show, withdraw_store);
  131. GFS2_ATTR(statfs_sync, 0200, NULL, statfs_sync_store);
  132. GFS2_ATTR(quota_sync, 0200, NULL, quota_sync_store);
  133. GFS2_ATTR(quota_refresh_user, 0200, NULL, quota_refresh_user_store);
  134. GFS2_ATTR(quota_refresh_group, 0200, NULL, quota_refresh_group_store);
  135. static struct attribute *gfs2_attrs[] = {
  136. &gfs2_attr_id.attr,
  137. &gfs2_attr_fsname.attr,
  138. &gfs2_attr_freeze.attr,
  139. &gfs2_attr_withdraw.attr,
  140. &gfs2_attr_statfs_sync.attr,
  141. &gfs2_attr_quota_sync.attr,
  142. &gfs2_attr_quota_refresh_user.attr,
  143. &gfs2_attr_quota_refresh_group.attr,
  144. NULL,
  145. };
  146. static ssize_t gfs2_attr_show(struct kobject *kobj, struct attribute *attr,
  147. char *buf)
  148. {
  149. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  150. struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  151. return a->show ? a->show(sdp, buf) : 0;
  152. }
  153. static ssize_t gfs2_attr_store(struct kobject *kobj, struct attribute *attr,
  154. const char *buf, size_t len)
  155. {
  156. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  157. struct gfs2_attr *a = container_of(attr, struct gfs2_attr, attr);
  158. return a->store ? a->store(sdp, buf, len) : len;
  159. }
  160. static struct sysfs_ops gfs2_attr_ops = {
  161. .show = gfs2_attr_show,
  162. .store = gfs2_attr_store,
  163. };
  164. static struct kobj_type gfs2_ktype = {
  165. .default_attrs = gfs2_attrs,
  166. .sysfs_ops = &gfs2_attr_ops,
  167. };
  168. static struct kset *gfs2_kset;
  169. /*
  170. * display struct lm_lockstruct fields
  171. */
  172. struct lockstruct_attr {
  173. struct attribute attr;
  174. ssize_t (*show)(struct gfs2_sbd *, char *);
  175. };
  176. #define LOCKSTRUCT_ATTR(name, fmt) \
  177. static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
  178. { \
  179. return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_lockstruct.ls_##name); \
  180. } \
  181. static struct lockstruct_attr lockstruct_attr_##name = __ATTR_RO(name)
  182. LOCKSTRUCT_ATTR(jid, "%u\n");
  183. LOCKSTRUCT_ATTR(first, "%u\n");
  184. LOCKSTRUCT_ATTR(lvb_size, "%u\n");
  185. LOCKSTRUCT_ATTR(flags, "%d\n");
  186. static struct attribute *lockstruct_attrs[] = {
  187. &lockstruct_attr_jid.attr,
  188. &lockstruct_attr_first.attr,
  189. &lockstruct_attr_lvb_size.attr,
  190. &lockstruct_attr_flags.attr,
  191. NULL,
  192. };
  193. /*
  194. * display struct gfs2_args fields
  195. */
  196. struct args_attr {
  197. struct attribute attr;
  198. ssize_t (*show)(struct gfs2_sbd *, char *);
  199. };
  200. #define ARGS_ATTR(name, fmt) \
  201. static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
  202. { \
  203. return snprintf(buf, PAGE_SIZE, fmt, sdp->sd_args.ar_##name); \
  204. } \
  205. static struct args_attr args_attr_##name = __ATTR_RO(name)
  206. ARGS_ATTR(lockproto, "%s\n");
  207. ARGS_ATTR(locktable, "%s\n");
  208. ARGS_ATTR(hostdata, "%s\n");
  209. ARGS_ATTR(spectator, "%d\n");
  210. ARGS_ATTR(ignore_local_fs, "%d\n");
  211. ARGS_ATTR(localcaching, "%d\n");
  212. ARGS_ATTR(localflocks, "%d\n");
  213. ARGS_ATTR(debug, "%d\n");
  214. ARGS_ATTR(upgrade, "%d\n");
  215. ARGS_ATTR(posix_acl, "%d\n");
  216. ARGS_ATTR(quota, "%u\n");
  217. ARGS_ATTR(suiddir, "%d\n");
  218. ARGS_ATTR(data, "%d\n");
  219. static struct attribute *args_attrs[] = {
  220. &args_attr_lockproto.attr,
  221. &args_attr_locktable.attr,
  222. &args_attr_hostdata.attr,
  223. &args_attr_spectator.attr,
  224. &args_attr_ignore_local_fs.attr,
  225. &args_attr_localcaching.attr,
  226. &args_attr_localflocks.attr,
  227. &args_attr_debug.attr,
  228. &args_attr_upgrade.attr,
  229. &args_attr_posix_acl.attr,
  230. &args_attr_quota.attr,
  231. &args_attr_suiddir.attr,
  232. &args_attr_data.attr,
  233. NULL,
  234. };
  235. /*
  236. * get and set struct gfs2_tune fields
  237. */
  238. static ssize_t quota_scale_show(struct gfs2_sbd *sdp, char *buf)
  239. {
  240. return snprintf(buf, PAGE_SIZE, "%u %u\n",
  241. sdp->sd_tune.gt_quota_scale_num,
  242. sdp->sd_tune.gt_quota_scale_den);
  243. }
  244. static ssize_t quota_scale_store(struct gfs2_sbd *sdp, const char *buf,
  245. size_t len)
  246. {
  247. struct gfs2_tune *gt = &sdp->sd_tune;
  248. unsigned int x, y;
  249. if (!capable(CAP_SYS_ADMIN))
  250. return -EACCES;
  251. if (sscanf(buf, "%u %u", &x, &y) != 2 || !y)
  252. return -EINVAL;
  253. spin_lock(&gt->gt_spin);
  254. gt->gt_quota_scale_num = x;
  255. gt->gt_quota_scale_den = y;
  256. spin_unlock(&gt->gt_spin);
  257. return len;
  258. }
  259. static ssize_t tune_set(struct gfs2_sbd *sdp, unsigned int *field,
  260. int check_zero, const char *buf, size_t len)
  261. {
  262. struct gfs2_tune *gt = &sdp->sd_tune;
  263. unsigned int x;
  264. if (!capable(CAP_SYS_ADMIN))
  265. return -EACCES;
  266. x = simple_strtoul(buf, NULL, 0);
  267. if (check_zero && !x)
  268. return -EINVAL;
  269. spin_lock(&gt->gt_spin);
  270. *field = x;
  271. spin_unlock(&gt->gt_spin);
  272. return len;
  273. }
  274. struct tune_attr {
  275. struct attribute attr;
  276. ssize_t (*show)(struct gfs2_sbd *, char *);
  277. ssize_t (*store)(struct gfs2_sbd *, const char *, size_t);
  278. };
  279. #define TUNE_ATTR_3(name, show, store) \
  280. static struct tune_attr tune_attr_##name = __ATTR(name, 0644, show, store)
  281. #define TUNE_ATTR_2(name, store) \
  282. static ssize_t name##_show(struct gfs2_sbd *sdp, char *buf) \
  283. { \
  284. return snprintf(buf, PAGE_SIZE, "%u\n", sdp->sd_tune.gt_##name); \
  285. } \
  286. TUNE_ATTR_3(name, name##_show, store)
  287. #define TUNE_ATTR(name, check_zero) \
  288. static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
  289. { \
  290. return tune_set(sdp, &sdp->sd_tune.gt_##name, check_zero, buf, len); \
  291. } \
  292. TUNE_ATTR_2(name, name##_store)
  293. #define TUNE_ATTR_DAEMON(name, process) \
  294. static ssize_t name##_store(struct gfs2_sbd *sdp, const char *buf, size_t len)\
  295. { \
  296. ssize_t r = tune_set(sdp, &sdp->sd_tune.gt_##name, 1, buf, len); \
  297. wake_up_process(sdp->sd_##process); \
  298. return r; \
  299. } \
  300. TUNE_ATTR_2(name, name##_store)
  301. TUNE_ATTR(incore_log_blocks, 0);
  302. TUNE_ATTR(log_flush_secs, 0);
  303. TUNE_ATTR(quota_warn_period, 0);
  304. TUNE_ATTR(quota_quantum, 0);
  305. TUNE_ATTR(max_readahead, 0);
  306. TUNE_ATTR(complain_secs, 0);
  307. TUNE_ATTR(statfs_slow, 0);
  308. TUNE_ATTR(new_files_jdata, 0);
  309. TUNE_ATTR(quota_simul_sync, 1);
  310. TUNE_ATTR(quota_cache_secs, 1);
  311. TUNE_ATTR(stall_secs, 1);
  312. TUNE_ATTR(statfs_quantum, 1);
  313. TUNE_ATTR_DAEMON(recoverd_secs, recoverd_process);
  314. TUNE_ATTR_DAEMON(logd_secs, logd_process);
  315. TUNE_ATTR_3(quota_scale, quota_scale_show, quota_scale_store);
  316. static struct attribute *tune_attrs[] = {
  317. &tune_attr_incore_log_blocks.attr,
  318. &tune_attr_log_flush_secs.attr,
  319. &tune_attr_quota_warn_period.attr,
  320. &tune_attr_quota_quantum.attr,
  321. &tune_attr_max_readahead.attr,
  322. &tune_attr_complain_secs.attr,
  323. &tune_attr_statfs_slow.attr,
  324. &tune_attr_quota_simul_sync.attr,
  325. &tune_attr_quota_cache_secs.attr,
  326. &tune_attr_stall_secs.attr,
  327. &tune_attr_statfs_quantum.attr,
  328. &tune_attr_recoverd_secs.attr,
  329. &tune_attr_logd_secs.attr,
  330. &tune_attr_quota_scale.attr,
  331. &tune_attr_new_files_jdata.attr,
  332. NULL,
  333. };
  334. static struct attribute_group lockstruct_group = {
  335. .name = "lockstruct",
  336. .attrs = lockstruct_attrs,
  337. };
  338. static struct attribute_group args_group = {
  339. .name = "args",
  340. .attrs = args_attrs,
  341. };
  342. static struct attribute_group tune_group = {
  343. .name = "tune",
  344. .attrs = tune_attrs,
  345. };
  346. int gfs2_sys_fs_add(struct gfs2_sbd *sdp)
  347. {
  348. int error;
  349. sdp->sd_kobj.kset = gfs2_kset;
  350. error = kobject_init_and_add(&sdp->sd_kobj, &gfs2_ktype, NULL,
  351. "%s", sdp->sd_table_name);
  352. if (error)
  353. goto fail;
  354. error = sysfs_create_group(&sdp->sd_kobj, &lockstruct_group);
  355. if (error)
  356. goto fail_reg;
  357. error = sysfs_create_group(&sdp->sd_kobj, &args_group);
  358. if (error)
  359. goto fail_lockstruct;
  360. error = sysfs_create_group(&sdp->sd_kobj, &tune_group);
  361. if (error)
  362. goto fail_args;
  363. kobject_uevent(&sdp->sd_kobj, KOBJ_ADD);
  364. return 0;
  365. fail_args:
  366. sysfs_remove_group(&sdp->sd_kobj, &args_group);
  367. fail_lockstruct:
  368. sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
  369. fail_reg:
  370. kobject_put(&sdp->sd_kobj);
  371. fail:
  372. fs_err(sdp, "error %d adding sysfs files", error);
  373. return error;
  374. }
  375. void gfs2_sys_fs_del(struct gfs2_sbd *sdp)
  376. {
  377. sysfs_remove_group(&sdp->sd_kobj, &tune_group);
  378. sysfs_remove_group(&sdp->sd_kobj, &args_group);
  379. sysfs_remove_group(&sdp->sd_kobj, &lockstruct_group);
  380. kobject_put(&sdp->sd_kobj);
  381. }
  382. static int gfs2_uevent(struct kset *kset, struct kobject *kobj,
  383. struct kobj_uevent_env *env)
  384. {
  385. struct gfs2_sbd *sdp = container_of(kobj, struct gfs2_sbd, sd_kobj);
  386. add_uevent_var(env, "LOCKTABLE=%s", sdp->sd_table_name);
  387. add_uevent_var(env, "LOCKPROTO=%s", sdp->sd_proto_name);
  388. return 0;
  389. }
  390. static struct kset_uevent_ops gfs2_uevent_ops = {
  391. .uevent = gfs2_uevent,
  392. };
  393. int gfs2_sys_init(void)
  394. {
  395. gfs2_kset = kset_create_and_add("gfs2", &gfs2_uevent_ops, fs_kobj);
  396. if (!gfs2_kset)
  397. return -ENOMEM;
  398. return 0;
  399. }
  400. void gfs2_sys_uninit(void)
  401. {
  402. kset_unregister(gfs2_kset);
  403. }