super.c 37 KB

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