ops_super.c 13 KB

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