ops_super.c 11 KB

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