super.c 38 KB

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