super.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/bio.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/statfs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/mount.h>
  16. #include <linux/kthread.h>
  17. #include <linux/delay.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/time.h>
  21. #include <linux/wait.h>
  22. #include <linux/writeback.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/kernel.h>
  25. #include "gfs2.h"
  26. #include "incore.h"
  27. #include "bmap.h"
  28. #include "dir.h"
  29. #include "glock.h"
  30. #include "glops.h"
  31. #include "inode.h"
  32. #include "log.h"
  33. #include "meta_io.h"
  34. #include "quota.h"
  35. #include "recovery.h"
  36. #include "rgrp.h"
  37. #include "super.h"
  38. #include "trans.h"
  39. #include "util.h"
  40. #include "sys.h"
  41. #include "xattr.h"
  42. #include "lops.h"
  43. enum evict_behavior {
  44. EVICT_SHOULD_DELETE,
  45. EVICT_SHOULD_SKIP_DELETE,
  46. EVICT_SHOULD_DEFER_DELETE,
  47. };
  48. /**
  49. * gfs2_jindex_free - Clear all the journal index information
  50. * @sdp: The GFS2 superblock
  51. *
  52. */
  53. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  54. {
  55. struct list_head list;
  56. struct gfs2_jdesc *jd;
  57. spin_lock(&sdp->sd_jindex_spin);
  58. list_add(&list, &sdp->sd_jindex_list);
  59. list_del_init(&sdp->sd_jindex_list);
  60. sdp->sd_journals = 0;
  61. spin_unlock(&sdp->sd_jindex_spin);
  62. down_write(&sdp->sd_log_flush_lock);
  63. sdp->sd_jdesc = NULL;
  64. up_write(&sdp->sd_log_flush_lock);
  65. while (!list_empty(&list)) {
  66. jd = list_first_entry(&list, struct gfs2_jdesc, jd_list);
  67. BUG_ON(jd->jd_log_bio);
  68. gfs2_free_journal_extents(jd);
  69. list_del(&jd->jd_list);
  70. iput(jd->jd_inode);
  71. jd->jd_inode = NULL;
  72. kfree(jd);
  73. }
  74. }
  75. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  76. {
  77. struct gfs2_jdesc *jd;
  78. list_for_each_entry(jd, head, jd_list) {
  79. if (jd->jd_jid == jid)
  80. return jd;
  81. }
  82. return NULL;
  83. }
  84. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  85. {
  86. struct gfs2_jdesc *jd;
  87. spin_lock(&sdp->sd_jindex_spin);
  88. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  89. spin_unlock(&sdp->sd_jindex_spin);
  90. return jd;
  91. }
  92. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  93. {
  94. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  95. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  96. u64 size = i_size_read(jd->jd_inode);
  97. if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
  98. return -EIO;
  99. jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
  100. if (gfs2_write_alloc_required(ip, 0, size)) {
  101. gfs2_consist_inode(ip);
  102. return -EIO;
  103. }
  104. return 0;
  105. }
  106. /**
  107. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  108. * @sdp: the filesystem
  109. *
  110. * Returns: errno
  111. */
  112. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  113. {
  114. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  115. struct gfs2_glock *j_gl = ip->i_gl;
  116. int error;
  117. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  118. if (gfs2_withdrawn(sdp))
  119. return -EIO;
  120. if (sdp->sd_log_sequence == 0) {
  121. fs_err(sdp, "unknown status of our own journal jid %d",
  122. sdp->sd_lockstruct.ls_jid);
  123. return -EIO;
  124. }
  125. error = gfs2_quota_init(sdp);
  126. if (!error && gfs2_withdrawn(sdp)) {
  127. gfs2_quota_cleanup(sdp);
  128. error = -EIO;
  129. }
  130. if (!error)
  131. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  132. return error;
  133. }
  134. void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  135. {
  136. const struct gfs2_statfs_change *str = buf;
  137. sc->sc_total = be64_to_cpu(str->sc_total);
  138. sc->sc_free = be64_to_cpu(str->sc_free);
  139. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  140. }
  141. void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  142. {
  143. struct gfs2_statfs_change *str = buf;
  144. str->sc_total = cpu_to_be64(sc->sc_total);
  145. str->sc_free = cpu_to_be64(sc->sc_free);
  146. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  147. }
  148. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  149. {
  150. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  151. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  152. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  153. struct buffer_head *m_bh;
  154. struct gfs2_holder gh;
  155. int error;
  156. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  157. &gh);
  158. if (error)
  159. return error;
  160. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  161. if (error)
  162. goto out;
  163. if (sdp->sd_args.ar_spectator) {
  164. spin_lock(&sdp->sd_statfs_spin);
  165. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  166. sizeof(struct gfs2_dinode));
  167. spin_unlock(&sdp->sd_statfs_spin);
  168. } else {
  169. spin_lock(&sdp->sd_statfs_spin);
  170. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  171. sizeof(struct gfs2_dinode));
  172. gfs2_statfs_change_in(l_sc, sdp->sd_sc_bh->b_data +
  173. sizeof(struct gfs2_dinode));
  174. spin_unlock(&sdp->sd_statfs_spin);
  175. }
  176. brelse(m_bh);
  177. out:
  178. gfs2_glock_dq_uninit(&gh);
  179. return 0;
  180. }
  181. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  182. s64 dinodes)
  183. {
  184. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  185. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  186. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  187. s64 x, y;
  188. int need_sync = 0;
  189. gfs2_trans_add_meta(l_ip->i_gl, sdp->sd_sc_bh);
  190. spin_lock(&sdp->sd_statfs_spin);
  191. l_sc->sc_total += total;
  192. l_sc->sc_free += free;
  193. l_sc->sc_dinodes += dinodes;
  194. gfs2_statfs_change_out(l_sc, sdp->sd_sc_bh->b_data +
  195. sizeof(struct gfs2_dinode));
  196. if (sdp->sd_args.ar_statfs_percent) {
  197. x = 100 * l_sc->sc_free;
  198. y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
  199. if (x >= y || x <= -y)
  200. need_sync = 1;
  201. }
  202. spin_unlock(&sdp->sd_statfs_spin);
  203. if (need_sync)
  204. gfs2_wake_up_statfs(sdp);
  205. }
  206. void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh)
  207. {
  208. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  209. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  210. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  211. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  212. gfs2_trans_add_meta(l_ip->i_gl, sdp->sd_sc_bh);
  213. gfs2_trans_add_meta(m_ip->i_gl, m_bh);
  214. spin_lock(&sdp->sd_statfs_spin);
  215. m_sc->sc_total += l_sc->sc_total;
  216. m_sc->sc_free += l_sc->sc_free;
  217. m_sc->sc_dinodes += l_sc->sc_dinodes;
  218. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  219. memset(sdp->sd_sc_bh->b_data + sizeof(struct gfs2_dinode),
  220. 0, sizeof(struct gfs2_statfs_change));
  221. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  222. spin_unlock(&sdp->sd_statfs_spin);
  223. }
  224. int gfs2_statfs_sync(struct super_block *sb, int type)
  225. {
  226. struct gfs2_sbd *sdp = sb->s_fs_info;
  227. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  228. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  229. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  230. struct gfs2_holder gh;
  231. struct buffer_head *m_bh;
  232. int error;
  233. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  234. &gh);
  235. if (error)
  236. goto out;
  237. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  238. if (error)
  239. goto out_unlock;
  240. spin_lock(&sdp->sd_statfs_spin);
  241. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  242. sizeof(struct gfs2_dinode));
  243. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  244. spin_unlock(&sdp->sd_statfs_spin);
  245. goto out_bh;
  246. }
  247. spin_unlock(&sdp->sd_statfs_spin);
  248. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  249. if (error)
  250. goto out_bh;
  251. update_statfs(sdp, m_bh);
  252. sdp->sd_statfs_force_sync = 0;
  253. gfs2_trans_end(sdp);
  254. out_bh:
  255. brelse(m_bh);
  256. out_unlock:
  257. gfs2_glock_dq_uninit(&gh);
  258. out:
  259. return error;
  260. }
  261. struct lfcc {
  262. struct list_head list;
  263. struct gfs2_holder gh;
  264. };
  265. /**
  266. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  267. * journals are clean
  268. * @sdp: the file system
  269. *
  270. * Returns: errno
  271. */
  272. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
  273. {
  274. struct gfs2_inode *ip;
  275. struct gfs2_jdesc *jd;
  276. struct lfcc *lfcc;
  277. LIST_HEAD(list);
  278. struct gfs2_log_header_host lh;
  279. int error, error2;
  280. /*
  281. * Grab all the journal glocks in SH mode. We are *probably* doing
  282. * that to prevent recovery.
  283. */
  284. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  285. lfcc = kmalloc_obj(struct lfcc);
  286. if (!lfcc) {
  287. error = -ENOMEM;
  288. goto out;
  289. }
  290. ip = GFS2_I(jd->jd_inode);
  291. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  292. if (error) {
  293. kfree(lfcc);
  294. goto out;
  295. }
  296. list_add(&lfcc->list, &list);
  297. }
  298. gfs2_freeze_unlock(sdp);
  299. error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
  300. LM_FLAG_RECOVER | GL_NOPID,
  301. &sdp->sd_freeze_gh);
  302. if (error)
  303. goto relock_shared;
  304. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  305. error = gfs2_jdesc_check(jd);
  306. if (error)
  307. break;
  308. error = gfs2_find_jhead(jd, &lh);
  309. if (error)
  310. break;
  311. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  312. error = -EBUSY;
  313. break;
  314. }
  315. }
  316. if (!error)
  317. goto out; /* success */
  318. gfs2_freeze_unlock(sdp);
  319. relock_shared:
  320. error2 = gfs2_freeze_lock_shared(sdp);
  321. gfs2_assert_withdraw(sdp, !error2);
  322. out:
  323. while (!list_empty(&list)) {
  324. lfcc = list_first_entry(&list, struct lfcc, list);
  325. list_del(&lfcc->list);
  326. gfs2_glock_dq_uninit(&lfcc->gh);
  327. kfree(lfcc);
  328. }
  329. return error;
  330. }
  331. void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
  332. {
  333. const struct inode *inode = &ip->i_inode;
  334. struct gfs2_dinode *str = buf;
  335. str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  336. str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
  337. str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
  338. str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
  339. str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
  340. str->di_mode = cpu_to_be32(inode->i_mode);
  341. str->di_uid = cpu_to_be32(i_uid_read(inode));
  342. str->di_gid = cpu_to_be32(i_gid_read(inode));
  343. str->di_nlink = cpu_to_be32(inode->i_nlink);
  344. str->di_size = cpu_to_be64(i_size_read(inode));
  345. str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(inode));
  346. str->di_atime = cpu_to_be64(inode_get_atime_sec(inode));
  347. str->di_mtime = cpu_to_be64(inode_get_mtime_sec(inode));
  348. str->di_ctime = cpu_to_be64(inode_get_ctime_sec(inode));
  349. str->di_goal_meta = cpu_to_be64(ip->i_goal);
  350. str->di_goal_data = cpu_to_be64(ip->i_goal);
  351. str->di_generation = cpu_to_be64(ip->i_generation);
  352. str->di_flags = cpu_to_be32(ip->i_diskflags);
  353. str->di_height = cpu_to_be16(ip->i_height);
  354. str->di_payload_format = cpu_to_be32(S_ISDIR(inode->i_mode) &&
  355. !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
  356. GFS2_FORMAT_DE : 0);
  357. str->di_depth = cpu_to_be16(ip->i_depth);
  358. str->di_entries = cpu_to_be32(ip->i_entries);
  359. str->di_eattr = cpu_to_be64(ip->i_eattr);
  360. str->di_atime_nsec = cpu_to_be32(inode_get_atime_nsec(inode));
  361. str->di_mtime_nsec = cpu_to_be32(inode_get_mtime_nsec(inode));
  362. str->di_ctime_nsec = cpu_to_be32(inode_get_ctime_nsec(inode));
  363. }
  364. /**
  365. * gfs2_write_inode - Make sure the inode is stable on the disk
  366. * @inode: The inode
  367. * @wbc: The writeback control structure
  368. *
  369. * Returns: errno
  370. */
  371. static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
  372. {
  373. struct gfs2_inode *ip = GFS2_I(inode);
  374. struct gfs2_sbd *sdp = GFS2_SB(inode);
  375. struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
  376. struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
  377. int ret = 0;
  378. bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
  379. if (flush_all)
  380. gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
  381. GFS2_LOG_HEAD_FLUSH_NORMAL |
  382. GFS2_LFC_WRITE_INODE);
  383. if (bdi->wb.dirty_exceeded)
  384. gfs2_ail1_flush(sdp, wbc);
  385. else
  386. filemap_fdatawrite(metamapping);
  387. if (flush_all)
  388. ret = filemap_fdatawait(metamapping);
  389. if (ret)
  390. mark_inode_dirty_sync(inode);
  391. else {
  392. spin_lock(&inode->i_lock);
  393. if (!(inode->i_flags & I_DIRTY))
  394. gfs2_ordered_del_inode(ip);
  395. spin_unlock(&inode->i_lock);
  396. }
  397. return ret;
  398. }
  399. /**
  400. * gfs2_dirty_inode - check for atime updates
  401. * @inode: The inode in question
  402. * @flags: The type of dirty
  403. *
  404. * Unfortunately it can be called under any combination of inode
  405. * glock and freeze glock, so we have to check carefully.
  406. *
  407. * At the moment this deals only with atime - it should be possible
  408. * to expand that role in future, once a review of the locking has
  409. * been carried out.
  410. */
  411. static void gfs2_dirty_inode(struct inode *inode, int flags)
  412. {
  413. struct gfs2_inode *ip = GFS2_I(inode);
  414. struct gfs2_sbd *sdp = GFS2_SB(inode);
  415. struct buffer_head *bh;
  416. struct gfs2_holder gh;
  417. int need_unlock = 0;
  418. int need_endtrans = 0;
  419. int ret;
  420. /* This can only happen during incomplete inode creation. */
  421. if (unlikely(!ip->i_gl))
  422. return;
  423. if (gfs2_withdrawn(sdp))
  424. return;
  425. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  426. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  427. if (ret) {
  428. fs_err(sdp, "dirty_inode: glock %d\n", ret);
  429. gfs2_dump_glock(NULL, ip->i_gl, true);
  430. return;
  431. }
  432. need_unlock = 1;
  433. } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
  434. return;
  435. if (current->journal_info == NULL) {
  436. ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
  437. if (ret) {
  438. fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
  439. goto out;
  440. }
  441. need_endtrans = 1;
  442. }
  443. ret = gfs2_meta_inode_buffer(ip, &bh);
  444. if (ret == 0) {
  445. gfs2_trans_add_meta(ip->i_gl, bh);
  446. gfs2_dinode_out(ip, bh->b_data);
  447. brelse(bh);
  448. }
  449. if (need_endtrans)
  450. gfs2_trans_end(sdp);
  451. out:
  452. if (need_unlock)
  453. gfs2_glock_dq_uninit(&gh);
  454. }
  455. /**
  456. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  457. * @sdp: the filesystem
  458. *
  459. * Returns: errno
  460. */
  461. void gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  462. {
  463. int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  464. if (!test_bit(SDF_KILL, &sdp->sd_flags))
  465. gfs2_flush_delete_work(sdp);
  466. gfs2_destroy_threads(sdp);
  467. if (log_write_allowed) {
  468. gfs2_quota_sync(sdp->sd_vfs, 0);
  469. gfs2_statfs_sync(sdp->sd_vfs, 0);
  470. /* We do two log flushes here. The first one commits dirty inodes
  471. * and rgrps to the journal, but queues up revokes to the ail list.
  472. * The second flush writes out and removes the revokes.
  473. *
  474. * The first must be done before the FLUSH_SHUTDOWN code
  475. * clears the LIVE flag, otherwise it will not be able to start
  476. * a transaction to write its revokes, and the error will cause
  477. * a withdraw of the file system. */
  478. gfs2_log_flush(sdp, NULL, GFS2_LFC_MAKE_FS_RO);
  479. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
  480. GFS2_LFC_MAKE_FS_RO);
  481. wait_event_timeout(sdp->sd_log_waitq,
  482. gfs2_log_is_empty(sdp),
  483. HZ * 5);
  484. gfs2_assert_warn(sdp, gfs2_log_is_empty(sdp));
  485. }
  486. gfs2_quota_cleanup(sdp);
  487. }
  488. /**
  489. * gfs2_put_super - Unmount the filesystem
  490. * @sb: The VFS superblock
  491. *
  492. */
  493. static void gfs2_put_super(struct super_block *sb)
  494. {
  495. struct gfs2_sbd *sdp = sb->s_fs_info;
  496. struct gfs2_jdesc *jd;
  497. /* No more recovery requests */
  498. set_bit(SDF_NORECOVERY, &sdp->sd_flags);
  499. smp_mb();
  500. /* Wait on outstanding recovery */
  501. restart:
  502. spin_lock(&sdp->sd_jindex_spin);
  503. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  504. if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
  505. continue;
  506. spin_unlock(&sdp->sd_jindex_spin);
  507. wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
  508. TASK_UNINTERRUPTIBLE);
  509. goto restart;
  510. }
  511. spin_unlock(&sdp->sd_jindex_spin);
  512. if (!sb_rdonly(sb))
  513. gfs2_make_fs_ro(sdp);
  514. else {
  515. if (gfs2_withdrawn(sdp))
  516. gfs2_destroy_threads(sdp);
  517. gfs2_quota_cleanup(sdp);
  518. }
  519. flush_work(&sdp->sd_withdraw_work);
  520. /* At this point, we're through modifying the disk */
  521. /* Release stuff */
  522. gfs2_freeze_unlock(sdp);
  523. iput(sdp->sd_jindex);
  524. iput(sdp->sd_statfs_inode);
  525. iput(sdp->sd_rindex);
  526. iput(sdp->sd_quota_inode);
  527. gfs2_glock_put(sdp->sd_rename_gl);
  528. gfs2_glock_put(sdp->sd_freeze_gl);
  529. if (!sdp->sd_args.ar_spectator) {
  530. if (gfs2_holder_initialized(&sdp->sd_journal_gh))
  531. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  532. if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
  533. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  534. brelse(sdp->sd_sc_bh);
  535. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  536. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  537. free_local_statfs_inodes(sdp);
  538. iput(sdp->sd_qc_inode);
  539. }
  540. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  541. gfs2_clear_rgrpd(sdp);
  542. gfs2_jindex_free(sdp);
  543. /* Take apart glock structures and buffer lists */
  544. gfs2_gl_hash_clear(sdp);
  545. iput(sdp->sd_inode);
  546. gfs2_delete_debugfs_file(sdp);
  547. gfs2_sys_fs_del(sdp);
  548. free_sbd(sdp);
  549. }
  550. /**
  551. * gfs2_sync_fs - sync the filesystem
  552. * @sb: the superblock
  553. * @wait: true to wait for completion
  554. *
  555. * Flushes the log to disk.
  556. */
  557. static int gfs2_sync_fs(struct super_block *sb, int wait)
  558. {
  559. struct gfs2_sbd *sdp = sb->s_fs_info;
  560. gfs2_quota_sync(sb, -1);
  561. if (wait)
  562. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  563. GFS2_LFC_SYNC_FS);
  564. return sdp->sd_log_error;
  565. }
  566. static int gfs2_do_thaw(struct gfs2_sbd *sdp, enum freeze_holder who, const void *freeze_owner)
  567. {
  568. struct super_block *sb = sdp->sd_vfs;
  569. int error;
  570. error = gfs2_freeze_lock_shared(sdp);
  571. if (error)
  572. goto fail;
  573. error = thaw_super(sb, who, freeze_owner);
  574. if (!error)
  575. return 0;
  576. fail:
  577. fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n", error);
  578. gfs2_assert_withdraw(sdp, 0);
  579. return error;
  580. }
  581. void gfs2_freeze_func(struct work_struct *work)
  582. {
  583. struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
  584. struct super_block *sb = sdp->sd_vfs;
  585. int error;
  586. mutex_lock(&sdp->sd_freeze_mutex);
  587. error = -EBUSY;
  588. if (test_bit(SDF_FROZEN, &sdp->sd_flags))
  589. goto freeze_failed;
  590. error = freeze_super(sb, FREEZE_HOLDER_USERSPACE, NULL);
  591. if (error)
  592. goto freeze_failed;
  593. gfs2_freeze_unlock(sdp);
  594. set_bit(SDF_FROZEN, &sdp->sd_flags);
  595. error = gfs2_do_thaw(sdp, FREEZE_HOLDER_USERSPACE, NULL);
  596. if (error)
  597. goto out;
  598. clear_bit(SDF_FROZEN, &sdp->sd_flags);
  599. goto out;
  600. freeze_failed:
  601. fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n", error);
  602. out:
  603. mutex_unlock(&sdp->sd_freeze_mutex);
  604. deactivate_super(sb);
  605. }
  606. /**
  607. * gfs2_freeze_super - prevent further writes to the filesystem
  608. * @sb: the VFS structure for the filesystem
  609. * @who: freeze flags
  610. * @freeze_owner: owner of the freeze
  611. *
  612. */
  613. static int gfs2_freeze_super(struct super_block *sb, enum freeze_holder who,
  614. const void *freeze_owner)
  615. {
  616. struct gfs2_sbd *sdp = sb->s_fs_info;
  617. int error;
  618. if (!mutex_trylock(&sdp->sd_freeze_mutex))
  619. return -EBUSY;
  620. if (test_bit(SDF_FROZEN, &sdp->sd_flags)) {
  621. mutex_unlock(&sdp->sd_freeze_mutex);
  622. return -EBUSY;
  623. }
  624. for (;;) {
  625. error = freeze_super(sb, who, freeze_owner);
  626. if (error) {
  627. fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
  628. error);
  629. goto out;
  630. }
  631. error = gfs2_lock_fs_check_clean(sdp);
  632. if (!error) {
  633. set_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
  634. set_bit(SDF_FROZEN, &sdp->sd_flags);
  635. break;
  636. }
  637. (void)gfs2_do_thaw(sdp, who, freeze_owner);
  638. if (error == -EBUSY)
  639. fs_err(sdp, "waiting for recovery before freeze\n");
  640. else if (error == -EIO) {
  641. fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
  642. "to recovery error.\n");
  643. goto out;
  644. } else {
  645. fs_err(sdp, "error freezing FS: %d\n", error);
  646. }
  647. fs_err(sdp, "retrying...\n");
  648. msleep(1000);
  649. }
  650. out:
  651. mutex_unlock(&sdp->sd_freeze_mutex);
  652. return error;
  653. }
  654. static int gfs2_freeze_fs(struct super_block *sb)
  655. {
  656. struct gfs2_sbd *sdp = sb->s_fs_info;
  657. if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  658. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
  659. GFS2_LFC_FREEZE_GO_SYNC);
  660. if (gfs2_withdrawn(sdp))
  661. return -EIO;
  662. }
  663. return 0;
  664. }
  665. /**
  666. * gfs2_thaw_super - reallow writes to the filesystem
  667. * @sb: the VFS structure for the filesystem
  668. * @who: freeze flags
  669. * @freeze_owner: owner of the freeze
  670. *
  671. */
  672. static int gfs2_thaw_super(struct super_block *sb, enum freeze_holder who,
  673. const void *freeze_owner)
  674. {
  675. struct gfs2_sbd *sdp = sb->s_fs_info;
  676. int error;
  677. if (!mutex_trylock(&sdp->sd_freeze_mutex))
  678. return -EBUSY;
  679. if (!test_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags)) {
  680. mutex_unlock(&sdp->sd_freeze_mutex);
  681. return -EINVAL;
  682. }
  683. atomic_inc(&sb->s_active);
  684. gfs2_freeze_unlock(sdp);
  685. error = gfs2_do_thaw(sdp, who, freeze_owner);
  686. if (!error) {
  687. clear_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
  688. clear_bit(SDF_FROZEN, &sdp->sd_flags);
  689. }
  690. mutex_unlock(&sdp->sd_freeze_mutex);
  691. deactivate_super(sb);
  692. return error;
  693. }
  694. /**
  695. * statfs_slow_fill - fill in the sg for a given RG
  696. * @rgd: the RG
  697. * @sc: the sc structure
  698. *
  699. * Returns: 0 on success, -ESTALE if the LVB is invalid
  700. */
  701. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  702. struct gfs2_statfs_change_host *sc)
  703. {
  704. gfs2_rgrp_verify(rgd);
  705. sc->sc_total += rgd->rd_data;
  706. sc->sc_free += rgd->rd_free;
  707. sc->sc_dinodes += rgd->rd_dinodes;
  708. return 0;
  709. }
  710. /**
  711. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  712. * @sdp: the filesystem
  713. * @sc: the sc info that will be returned
  714. *
  715. * Any error (other than a signal) will cause this routine to fall back
  716. * to the synchronous version.
  717. *
  718. * FIXME: This really shouldn't busy wait like this.
  719. *
  720. * Returns: errno
  721. */
  722. static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  723. {
  724. struct gfs2_rgrpd *rgd_next;
  725. struct gfs2_holder *gha, *gh;
  726. unsigned int slots = 64;
  727. unsigned int x;
  728. int done;
  729. int error = 0, err;
  730. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  731. gha = kmalloc_objs(struct gfs2_holder, slots);
  732. if (!gha)
  733. return -ENOMEM;
  734. for (x = 0; x < slots; x++)
  735. gfs2_holder_mark_uninitialized(gha + x);
  736. rgd_next = gfs2_rgrpd_get_first(sdp);
  737. for (;;) {
  738. done = 1;
  739. for (x = 0; x < slots; x++) {
  740. gh = gha + x;
  741. if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
  742. err = gfs2_glock_wait(gh);
  743. if (err) {
  744. gfs2_holder_uninit(gh);
  745. error = err;
  746. } else {
  747. if (!error) {
  748. struct gfs2_rgrpd *rgd =
  749. gfs2_glock2rgrp(gh->gh_gl);
  750. error = statfs_slow_fill(rgd, sc);
  751. }
  752. gfs2_glock_dq_uninit(gh);
  753. }
  754. }
  755. if (gfs2_holder_initialized(gh))
  756. done = 0;
  757. else if (rgd_next && !error) {
  758. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  759. LM_ST_SHARED,
  760. GL_ASYNC,
  761. gh);
  762. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  763. done = 0;
  764. }
  765. if (signal_pending(current))
  766. error = -ERESTARTSYS;
  767. }
  768. if (done)
  769. break;
  770. yield();
  771. }
  772. kfree(gha);
  773. return error;
  774. }
  775. /**
  776. * gfs2_statfs_i - Do a statfs
  777. * @sdp: the filesystem
  778. * @sc: the sc structure
  779. *
  780. * Returns: errno
  781. */
  782. static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  783. {
  784. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  785. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  786. spin_lock(&sdp->sd_statfs_spin);
  787. *sc = *m_sc;
  788. sc->sc_total += l_sc->sc_total;
  789. sc->sc_free += l_sc->sc_free;
  790. sc->sc_dinodes += l_sc->sc_dinodes;
  791. spin_unlock(&sdp->sd_statfs_spin);
  792. if (sc->sc_free < 0)
  793. sc->sc_free = 0;
  794. if (sc->sc_free > sc->sc_total)
  795. sc->sc_free = sc->sc_total;
  796. if (sc->sc_dinodes < 0)
  797. sc->sc_dinodes = 0;
  798. return 0;
  799. }
  800. /**
  801. * gfs2_statfs - Gather and return stats about the filesystem
  802. * @dentry: The name of the link
  803. * @buf: The buffer
  804. *
  805. * Returns: 0 on success or error code
  806. */
  807. static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
  808. {
  809. struct super_block *sb = dentry->d_sb;
  810. struct gfs2_sbd *sdp = sb->s_fs_info;
  811. struct gfs2_statfs_change_host sc;
  812. int error;
  813. error = gfs2_rindex_update(sdp);
  814. if (error)
  815. return error;
  816. if (gfs2_tune_get(sdp, gt_statfs_slow))
  817. error = gfs2_statfs_slow(sdp, &sc);
  818. else
  819. error = gfs2_statfs_i(sdp, &sc);
  820. if (error)
  821. return error;
  822. buf->f_type = GFS2_MAGIC;
  823. buf->f_bsize = sdp->sd_sb.sb_bsize;
  824. buf->f_blocks = sc.sc_total;
  825. buf->f_bfree = sc.sc_free;
  826. buf->f_bavail = sc.sc_free;
  827. buf->f_files = sc.sc_dinodes + sc.sc_free;
  828. buf->f_ffree = sc.sc_free;
  829. buf->f_namelen = GFS2_FNAMESIZE;
  830. buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
  831. return 0;
  832. }
  833. /**
  834. * gfs2_drop_inode - Drop an inode (test for remote unlink)
  835. * @inode: The inode to drop
  836. *
  837. * If we've received a callback on an iopen lock then it's because a
  838. * remote node tried to deallocate the inode but failed due to this node
  839. * still having the inode open. Here we mark the link count zero
  840. * since we know that it must have reached zero if the GLF_DEMOTE flag
  841. * is set on the iopen glock. If we didn't do a disk read since the
  842. * remote node removed the final link then we might otherwise miss
  843. * this event. This check ensures that this node will deallocate the
  844. * inode's blocks, or alternatively pass the baton on to another
  845. * node for later deallocation.
  846. */
  847. static int gfs2_drop_inode(struct inode *inode)
  848. {
  849. struct gfs2_inode *ip = GFS2_I(inode);
  850. struct gfs2_sbd *sdp = GFS2_SB(inode);
  851. if (inode->i_nlink &&
  852. gfs2_holder_initialized(&ip->i_iopen_gh)) {
  853. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  854. if (glock_needs_demote(gl))
  855. clear_nlink(inode);
  856. }
  857. /*
  858. * When under memory pressure when an inode's link count has dropped to
  859. * zero, defer deleting the inode to the delete workqueue. This avoids
  860. * calling into DLM under memory pressure, which can deadlock.
  861. */
  862. if (!inode->i_nlink &&
  863. unlikely(current->flags & PF_MEMALLOC) &&
  864. gfs2_holder_initialized(&ip->i_iopen_gh)) {
  865. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  866. gfs2_glock_hold(gl);
  867. if (!gfs2_queue_verify_delete(gl, true))
  868. gfs2_glock_put_async(gl);
  869. return 0;
  870. }
  871. /*
  872. * No longer cache inodes when trying to evict them all.
  873. */
  874. if (test_bit(SDF_EVICTING, &sdp->sd_flags))
  875. return 1;
  876. return inode_generic_drop(inode);
  877. }
  878. /**
  879. * gfs2_show_options - Show mount options for /proc/mounts
  880. * @s: seq_file structure
  881. * @root: root of this (sub)tree
  882. *
  883. * Returns: 0 on success or error code
  884. */
  885. static int gfs2_show_options(struct seq_file *s, struct dentry *root)
  886. {
  887. struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
  888. struct gfs2_args *args = &sdp->sd_args;
  889. unsigned int logd_secs, statfs_slow, statfs_quantum, quota_quantum;
  890. spin_lock(&sdp->sd_tune.gt_spin);
  891. logd_secs = sdp->sd_tune.gt_logd_secs;
  892. quota_quantum = sdp->sd_tune.gt_quota_quantum;
  893. statfs_quantum = sdp->sd_tune.gt_statfs_quantum;
  894. statfs_slow = sdp->sd_tune.gt_statfs_slow;
  895. spin_unlock(&sdp->sd_tune.gt_spin);
  896. if (is_subdir(root, sdp->sd_master_dir))
  897. seq_puts(s, ",meta");
  898. if (args->ar_lockproto[0])
  899. seq_show_option(s, "lockproto", args->ar_lockproto);
  900. if (args->ar_locktable[0])
  901. seq_show_option(s, "locktable", args->ar_locktable);
  902. if (args->ar_hostdata[0])
  903. seq_show_option(s, "hostdata", args->ar_hostdata);
  904. if (args->ar_spectator)
  905. seq_puts(s, ",spectator");
  906. if (args->ar_localflocks)
  907. seq_puts(s, ",localflocks");
  908. if (args->ar_debug)
  909. seq_puts(s, ",debug");
  910. if (args->ar_posix_acl)
  911. seq_puts(s, ",acl");
  912. if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
  913. char *state;
  914. switch (args->ar_quota) {
  915. case GFS2_QUOTA_OFF:
  916. state = "off";
  917. break;
  918. case GFS2_QUOTA_ACCOUNT:
  919. state = "account";
  920. break;
  921. case GFS2_QUOTA_ON:
  922. state = "on";
  923. break;
  924. case GFS2_QUOTA_QUIET:
  925. state = "quiet";
  926. break;
  927. default:
  928. state = "unknown";
  929. break;
  930. }
  931. seq_printf(s, ",quota=%s", state);
  932. }
  933. if (args->ar_suiddir)
  934. seq_puts(s, ",suiddir");
  935. if (args->ar_data != GFS2_DATA_DEFAULT) {
  936. char *state;
  937. switch (args->ar_data) {
  938. case GFS2_DATA_WRITEBACK:
  939. state = "writeback";
  940. break;
  941. case GFS2_DATA_ORDERED:
  942. state = "ordered";
  943. break;
  944. default:
  945. state = "unknown";
  946. break;
  947. }
  948. seq_printf(s, ",data=%s", state);
  949. }
  950. if (args->ar_discard)
  951. seq_puts(s, ",discard");
  952. if (logd_secs != 30)
  953. seq_printf(s, ",commit=%d", logd_secs);
  954. if (statfs_quantum != 30)
  955. seq_printf(s, ",statfs_quantum=%d", statfs_quantum);
  956. else if (statfs_slow)
  957. seq_puts(s, ",statfs_quantum=0");
  958. if (quota_quantum != 60)
  959. seq_printf(s, ",quota_quantum=%d", quota_quantum);
  960. if (args->ar_statfs_percent)
  961. seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
  962. if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
  963. const char *state;
  964. switch (args->ar_errors) {
  965. case GFS2_ERRORS_WITHDRAW:
  966. state = "withdraw";
  967. break;
  968. case GFS2_ERRORS_DEACTIVATE:
  969. state = "deactivate";
  970. break;
  971. case GFS2_ERRORS_PANIC:
  972. state = "panic";
  973. break;
  974. default:
  975. state = "unknown";
  976. break;
  977. }
  978. seq_printf(s, ",errors=%s", state);
  979. }
  980. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
  981. seq_puts(s, ",nobarrier");
  982. if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
  983. seq_puts(s, ",demote_interface_used");
  984. if (args->ar_rgrplvb)
  985. seq_puts(s, ",rgrplvb");
  986. if (args->ar_loccookie)
  987. seq_puts(s, ",loccookie");
  988. return 0;
  989. }
  990. /**
  991. * gfs2_glock_put_eventually
  992. * @gl: The glock to put
  993. *
  994. * When under memory pressure, trigger a deferred glock put to make sure we
  995. * won't call into DLM and deadlock. Otherwise, put the glock directly.
  996. */
  997. static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
  998. {
  999. if (current->flags & PF_MEMALLOC)
  1000. gfs2_glock_put_async(gl);
  1001. else
  1002. gfs2_glock_put(gl);
  1003. }
  1004. static enum evict_behavior gfs2_upgrade_iopen_glock(struct inode *inode)
  1005. {
  1006. struct gfs2_inode *ip = GFS2_I(inode);
  1007. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1008. struct gfs2_holder *gh = &ip->i_iopen_gh;
  1009. int error;
  1010. gh->gh_flags |= GL_NOCACHE;
  1011. gfs2_glock_dq_wait(gh);
  1012. /*
  1013. * If there are no other lock holders, we will immediately get
  1014. * exclusive access to the iopen glock here.
  1015. *
  1016. * Otherwise, the other nodes holding the lock will be notified about
  1017. * our locking request (see iopen_go_callback()). If they do not have
  1018. * the inode open, they are expected to evict the cached inode and
  1019. * release the lock, allowing us to proceed.
  1020. *
  1021. * Otherwise, if they cannot evict the inode, they are expected to poke
  1022. * the inode glock (note: not the iopen glock). We will notice that
  1023. * and stop waiting for the iopen glock immediately. The other node(s)
  1024. * are then expected to take care of deleting the inode when they no
  1025. * longer use it.
  1026. *
  1027. * As a last resort, if another node keeps holding the iopen glock
  1028. * without showing any activity on the inode glock, we will eventually
  1029. * time out and fail the iopen glock upgrade.
  1030. */
  1031. gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
  1032. error = gfs2_glock_nq(gh);
  1033. if (error)
  1034. return EVICT_SHOULD_SKIP_DELETE;
  1035. wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
  1036. !test_bit(HIF_WAIT, &gh->gh_iflags) ||
  1037. glock_needs_demote(ip->i_gl),
  1038. 5 * HZ);
  1039. if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
  1040. gfs2_glock_dq(gh);
  1041. if (glock_needs_demote(ip->i_gl))
  1042. return EVICT_SHOULD_SKIP_DELETE;
  1043. return EVICT_SHOULD_DEFER_DELETE;
  1044. }
  1045. error = gfs2_glock_holder_ready(gh);
  1046. if (error)
  1047. return EVICT_SHOULD_SKIP_DELETE;
  1048. return EVICT_SHOULD_DELETE;
  1049. }
  1050. /**
  1051. * evict_should_delete - determine whether the inode is eligible for deletion
  1052. * @inode: The inode to evict
  1053. * @gh: The glock holder structure
  1054. *
  1055. * This function determines whether the evicted inode is eligible to be deleted
  1056. * and locks the inode glock.
  1057. *
  1058. * Returns: the fate of the dinode
  1059. */
  1060. static enum evict_behavior evict_should_delete(struct inode *inode,
  1061. struct gfs2_holder *gh)
  1062. {
  1063. struct gfs2_inode *ip = GFS2_I(inode);
  1064. struct super_block *sb = inode->i_sb;
  1065. struct gfs2_sbd *sdp = sb->s_fs_info;
  1066. int ret;
  1067. if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
  1068. test_bit(GLF_DEFER_DELETE, &ip->i_iopen_gh.gh_gl->gl_flags))
  1069. return EVICT_SHOULD_DEFER_DELETE;
  1070. /* Deletes should never happen under memory pressure anymore. */
  1071. if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
  1072. return EVICT_SHOULD_DEFER_DELETE;
  1073. /* Must not read inode block until block type has been verified */
  1074. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
  1075. if (unlikely(ret))
  1076. return EVICT_SHOULD_SKIP_DELETE;
  1077. if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
  1078. return EVICT_SHOULD_SKIP_DELETE;
  1079. ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
  1080. if (ret)
  1081. return EVICT_SHOULD_SKIP_DELETE;
  1082. ret = gfs2_instantiate(gh);
  1083. if (ret)
  1084. return EVICT_SHOULD_SKIP_DELETE;
  1085. /*
  1086. * The inode may have been recreated in the meantime.
  1087. */
  1088. if (inode->i_nlink)
  1089. return EVICT_SHOULD_SKIP_DELETE;
  1090. if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
  1091. test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
  1092. return gfs2_upgrade_iopen_glock(inode);
  1093. return EVICT_SHOULD_DELETE;
  1094. }
  1095. /**
  1096. * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
  1097. * @inode: The inode to evict
  1098. */
  1099. static int evict_unlinked_inode(struct inode *inode)
  1100. {
  1101. struct gfs2_inode *ip = GFS2_I(inode);
  1102. int ret;
  1103. if (S_ISDIR(inode->i_mode) &&
  1104. (ip->i_diskflags & GFS2_DIF_EXHASH)) {
  1105. ret = gfs2_dir_exhash_dealloc(ip);
  1106. if (ret)
  1107. goto out;
  1108. }
  1109. if (ip->i_eattr) {
  1110. ret = gfs2_ea_dealloc(ip, true);
  1111. if (ret)
  1112. goto out;
  1113. }
  1114. if (!gfs2_is_stuffed(ip)) {
  1115. ret = gfs2_file_dealloc(ip);
  1116. if (ret)
  1117. goto out;
  1118. }
  1119. /*
  1120. * As soon as we clear the bitmap for the dinode, gfs2_create_inode()
  1121. * can get called to recreate it, or even gfs2_inode_lookup() if the
  1122. * inode was recreated on another node in the meantime.
  1123. *
  1124. * However, inserting the new inode into the inode hash table will not
  1125. * succeed until the old inode is removed, and that only happens after
  1126. * ->evict_inode() returns. The new inode is attached to its inode and
  1127. * iopen glocks after inserting it into the inode hash table, so at
  1128. * that point we can be sure that both glocks are unused.
  1129. */
  1130. ret = gfs2_dinode_dealloc(ip);
  1131. if (!ret && ip->i_gl)
  1132. gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
  1133. out:
  1134. return ret;
  1135. }
  1136. /*
  1137. * evict_linked_inode - evict an inode whose dinode has not been unlinked
  1138. * @inode: The inode to evict
  1139. */
  1140. static int evict_linked_inode(struct inode *inode)
  1141. {
  1142. struct super_block *sb = inode->i_sb;
  1143. struct gfs2_sbd *sdp = sb->s_fs_info;
  1144. struct gfs2_inode *ip = GFS2_I(inode);
  1145. struct address_space *metamapping;
  1146. int ret;
  1147. gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
  1148. GFS2_LFC_EVICT_INODE);
  1149. metamapping = gfs2_glock2aspace(ip->i_gl);
  1150. if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
  1151. filemap_fdatawrite(metamapping);
  1152. filemap_fdatawait(metamapping);
  1153. }
  1154. write_inode_now(inode, 1);
  1155. gfs2_ail_flush(ip->i_gl, 0);
  1156. ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  1157. if (ret)
  1158. return ret;
  1159. /* Needs to be done before glock release & also in a transaction */
  1160. truncate_inode_pages(&inode->i_data, 0);
  1161. truncate_inode_pages(metamapping, 0);
  1162. gfs2_trans_end(sdp);
  1163. return 0;
  1164. }
  1165. /**
  1166. * gfs2_evict_inode - Remove an inode from cache
  1167. * @inode: The inode to evict
  1168. *
  1169. * There are three cases to consider:
  1170. * 1. i_nlink == 0, we are final opener (and must deallocate)
  1171. * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
  1172. * 3. i_nlink > 0
  1173. *
  1174. * If the fs is read only, then we have to treat all cases as per #3
  1175. * since we are unable to do any deallocation. The inode will be
  1176. * deallocated by the next read/write node to attempt an allocation
  1177. * in the same resource group
  1178. *
  1179. * We have to (at the moment) hold the inodes main lock to cover
  1180. * the gap between unlocking the shared lock on the iopen lock and
  1181. * taking the exclusive lock. I'd rather do a shared -> exclusive
  1182. * conversion on the iopen lock, but we can change that later. This
  1183. * is safe, just less efficient.
  1184. */
  1185. static void gfs2_evict_inode(struct inode *inode)
  1186. {
  1187. struct super_block *sb = inode->i_sb;
  1188. struct gfs2_sbd *sdp = sb->s_fs_info;
  1189. struct gfs2_inode *ip = GFS2_I(inode);
  1190. struct gfs2_holder gh;
  1191. enum evict_behavior behavior;
  1192. int ret;
  1193. gfs2_holder_mark_uninitialized(&gh);
  1194. if (inode->i_nlink || sb_rdonly(sb) || !ip->i_no_addr)
  1195. goto out;
  1196. /*
  1197. * In case of an incomplete mount, gfs2_evict_inode() may be called for
  1198. * system files without having an active journal to write to. In that
  1199. * case, skip the filesystem evict.
  1200. */
  1201. if (!sdp->sd_jdesc)
  1202. goto out;
  1203. behavior = evict_should_delete(inode, &gh);
  1204. if (behavior == EVICT_SHOULD_DEFER_DELETE &&
  1205. !test_bit(SDF_KILL, &sdp->sd_flags)) {
  1206. struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
  1207. if (io_gl) {
  1208. gfs2_glock_hold(io_gl);
  1209. if (!gfs2_queue_verify_delete(io_gl, true))
  1210. gfs2_glock_put(io_gl);
  1211. goto out;
  1212. }
  1213. behavior = EVICT_SHOULD_SKIP_DELETE;
  1214. }
  1215. if (behavior == EVICT_SHOULD_DELETE)
  1216. ret = evict_unlinked_inode(inode);
  1217. else
  1218. ret = evict_linked_inode(inode);
  1219. if (gfs2_rs_active(&ip->i_res))
  1220. gfs2_rs_deltree(&ip->i_res);
  1221. if (ret && ret != GLR_TRYFAILED && ret != -EROFS)
  1222. fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
  1223. out:
  1224. if (gfs2_holder_initialized(&gh))
  1225. gfs2_glock_dq_uninit(&gh);
  1226. truncate_inode_pages_final(&inode->i_data);
  1227. if (ip->i_qadata)
  1228. gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
  1229. gfs2_rs_deltree(&ip->i_res);
  1230. gfs2_ordered_del_inode(ip);
  1231. clear_inode(inode);
  1232. gfs2_dir_hash_inval(ip);
  1233. if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
  1234. struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
  1235. glock_clear_object(gl, ip);
  1236. gfs2_glock_hold(gl);
  1237. ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
  1238. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  1239. gfs2_glock_put_eventually(gl);
  1240. }
  1241. if (ip->i_gl) {
  1242. glock_clear_object(ip->i_gl, ip);
  1243. wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
  1244. gfs2_glock_put_eventually(ip->i_gl);
  1245. rcu_assign_pointer(ip->i_gl, NULL);
  1246. }
  1247. }
  1248. static struct inode *gfs2_alloc_inode(struct super_block *sb)
  1249. {
  1250. struct gfs2_inode *ip;
  1251. ip = alloc_inode_sb(sb, gfs2_inode_cachep, GFP_KERNEL);
  1252. if (!ip)
  1253. return NULL;
  1254. ip->i_no_addr = 0;
  1255. ip->i_no_formal_ino = 0;
  1256. ip->i_flags = 0;
  1257. ip->i_gl = NULL;
  1258. gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
  1259. memset(&ip->i_res, 0, sizeof(ip->i_res));
  1260. RB_CLEAR_NODE(&ip->i_res.rs_node);
  1261. ip->i_diskflags = 0;
  1262. ip->i_rahead = 0;
  1263. return &ip->i_inode;
  1264. }
  1265. static void gfs2_free_inode(struct inode *inode)
  1266. {
  1267. kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
  1268. }
  1269. void free_local_statfs_inodes(struct gfs2_sbd *sdp)
  1270. {
  1271. struct local_statfs_inode *lsi, *safe;
  1272. /* Run through the statfs inodes list to iput and free memory */
  1273. list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
  1274. if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
  1275. sdp->sd_sc_inode = NULL; /* belongs to this node */
  1276. if (lsi->si_sc_inode)
  1277. iput(lsi->si_sc_inode);
  1278. list_del(&lsi->si_list);
  1279. kfree(lsi);
  1280. }
  1281. }
  1282. struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
  1283. unsigned int index)
  1284. {
  1285. struct local_statfs_inode *lsi;
  1286. /* Return the local (per node) statfs inode in the
  1287. * sdp->sd_sc_inodes_list corresponding to the 'index'. */
  1288. list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
  1289. if (lsi->si_jid == index)
  1290. return lsi->si_sc_inode;
  1291. }
  1292. return NULL;
  1293. }
  1294. const struct super_operations gfs2_super_ops = {
  1295. .alloc_inode = gfs2_alloc_inode,
  1296. .free_inode = gfs2_free_inode,
  1297. .write_inode = gfs2_write_inode,
  1298. .dirty_inode = gfs2_dirty_inode,
  1299. .evict_inode = gfs2_evict_inode,
  1300. .put_super = gfs2_put_super,
  1301. .sync_fs = gfs2_sync_fs,
  1302. .freeze_super = gfs2_freeze_super,
  1303. .freeze_fs = gfs2_freeze_fs,
  1304. .thaw_super = gfs2_thaw_super,
  1305. .statfs = gfs2_statfs,
  1306. .drop_inode = gfs2_drop_inode,
  1307. .show_options = gfs2_show_options,
  1308. };