super.c 38 KB

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