ops_super.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/statfs.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/mount.h>
  18. #include <linux/kthread.h>
  19. #include <linux/delay.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/crc32.h>
  22. #include "gfs2.h"
  23. #include "lm_interface.h"
  24. #include "incore.h"
  25. #include "glock.h"
  26. #include "inode.h"
  27. #include "lm.h"
  28. #include "log.h"
  29. #include "mount.h"
  30. #include "ops_super.h"
  31. #include "quota.h"
  32. #include "recovery.h"
  33. #include "rgrp.h"
  34. #include "super.h"
  35. #include "sys.h"
  36. #include "util.h"
  37. #include "trans.h"
  38. #include "dir.h"
  39. #include "eattr.h"
  40. #include "bmap.h"
  41. /**
  42. * gfs2_write_inode - Make sure the inode is stable on the disk
  43. * @inode: The inode
  44. * @sync: synchronous write flag
  45. *
  46. * Returns: errno
  47. */
  48. static int gfs2_write_inode(struct inode *inode, int sync)
  49. {
  50. struct gfs2_inode *ip = GFS2_I(inode);
  51. /* Check this is a "normal" inode */
  52. if (inode->u.generic_ip) {
  53. if (current->flags & PF_MEMALLOC)
  54. return 0;
  55. if (sync)
  56. gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
  57. }
  58. return 0;
  59. }
  60. /**
  61. * gfs2_put_super - Unmount the filesystem
  62. * @sb: The VFS superblock
  63. *
  64. */
  65. static void gfs2_put_super(struct super_block *sb)
  66. {
  67. struct gfs2_sbd *sdp = sb->s_fs_info;
  68. int error;
  69. if (!sdp)
  70. return;
  71. if (!strncmp(sb->s_type->name, "gfs2meta", 8))
  72. return; /* Nothing to do */
  73. /* Unfreeze the filesystem, if we need to */
  74. mutex_lock(&sdp->sd_freeze_lock);
  75. if (sdp->sd_freeze_count)
  76. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  77. mutex_unlock(&sdp->sd_freeze_lock);
  78. kthread_stop(sdp->sd_quotad_process);
  79. kthread_stop(sdp->sd_logd_process);
  80. kthread_stop(sdp->sd_recoverd_process);
  81. while (sdp->sd_glockd_num--)
  82. kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
  83. kthread_stop(sdp->sd_scand_process);
  84. if (!(sb->s_flags & MS_RDONLY)) {
  85. error = gfs2_make_fs_ro(sdp);
  86. if (error)
  87. gfs2_io_error(sdp);
  88. }
  89. /* At this point, we're through modifying the disk */
  90. /* Release stuff */
  91. iput(sdp->sd_master_dir);
  92. iput(sdp->sd_jindex);
  93. iput(sdp->sd_inum_inode);
  94. iput(sdp->sd_statfs_inode);
  95. iput(sdp->sd_rindex);
  96. iput(sdp->sd_quota_inode);
  97. gfs2_glock_put(sdp->sd_rename_gl);
  98. gfs2_glock_put(sdp->sd_trans_gl);
  99. if (!sdp->sd_args.ar_spectator) {
  100. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  101. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  102. gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
  103. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  104. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  105. iput(sdp->sd_ir_inode);
  106. iput(sdp->sd_sc_inode);
  107. iput(sdp->sd_qc_inode);
  108. }
  109. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  110. gfs2_clear_rgrpd(sdp);
  111. gfs2_jindex_free(sdp);
  112. /* Take apart glock structures and buffer lists */
  113. gfs2_gl_hash_clear(sdp, WAIT);
  114. /* Unmount the locking protocol */
  115. gfs2_lm_unmount(sdp);
  116. /* At this point, we're through participating in the lockspace */
  117. gfs2_sys_fs_del(sdp);
  118. vfree(sdp);
  119. }
  120. /**
  121. * gfs2_write_super - disk commit all incore transactions
  122. * @sb: the filesystem
  123. *
  124. * This function is called every time sync(2) is called.
  125. * After this exits, all dirty buffers are synced.
  126. */
  127. static void gfs2_write_super(struct super_block *sb)
  128. {
  129. gfs2_log_flush(sb->s_fs_info, NULL);
  130. }
  131. /**
  132. * gfs2_write_super_lockfs - prevent further writes to the filesystem
  133. * @sb: the VFS structure for the filesystem
  134. *
  135. */
  136. static void gfs2_write_super_lockfs(struct super_block *sb)
  137. {
  138. struct gfs2_sbd *sdp = sb->s_fs_info;
  139. int error;
  140. for (;;) {
  141. error = gfs2_freeze_fs(sdp);
  142. if (!error)
  143. break;
  144. switch (error) {
  145. case -EBUSY:
  146. fs_err(sdp, "waiting for recovery before freeze\n");
  147. break;
  148. default:
  149. fs_err(sdp, "error freezing FS: %d\n", error);
  150. break;
  151. }
  152. fs_err(sdp, "retrying...\n");
  153. msleep(1000);
  154. }
  155. }
  156. /**
  157. * gfs2_unlockfs - reallow writes to the filesystem
  158. * @sb: the VFS structure for the filesystem
  159. *
  160. */
  161. static void gfs2_unlockfs(struct super_block *sb)
  162. {
  163. gfs2_unfreeze_fs(sb->s_fs_info);
  164. }
  165. /**
  166. * gfs2_statfs - Gather and return stats about the filesystem
  167. * @sb: The superblock
  168. * @statfsbuf: The buffer
  169. *
  170. * Returns: 0 on success or error code
  171. */
  172. static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  173. {
  174. struct super_block *sb = dentry->d_inode->i_sb;
  175. struct gfs2_sbd *sdp = sb->s_fs_info;
  176. struct gfs2_statfs_change sc;
  177. int error;
  178. if (gfs2_tune_get(sdp, gt_statfs_slow))
  179. error = gfs2_statfs_slow(sdp, &sc);
  180. else
  181. error = gfs2_statfs_i(sdp, &sc);
  182. if (error)
  183. return error;
  184. buf->f_type = GFS2_MAGIC;
  185. buf->f_bsize = sdp->sd_sb.sb_bsize;
  186. buf->f_blocks = sc.sc_total;
  187. buf->f_bfree = sc.sc_free;
  188. buf->f_bavail = sc.sc_free;
  189. buf->f_files = sc.sc_dinodes + sc.sc_free;
  190. buf->f_ffree = sc.sc_free;
  191. buf->f_namelen = GFS2_FNAMESIZE;
  192. return 0;
  193. }
  194. /**
  195. * gfs2_remount_fs - called when the FS is remounted
  196. * @sb: the filesystem
  197. * @flags: the remount flags
  198. * @data: extra data passed in (not used right now)
  199. *
  200. * Returns: errno
  201. */
  202. static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
  203. {
  204. struct gfs2_sbd *sdp = sb->s_fs_info;
  205. int error;
  206. error = gfs2_mount_args(sdp, data, 1);
  207. if (error)
  208. return error;
  209. if (sdp->sd_args.ar_spectator)
  210. *flags |= MS_RDONLY;
  211. else {
  212. if (*flags & MS_RDONLY) {
  213. if (!(sb->s_flags & MS_RDONLY))
  214. error = gfs2_make_fs_ro(sdp);
  215. } else if (!(*flags & MS_RDONLY) &&
  216. (sb->s_flags & MS_RDONLY)) {
  217. error = gfs2_make_fs_rw(sdp);
  218. }
  219. }
  220. if (*flags & (MS_NOATIME | MS_NODIRATIME))
  221. set_bit(SDF_NOATIME, &sdp->sd_flags);
  222. else
  223. clear_bit(SDF_NOATIME, &sdp->sd_flags);
  224. /* Don't let the VFS update atimes. GFS2 handles this itself. */
  225. *flags |= MS_NOATIME | MS_NODIRATIME;
  226. return error;
  227. }
  228. /**
  229. * gfs2_clear_inode - Deallocate an inode when VFS is done with it
  230. * @inode: The VFS inode
  231. *
  232. */
  233. static void gfs2_clear_inode(struct inode *inode)
  234. {
  235. /* This tells us its a "real" inode and not one which only
  236. * serves to contain an address space (see rgrp.c, meta_io.c)
  237. * which therefore doesn't have its own glocks.
  238. */
  239. if (inode->u.generic_ip) {
  240. struct gfs2_inode *ip = GFS2_I(inode);
  241. gfs2_glock_inode_squish(inode);
  242. gfs2_assert(inode->i_sb->s_fs_info, ip->i_gl->gl_state == LM_ST_UNLOCKED);
  243. ip->i_gl->gl_object = NULL;
  244. gfs2_glock_schedule_for_reclaim(ip->i_gl);
  245. gfs2_glock_put(ip->i_gl);
  246. ip->i_gl = NULL;
  247. if (ip->i_iopen_gh.gh_gl)
  248. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  249. }
  250. }
  251. /**
  252. * gfs2_show_options - Show mount options for /proc/mounts
  253. * @s: seq_file structure
  254. * @mnt: vfsmount
  255. *
  256. * Returns: 0 on success or error code
  257. */
  258. static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
  259. {
  260. struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
  261. struct gfs2_args *args = &sdp->sd_args;
  262. if (args->ar_lockproto[0])
  263. seq_printf(s, ",lockproto=%s", args->ar_lockproto);
  264. if (args->ar_locktable[0])
  265. seq_printf(s, ",locktable=%s", args->ar_locktable);
  266. if (args->ar_hostdata[0])
  267. seq_printf(s, ",hostdata=%s", args->ar_hostdata);
  268. if (args->ar_spectator)
  269. seq_printf(s, ",spectator");
  270. if (args->ar_ignore_local_fs)
  271. seq_printf(s, ",ignore_local_fs");
  272. if (args->ar_localflocks)
  273. seq_printf(s, ",localflocks");
  274. if (args->ar_localcaching)
  275. seq_printf(s, ",localcaching");
  276. if (args->ar_debug)
  277. seq_printf(s, ",debug");
  278. if (args->ar_upgrade)
  279. seq_printf(s, ",upgrade");
  280. if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
  281. seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
  282. if (args->ar_posix_acl)
  283. seq_printf(s, ",acl");
  284. if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
  285. char *state;
  286. switch (args->ar_quota) {
  287. case GFS2_QUOTA_OFF:
  288. state = "off";
  289. break;
  290. case GFS2_QUOTA_ACCOUNT:
  291. state = "account";
  292. break;
  293. case GFS2_QUOTA_ON:
  294. state = "on";
  295. break;
  296. default:
  297. state = "unknown";
  298. break;
  299. }
  300. seq_printf(s, ",quota=%s", state);
  301. }
  302. if (args->ar_suiddir)
  303. seq_printf(s, ",suiddir");
  304. if (args->ar_data != GFS2_DATA_DEFAULT) {
  305. char *state;
  306. switch (args->ar_data) {
  307. case GFS2_DATA_WRITEBACK:
  308. state = "writeback";
  309. break;
  310. case GFS2_DATA_ORDERED:
  311. state = "ordered";
  312. break;
  313. default:
  314. state = "unknown";
  315. break;
  316. }
  317. seq_printf(s, ",data=%s", state);
  318. }
  319. return 0;
  320. }
  321. /*
  322. * We have to (at the moment) hold the inodes main lock to cover
  323. * the gap between unlocking the shared lock on the iopen lock and
  324. * taking the exclusive lock. I'd rather do a shared -> exclusive
  325. * conversion on the iopen lock, but we can change that later. This
  326. * is safe, just less efficient.
  327. */
  328. static void gfs2_delete_inode(struct inode *inode)
  329. {
  330. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  331. struct gfs2_inode *ip = GFS2_I(inode);
  332. struct gfs2_holder gh;
  333. int error;
  334. if (!inode->u.generic_ip)
  335. goto out;
  336. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &gh);
  337. if (unlikely(error)) {
  338. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  339. goto out;
  340. }
  341. gfs2_glock_dq(&ip->i_iopen_gh);
  342. gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
  343. error = gfs2_glock_nq(&ip->i_iopen_gh);
  344. if (error)
  345. goto out_uninit;
  346. if (S_ISDIR(ip->i_di.di_mode) &&
  347. (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  348. error = gfs2_dir_exhash_dealloc(ip);
  349. if (error)
  350. goto out_unlock;
  351. }
  352. if (ip->i_di.di_eattr) {
  353. error = gfs2_ea_dealloc(ip);
  354. if (error)
  355. goto out_unlock;
  356. }
  357. if (!gfs2_is_stuffed(ip)) {
  358. error = gfs2_file_dealloc(ip);
  359. if (error)
  360. goto out_unlock;
  361. }
  362. error = gfs2_dinode_dealloc(ip);
  363. out_unlock:
  364. gfs2_glock_dq(&ip->i_iopen_gh);
  365. out_uninit:
  366. gfs2_holder_uninit(&ip->i_iopen_gh);
  367. gfs2_glock_dq_uninit(&gh);
  368. if (error)
  369. fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
  370. out:
  371. truncate_inode_pages(&inode->i_data, 0);
  372. clear_inode(inode);
  373. }
  374. static struct inode *gfs2_alloc_inode(struct super_block *sb)
  375. {
  376. struct gfs2_sbd *sdp = sb->s_fs_info;
  377. struct gfs2_inode *ip;
  378. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  379. if (ip) {
  380. ip->i_flags = 0;
  381. ip->i_gl = NULL;
  382. ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
  383. ip->i_last_pfault = jiffies;
  384. }
  385. return &ip->i_inode;
  386. }
  387. static void gfs2_destroy_inode(struct inode *inode)
  388. {
  389. kmem_cache_free(gfs2_inode_cachep, inode);
  390. }
  391. struct super_operations gfs2_super_ops = {
  392. .alloc_inode = gfs2_alloc_inode,
  393. .destroy_inode = gfs2_destroy_inode,
  394. .write_inode = gfs2_write_inode,
  395. .delete_inode = gfs2_delete_inode,
  396. .put_super = gfs2_put_super,
  397. .write_super = gfs2_write_super,
  398. .write_super_lockfs = gfs2_write_super_lockfs,
  399. .unlockfs = gfs2_unlockfs,
  400. .statfs = gfs2_statfs,
  401. .remount_fs = gfs2_remount_fs,
  402. .clear_inode = gfs2_clear_inode,
  403. .show_options = gfs2_show_options,
  404. };