super.c 42 KB

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