ops_super.c 12 KB

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