super.c 38 KB

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