super.c 37 KB

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