ops_super.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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/time.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "glock.h"
  25. #include "inode.h"
  26. #include "log.h"
  27. #include "quota.h"
  28. #include "recovery.h"
  29. #include "rgrp.h"
  30. #include "super.h"
  31. #include "sys.h"
  32. #include "util.h"
  33. #include "trans.h"
  34. #include "dir.h"
  35. #include "eattr.h"
  36. #include "bmap.h"
  37. #include "meta_io.h"
  38. #define args_neq(a1, a2, x) ((a1)->ar_##x != (a2)->ar_##x)
  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. struct gfs2_sbd *sdp = GFS2_SB(inode);
  50. struct gfs2_holder gh;
  51. struct buffer_head *bh;
  52. struct timespec atime;
  53. struct gfs2_dinode *di;
  54. int ret = 0;
  55. /* Check this is a "normal" inode, etc */
  56. if (!test_bit(GIF_USER, &ip->i_flags) ||
  57. (current->flags & PF_MEMALLOC))
  58. return 0;
  59. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  60. if (ret)
  61. goto do_flush;
  62. ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
  63. if (ret)
  64. goto do_unlock;
  65. ret = gfs2_meta_inode_buffer(ip, &bh);
  66. if (ret == 0) {
  67. di = (struct gfs2_dinode *)bh->b_data;
  68. atime.tv_sec = be64_to_cpu(di->di_atime);
  69. atime.tv_nsec = be32_to_cpu(di->di_atime_nsec);
  70. if (timespec_compare(&inode->i_atime, &atime) > 0) {
  71. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  72. gfs2_dinode_out(ip, bh->b_data);
  73. }
  74. brelse(bh);
  75. }
  76. gfs2_trans_end(sdp);
  77. do_unlock:
  78. gfs2_glock_dq_uninit(&gh);
  79. do_flush:
  80. if (sync != 0)
  81. gfs2_log_flush(GFS2_SB(inode), ip->i_gl);
  82. return ret;
  83. }
  84. /**
  85. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  86. * @sdp: the filesystem
  87. *
  88. * Returns: errno
  89. */
  90. static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  91. {
  92. struct gfs2_holder t_gh;
  93. int error;
  94. gfs2_quota_sync(sdp);
  95. gfs2_statfs_sync(sdp);
  96. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
  97. &t_gh);
  98. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  99. return error;
  100. gfs2_meta_syncfs(sdp);
  101. gfs2_log_shutdown(sdp);
  102. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  103. if (t_gh.gh_gl)
  104. gfs2_glock_dq_uninit(&t_gh);
  105. gfs2_quota_cleanup(sdp);
  106. return error;
  107. }
  108. static int gfs2_umount_recovery_wait(void *word)
  109. {
  110. schedule();
  111. return 0;
  112. }
  113. /**
  114. * gfs2_put_super - Unmount the filesystem
  115. * @sb: The VFS superblock
  116. *
  117. */
  118. static void gfs2_put_super(struct super_block *sb)
  119. {
  120. struct gfs2_sbd *sdp = sb->s_fs_info;
  121. int error;
  122. struct gfs2_jdesc *jd;
  123. /* Unfreeze the filesystem, if we need to */
  124. mutex_lock(&sdp->sd_freeze_lock);
  125. if (sdp->sd_freeze_count)
  126. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  127. mutex_unlock(&sdp->sd_freeze_lock);
  128. /* No more recovery requests */
  129. set_bit(SDF_NORECOVERY, &sdp->sd_flags);
  130. smp_mb();
  131. /* Wait on outstanding recovery */
  132. restart:
  133. spin_lock(&sdp->sd_jindex_spin);
  134. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  135. if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
  136. continue;
  137. spin_unlock(&sdp->sd_jindex_spin);
  138. wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
  139. gfs2_umount_recovery_wait, TASK_UNINTERRUPTIBLE);
  140. goto restart;
  141. }
  142. spin_unlock(&sdp->sd_jindex_spin);
  143. kthread_stop(sdp->sd_quotad_process);
  144. kthread_stop(sdp->sd_logd_process);
  145. if (!(sb->s_flags & MS_RDONLY)) {
  146. error = gfs2_make_fs_ro(sdp);
  147. if (error)
  148. gfs2_io_error(sdp);
  149. }
  150. /* At this point, we're through modifying the disk */
  151. /* Release stuff */
  152. iput(sdp->sd_jindex);
  153. iput(sdp->sd_inum_inode);
  154. iput(sdp->sd_statfs_inode);
  155. iput(sdp->sd_rindex);
  156. iput(sdp->sd_quota_inode);
  157. gfs2_glock_put(sdp->sd_rename_gl);
  158. gfs2_glock_put(sdp->sd_trans_gl);
  159. if (!sdp->sd_args.ar_spectator) {
  160. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  161. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  162. gfs2_glock_dq_uninit(&sdp->sd_ir_gh);
  163. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  164. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  165. iput(sdp->sd_ir_inode);
  166. iput(sdp->sd_sc_inode);
  167. iput(sdp->sd_qc_inode);
  168. }
  169. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  170. gfs2_clear_rgrpd(sdp);
  171. gfs2_jindex_free(sdp);
  172. /* Take apart glock structures and buffer lists */
  173. gfs2_gl_hash_clear(sdp);
  174. /* Unmount the locking protocol */
  175. gfs2_lm_unmount(sdp);
  176. /* At this point, we're through participating in the lockspace */
  177. gfs2_sys_fs_del(sdp);
  178. }
  179. /**
  180. * gfs2_write_super
  181. * @sb: the superblock
  182. *
  183. */
  184. static void gfs2_write_super(struct super_block *sb)
  185. {
  186. sb->s_dirt = 0;
  187. }
  188. /**
  189. * gfs2_sync_fs - sync the filesystem
  190. * @sb: the superblock
  191. *
  192. * Flushes the log to disk.
  193. */
  194. static int gfs2_sync_fs(struct super_block *sb, int wait)
  195. {
  196. sb->s_dirt = 0;
  197. if (wait && sb->s_fs_info)
  198. gfs2_log_flush(sb->s_fs_info, NULL);
  199. return 0;
  200. }
  201. /**
  202. * gfs2_freeze - prevent further writes to the filesystem
  203. * @sb: the VFS structure for the filesystem
  204. *
  205. */
  206. static int gfs2_freeze(struct super_block *sb)
  207. {
  208. struct gfs2_sbd *sdp = sb->s_fs_info;
  209. int error;
  210. if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  211. return -EINVAL;
  212. for (;;) {
  213. error = gfs2_freeze_fs(sdp);
  214. if (!error)
  215. break;
  216. switch (error) {
  217. case -EBUSY:
  218. fs_err(sdp, "waiting for recovery before freeze\n");
  219. break;
  220. default:
  221. fs_err(sdp, "error freezing FS: %d\n", error);
  222. break;
  223. }
  224. fs_err(sdp, "retrying...\n");
  225. msleep(1000);
  226. }
  227. return 0;
  228. }
  229. /**
  230. * gfs2_unfreeze - reallow writes to the filesystem
  231. * @sb: the VFS structure for the filesystem
  232. *
  233. */
  234. static int gfs2_unfreeze(struct super_block *sb)
  235. {
  236. gfs2_unfreeze_fs(sb->s_fs_info);
  237. return 0;
  238. }
  239. /**
  240. * statfs_fill - fill in the sg for a given RG
  241. * @rgd: the RG
  242. * @sc: the sc structure
  243. *
  244. * Returns: 0 on success, -ESTALE if the LVB is invalid
  245. */
  246. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  247. struct gfs2_statfs_change_host *sc)
  248. {
  249. gfs2_rgrp_verify(rgd);
  250. sc->sc_total += rgd->rd_data;
  251. sc->sc_free += rgd->rd_free;
  252. sc->sc_dinodes += rgd->rd_dinodes;
  253. return 0;
  254. }
  255. /**
  256. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  257. * @sdp: the filesystem
  258. * @sc: the sc info that will be returned
  259. *
  260. * Any error (other than a signal) will cause this routine to fall back
  261. * to the synchronous version.
  262. *
  263. * FIXME: This really shouldn't busy wait like this.
  264. *
  265. * Returns: errno
  266. */
  267. static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  268. {
  269. struct gfs2_holder ri_gh;
  270. struct gfs2_rgrpd *rgd_next;
  271. struct gfs2_holder *gha, *gh;
  272. unsigned int slots = 64;
  273. unsigned int x;
  274. int done;
  275. int error = 0, err;
  276. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  277. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  278. if (!gha)
  279. return -ENOMEM;
  280. error = gfs2_rindex_hold(sdp, &ri_gh);
  281. if (error)
  282. goto out;
  283. rgd_next = gfs2_rgrpd_get_first(sdp);
  284. for (;;) {
  285. done = 1;
  286. for (x = 0; x < slots; x++) {
  287. gh = gha + x;
  288. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  289. err = gfs2_glock_wait(gh);
  290. if (err) {
  291. gfs2_holder_uninit(gh);
  292. error = err;
  293. } else {
  294. if (!error)
  295. error = statfs_slow_fill(
  296. gh->gh_gl->gl_object, sc);
  297. gfs2_glock_dq_uninit(gh);
  298. }
  299. }
  300. if (gh->gh_gl)
  301. done = 0;
  302. else if (rgd_next && !error) {
  303. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  304. LM_ST_SHARED,
  305. GL_ASYNC,
  306. gh);
  307. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  308. done = 0;
  309. }
  310. if (signal_pending(current))
  311. error = -ERESTARTSYS;
  312. }
  313. if (done)
  314. break;
  315. yield();
  316. }
  317. gfs2_glock_dq_uninit(&ri_gh);
  318. out:
  319. kfree(gha);
  320. return error;
  321. }
  322. /**
  323. * gfs2_statfs_i - Do a statfs
  324. * @sdp: the filesystem
  325. * @sg: the sg structure
  326. *
  327. * Returns: errno
  328. */
  329. static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  330. {
  331. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  332. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  333. spin_lock(&sdp->sd_statfs_spin);
  334. *sc = *m_sc;
  335. sc->sc_total += l_sc->sc_total;
  336. sc->sc_free += l_sc->sc_free;
  337. sc->sc_dinodes += l_sc->sc_dinodes;
  338. spin_unlock(&sdp->sd_statfs_spin);
  339. if (sc->sc_free < 0)
  340. sc->sc_free = 0;
  341. if (sc->sc_free > sc->sc_total)
  342. sc->sc_free = sc->sc_total;
  343. if (sc->sc_dinodes < 0)
  344. sc->sc_dinodes = 0;
  345. return 0;
  346. }
  347. /**
  348. * gfs2_statfs - Gather and return stats about the filesystem
  349. * @sb: The superblock
  350. * @statfsbuf: The buffer
  351. *
  352. * Returns: 0 on success or error code
  353. */
  354. static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  355. {
  356. struct super_block *sb = dentry->d_inode->i_sb;
  357. struct gfs2_sbd *sdp = sb->s_fs_info;
  358. struct gfs2_statfs_change_host sc;
  359. int error;
  360. if (gfs2_tune_get(sdp, gt_statfs_slow))
  361. error = gfs2_statfs_slow(sdp, &sc);
  362. else
  363. error = gfs2_statfs_i(sdp, &sc);
  364. if (error)
  365. return error;
  366. buf->f_type = GFS2_MAGIC;
  367. buf->f_bsize = sdp->sd_sb.sb_bsize;
  368. buf->f_blocks = sc.sc_total;
  369. buf->f_bfree = sc.sc_free;
  370. buf->f_bavail = sc.sc_free;
  371. buf->f_files = sc.sc_dinodes + sc.sc_free;
  372. buf->f_ffree = sc.sc_free;
  373. buf->f_namelen = GFS2_FNAMESIZE;
  374. return 0;
  375. }
  376. /**
  377. * gfs2_remount_fs - called when the FS is remounted
  378. * @sb: the filesystem
  379. * @flags: the remount flags
  380. * @data: extra data passed in (not used right now)
  381. *
  382. * Returns: errno
  383. */
  384. static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
  385. {
  386. struct gfs2_sbd *sdp = sb->s_fs_info;
  387. struct gfs2_args args = sdp->sd_args; /* Default to current settings */
  388. struct gfs2_tune *gt = &sdp->sd_tune;
  389. int error;
  390. spin_lock(&gt->gt_spin);
  391. args.ar_commit = gt->gt_log_flush_secs;
  392. spin_unlock(&gt->gt_spin);
  393. error = gfs2_mount_args(sdp, &args, data);
  394. if (error)
  395. return error;
  396. /* Not allowed to change locking details */
  397. if (strcmp(args.ar_lockproto, sdp->sd_args.ar_lockproto) ||
  398. strcmp(args.ar_locktable, sdp->sd_args.ar_locktable) ||
  399. strcmp(args.ar_hostdata, sdp->sd_args.ar_hostdata))
  400. return -EINVAL;
  401. /* Some flags must not be changed */
  402. if (args_neq(&args, &sdp->sd_args, spectator) ||
  403. args_neq(&args, &sdp->sd_args, ignore_local_fs) ||
  404. args_neq(&args, &sdp->sd_args, localflocks) ||
  405. args_neq(&args, &sdp->sd_args, localcaching) ||
  406. args_neq(&args, &sdp->sd_args, meta))
  407. return -EINVAL;
  408. if (sdp->sd_args.ar_spectator)
  409. *flags |= MS_RDONLY;
  410. if ((sb->s_flags ^ *flags) & MS_RDONLY) {
  411. if (*flags & MS_RDONLY)
  412. error = gfs2_make_fs_ro(sdp);
  413. else
  414. error = gfs2_make_fs_rw(sdp);
  415. if (error)
  416. return error;
  417. }
  418. sdp->sd_args = args;
  419. if (sdp->sd_args.ar_posix_acl)
  420. sb->s_flags |= MS_POSIXACL;
  421. else
  422. sb->s_flags &= ~MS_POSIXACL;
  423. spin_lock(&gt->gt_spin);
  424. gt->gt_log_flush_secs = args.ar_commit;
  425. spin_unlock(&gt->gt_spin);
  426. return 0;
  427. }
  428. /**
  429. * gfs2_drop_inode - Drop an inode (test for remote unlink)
  430. * @inode: The inode to drop
  431. *
  432. * If we've received a callback on an iopen lock then its because a
  433. * remote node tried to deallocate the inode but failed due to this node
  434. * still having the inode open. Here we mark the link count zero
  435. * since we know that it must have reached zero if the GLF_DEMOTE flag
  436. * is set on the iopen glock. If we didn't do a disk read since the
  437. * remote node removed the final link then we might otherwise miss
  438. * this event. This check ensures that this node will deallocate the
  439. * inode's blocks, or alternatively pass the baton on to another
  440. * node for later deallocation.
  441. */
  442. static void gfs2_drop_inode(struct inode *inode)
  443. {
  444. struct gfs2_inode *ip = GFS2_I(inode);
  445. if (test_bit(GIF_USER, &ip->i_flags) && inode->i_nlink) {
  446. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  447. if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
  448. clear_nlink(inode);
  449. }
  450. generic_drop_inode(inode);
  451. }
  452. /**
  453. * gfs2_clear_inode - Deallocate an inode when VFS is done with it
  454. * @inode: The VFS inode
  455. *
  456. */
  457. static void gfs2_clear_inode(struct inode *inode)
  458. {
  459. struct gfs2_inode *ip = GFS2_I(inode);
  460. /* This tells us its a "real" inode and not one which only
  461. * serves to contain an address space (see rgrp.c, meta_io.c)
  462. * which therefore doesn't have its own glocks.
  463. */
  464. if (test_bit(GIF_USER, &ip->i_flags)) {
  465. ip->i_gl->gl_object = NULL;
  466. gfs2_glock_put(ip->i_gl);
  467. ip->i_gl = NULL;
  468. if (ip->i_iopen_gh.gh_gl) {
  469. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  470. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  471. }
  472. }
  473. }
  474. static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
  475. {
  476. do {
  477. if (d1 == d2)
  478. return 1;
  479. d1 = d1->d_parent;
  480. } while (!IS_ROOT(d1));
  481. return 0;
  482. }
  483. /**
  484. * gfs2_show_options - Show mount options for /proc/mounts
  485. * @s: seq_file structure
  486. * @mnt: vfsmount
  487. *
  488. * Returns: 0 on success or error code
  489. */
  490. static int gfs2_show_options(struct seq_file *s, struct vfsmount *mnt)
  491. {
  492. struct gfs2_sbd *sdp = mnt->mnt_sb->s_fs_info;
  493. struct gfs2_args *args = &sdp->sd_args;
  494. int lfsecs;
  495. if (is_ancestor(mnt->mnt_root, sdp->sd_master_dir))
  496. seq_printf(s, ",meta");
  497. if (args->ar_lockproto[0])
  498. seq_printf(s, ",lockproto=%s", args->ar_lockproto);
  499. if (args->ar_locktable[0])
  500. seq_printf(s, ",locktable=%s", args->ar_locktable);
  501. if (args->ar_hostdata[0])
  502. seq_printf(s, ",hostdata=%s", args->ar_hostdata);
  503. if (args->ar_spectator)
  504. seq_printf(s, ",spectator");
  505. if (args->ar_ignore_local_fs)
  506. seq_printf(s, ",ignore_local_fs");
  507. if (args->ar_localflocks)
  508. seq_printf(s, ",localflocks");
  509. if (args->ar_localcaching)
  510. seq_printf(s, ",localcaching");
  511. if (args->ar_debug)
  512. seq_printf(s, ",debug");
  513. if (args->ar_upgrade)
  514. seq_printf(s, ",upgrade");
  515. if (args->ar_posix_acl)
  516. seq_printf(s, ",acl");
  517. if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
  518. char *state;
  519. switch (args->ar_quota) {
  520. case GFS2_QUOTA_OFF:
  521. state = "off";
  522. break;
  523. case GFS2_QUOTA_ACCOUNT:
  524. state = "account";
  525. break;
  526. case GFS2_QUOTA_ON:
  527. state = "on";
  528. break;
  529. default:
  530. state = "unknown";
  531. break;
  532. }
  533. seq_printf(s, ",quota=%s", state);
  534. }
  535. if (args->ar_suiddir)
  536. seq_printf(s, ",suiddir");
  537. if (args->ar_data != GFS2_DATA_DEFAULT) {
  538. char *state;
  539. switch (args->ar_data) {
  540. case GFS2_DATA_WRITEBACK:
  541. state = "writeback";
  542. break;
  543. case GFS2_DATA_ORDERED:
  544. state = "ordered";
  545. break;
  546. default:
  547. state = "unknown";
  548. break;
  549. }
  550. seq_printf(s, ",data=%s", state);
  551. }
  552. if (args->ar_discard)
  553. seq_printf(s, ",discard");
  554. lfsecs = sdp->sd_tune.gt_log_flush_secs;
  555. if (lfsecs != 60)
  556. seq_printf(s, ",commit=%d", lfsecs);
  557. return 0;
  558. }
  559. /*
  560. * We have to (at the moment) hold the inodes main lock to cover
  561. * the gap between unlocking the shared lock on the iopen lock and
  562. * taking the exclusive lock. I'd rather do a shared -> exclusive
  563. * conversion on the iopen lock, but we can change that later. This
  564. * is safe, just less efficient.
  565. */
  566. static void gfs2_delete_inode(struct inode *inode)
  567. {
  568. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  569. struct gfs2_inode *ip = GFS2_I(inode);
  570. struct gfs2_holder gh;
  571. int error;
  572. if (!test_bit(GIF_USER, &ip->i_flags))
  573. goto out;
  574. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  575. if (unlikely(error)) {
  576. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  577. goto out;
  578. }
  579. gfs2_glock_dq_wait(&ip->i_iopen_gh);
  580. gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
  581. error = gfs2_glock_nq(&ip->i_iopen_gh);
  582. if (error)
  583. goto out_truncate;
  584. if (S_ISDIR(inode->i_mode) &&
  585. (ip->i_diskflags & GFS2_DIF_EXHASH)) {
  586. error = gfs2_dir_exhash_dealloc(ip);
  587. if (error)
  588. goto out_unlock;
  589. }
  590. if (ip->i_eattr) {
  591. error = gfs2_ea_dealloc(ip);
  592. if (error)
  593. goto out_unlock;
  594. }
  595. if (!gfs2_is_stuffed(ip)) {
  596. error = gfs2_file_dealloc(ip);
  597. if (error)
  598. goto out_unlock;
  599. }
  600. error = gfs2_dinode_dealloc(ip);
  601. if (error)
  602. goto out_unlock;
  603. out_truncate:
  604. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  605. if (error)
  606. goto out_unlock;
  607. /* Needs to be done before glock release & also in a transaction */
  608. truncate_inode_pages(&inode->i_data, 0);
  609. gfs2_trans_end(sdp);
  610. out_unlock:
  611. if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
  612. gfs2_glock_dq(&ip->i_iopen_gh);
  613. gfs2_holder_uninit(&ip->i_iopen_gh);
  614. gfs2_glock_dq_uninit(&gh);
  615. if (error && error != GLR_TRYFAILED && error != -EROFS)
  616. fs_warn(sdp, "gfs2_delete_inode: %d\n", error);
  617. out:
  618. truncate_inode_pages(&inode->i_data, 0);
  619. clear_inode(inode);
  620. }
  621. static struct inode *gfs2_alloc_inode(struct super_block *sb)
  622. {
  623. struct gfs2_inode *ip;
  624. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  625. if (ip) {
  626. ip->i_flags = 0;
  627. ip->i_gl = NULL;
  628. }
  629. return &ip->i_inode;
  630. }
  631. static void gfs2_destroy_inode(struct inode *inode)
  632. {
  633. kmem_cache_free(gfs2_inode_cachep, inode);
  634. }
  635. const struct super_operations gfs2_super_ops = {
  636. .alloc_inode = gfs2_alloc_inode,
  637. .destroy_inode = gfs2_destroy_inode,
  638. .write_inode = gfs2_write_inode,
  639. .delete_inode = gfs2_delete_inode,
  640. .put_super = gfs2_put_super,
  641. .write_super = gfs2_write_super,
  642. .sync_fs = gfs2_sync_fs,
  643. .freeze_fs = gfs2_freeze,
  644. .unfreeze_fs = gfs2_unfreeze,
  645. .statfs = gfs2_statfs,
  646. .remount_fs = gfs2_remount_fs,
  647. .clear_inode = gfs2_clear_inode,
  648. .drop_inode = gfs2_drop_inode,
  649. .show_options = gfs2_show_options,
  650. };