super.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2007 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/bio.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/completion.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/statfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/mount.h>
  19. #include <linux/kthread.h>
  20. #include <linux/delay.h>
  21. #include <linux/gfs2_ondisk.h>
  22. #include <linux/crc32.h>
  23. #include <linux/time.h>
  24. #include <linux/wait.h>
  25. #include <linux/writeback.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/kernel.h>
  28. #include "gfs2.h"
  29. #include "incore.h"
  30. #include "bmap.h"
  31. #include "dir.h"
  32. #include "glock.h"
  33. #include "glops.h"
  34. #include "inode.h"
  35. #include "log.h"
  36. #include "meta_io.h"
  37. #include "quota.h"
  38. #include "recovery.h"
  39. #include "rgrp.h"
  40. #include "super.h"
  41. #include "trans.h"
  42. #include "util.h"
  43. #include "sys.h"
  44. #include "xattr.h"
  45. #define args_neq(a1, a2, x) ((a1)->ar_##x != (a2)->ar_##x)
  46. enum {
  47. Opt_lockproto,
  48. Opt_locktable,
  49. Opt_hostdata,
  50. Opt_spectator,
  51. Opt_ignore_local_fs,
  52. Opt_localflocks,
  53. Opt_localcaching,
  54. Opt_debug,
  55. Opt_nodebug,
  56. Opt_upgrade,
  57. Opt_acl,
  58. Opt_noacl,
  59. Opt_quota_off,
  60. Opt_quota_account,
  61. Opt_quota_on,
  62. Opt_quota,
  63. Opt_noquota,
  64. Opt_suiddir,
  65. Opt_nosuiddir,
  66. Opt_data_writeback,
  67. Opt_data_ordered,
  68. Opt_meta,
  69. Opt_discard,
  70. Opt_nodiscard,
  71. Opt_commit,
  72. Opt_err_withdraw,
  73. Opt_err_panic,
  74. Opt_statfs_quantum,
  75. Opt_statfs_percent,
  76. Opt_quota_quantum,
  77. Opt_barrier,
  78. Opt_nobarrier,
  79. Opt_rgrplvb,
  80. Opt_norgrplvb,
  81. Opt_error,
  82. };
  83. static const match_table_t tokens = {
  84. {Opt_lockproto, "lockproto=%s"},
  85. {Opt_locktable, "locktable=%s"},
  86. {Opt_hostdata, "hostdata=%s"},
  87. {Opt_spectator, "spectator"},
  88. {Opt_spectator, "norecovery"},
  89. {Opt_ignore_local_fs, "ignore_local_fs"},
  90. {Opt_localflocks, "localflocks"},
  91. {Opt_localcaching, "localcaching"},
  92. {Opt_debug, "debug"},
  93. {Opt_nodebug, "nodebug"},
  94. {Opt_upgrade, "upgrade"},
  95. {Opt_acl, "acl"},
  96. {Opt_noacl, "noacl"},
  97. {Opt_quota_off, "quota=off"},
  98. {Opt_quota_account, "quota=account"},
  99. {Opt_quota_on, "quota=on"},
  100. {Opt_quota, "quota"},
  101. {Opt_noquota, "noquota"},
  102. {Opt_suiddir, "suiddir"},
  103. {Opt_nosuiddir, "nosuiddir"},
  104. {Opt_data_writeback, "data=writeback"},
  105. {Opt_data_ordered, "data=ordered"},
  106. {Opt_meta, "meta"},
  107. {Opt_discard, "discard"},
  108. {Opt_nodiscard, "nodiscard"},
  109. {Opt_commit, "commit=%d"},
  110. {Opt_err_withdraw, "errors=withdraw"},
  111. {Opt_err_panic, "errors=panic"},
  112. {Opt_statfs_quantum, "statfs_quantum=%d"},
  113. {Opt_statfs_percent, "statfs_percent=%d"},
  114. {Opt_quota_quantum, "quota_quantum=%d"},
  115. {Opt_barrier, "barrier"},
  116. {Opt_nobarrier, "nobarrier"},
  117. {Opt_rgrplvb, "rgrplvb"},
  118. {Opt_norgrplvb, "norgrplvb"},
  119. {Opt_error, NULL}
  120. };
  121. /**
  122. * gfs2_mount_args - Parse mount options
  123. * @args: The structure into which the parsed options will be written
  124. * @options: The options to parse
  125. *
  126. * Return: errno
  127. */
  128. int gfs2_mount_args(struct gfs2_args *args, char *options)
  129. {
  130. char *o;
  131. int token;
  132. substring_t tmp[MAX_OPT_ARGS];
  133. int rv;
  134. /* Split the options into tokens with the "," character and
  135. process them */
  136. while (1) {
  137. o = strsep(&options, ",");
  138. if (o == NULL)
  139. break;
  140. if (*o == '\0')
  141. continue;
  142. token = match_token(o, tokens, tmp);
  143. switch (token) {
  144. case Opt_lockproto:
  145. match_strlcpy(args->ar_lockproto, &tmp[0],
  146. GFS2_LOCKNAME_LEN);
  147. break;
  148. case Opt_locktable:
  149. match_strlcpy(args->ar_locktable, &tmp[0],
  150. GFS2_LOCKNAME_LEN);
  151. break;
  152. case Opt_hostdata:
  153. match_strlcpy(args->ar_hostdata, &tmp[0],
  154. GFS2_LOCKNAME_LEN);
  155. break;
  156. case Opt_spectator:
  157. args->ar_spectator = 1;
  158. break;
  159. case Opt_ignore_local_fs:
  160. /* Retained for backwards compat only */
  161. break;
  162. case Opt_localflocks:
  163. args->ar_localflocks = 1;
  164. break;
  165. case Opt_localcaching:
  166. /* Retained for backwards compat only */
  167. break;
  168. case Opt_debug:
  169. if (args->ar_errors == GFS2_ERRORS_PANIC) {
  170. pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
  171. return -EINVAL;
  172. }
  173. args->ar_debug = 1;
  174. break;
  175. case Opt_nodebug:
  176. args->ar_debug = 0;
  177. break;
  178. case Opt_upgrade:
  179. /* Retained for backwards compat only */
  180. break;
  181. case Opt_acl:
  182. args->ar_posix_acl = 1;
  183. break;
  184. case Opt_noacl:
  185. args->ar_posix_acl = 0;
  186. break;
  187. case Opt_quota_off:
  188. case Opt_noquota:
  189. args->ar_quota = GFS2_QUOTA_OFF;
  190. break;
  191. case Opt_quota_account:
  192. args->ar_quota = GFS2_QUOTA_ACCOUNT;
  193. break;
  194. case Opt_quota_on:
  195. case Opt_quota:
  196. args->ar_quota = GFS2_QUOTA_ON;
  197. break;
  198. case Opt_suiddir:
  199. args->ar_suiddir = 1;
  200. break;
  201. case Opt_nosuiddir:
  202. args->ar_suiddir = 0;
  203. break;
  204. case Opt_data_writeback:
  205. args->ar_data = GFS2_DATA_WRITEBACK;
  206. break;
  207. case Opt_data_ordered:
  208. args->ar_data = GFS2_DATA_ORDERED;
  209. break;
  210. case Opt_meta:
  211. args->ar_meta = 1;
  212. break;
  213. case Opt_discard:
  214. args->ar_discard = 1;
  215. break;
  216. case Opt_nodiscard:
  217. args->ar_discard = 0;
  218. break;
  219. case Opt_commit:
  220. rv = match_int(&tmp[0], &args->ar_commit);
  221. if (rv || args->ar_commit <= 0) {
  222. pr_warn("commit mount option requires a positive numeric argument\n");
  223. return rv ? rv : -EINVAL;
  224. }
  225. break;
  226. case Opt_statfs_quantum:
  227. rv = match_int(&tmp[0], &args->ar_statfs_quantum);
  228. if (rv || args->ar_statfs_quantum < 0) {
  229. pr_warn("statfs_quantum mount option requires a non-negative numeric argument\n");
  230. return rv ? rv : -EINVAL;
  231. }
  232. break;
  233. case Opt_quota_quantum:
  234. rv = match_int(&tmp[0], &args->ar_quota_quantum);
  235. if (rv || args->ar_quota_quantum <= 0) {
  236. pr_warn("quota_quantum mount option requires a positive numeric argument\n");
  237. return rv ? rv : -EINVAL;
  238. }
  239. break;
  240. case Opt_statfs_percent:
  241. rv = match_int(&tmp[0], &args->ar_statfs_percent);
  242. if (rv || args->ar_statfs_percent < 0 ||
  243. args->ar_statfs_percent > 100) {
  244. pr_warn("statfs_percent mount option requires a numeric argument between 0 and 100\n");
  245. return rv ? rv : -EINVAL;
  246. }
  247. break;
  248. case Opt_err_withdraw:
  249. args->ar_errors = GFS2_ERRORS_WITHDRAW;
  250. break;
  251. case Opt_err_panic:
  252. if (args->ar_debug) {
  253. pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
  254. return -EINVAL;
  255. }
  256. args->ar_errors = GFS2_ERRORS_PANIC;
  257. break;
  258. case Opt_barrier:
  259. args->ar_nobarrier = 0;
  260. break;
  261. case Opt_nobarrier:
  262. args->ar_nobarrier = 1;
  263. break;
  264. case Opt_rgrplvb:
  265. args->ar_rgrplvb = 1;
  266. break;
  267. case Opt_norgrplvb:
  268. args->ar_rgrplvb = 0;
  269. break;
  270. case Opt_error:
  271. default:
  272. pr_warn("invalid mount option: %s\n", o);
  273. return -EINVAL;
  274. }
  275. }
  276. return 0;
  277. }
  278. /**
  279. * gfs2_jindex_free - Clear all the journal index information
  280. * @sdp: The GFS2 superblock
  281. *
  282. */
  283. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  284. {
  285. struct list_head list;
  286. struct gfs2_jdesc *jd;
  287. spin_lock(&sdp->sd_jindex_spin);
  288. list_add(&list, &sdp->sd_jindex_list);
  289. list_del_init(&sdp->sd_jindex_list);
  290. sdp->sd_journals = 0;
  291. spin_unlock(&sdp->sd_jindex_spin);
  292. while (!list_empty(&list)) {
  293. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  294. gfs2_free_journal_extents(jd);
  295. list_del(&jd->jd_list);
  296. iput(jd->jd_inode);
  297. kfree(jd);
  298. }
  299. }
  300. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  301. {
  302. struct gfs2_jdesc *jd;
  303. int found = 0;
  304. list_for_each_entry(jd, head, jd_list) {
  305. if (jd->jd_jid == jid) {
  306. found = 1;
  307. break;
  308. }
  309. }
  310. if (!found)
  311. jd = NULL;
  312. return jd;
  313. }
  314. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  315. {
  316. struct gfs2_jdesc *jd;
  317. spin_lock(&sdp->sd_jindex_spin);
  318. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  319. spin_unlock(&sdp->sd_jindex_spin);
  320. return jd;
  321. }
  322. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  323. {
  324. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  325. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  326. u64 size = i_size_read(jd->jd_inode);
  327. if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, 1 << 30))
  328. return -EIO;
  329. jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
  330. if (gfs2_write_alloc_required(ip, 0, size)) {
  331. gfs2_consist_inode(ip);
  332. return -EIO;
  333. }
  334. return 0;
  335. }
  336. static int init_threads(struct gfs2_sbd *sdp)
  337. {
  338. struct task_struct *p;
  339. int error = 0;
  340. p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
  341. if (IS_ERR(p)) {
  342. error = PTR_ERR(p);
  343. fs_err(sdp, "can't start logd thread: %d\n", error);
  344. return error;
  345. }
  346. sdp->sd_logd_process = p;
  347. p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
  348. if (IS_ERR(p)) {
  349. error = PTR_ERR(p);
  350. fs_err(sdp, "can't start quotad thread: %d\n", error);
  351. goto fail;
  352. }
  353. sdp->sd_quotad_process = p;
  354. return 0;
  355. fail:
  356. kthread_stop(sdp->sd_logd_process);
  357. return error;
  358. }
  359. /**
  360. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  361. * @sdp: the filesystem
  362. *
  363. * Returns: errno
  364. */
  365. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  366. {
  367. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  368. struct gfs2_glock *j_gl = ip->i_gl;
  369. struct gfs2_holder freeze_gh;
  370. struct gfs2_log_header_host head;
  371. int error;
  372. error = init_threads(sdp);
  373. if (error)
  374. return error;
  375. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
  376. &freeze_gh);
  377. if (error)
  378. goto fail_threads;
  379. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  380. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  381. if (error)
  382. goto fail;
  383. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  384. gfs2_consist(sdp);
  385. error = -EIO;
  386. goto fail;
  387. }
  388. /* Initialize some head of the log stuff */
  389. sdp->sd_log_sequence = head.lh_sequence + 1;
  390. gfs2_log_pointers_init(sdp, head.lh_blkno);
  391. error = gfs2_quota_init(sdp);
  392. if (error)
  393. goto fail;
  394. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  395. gfs2_glock_dq_uninit(&freeze_gh);
  396. return 0;
  397. fail:
  398. freeze_gh.gh_flags |= GL_NOCACHE;
  399. gfs2_glock_dq_uninit(&freeze_gh);
  400. fail_threads:
  401. kthread_stop(sdp->sd_quotad_process);
  402. kthread_stop(sdp->sd_logd_process);
  403. return error;
  404. }
  405. void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  406. {
  407. const struct gfs2_statfs_change *str = buf;
  408. sc->sc_total = be64_to_cpu(str->sc_total);
  409. sc->sc_free = be64_to_cpu(str->sc_free);
  410. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  411. }
  412. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  413. {
  414. struct gfs2_statfs_change *str = buf;
  415. str->sc_total = cpu_to_be64(sc->sc_total);
  416. str->sc_free = cpu_to_be64(sc->sc_free);
  417. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  418. }
  419. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  420. {
  421. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  422. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  423. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  424. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  425. struct buffer_head *m_bh, *l_bh;
  426. struct gfs2_holder gh;
  427. int error;
  428. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  429. &gh);
  430. if (error)
  431. return error;
  432. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  433. if (error)
  434. goto out;
  435. if (sdp->sd_args.ar_spectator) {
  436. spin_lock(&sdp->sd_statfs_spin);
  437. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  438. sizeof(struct gfs2_dinode));
  439. spin_unlock(&sdp->sd_statfs_spin);
  440. } else {
  441. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  442. if (error)
  443. goto out_m_bh;
  444. spin_lock(&sdp->sd_statfs_spin);
  445. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  446. sizeof(struct gfs2_dinode));
  447. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  448. sizeof(struct gfs2_dinode));
  449. spin_unlock(&sdp->sd_statfs_spin);
  450. brelse(l_bh);
  451. }
  452. out_m_bh:
  453. brelse(m_bh);
  454. out:
  455. gfs2_glock_dq_uninit(&gh);
  456. return 0;
  457. }
  458. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  459. s64 dinodes)
  460. {
  461. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  462. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  463. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  464. struct buffer_head *l_bh;
  465. s64 x, y;
  466. int need_sync = 0;
  467. int error;
  468. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  469. if (error)
  470. return;
  471. gfs2_trans_add_meta(l_ip->i_gl, l_bh);
  472. spin_lock(&sdp->sd_statfs_spin);
  473. l_sc->sc_total += total;
  474. l_sc->sc_free += free;
  475. l_sc->sc_dinodes += dinodes;
  476. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  477. if (sdp->sd_args.ar_statfs_percent) {
  478. x = 100 * l_sc->sc_free;
  479. y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
  480. if (x >= y || x <= -y)
  481. need_sync = 1;
  482. }
  483. spin_unlock(&sdp->sd_statfs_spin);
  484. brelse(l_bh);
  485. if (need_sync)
  486. gfs2_wake_up_statfs(sdp);
  487. }
  488. void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh,
  489. struct buffer_head *l_bh)
  490. {
  491. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  492. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  493. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  494. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  495. gfs2_trans_add_meta(l_ip->i_gl, l_bh);
  496. spin_lock(&sdp->sd_statfs_spin);
  497. m_sc->sc_total += l_sc->sc_total;
  498. m_sc->sc_free += l_sc->sc_free;
  499. m_sc->sc_dinodes += l_sc->sc_dinodes;
  500. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  501. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  502. 0, sizeof(struct gfs2_statfs_change));
  503. spin_unlock(&sdp->sd_statfs_spin);
  504. gfs2_trans_add_meta(m_ip->i_gl, m_bh);
  505. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  506. }
  507. int gfs2_statfs_sync(struct super_block *sb, int type)
  508. {
  509. struct gfs2_sbd *sdp = sb->s_fs_info;
  510. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  511. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  512. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  513. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  514. struct gfs2_holder gh;
  515. struct buffer_head *m_bh, *l_bh;
  516. int error;
  517. sb_start_write(sb);
  518. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  519. &gh);
  520. if (error)
  521. goto out;
  522. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  523. if (error)
  524. goto out_unlock;
  525. spin_lock(&sdp->sd_statfs_spin);
  526. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  527. sizeof(struct gfs2_dinode));
  528. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  529. spin_unlock(&sdp->sd_statfs_spin);
  530. goto out_bh;
  531. }
  532. spin_unlock(&sdp->sd_statfs_spin);
  533. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  534. if (error)
  535. goto out_bh;
  536. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  537. if (error)
  538. goto out_bh2;
  539. update_statfs(sdp, m_bh, l_bh);
  540. sdp->sd_statfs_force_sync = 0;
  541. gfs2_trans_end(sdp);
  542. out_bh2:
  543. brelse(l_bh);
  544. out_bh:
  545. brelse(m_bh);
  546. out_unlock:
  547. gfs2_glock_dq_uninit(&gh);
  548. out:
  549. sb_end_write(sb);
  550. return error;
  551. }
  552. struct lfcc {
  553. struct list_head list;
  554. struct gfs2_holder gh;
  555. };
  556. /**
  557. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  558. * journals are clean
  559. * @sdp: the file system
  560. * @state: the state to put the transaction lock into
  561. * @t_gh: the hold on the transaction lock
  562. *
  563. * Returns: errno
  564. */
  565. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  566. struct gfs2_holder *freeze_gh)
  567. {
  568. struct gfs2_inode *ip;
  569. struct gfs2_jdesc *jd;
  570. struct lfcc *lfcc;
  571. LIST_HEAD(list);
  572. struct gfs2_log_header_host lh;
  573. int error;
  574. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  575. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  576. if (!lfcc) {
  577. error = -ENOMEM;
  578. goto out;
  579. }
  580. ip = GFS2_I(jd->jd_inode);
  581. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  582. if (error) {
  583. kfree(lfcc);
  584. goto out;
  585. }
  586. list_add(&lfcc->list, &list);
  587. }
  588. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
  589. GL_NOCACHE, freeze_gh);
  590. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  591. error = gfs2_jdesc_check(jd);
  592. if (error)
  593. break;
  594. error = gfs2_find_jhead(jd, &lh);
  595. if (error)
  596. break;
  597. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  598. error = -EBUSY;
  599. break;
  600. }
  601. }
  602. if (error)
  603. gfs2_glock_dq_uninit(freeze_gh);
  604. out:
  605. while (!list_empty(&list)) {
  606. lfcc = list_entry(list.next, struct lfcc, list);
  607. list_del(&lfcc->list);
  608. gfs2_glock_dq_uninit(&lfcc->gh);
  609. kfree(lfcc);
  610. }
  611. return error;
  612. }
  613. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  614. {
  615. struct gfs2_dinode *str = buf;
  616. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  617. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  618. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  619. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  620. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  621. str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
  622. str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
  623. str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
  624. str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
  625. str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
  626. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  627. str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
  628. str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
  629. str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
  630. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  631. str->di_goal_data = cpu_to_be64(ip->i_goal);
  632. str->di_generation = cpu_to_be64(ip->i_generation);
  633. str->di_flags = cpu_to_be32(ip->i_diskflags);
  634. str->di_height = cpu_to_be16(ip->i_height);
  635. str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
  636. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  637. GFS2_FORMAT_DE : 0);
  638. str->di_depth = cpu_to_be16(ip->i_depth);
  639. str->di_entries = cpu_to_be32(ip->i_entries);
  640. str->di_eattr = cpu_to_be64(ip->i_eattr);
  641. str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
  642. str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
  643. str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
  644. }
  645. /**
  646. * gfs2_write_inode - Make sure the inode is stable on the disk
  647. * @inode: The inode
  648. * @wbc: The writeback control structure
  649. *
  650. * Returns: errno
  651. */
  652. static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
  653. {
  654. struct gfs2_inode *ip = GFS2_I(inode);
  655. struct gfs2_sbd *sdp = GFS2_SB(inode);
  656. struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
  657. struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
  658. int ret = 0;
  659. if (wbc->sync_mode == WB_SYNC_ALL)
  660. gfs2_log_flush(GFS2_SB(inode), ip->i_gl, NORMAL_FLUSH);
  661. if (bdi->wb.dirty_exceeded)
  662. gfs2_ail1_flush(sdp, wbc);
  663. else
  664. filemap_fdatawrite(metamapping);
  665. if (wbc->sync_mode == WB_SYNC_ALL)
  666. ret = filemap_fdatawait(metamapping);
  667. if (ret)
  668. mark_inode_dirty_sync(inode);
  669. return ret;
  670. }
  671. /**
  672. * gfs2_dirty_inode - check for atime updates
  673. * @inode: The inode in question
  674. * @flags: The type of dirty
  675. *
  676. * Unfortunately it can be called under any combination of inode
  677. * glock and transaction lock, so we have to check carefully.
  678. *
  679. * At the moment this deals only with atime - it should be possible
  680. * to expand that role in future, once a review of the locking has
  681. * been carried out.
  682. */
  683. static void gfs2_dirty_inode(struct inode *inode, int flags)
  684. {
  685. struct gfs2_inode *ip = GFS2_I(inode);
  686. struct gfs2_sbd *sdp = GFS2_SB(inode);
  687. struct buffer_head *bh;
  688. struct gfs2_holder gh;
  689. int need_unlock = 0;
  690. int need_endtrans = 0;
  691. int ret;
  692. if (!(flags & (I_DIRTY_DATASYNC|I_DIRTY_SYNC)))
  693. return;
  694. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  695. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  696. if (ret) {
  697. fs_err(sdp, "dirty_inode: glock %d\n", ret);
  698. return;
  699. }
  700. need_unlock = 1;
  701. } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
  702. return;
  703. if (current->journal_info == NULL) {
  704. ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
  705. if (ret) {
  706. fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
  707. goto out;
  708. }
  709. need_endtrans = 1;
  710. }
  711. ret = gfs2_meta_inode_buffer(ip, &bh);
  712. if (ret == 0) {
  713. gfs2_trans_add_meta(ip->i_gl, bh);
  714. gfs2_dinode_out(ip, bh->b_data);
  715. brelse(bh);
  716. }
  717. if (need_endtrans)
  718. gfs2_trans_end(sdp);
  719. out:
  720. if (need_unlock)
  721. gfs2_glock_dq_uninit(&gh);
  722. }
  723. /**
  724. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  725. * @sdp: the filesystem
  726. *
  727. * Returns: errno
  728. */
  729. static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  730. {
  731. struct gfs2_holder freeze_gh;
  732. int error;
  733. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_NOCACHE,
  734. &freeze_gh);
  735. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  736. return error;
  737. kthread_stop(sdp->sd_quotad_process);
  738. kthread_stop(sdp->sd_logd_process);
  739. flush_workqueue(gfs2_delete_workqueue);
  740. gfs2_quota_sync(sdp->sd_vfs, 0);
  741. gfs2_statfs_sync(sdp->sd_vfs, 0);
  742. down_write(&sdp->sd_log_flush_lock);
  743. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  744. up_write(&sdp->sd_log_flush_lock);
  745. gfs2_log_flush(sdp, NULL, SHUTDOWN_FLUSH);
  746. wait_event(sdp->sd_reserving_log_wait, atomic_read(&sdp->sd_reserving_log) == 0);
  747. gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
  748. if (freeze_gh.gh_gl)
  749. gfs2_glock_dq_uninit(&freeze_gh);
  750. gfs2_quota_cleanup(sdp);
  751. return error;
  752. }
  753. /**
  754. * gfs2_put_super - Unmount the filesystem
  755. * @sb: The VFS superblock
  756. *
  757. */
  758. static void gfs2_put_super(struct super_block *sb)
  759. {
  760. struct gfs2_sbd *sdp = sb->s_fs_info;
  761. int error;
  762. struct gfs2_jdesc *jd;
  763. /* No more recovery requests */
  764. set_bit(SDF_NORECOVERY, &sdp->sd_flags);
  765. smp_mb();
  766. /* Wait on outstanding recovery */
  767. restart:
  768. spin_lock(&sdp->sd_jindex_spin);
  769. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  770. if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
  771. continue;
  772. spin_unlock(&sdp->sd_jindex_spin);
  773. wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
  774. TASK_UNINTERRUPTIBLE);
  775. goto restart;
  776. }
  777. spin_unlock(&sdp->sd_jindex_spin);
  778. if (!(sb->s_flags & MS_RDONLY)) {
  779. error = gfs2_make_fs_ro(sdp);
  780. if (error)
  781. gfs2_io_error(sdp);
  782. }
  783. /* At this point, we're through modifying the disk */
  784. /* Release stuff */
  785. iput(sdp->sd_jindex);
  786. iput(sdp->sd_statfs_inode);
  787. iput(sdp->sd_rindex);
  788. iput(sdp->sd_quota_inode);
  789. gfs2_glock_put(sdp->sd_rename_gl);
  790. gfs2_glock_put(sdp->sd_freeze_gl);
  791. if (!sdp->sd_args.ar_spectator) {
  792. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  793. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  794. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  795. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  796. iput(sdp->sd_sc_inode);
  797. iput(sdp->sd_qc_inode);
  798. }
  799. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  800. gfs2_clear_rgrpd(sdp);
  801. gfs2_jindex_free(sdp);
  802. /* Take apart glock structures and buffer lists */
  803. gfs2_gl_hash_clear(sdp);
  804. /* Unmount the locking protocol */
  805. gfs2_lm_unmount(sdp);
  806. /* At this point, we're through participating in the lockspace */
  807. gfs2_sys_fs_del(sdp);
  808. }
  809. /**
  810. * gfs2_sync_fs - sync the filesystem
  811. * @sb: the superblock
  812. *
  813. * Flushes the log to disk.
  814. */
  815. static int gfs2_sync_fs(struct super_block *sb, int wait)
  816. {
  817. struct gfs2_sbd *sdp = sb->s_fs_info;
  818. gfs2_quota_sync(sb, -1);
  819. if (wait && sdp)
  820. gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
  821. return 0;
  822. }
  823. void gfs2_freeze_func(struct work_struct *work)
  824. {
  825. int error;
  826. struct gfs2_holder freeze_gh;
  827. struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
  828. struct super_block *sb = sdp->sd_vfs;
  829. atomic_inc(&sb->s_active);
  830. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
  831. &freeze_gh);
  832. if (error) {
  833. printk(KERN_INFO "GFS2: couln't get freeze lock : %d\n", error);
  834. gfs2_assert_withdraw(sdp, 0);
  835. }
  836. else {
  837. atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
  838. error = thaw_super(sb);
  839. if (error) {
  840. printk(KERN_INFO "GFS2: couldn't thaw filesystem: %d\n",
  841. error);
  842. gfs2_assert_withdraw(sdp, 0);
  843. }
  844. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  845. freeze_gh.gh_flags |= GL_NOCACHE;
  846. gfs2_glock_dq_uninit(&freeze_gh);
  847. }
  848. deactivate_super(sb);
  849. return;
  850. }
  851. /**
  852. * gfs2_freeze - prevent further writes to the filesystem
  853. * @sb: the VFS structure for the filesystem
  854. *
  855. */
  856. static int gfs2_freeze(struct super_block *sb)
  857. {
  858. struct gfs2_sbd *sdp = sb->s_fs_info;
  859. int error = 0;
  860. mutex_lock(&sdp->sd_freeze_mutex);
  861. if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN)
  862. goto out;
  863. if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
  864. error = -EINVAL;
  865. goto out;
  866. }
  867. for (;;) {
  868. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  869. if (!error)
  870. break;
  871. switch (error) {
  872. case -EBUSY:
  873. fs_err(sdp, "waiting for recovery before freeze\n");
  874. break;
  875. default:
  876. fs_err(sdp, "error freezing FS: %d\n", error);
  877. break;
  878. }
  879. fs_err(sdp, "retrying...\n");
  880. msleep(1000);
  881. }
  882. error = 0;
  883. out:
  884. mutex_unlock(&sdp->sd_freeze_mutex);
  885. return error;
  886. }
  887. /**
  888. * gfs2_unfreeze - reallow writes to the filesystem
  889. * @sb: the VFS structure for the filesystem
  890. *
  891. */
  892. static int gfs2_unfreeze(struct super_block *sb)
  893. {
  894. struct gfs2_sbd *sdp = sb->s_fs_info;
  895. mutex_lock(&sdp->sd_freeze_mutex);
  896. if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
  897. sdp->sd_freeze_gh.gh_gl == NULL) {
  898. mutex_unlock(&sdp->sd_freeze_mutex);
  899. return 0;
  900. }
  901. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  902. mutex_unlock(&sdp->sd_freeze_mutex);
  903. return 0;
  904. }
  905. /**
  906. * statfs_fill - fill in the sg for a given RG
  907. * @rgd: the RG
  908. * @sc: the sc structure
  909. *
  910. * Returns: 0 on success, -ESTALE if the LVB is invalid
  911. */
  912. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  913. struct gfs2_statfs_change_host *sc)
  914. {
  915. gfs2_rgrp_verify(rgd);
  916. sc->sc_total += rgd->rd_data;
  917. sc->sc_free += rgd->rd_free;
  918. sc->sc_dinodes += rgd->rd_dinodes;
  919. return 0;
  920. }
  921. /**
  922. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  923. * @sdp: the filesystem
  924. * @sc: the sc info that will be returned
  925. *
  926. * Any error (other than a signal) will cause this routine to fall back
  927. * to the synchronous version.
  928. *
  929. * FIXME: This really shouldn't busy wait like this.
  930. *
  931. * Returns: errno
  932. */
  933. static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  934. {
  935. struct gfs2_rgrpd *rgd_next;
  936. struct gfs2_holder *gha, *gh;
  937. unsigned int slots = 64;
  938. unsigned int x;
  939. int done;
  940. int error = 0, err;
  941. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  942. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  943. if (!gha)
  944. return -ENOMEM;
  945. rgd_next = gfs2_rgrpd_get_first(sdp);
  946. for (;;) {
  947. done = 1;
  948. for (x = 0; x < slots; x++) {
  949. gh = gha + x;
  950. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  951. err = gfs2_glock_wait(gh);
  952. if (err) {
  953. gfs2_holder_uninit(gh);
  954. error = err;
  955. } else {
  956. if (!error)
  957. error = statfs_slow_fill(
  958. gh->gh_gl->gl_object, sc);
  959. gfs2_glock_dq_uninit(gh);
  960. }
  961. }
  962. if (gh->gh_gl)
  963. done = 0;
  964. else if (rgd_next && !error) {
  965. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  966. LM_ST_SHARED,
  967. GL_ASYNC,
  968. gh);
  969. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  970. done = 0;
  971. }
  972. if (signal_pending(current))
  973. error = -ERESTARTSYS;
  974. }
  975. if (done)
  976. break;
  977. yield();
  978. }
  979. kfree(gha);
  980. return error;
  981. }
  982. /**
  983. * gfs2_statfs_i - Do a statfs
  984. * @sdp: the filesystem
  985. * @sg: the sg structure
  986. *
  987. * Returns: errno
  988. */
  989. static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  990. {
  991. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  992. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  993. spin_lock(&sdp->sd_statfs_spin);
  994. *sc = *m_sc;
  995. sc->sc_total += l_sc->sc_total;
  996. sc->sc_free += l_sc->sc_free;
  997. sc->sc_dinodes += l_sc->sc_dinodes;
  998. spin_unlock(&sdp->sd_statfs_spin);
  999. if (sc->sc_free < 0)
  1000. sc->sc_free = 0;
  1001. if (sc->sc_free > sc->sc_total)
  1002. sc->sc_free = sc->sc_total;
  1003. if (sc->sc_dinodes < 0)
  1004. sc->sc_dinodes = 0;
  1005. return 0;
  1006. }
  1007. /**
  1008. * gfs2_statfs - Gather and return stats about the filesystem
  1009. * @sb: The superblock
  1010. * @statfsbuf: The buffer
  1011. *
  1012. * Returns: 0 on success or error code
  1013. */
  1014. static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  1015. {
  1016. struct super_block *sb = d_inode(dentry)->i_sb;
  1017. struct gfs2_sbd *sdp = sb->s_fs_info;
  1018. struct gfs2_statfs_change_host sc;
  1019. int error;
  1020. error = gfs2_rindex_update(sdp);
  1021. if (error)
  1022. return error;
  1023. if (gfs2_tune_get(sdp, gt_statfs_slow))
  1024. error = gfs2_statfs_slow(sdp, &sc);
  1025. else
  1026. error = gfs2_statfs_i(sdp, &sc);
  1027. if (error)
  1028. return error;
  1029. buf->f_type = GFS2_MAGIC;
  1030. buf->f_bsize = sdp->sd_sb.sb_bsize;
  1031. buf->f_blocks = sc.sc_total;
  1032. buf->f_bfree = sc.sc_free;
  1033. buf->f_bavail = sc.sc_free;
  1034. buf->f_files = sc.sc_dinodes + sc.sc_free;
  1035. buf->f_ffree = sc.sc_free;
  1036. buf->f_namelen = GFS2_FNAMESIZE;
  1037. return 0;
  1038. }
  1039. /**
  1040. * gfs2_remount_fs - called when the FS is remounted
  1041. * @sb: the filesystem
  1042. * @flags: the remount flags
  1043. * @data: extra data passed in (not used right now)
  1044. *
  1045. * Returns: errno
  1046. */
  1047. static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
  1048. {
  1049. struct gfs2_sbd *sdp = sb->s_fs_info;
  1050. struct gfs2_args args = sdp->sd_args; /* Default to current settings */
  1051. struct gfs2_tune *gt = &sdp->sd_tune;
  1052. int error;
  1053. sync_filesystem(sb);
  1054. spin_lock(&gt->gt_spin);
  1055. args.ar_commit = gt->gt_logd_secs;
  1056. args.ar_quota_quantum = gt->gt_quota_quantum;
  1057. if (gt->gt_statfs_slow)
  1058. args.ar_statfs_quantum = 0;
  1059. else
  1060. args.ar_statfs_quantum = gt->gt_statfs_quantum;
  1061. spin_unlock(&gt->gt_spin);
  1062. error = gfs2_mount_args(&args, data);
  1063. if (error)
  1064. return error;
  1065. /* Not allowed to change locking details */
  1066. if (strcmp(args.ar_lockproto, sdp->sd_args.ar_lockproto) ||
  1067. strcmp(args.ar_locktable, sdp->sd_args.ar_locktable) ||
  1068. strcmp(args.ar_hostdata, sdp->sd_args.ar_hostdata))
  1069. return -EINVAL;
  1070. /* Some flags must not be changed */
  1071. if (args_neq(&args, &sdp->sd_args, spectator) ||
  1072. args_neq(&args, &sdp->sd_args, localflocks) ||
  1073. args_neq(&args, &sdp->sd_args, meta))
  1074. return -EINVAL;
  1075. if (sdp->sd_args.ar_spectator)
  1076. *flags |= MS_RDONLY;
  1077. if ((sb->s_flags ^ *flags) & MS_RDONLY) {
  1078. if (*flags & MS_RDONLY)
  1079. error = gfs2_make_fs_ro(sdp);
  1080. else
  1081. error = gfs2_make_fs_rw(sdp);
  1082. if (error)
  1083. return error;
  1084. }
  1085. sdp->sd_args = args;
  1086. if (sdp->sd_args.ar_posix_acl)
  1087. sb->s_flags |= MS_POSIXACL;
  1088. else
  1089. sb->s_flags &= ~MS_POSIXACL;
  1090. if (sdp->sd_args.ar_nobarrier)
  1091. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  1092. else
  1093. clear_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  1094. spin_lock(&gt->gt_spin);
  1095. gt->gt_logd_secs = args.ar_commit;
  1096. gt->gt_quota_quantum = args.ar_quota_quantum;
  1097. if (args.ar_statfs_quantum) {
  1098. gt->gt_statfs_slow = 0;
  1099. gt->gt_statfs_quantum = args.ar_statfs_quantum;
  1100. }
  1101. else {
  1102. gt->gt_statfs_slow = 1;
  1103. gt->gt_statfs_quantum = 30;
  1104. }
  1105. spin_unlock(&gt->gt_spin);
  1106. gfs2_online_uevent(sdp);
  1107. return 0;
  1108. }
  1109. /**
  1110. * gfs2_drop_inode - Drop an inode (test for remote unlink)
  1111. * @inode: The inode to drop
  1112. *
  1113. * If we've received a callback on an iopen lock then its because a
  1114. * remote node tried to deallocate the inode but failed due to this node
  1115. * still having the inode open. Here we mark the link count zero
  1116. * since we know that it must have reached zero if the GLF_DEMOTE flag
  1117. * is set on the iopen glock. If we didn't do a disk read since the
  1118. * remote node removed the final link then we might otherwise miss
  1119. * this event. This check ensures that this node will deallocate the
  1120. * inode's blocks, or alternatively pass the baton on to another
  1121. * node for later deallocation.
  1122. */
  1123. static int gfs2_drop_inode(struct inode *inode)
  1124. {
  1125. struct gfs2_inode *ip = GFS2_I(inode);
  1126. if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && inode->i_nlink) {
  1127. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  1128. if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
  1129. clear_nlink(inode);
  1130. }
  1131. return generic_drop_inode(inode);
  1132. }
  1133. static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
  1134. {
  1135. do {
  1136. if (d1 == d2)
  1137. return 1;
  1138. d1 = d1->d_parent;
  1139. } while (!IS_ROOT(d1));
  1140. return 0;
  1141. }
  1142. /**
  1143. * gfs2_show_options - Show mount options for /proc/mounts
  1144. * @s: seq_file structure
  1145. * @root: root of this (sub)tree
  1146. *
  1147. * Returns: 0 on success or error code
  1148. */
  1149. static int gfs2_show_options(struct seq_file *s, struct dentry *root)
  1150. {
  1151. struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
  1152. struct gfs2_args *args = &sdp->sd_args;
  1153. int val;
  1154. if (is_ancestor(root, sdp->sd_master_dir))
  1155. seq_puts(s, ",meta");
  1156. if (args->ar_lockproto[0])
  1157. seq_show_option(s, "lockproto", args->ar_lockproto);
  1158. if (args->ar_locktable[0])
  1159. seq_show_option(s, "locktable", args->ar_locktable);
  1160. if (args->ar_hostdata[0])
  1161. seq_show_option(s, "hostdata", args->ar_hostdata);
  1162. if (args->ar_spectator)
  1163. seq_puts(s, ",spectator");
  1164. if (args->ar_localflocks)
  1165. seq_puts(s, ",localflocks");
  1166. if (args->ar_debug)
  1167. seq_puts(s, ",debug");
  1168. if (args->ar_posix_acl)
  1169. seq_puts(s, ",acl");
  1170. if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
  1171. char *state;
  1172. switch (args->ar_quota) {
  1173. case GFS2_QUOTA_OFF:
  1174. state = "off";
  1175. break;
  1176. case GFS2_QUOTA_ACCOUNT:
  1177. state = "account";
  1178. break;
  1179. case GFS2_QUOTA_ON:
  1180. state = "on";
  1181. break;
  1182. default:
  1183. state = "unknown";
  1184. break;
  1185. }
  1186. seq_printf(s, ",quota=%s", state);
  1187. }
  1188. if (args->ar_suiddir)
  1189. seq_puts(s, ",suiddir");
  1190. if (args->ar_data != GFS2_DATA_DEFAULT) {
  1191. char *state;
  1192. switch (args->ar_data) {
  1193. case GFS2_DATA_WRITEBACK:
  1194. state = "writeback";
  1195. break;
  1196. case GFS2_DATA_ORDERED:
  1197. state = "ordered";
  1198. break;
  1199. default:
  1200. state = "unknown";
  1201. break;
  1202. }
  1203. seq_printf(s, ",data=%s", state);
  1204. }
  1205. if (args->ar_discard)
  1206. seq_puts(s, ",discard");
  1207. val = sdp->sd_tune.gt_logd_secs;
  1208. if (val != 30)
  1209. seq_printf(s, ",commit=%d", val);
  1210. val = sdp->sd_tune.gt_statfs_quantum;
  1211. if (val != 30)
  1212. seq_printf(s, ",statfs_quantum=%d", val);
  1213. else if (sdp->sd_tune.gt_statfs_slow)
  1214. seq_puts(s, ",statfs_quantum=0");
  1215. val = sdp->sd_tune.gt_quota_quantum;
  1216. if (val != 60)
  1217. seq_printf(s, ",quota_quantum=%d", val);
  1218. if (args->ar_statfs_percent)
  1219. seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
  1220. if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
  1221. const char *state;
  1222. switch (args->ar_errors) {
  1223. case GFS2_ERRORS_WITHDRAW:
  1224. state = "withdraw";
  1225. break;
  1226. case GFS2_ERRORS_PANIC:
  1227. state = "panic";
  1228. break;
  1229. default:
  1230. state = "unknown";
  1231. break;
  1232. }
  1233. seq_printf(s, ",errors=%s", state);
  1234. }
  1235. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  1236. seq_puts(s, ",nobarrier");
  1237. if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
  1238. seq_puts(s, ",demote_interface_used");
  1239. if (args->ar_rgrplvb)
  1240. seq_puts(s, ",rgrplvb");
  1241. return 0;
  1242. }
  1243. static void gfs2_final_release_pages(struct gfs2_inode *ip)
  1244. {
  1245. struct inode *inode = &ip->i_inode;
  1246. struct gfs2_glock *gl = ip->i_gl;
  1247. truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
  1248. truncate_inode_pages(&inode->i_data, 0);
  1249. if (atomic_read(&gl->gl_revokes) == 0) {
  1250. clear_bit(GLF_LFLUSH, &gl->gl_flags);
  1251. clear_bit(GLF_DIRTY, &gl->gl_flags);
  1252. }
  1253. }
  1254. static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
  1255. {
  1256. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1257. struct gfs2_rgrpd *rgd;
  1258. struct gfs2_holder gh;
  1259. int error;
  1260. if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
  1261. gfs2_consist_inode(ip);
  1262. return -EIO;
  1263. }
  1264. error = gfs2_rindex_update(sdp);
  1265. if (error)
  1266. return error;
  1267. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1268. if (error)
  1269. return error;
  1270. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
  1271. if (!rgd) {
  1272. gfs2_consist_inode(ip);
  1273. error = -EIO;
  1274. goto out_qs;
  1275. }
  1276. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1277. if (error)
  1278. goto out_qs;
  1279. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
  1280. sdp->sd_jdesc->jd_blocks);
  1281. if (error)
  1282. goto out_rg_gunlock;
  1283. gfs2_free_di(rgd, ip);
  1284. gfs2_final_release_pages(ip);
  1285. gfs2_trans_end(sdp);
  1286. out_rg_gunlock:
  1287. gfs2_glock_dq_uninit(&gh);
  1288. out_qs:
  1289. gfs2_quota_unhold(ip);
  1290. return error;
  1291. }
  1292. /**
  1293. * gfs2_evict_inode - Remove an inode from cache
  1294. * @inode: The inode to evict
  1295. *
  1296. * There are three cases to consider:
  1297. * 1. i_nlink == 0, we are final opener (and must deallocate)
  1298. * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
  1299. * 3. i_nlink > 0
  1300. *
  1301. * If the fs is read only, then we have to treat all cases as per #3
  1302. * since we are unable to do any deallocation. The inode will be
  1303. * deallocated by the next read/write node to attempt an allocation
  1304. * in the same resource group
  1305. *
  1306. * We have to (at the moment) hold the inodes main lock to cover
  1307. * the gap between unlocking the shared lock on the iopen lock and
  1308. * taking the exclusive lock. I'd rather do a shared -> exclusive
  1309. * conversion on the iopen lock, but we can change that later. This
  1310. * is safe, just less efficient.
  1311. */
  1312. static void gfs2_evict_inode(struct inode *inode)
  1313. {
  1314. struct super_block *sb = inode->i_sb;
  1315. struct gfs2_sbd *sdp = sb->s_fs_info;
  1316. struct gfs2_inode *ip = GFS2_I(inode);
  1317. struct gfs2_holder gh;
  1318. int error;
  1319. if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
  1320. clear_inode(inode);
  1321. return;
  1322. }
  1323. if (inode->i_nlink || (sb->s_flags & MS_RDONLY))
  1324. goto out;
  1325. /* Must not read inode block until block type has been verified */
  1326. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
  1327. if (unlikely(error)) {
  1328. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  1329. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  1330. goto out;
  1331. }
  1332. if (!test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
  1333. error = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
  1334. if (error)
  1335. goto out_truncate;
  1336. }
  1337. if (test_bit(GIF_INVALID, &ip->i_flags)) {
  1338. error = gfs2_inode_refresh(ip);
  1339. if (error)
  1340. goto out_truncate;
  1341. }
  1342. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  1343. gfs2_glock_dq_wait(&ip->i_iopen_gh);
  1344. gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
  1345. error = gfs2_glock_nq(&ip->i_iopen_gh);
  1346. if (error)
  1347. goto out_truncate;
  1348. /* Case 1 starts here */
  1349. if (S_ISDIR(inode->i_mode) &&
  1350. (ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1351. error = gfs2_dir_exhash_dealloc(ip);
  1352. if (error)
  1353. goto out_unlock;
  1354. }
  1355. if (ip->i_eattr) {
  1356. error = gfs2_ea_dealloc(ip);
  1357. if (error)
  1358. goto out_unlock;
  1359. }
  1360. if (!gfs2_is_stuffed(ip)) {
  1361. error = gfs2_file_dealloc(ip);
  1362. if (error)
  1363. goto out_unlock;
  1364. }
  1365. error = gfs2_dinode_dealloc(ip);
  1366. goto out_unlock;
  1367. out_truncate:
  1368. gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
  1369. if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
  1370. struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
  1371. filemap_fdatawrite(metamapping);
  1372. filemap_fdatawait(metamapping);
  1373. }
  1374. write_inode_now(inode, 1);
  1375. gfs2_ail_flush(ip->i_gl, 0);
  1376. /* Case 2 starts here */
  1377. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  1378. if (error)
  1379. goto out_unlock;
  1380. /* Needs to be done before glock release & also in a transaction */
  1381. truncate_inode_pages(&inode->i_data, 0);
  1382. gfs2_trans_end(sdp);
  1383. out_unlock:
  1384. /* Error path for case 1 */
  1385. if (gfs2_rs_active(ip->i_res))
  1386. gfs2_rs_deltree(ip->i_res);
  1387. if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
  1388. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  1389. gfs2_glock_dq(&ip->i_iopen_gh);
  1390. }
  1391. gfs2_holder_uninit(&ip->i_iopen_gh);
  1392. gfs2_glock_dq_uninit(&gh);
  1393. if (error && error != GLR_TRYFAILED && error != -EROFS)
  1394. fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
  1395. out:
  1396. /* Case 3 starts here */
  1397. truncate_inode_pages_final(&inode->i_data);
  1398. gfs2_rs_delete(ip, NULL);
  1399. gfs2_ordered_del_inode(ip);
  1400. clear_inode(inode);
  1401. gfs2_dir_hash_inval(ip);
  1402. ip->i_gl->gl_object = NULL;
  1403. flush_delayed_work(&ip->i_gl->gl_work);
  1404. gfs2_glock_add_to_lru(ip->i_gl);
  1405. gfs2_glock_put(ip->i_gl);
  1406. ip->i_gl = NULL;
  1407. if (ip->i_iopen_gh.gh_gl) {
  1408. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  1409. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  1410. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  1411. }
  1412. }
  1413. static struct inode *gfs2_alloc_inode(struct super_block *sb)
  1414. {
  1415. struct gfs2_inode *ip;
  1416. ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
  1417. if (ip) {
  1418. ip->i_flags = 0;
  1419. ip->i_gl = NULL;
  1420. ip->i_rgd = NULL;
  1421. ip->i_res = NULL;
  1422. }
  1423. return &ip->i_inode;
  1424. }
  1425. static void gfs2_i_callback(struct rcu_head *head)
  1426. {
  1427. struct inode *inode = container_of(head, struct inode, i_rcu);
  1428. kmem_cache_free(gfs2_inode_cachep, inode);
  1429. }
  1430. static void gfs2_destroy_inode(struct inode *inode)
  1431. {
  1432. call_rcu(&inode->i_rcu, gfs2_i_callback);
  1433. }
  1434. const struct super_operations gfs2_super_ops = {
  1435. .alloc_inode = gfs2_alloc_inode,
  1436. .destroy_inode = gfs2_destroy_inode,
  1437. .write_inode = gfs2_write_inode,
  1438. .dirty_inode = gfs2_dirty_inode,
  1439. .evict_inode = gfs2_evict_inode,
  1440. .put_super = gfs2_put_super,
  1441. .sync_fs = gfs2_sync_fs,
  1442. .freeze_super = gfs2_freeze,
  1443. .thaw_super = gfs2_unfreeze,
  1444. .statfs = gfs2_statfs,
  1445. .remount_fs = gfs2_remount_fs,
  1446. .drop_inode = gfs2_drop_inode,
  1447. .show_options = gfs2_show_options,
  1448. };