ops_super.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 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 "log.h"
  27. #include "mount.h"
  28. #include "ops_super.h"
  29. #include "quota.h"
  30. #include "recovery.h"
  31. #include "rgrp.h"
  32. #include "super.h"
  33. #include "sys.h"
  34. #include "util.h"
  35. #include "trans.h"
  36. #include "dir.h"
  37. #include "eattr.h"
  38. #include "bmap.h"
  39. /**
  40. * gfs2_write_inode - Make sure the inode is stable on the disk
  41. * @inode: The inode
  42. * @sync: synchronous write flag
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_write_inode(struct inode *inode, int sync)
  47. {
  48. struct gfs2_inode *ip = GFS2_I(inode);
  49. /* Check this is a "normal" inode */
  50. if (test_bit(GIF_USER, &ip->i_flags)) {
  51. if (current->flags & PF_MEMALLOC)
  52. return 0;
  53. if (sync)
  54. gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
  55. }
  56. return 0;
  57. }
  58. /**
  59. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  60. * @sdp: the filesystem
  61. *
  62. * Returns: errno
  63. */
  64. static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  65. {
  66. struct gfs2_holder t_gh;
  67. int error;
  68. gfs2_quota_sync(sdp);
  69. gfs2_statfs_sync(sdp);
  70. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
  71. &t_gh);
  72. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  73. return error;
  74. gfs2_meta_syncfs(sdp);
  75. gfs2_log_shutdown(sdp);
  76. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  77. if (t_gh.gh_gl)
  78. gfs2_glock_dq_uninit(&t_gh);
  79. gfs2_quota_cleanup(sdp);
  80. return error;
  81. }
  82. /**
  83. * gfs2_put_super - Unmount the filesystem
  84. * @sb: The VFS superblock
  85. *
  86. */
  87. static void gfs2_put_super(struct super_block *sb)
  88. {
  89. struct gfs2_sbd *sdp = sb->s_fs_info;
  90. int error;
  91. /* Unfreeze the filesystem, if we need to */
  92. mutex_lock(&sdp->sd_freeze_lock);
  93. if (sdp->sd_freeze_count)
  94. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  95. mutex_unlock(&sdp->sd_freeze_lock);
  96. kthread_stop(sdp->sd_quotad_process);
  97. kthread_stop(sdp->sd_logd_process);
  98. kthread_stop(sdp->sd_recoverd_process);
  99. while (sdp->sd_glockd_num--)
  100. kthread_stop(sdp->sd_glockd_process[sdp->sd_glockd_num]);
  101. if (!(sb->s_flags & MS_RDONLY)) {
  102. error = gfs2_make_fs_ro(sdp);
  103. if (error)
  104. gfs2_io_error(sdp);
  105. }
  106. /* At this point, we're through modifying the disk */
  107. /* Release stuff */
  108. iput(sdp->sd_jindex);
  109. iput(sdp->sd_inum_inode);
  110. iput(sdp->sd_statfs_inode);
  111. iput(sdp->sd_rindex);
  112. iput(sdp->sd_quota_inode);
  113. gfs2_glock_put(sdp->sd_rename_gl);
  114. gfs2_glock_put(sdp->sd_trans_gl);
  115. if (!sdp->sd_args.ar_spectator) {
  116. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  117. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  118. gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
  119. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  120. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  121. iput(sdp->sd_ir_inode);
  122. iput(sdp->sd_sc_inode);
  123. iput(sdp->sd_qc_inode);
  124. }
  125. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  126. gfs2_clear_rgrpd(sdp);
  127. gfs2_jindex_free(sdp);
  128. /* Take apart glock structures and buffer lists */
  129. gfs2_gl_hash_clear(sdp);
  130. /* Unmount the locking protocol */
  131. gfs2_lm_unmount(sdp);
  132. /* At this point, we're through participating in the lockspace */
  133. gfs2_sys_fs_del(sdp);
  134. kfree(sdp);
  135. }
  136. /**
  137. * gfs2_write_super
  138. * @sb: the superblock
  139. *
  140. */
  141. static void gfs2_write_super(struct super_block *sb)
  142. {
  143. sb->s_dirt = 0;
  144. }
  145. /**
  146. * gfs2_sync_fs - sync the filesystem
  147. * @sb: the superblock
  148. *
  149. * Flushes the log to disk.
  150. */
  151. static int gfs2_sync_fs(struct super_block *sb, int wait)
  152. {
  153. sb->s_dirt = 0;
  154. if (wait && sb->s_fs_info)
  155. gfs2_log_flush(sb->s_fs_info, NULL);
  156. return 0;
  157. }
  158. /**
  159. * gfs2_write_super_lockfs - prevent further writes to the filesystem
  160. * @sb: the VFS structure for the filesystem
  161. *
  162. */
  163. static void gfs2_write_super_lockfs(struct super_block *sb)
  164. {
  165. struct gfs2_sbd *sdp = sb->s_fs_info;
  166. int error;
  167. if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  168. return;
  169. for (;;) {
  170. error = gfs2_freeze_fs(sdp);
  171. if (!error)
  172. break;
  173. switch (error) {
  174. case -EBUSY:
  175. fs_err(sdp, "waiting for recovery before freeze\n");
  176. break;
  177. default:
  178. fs_err(sdp, "error freezing FS: %d\n", error);
  179. break;
  180. }
  181. fs_err(sdp, "retrying...\n");
  182. msleep(1000);
  183. }
  184. }
  185. /**
  186. * gfs2_unlockfs - reallow writes to the filesystem
  187. * @sb: the VFS structure for the filesystem
  188. *
  189. */
  190. static void gfs2_unlockfs(struct super_block *sb)
  191. {
  192. gfs2_unfreeze_fs(sb->s_fs_info);
  193. }
  194. /**
  195. * gfs2_statfs - Gather and return stats about the filesystem
  196. * @sb: The superblock
  197. * @statfsbuf: The buffer
  198. *
  199. * Returns: 0 on success or error code
  200. */
  201. static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  202. {
  203. struct super_block *sb = dentry->d_inode->i_sb;
  204. struct gfs2_sbd *sdp = sb->s_fs_info;
  205. struct gfs2_statfs_change_host sc;
  206. int error;
  207. if (gfs2_tune_get(sdp, gt_statfs_slow))
  208. error = gfs2_statfs_slow(sdp, &sc);
  209. else
  210. error = gfs2_statfs_i(sdp, &sc);
  211. if (error)
  212. return error;
  213. buf->f_type = GFS2_MAGIC;
  214. buf->f_bsize = sdp->sd_sb.sb_bsize;
  215. buf->f_blocks = sc.sc_total;
  216. buf->f_bfree = sc.sc_free;
  217. buf->f_bavail = sc.sc_free;
  218. buf->f_files = sc.sc_dinodes + sc.sc_free;
  219. buf->f_ffree = sc.sc_free;
  220. buf->f_namelen = GFS2_FNAMESIZE;
  221. return 0;
  222. }
  223. /**
  224. * gfs2_remount_fs - called when the FS is remounted
  225. * @sb: the filesystem
  226. * @flags: the remount flags
  227. * @data: extra data passed in (not used right now)
  228. *
  229. * Returns: errno
  230. */
  231. static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
  232. {
  233. struct gfs2_sbd *sdp = sb->s_fs_info;
  234. int error;
  235. error = gfs2_mount_args(sdp, data, 1);
  236. if (error)
  237. return error;
  238. if (sdp->sd_args.ar_spectator)
  239. *flags |= MS_RDONLY;
  240. else {
  241. if (*flags & MS_RDONLY) {
  242. if (!(sb->s_flags & MS_RDONLY))
  243. error = gfs2_make_fs_ro(sdp);
  244. } else if (!(*flags & MS_RDONLY) &&
  245. (sb->s_flags & MS_RDONLY)) {
  246. error = gfs2_make_fs_rw(sdp);
  247. }
  248. }
  249. if (*flags & (MS_NOATIME | MS_NODIRATIME))
  250. set_bit(SDF_NOATIME, &sdp->sd_flags);
  251. else
  252. clear_bit(SDF_NOATIME, &sdp->sd_flags);
  253. /* Don't let the VFS update atimes. GFS2 handles this itself. */
  254. *flags |= MS_NOATIME | MS_NODIRATIME;
  255. return error;
  256. }
  257. /**
  258. * gfs2_drop_inode - Drop an inode (test for remote unlink)
  259. * @inode: The inode to drop
  260. *
  261. * If we've received a callback on an iopen lock then its because a
  262. * remote node tried to deallocate the inode but failed due to this node
  263. * still having the inode open. Here we mark the link count zero
  264. * since we know that it must have reached zero if the GLF_DEMOTE flag
  265. * is set on the iopen glock. If we didn't do a disk read since the
  266. * remote node removed the final link then we might otherwise miss
  267. * this event. This check ensures that this node will deallocate the
  268. * inode's blocks, or alternatively pass the baton on to another
  269. * node for later deallocation.
  270. */
  271. static void gfs2_drop_inode(struct inode *inode)
  272. {
  273. struct gfs2_inode *ip = GFS2_I(inode);
  274. if (test_bit(GIF_USER, &ip->i_flags) && inode->i_nlink) {
  275. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  276. if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
  277. clear_nlink(inode);
  278. }
  279. generic_drop_inode(inode);
  280. }
  281. /**
  282. * gfs2_clear_inode - Deallocate an inode when VFS is done with it
  283. * @inode: The VFS inode
  284. *
  285. */
  286. static void gfs2_clear_inode(struct inode *inode)
  287. {
  288. struct gfs2_inode *ip = GFS2_I(inode);
  289. /* This tells us its a "real" inode and not one which only
  290. * serves to contain an address space (see rgrp.c, meta_io.c)
  291. * which therefore doesn't have its own glocks.
  292. */
  293. if (test_bit(GIF_USER, &ip->i_flags)) {
  294. ip->i_gl->gl_object = NULL;
  295. gfs2_glock_schedule_for_reclaim(ip->i_gl);
  296. gfs2_glock_put(ip->i_gl);
  297. ip->i_gl = NULL;
  298. if (ip->i_iopen_gh.gh_gl) {
  299. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  300. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  301. }
  302. }
  303. }
  304. static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
  305. {
  306. do {
  307. if (d1 == d2)
  308. return 1;
  309. d1 = d1->d_parent;
  310. } while (!IS_ROOT(d1));
  311. return 0;
  312. }
  313. /**
  314. * gfs2_show_options - Show mount options for /proc/mounts
  315. * @s: seq_file structure
  316. * @mnt: vfsmount
  317. *
  318. * Returns: 0 on success or error code
  319. */
  320. static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
  321. {
  322. struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
  323. struct gfs2_args *args = &sdp->sd_args;
  324. if (is_ancestor(mnt->mnt_root, sdp->sd_master_dir))
  325. seq_printf(s, ",meta");
  326. if (args->ar_lockproto[0])
  327. seq_printf(s, ",lockproto=%s", args->ar_lockproto);
  328. if (args->ar_locktable[0])
  329. seq_printf(s, ",locktable=%s", args->ar_locktable);
  330. if (args->ar_hostdata[0])
  331. seq_printf(s, ",hostdata=%s", args->ar_hostdata);
  332. if (args->ar_spectator)
  333. seq_printf(s, ",spectator");
  334. if (args->ar_ignore_local_fs)
  335. seq_printf(s, ",ignore_local_fs");
  336. if (args->ar_localflocks)
  337. seq_printf(s, ",localflocks");
  338. if (args->ar_localcaching)
  339. seq_printf(s, ",localcaching");
  340. if (args->ar_debug)
  341. seq_printf(s, ",debug");
  342. if (args->ar_upgrade)
  343. seq_printf(s, ",upgrade");
  344. if (args->ar_num_glockd != GFS2_GLOCKD_DEFAULT)
  345. seq_printf(s, ",num_glockd=%u", args->ar_num_glockd);
  346. if (args->ar_posix_acl)
  347. seq_printf(s, ",acl");
  348. if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
  349. char *state;
  350. switch (args->ar_quota) {
  351. case GFS2_QUOTA_OFF:
  352. state = "off";
  353. break;
  354. case GFS2_QUOTA_ACCOUNT:
  355. state = "account";
  356. break;
  357. case GFS2_QUOTA_ON:
  358. state = "on";
  359. break;
  360. default:
  361. state = "unknown";
  362. break;
  363. }
  364. seq_printf(s, ",quota=%s", state);
  365. }
  366. if (args->ar_suiddir)
  367. seq_printf(s, ",suiddir");
  368. if (args->ar_data != GFS2_DATA_DEFAULT) {
  369. char *state;
  370. switch (args->ar_data) {
  371. case GFS2_DATA_WRITEBACK:
  372. state = "writeback";
  373. break;
  374. case GFS2_DATA_ORDERED:
  375. state = "ordered";
  376. break;
  377. default:
  378. state = "unknown";
  379. break;
  380. }
  381. seq_printf(s, ",data=%s", state);
  382. }
  383. return 0;
  384. }
  385. /*
  386. * We have to (at the moment) hold the inodes main lock to cover
  387. * the gap between unlocking the shared lock on the iopen lock and
  388. * taking the exclusive lock. I'd rather do a shared -> exclusive
  389. * conversion on the iopen lock, but we can change that later. This
  390. * is safe, just less efficient.
  391. */
  392. static void gfs2_delete_inode(struct inode *inode)
  393. {
  394. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  395. struct gfs2_inode *ip = GFS2_I(inode);
  396. struct gfs2_holder gh;
  397. int error;
  398. if (!test_bit(GIF_USER, &ip->i_flags))
  399. goto out;
  400. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  401. if (unlikely(error)) {
  402. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  403. goto out;
  404. }
  405. gfs2_glock_dq_wait(&ip->i_iopen_gh);
  406. gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
  407. error = gfs2_glock_nq(&ip->i_iopen_gh);
  408. if (error)
  409. goto out_uninit;
  410. if (S_ISDIR(inode->i_mode) &&
  411. (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  412. error = gfs2_dir_exhash_dealloc(ip);
  413. if (error)
  414. goto out_unlock;
  415. }
  416. if (ip->i_di.di_eattr) {
  417. error = gfs2_ea_dealloc(ip);
  418. if (error)
  419. goto out_unlock;
  420. }
  421. if (!gfs2_is_stuffed(ip)) {
  422. error = gfs2_file_dealloc(ip);
  423. if (error)
  424. goto out_unlock;
  425. }
  426. error = gfs2_dinode_dealloc(ip);
  427. if (error)
  428. goto out_unlock;
  429. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  430. if (error)
  431. goto out_unlock;
  432. /* Needs to be done before glock release & also in a transaction */
  433. truncate_inode_pages(&inode->i_data, 0);
  434. gfs2_trans_end(sdp);
  435. out_unlock:
  436. gfs2_glock_dq(&ip->i_iopen_gh);
  437. out_uninit:
  438. gfs2_holder_uninit(&ip->i_iopen_gh);
  439. gfs2_glock_dq_uninit(&gh);
  440. if (error && error != GLR_TRYFAILED)
  441. fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
  442. out:
  443. truncate_inode_pages(&inode->i_data, 0);
  444. clear_inode(inode);
  445. }
  446. static struct inode *gfs2_alloc_inode(struct super_block *sb)
  447. {
  448. struct gfs2_inode *ip;
  449. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  450. if (ip) {
  451. ip->i_flags = 0;
  452. ip->i_gl = NULL;
  453. }
  454. return &ip->i_inode;
  455. }
  456. static void gfs2_destroy_inode(struct inode *inode)
  457. {
  458. kmem_cache_free(gfs2_inode_cachep, inode);
  459. }
  460. const struct super_operations gfs2_super_ops = {
  461. .alloc_inode = gfs2_alloc_inode,
  462. .destroy_inode = gfs2_destroy_inode,
  463. .write_inode = gfs2_write_inode,
  464. .delete_inode = gfs2_delete_inode,
  465. .put_super = gfs2_put_super,
  466. .write_super = gfs2_write_super,
  467. .sync_fs = gfs2_sync_fs,
  468. .write_super_lockfs = gfs2_write_super_lockfs,
  469. .unlockfs = gfs2_unlockfs,
  470. .statfs = gfs2_statfs,
  471. .remount_fs = gfs2_remount_fs,
  472. .clear_inode = gfs2_clear_inode,
  473. .drop_inode = gfs2_drop_inode,
  474. .show_options = gfs2_show_options,
  475. };