ops_super.c 13 KB

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