super.c 39 KB

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