super.c 40 KB

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