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