bmap.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/spinlock.h>
  7. #include <linux/completion.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/gfs2_ondisk.h>
  11. #include <linux/crc32.h>
  12. #include <linux/iomap.h>
  13. #include <linux/ktime.h>
  14. #include "gfs2.h"
  15. #include "incore.h"
  16. #include "bmap.h"
  17. #include "glock.h"
  18. #include "inode.h"
  19. #include "meta_io.h"
  20. #include "quota.h"
  21. #include "rgrp.h"
  22. #include "log.h"
  23. #include "super.h"
  24. #include "trans.h"
  25. #include "dir.h"
  26. #include "util.h"
  27. #include "aops.h"
  28. #include "trace_gfs2.h"
  29. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  30. * block is 512, so __u16 is fine for that. It saves stack space to
  31. * keep it small.
  32. */
  33. struct metapath {
  34. struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
  35. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  36. int mp_fheight; /* find_metapath height */
  37. int mp_aheight; /* actual height (lookup height) */
  38. };
  39. static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length);
  40. /**
  41. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  42. * @ip: the inode
  43. * @dibh: the dinode buffer
  44. * @block: the block number that was allocated
  45. * @page: The (optional) page. This is looked up if @page is NULL
  46. *
  47. * Returns: errno
  48. */
  49. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  50. u64 block, struct page *page)
  51. {
  52. struct inode *inode = &ip->i_inode;
  53. int release = 0;
  54. if (!page || page->index) {
  55. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  56. if (!page)
  57. return -ENOMEM;
  58. release = 1;
  59. }
  60. if (!PageUptodate(page)) {
  61. void *kaddr = kmap(page);
  62. u64 dsize = i_size_read(inode);
  63. if (dsize > gfs2_max_stuffed_size(ip))
  64. dsize = gfs2_max_stuffed_size(ip);
  65. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  66. memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
  67. kunmap(page);
  68. SetPageUptodate(page);
  69. }
  70. if (gfs2_is_jdata(ip)) {
  71. struct buffer_head *bh;
  72. if (!page_has_buffers(page))
  73. create_empty_buffers(page, BIT(inode->i_blkbits),
  74. BIT(BH_Uptodate));
  75. bh = page_buffers(page);
  76. if (!buffer_mapped(bh))
  77. map_bh(bh, inode->i_sb, block);
  78. set_buffer_uptodate(bh);
  79. gfs2_trans_add_data(ip->i_gl, bh);
  80. } else {
  81. set_page_dirty(page);
  82. gfs2_ordered_add_inode(ip);
  83. }
  84. if (release) {
  85. unlock_page(page);
  86. put_page(page);
  87. }
  88. return 0;
  89. }
  90. /**
  91. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  92. * @ip: The GFS2 inode to unstuff
  93. * @page: The (optional) page. This is looked up if the @page is NULL
  94. *
  95. * This routine unstuffs a dinode and returns it to a "normal" state such
  96. * that the height can be grown in the traditional way.
  97. *
  98. * Returns: errno
  99. */
  100. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  101. {
  102. struct buffer_head *bh, *dibh;
  103. struct gfs2_dinode *di;
  104. u64 block = 0;
  105. int isdir = gfs2_is_dir(ip);
  106. int error;
  107. down_write(&ip->i_rw_mutex);
  108. error = gfs2_meta_inode_buffer(ip, &dibh);
  109. if (error)
  110. goto out;
  111. if (i_size_read(&ip->i_inode)) {
  112. /* Get a free block, fill it with the stuffed data,
  113. and write it out to disk */
  114. unsigned int n = 1;
  115. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  116. if (error)
  117. goto out_brelse;
  118. if (isdir) {
  119. gfs2_trans_remove_revoke(GFS2_SB(&ip->i_inode), block, 1);
  120. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  121. if (error)
  122. goto out_brelse;
  123. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  124. dibh, sizeof(struct gfs2_dinode));
  125. brelse(bh);
  126. } else {
  127. error = gfs2_unstuffer_page(ip, dibh, block, page);
  128. if (error)
  129. goto out_brelse;
  130. }
  131. }
  132. /* Set up the pointer to the new block */
  133. gfs2_trans_add_meta(ip->i_gl, dibh);
  134. di = (struct gfs2_dinode *)dibh->b_data;
  135. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  136. if (i_size_read(&ip->i_inode)) {
  137. *(__be64 *)(di + 1) = cpu_to_be64(block);
  138. gfs2_add_inode_blocks(&ip->i_inode, 1);
  139. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  140. }
  141. ip->i_height = 1;
  142. di->di_height = cpu_to_be16(1);
  143. out_brelse:
  144. brelse(dibh);
  145. out:
  146. up_write(&ip->i_rw_mutex);
  147. return error;
  148. }
  149. /**
  150. * find_metapath - Find path through the metadata tree
  151. * @sdp: The superblock
  152. * @block: The disk block to look up
  153. * @mp: The metapath to return the result in
  154. * @height: The pre-calculated height of the metadata tree
  155. *
  156. * This routine returns a struct metapath structure that defines a path
  157. * through the metadata of inode "ip" to get to block "block".
  158. *
  159. * Example:
  160. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  161. * filesystem with a blocksize of 4096.
  162. *
  163. * find_metapath() would return a struct metapath structure set to:
  164. * mp_fheight = 3, mp_list[0] = 0, mp_list[1] = 48, and mp_list[2] = 165.
  165. *
  166. * That means that in order to get to the block containing the byte at
  167. * offset 101342453, we would load the indirect block pointed to by pointer
  168. * 0 in the dinode. We would then load the indirect block pointed to by
  169. * pointer 48 in that indirect block. We would then load the data block
  170. * pointed to by pointer 165 in that indirect block.
  171. *
  172. * ----------------------------------------
  173. * | Dinode | |
  174. * | | 4|
  175. * | |0 1 2 3 4 5 9|
  176. * | | 6|
  177. * ----------------------------------------
  178. * |
  179. * |
  180. * V
  181. * ----------------------------------------
  182. * | Indirect Block |
  183. * | 5|
  184. * | 4 4 4 4 4 5 5 1|
  185. * |0 5 6 7 8 9 0 1 2|
  186. * ----------------------------------------
  187. * |
  188. * |
  189. * V
  190. * ----------------------------------------
  191. * | Indirect Block |
  192. * | 1 1 1 1 1 5|
  193. * | 6 6 6 6 6 1|
  194. * |0 3 4 5 6 7 2|
  195. * ----------------------------------------
  196. * |
  197. * |
  198. * V
  199. * ----------------------------------------
  200. * | Data block containing offset |
  201. * | 101342453 |
  202. * | |
  203. * | |
  204. * ----------------------------------------
  205. *
  206. */
  207. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  208. struct metapath *mp, unsigned int height)
  209. {
  210. unsigned int i;
  211. mp->mp_fheight = height;
  212. for (i = height; i--;)
  213. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  214. }
  215. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  216. {
  217. if (mp->mp_list[0] == 0)
  218. return 2;
  219. return 1;
  220. }
  221. /**
  222. * metaptr1 - Return the first possible metadata pointer in a metapath buffer
  223. * @height: The metadata height (0 = dinode)
  224. * @mp: The metapath
  225. */
  226. static inline __be64 *metaptr1(unsigned int height, const struct metapath *mp)
  227. {
  228. struct buffer_head *bh = mp->mp_bh[height];
  229. if (height == 0)
  230. return ((__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)));
  231. return ((__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)));
  232. }
  233. /**
  234. * metapointer - Return pointer to start of metadata in a buffer
  235. * @height: The metadata height (0 = dinode)
  236. * @mp: The metapath
  237. *
  238. * Return a pointer to the block number of the next height of the metadata
  239. * tree given a buffer containing the pointer to the current height of the
  240. * metadata tree.
  241. */
  242. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  243. {
  244. __be64 *p = metaptr1(height, mp);
  245. return p + mp->mp_list[height];
  246. }
  247. static inline const __be64 *metaend(unsigned int height, const struct metapath *mp)
  248. {
  249. const struct buffer_head *bh = mp->mp_bh[height];
  250. return (const __be64 *)(bh->b_data + bh->b_size);
  251. }
  252. static void clone_metapath(struct metapath *clone, struct metapath *mp)
  253. {
  254. unsigned int hgt;
  255. *clone = *mp;
  256. for (hgt = 0; hgt < mp->mp_aheight; hgt++)
  257. get_bh(clone->mp_bh[hgt]);
  258. }
  259. static void gfs2_metapath_ra(struct gfs2_glock *gl, __be64 *start, __be64 *end)
  260. {
  261. const __be64 *t;
  262. for (t = start; t < end; t++) {
  263. struct buffer_head *rabh;
  264. if (!*t)
  265. continue;
  266. rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
  267. if (trylock_buffer(rabh)) {
  268. if (!buffer_uptodate(rabh)) {
  269. rabh->b_end_io = end_buffer_read_sync;
  270. submit_bh(REQ_OP_READ,
  271. REQ_RAHEAD | REQ_META | REQ_PRIO,
  272. rabh);
  273. continue;
  274. }
  275. unlock_buffer(rabh);
  276. }
  277. brelse(rabh);
  278. }
  279. }
  280. static int __fillup_metapath(struct gfs2_inode *ip, struct metapath *mp,
  281. unsigned int x, unsigned int h)
  282. {
  283. for (; x < h; x++) {
  284. __be64 *ptr = metapointer(x, mp);
  285. u64 dblock = be64_to_cpu(*ptr);
  286. int ret;
  287. if (!dblock)
  288. break;
  289. ret = gfs2_meta_indirect_buffer(ip, x + 1, dblock, &mp->mp_bh[x + 1]);
  290. if (ret)
  291. return ret;
  292. }
  293. mp->mp_aheight = x + 1;
  294. return 0;
  295. }
  296. /**
  297. * lookup_metapath - Walk the metadata tree to a specific point
  298. * @ip: The inode
  299. * @mp: The metapath
  300. *
  301. * Assumes that the inode's buffer has already been looked up and
  302. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  303. * by find_metapath().
  304. *
  305. * If this function encounters part of the tree which has not been
  306. * allocated, it returns the current height of the tree at the point
  307. * at which it found the unallocated block. Blocks which are found are
  308. * added to the mp->mp_bh[] list.
  309. *
  310. * Returns: error
  311. */
  312. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  313. {
  314. return __fillup_metapath(ip, mp, 0, ip->i_height - 1);
  315. }
  316. /**
  317. * fillup_metapath - fill up buffers for the metadata path to a specific height
  318. * @ip: The inode
  319. * @mp: The metapath
  320. * @h: The height to which it should be mapped
  321. *
  322. * Similar to lookup_metapath, but does lookups for a range of heights
  323. *
  324. * Returns: error or the number of buffers filled
  325. */
  326. static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h)
  327. {
  328. unsigned int x = 0;
  329. int ret;
  330. if (h) {
  331. /* find the first buffer we need to look up. */
  332. for (x = h - 1; x > 0; x--) {
  333. if (mp->mp_bh[x])
  334. break;
  335. }
  336. }
  337. ret = __fillup_metapath(ip, mp, x, h);
  338. if (ret)
  339. return ret;
  340. return mp->mp_aheight - x - 1;
  341. }
  342. static sector_t metapath_to_block(struct gfs2_sbd *sdp, struct metapath *mp)
  343. {
  344. sector_t factor = 1, block = 0;
  345. int hgt;
  346. for (hgt = mp->mp_fheight - 1; hgt >= 0; hgt--) {
  347. if (hgt < mp->mp_aheight)
  348. block += mp->mp_list[hgt] * factor;
  349. factor *= sdp->sd_inptrs;
  350. }
  351. return block;
  352. }
  353. static void release_metapath(struct metapath *mp)
  354. {
  355. int i;
  356. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  357. if (mp->mp_bh[i] == NULL)
  358. break;
  359. brelse(mp->mp_bh[i]);
  360. mp->mp_bh[i] = NULL;
  361. }
  362. }
  363. /**
  364. * gfs2_extent_length - Returns length of an extent of blocks
  365. * @bh: The metadata block
  366. * @ptr: Current position in @bh
  367. * @limit: Max extent length to return
  368. * @eob: Set to 1 if we hit "end of block"
  369. *
  370. * Returns: The length of the extent (minimum of one block)
  371. */
  372. static inline unsigned int gfs2_extent_length(struct buffer_head *bh, __be64 *ptr, size_t limit, int *eob)
  373. {
  374. const __be64 *end = (__be64 *)(bh->b_data + bh->b_size);
  375. const __be64 *first = ptr;
  376. u64 d = be64_to_cpu(*ptr);
  377. *eob = 0;
  378. do {
  379. ptr++;
  380. if (ptr >= end)
  381. break;
  382. d++;
  383. } while(be64_to_cpu(*ptr) == d);
  384. if (ptr >= end)
  385. *eob = 1;
  386. return ptr - first;
  387. }
  388. enum walker_status { WALK_STOP, WALK_FOLLOW, WALK_CONTINUE };
  389. /*
  390. * gfs2_metadata_walker - walk an indirect block
  391. * @mp: Metapath to indirect block
  392. * @ptrs: Number of pointers to look at
  393. *
  394. * When returning WALK_FOLLOW, the walker must update @mp to point at the right
  395. * indirect block to follow.
  396. */
  397. typedef enum walker_status (*gfs2_metadata_walker)(struct metapath *mp,
  398. unsigned int ptrs);
  399. /*
  400. * gfs2_walk_metadata - walk a tree of indirect blocks
  401. * @inode: The inode
  402. * @mp: Starting point of walk
  403. * @max_len: Maximum number of blocks to walk
  404. * @walker: Called during the walk
  405. *
  406. * Returns 1 if the walk was stopped by @walker, 0 if we went past @max_len or
  407. * past the end of metadata, and a negative error code otherwise.
  408. */
  409. static int gfs2_walk_metadata(struct inode *inode, struct metapath *mp,
  410. u64 max_len, gfs2_metadata_walker walker)
  411. {
  412. struct gfs2_inode *ip = GFS2_I(inode);
  413. struct gfs2_sbd *sdp = GFS2_SB(inode);
  414. u64 factor = 1;
  415. unsigned int hgt;
  416. int ret;
  417. /*
  418. * The walk starts in the lowest allocated indirect block, which may be
  419. * before the position indicated by @mp. Adjust @max_len accordingly
  420. * to avoid a short walk.
  421. */
  422. for (hgt = mp->mp_fheight - 1; hgt >= mp->mp_aheight; hgt--) {
  423. max_len += mp->mp_list[hgt] * factor;
  424. mp->mp_list[hgt] = 0;
  425. factor *= sdp->sd_inptrs;
  426. }
  427. for (;;) {
  428. u16 start = mp->mp_list[hgt];
  429. enum walker_status status;
  430. unsigned int ptrs;
  431. u64 len;
  432. /* Walk indirect block. */
  433. ptrs = (hgt >= 1 ? sdp->sd_inptrs : sdp->sd_diptrs) - start;
  434. len = ptrs * factor;
  435. if (len > max_len)
  436. ptrs = DIV_ROUND_UP_ULL(max_len, factor);
  437. status = walker(mp, ptrs);
  438. switch (status) {
  439. case WALK_STOP:
  440. return 1;
  441. case WALK_FOLLOW:
  442. BUG_ON(mp->mp_aheight == mp->mp_fheight);
  443. ptrs = mp->mp_list[hgt] - start;
  444. len = ptrs * factor;
  445. break;
  446. case WALK_CONTINUE:
  447. break;
  448. }
  449. if (len >= max_len)
  450. break;
  451. max_len -= len;
  452. if (status == WALK_FOLLOW)
  453. goto fill_up_metapath;
  454. lower_metapath:
  455. /* Decrease height of metapath. */
  456. brelse(mp->mp_bh[hgt]);
  457. mp->mp_bh[hgt] = NULL;
  458. mp->mp_list[hgt] = 0;
  459. if (!hgt)
  460. break;
  461. hgt--;
  462. factor *= sdp->sd_inptrs;
  463. /* Advance in metadata tree. */
  464. (mp->mp_list[hgt])++;
  465. if (hgt) {
  466. if (mp->mp_list[hgt] >= sdp->sd_inptrs)
  467. goto lower_metapath;
  468. } else {
  469. if (mp->mp_list[hgt] >= sdp->sd_diptrs)
  470. break;
  471. }
  472. fill_up_metapath:
  473. /* Increase height of metapath. */
  474. ret = fillup_metapath(ip, mp, ip->i_height - 1);
  475. if (ret < 0)
  476. return ret;
  477. hgt += ret;
  478. for (; ret; ret--)
  479. do_div(factor, sdp->sd_inptrs);
  480. mp->mp_aheight = hgt + 1;
  481. }
  482. return 0;
  483. }
  484. static enum walker_status gfs2_hole_walker(struct metapath *mp,
  485. unsigned int ptrs)
  486. {
  487. const __be64 *start, *ptr, *end;
  488. unsigned int hgt;
  489. hgt = mp->mp_aheight - 1;
  490. start = metapointer(hgt, mp);
  491. end = start + ptrs;
  492. for (ptr = start; ptr < end; ptr++) {
  493. if (*ptr) {
  494. mp->mp_list[hgt] += ptr - start;
  495. if (mp->mp_aheight == mp->mp_fheight)
  496. return WALK_STOP;
  497. return WALK_FOLLOW;
  498. }
  499. }
  500. return WALK_CONTINUE;
  501. }
  502. /**
  503. * gfs2_hole_size - figure out the size of a hole
  504. * @inode: The inode
  505. * @lblock: The logical starting block number
  506. * @len: How far to look (in blocks)
  507. * @mp: The metapath at lblock
  508. * @iomap: The iomap to store the hole size in
  509. *
  510. * This function modifies @mp.
  511. *
  512. * Returns: errno on error
  513. */
  514. static int gfs2_hole_size(struct inode *inode, sector_t lblock, u64 len,
  515. struct metapath *mp, struct iomap *iomap)
  516. {
  517. struct metapath clone;
  518. u64 hole_size;
  519. int ret;
  520. clone_metapath(&clone, mp);
  521. ret = gfs2_walk_metadata(inode, &clone, len, gfs2_hole_walker);
  522. if (ret < 0)
  523. goto out;
  524. if (ret == 1)
  525. hole_size = metapath_to_block(GFS2_SB(inode), &clone) - lblock;
  526. else
  527. hole_size = len;
  528. iomap->length = hole_size << inode->i_blkbits;
  529. ret = 0;
  530. out:
  531. release_metapath(&clone);
  532. return ret;
  533. }
  534. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  535. struct gfs2_glock *gl, unsigned int i,
  536. unsigned offset, u64 bn)
  537. {
  538. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  539. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  540. sizeof(struct gfs2_dinode)));
  541. BUG_ON(i < 1);
  542. BUG_ON(mp->mp_bh[i] != NULL);
  543. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  544. gfs2_trans_add_meta(gl, mp->mp_bh[i]);
  545. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  546. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  547. ptr += offset;
  548. *ptr = cpu_to_be64(bn);
  549. return ptr;
  550. }
  551. enum alloc_state {
  552. ALLOC_DATA = 0,
  553. ALLOC_GROW_DEPTH = 1,
  554. ALLOC_GROW_HEIGHT = 2,
  555. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  556. };
  557. /**
  558. * gfs2_iomap_alloc - Build a metadata tree of the requested height
  559. * @inode: The GFS2 inode
  560. * @iomap: The iomap structure
  561. * @mp: The metapath, with proper height information calculated
  562. *
  563. * In this routine we may have to alloc:
  564. * i) Indirect blocks to grow the metadata tree height
  565. * ii) Indirect blocks to fill in lower part of the metadata tree
  566. * iii) Data blocks
  567. *
  568. * This function is called after gfs2_iomap_get, which works out the
  569. * total number of blocks which we need via gfs2_alloc_size.
  570. *
  571. * We then do the actual allocation asking for an extent at a time (if
  572. * enough contiguous free blocks are available, there will only be one
  573. * allocation request per call) and uses the state machine to initialise
  574. * the blocks in order.
  575. *
  576. * Right now, this function will allocate at most one indirect block
  577. * worth of data -- with a default block size of 4K, that's slightly
  578. * less than 2M. If this limitation is ever removed to allow huge
  579. * allocations, we would probably still want to limit the iomap size we
  580. * return to avoid stalling other tasks during huge writes; the next
  581. * iomap iteration would then find the blocks already allocated.
  582. *
  583. * Returns: errno on error
  584. */
  585. static int gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap,
  586. struct metapath *mp)
  587. {
  588. struct gfs2_inode *ip = GFS2_I(inode);
  589. struct gfs2_sbd *sdp = GFS2_SB(inode);
  590. struct buffer_head *dibh = mp->mp_bh[0];
  591. u64 bn;
  592. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  593. size_t dblks = iomap->length >> inode->i_blkbits;
  594. const unsigned end_of_metadata = mp->mp_fheight - 1;
  595. int ret;
  596. enum alloc_state state;
  597. __be64 *ptr;
  598. __be64 zero_bn = 0;
  599. BUG_ON(mp->mp_aheight < 1);
  600. BUG_ON(dibh == NULL);
  601. BUG_ON(dblks < 1);
  602. gfs2_trans_add_meta(ip->i_gl, dibh);
  603. down_write(&ip->i_rw_mutex);
  604. if (mp->mp_fheight == mp->mp_aheight) {
  605. /* Bottom indirect block exists */
  606. state = ALLOC_DATA;
  607. } else {
  608. /* Need to allocate indirect blocks */
  609. if (mp->mp_fheight == ip->i_height) {
  610. /* Writing into existing tree, extend tree down */
  611. iblks = mp->mp_fheight - mp->mp_aheight;
  612. state = ALLOC_GROW_DEPTH;
  613. } else {
  614. /* Building up tree height */
  615. state = ALLOC_GROW_HEIGHT;
  616. iblks = mp->mp_fheight - ip->i_height;
  617. branch_start = metapath_branch_start(mp);
  618. iblks += (mp->mp_fheight - branch_start);
  619. }
  620. }
  621. /* start of the second part of the function (state machine) */
  622. blks = dblks + iblks;
  623. i = mp->mp_aheight;
  624. do {
  625. n = blks - alloced;
  626. ret = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  627. if (ret)
  628. goto out;
  629. alloced += n;
  630. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  631. gfs2_trans_remove_revoke(sdp, bn, n);
  632. switch (state) {
  633. /* Growing height of tree */
  634. case ALLOC_GROW_HEIGHT:
  635. if (i == 1) {
  636. ptr = (__be64 *)(dibh->b_data +
  637. sizeof(struct gfs2_dinode));
  638. zero_bn = *ptr;
  639. }
  640. for (; i - 1 < mp->mp_fheight - ip->i_height && n > 0;
  641. i++, n--)
  642. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  643. if (i - 1 == mp->mp_fheight - ip->i_height) {
  644. i--;
  645. gfs2_buffer_copy_tail(mp->mp_bh[i],
  646. sizeof(struct gfs2_meta_header),
  647. dibh, sizeof(struct gfs2_dinode));
  648. gfs2_buffer_clear_tail(dibh,
  649. sizeof(struct gfs2_dinode) +
  650. sizeof(__be64));
  651. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  652. sizeof(struct gfs2_meta_header));
  653. *ptr = zero_bn;
  654. state = ALLOC_GROW_DEPTH;
  655. for(i = branch_start; i < mp->mp_fheight; i++) {
  656. if (mp->mp_bh[i] == NULL)
  657. break;
  658. brelse(mp->mp_bh[i]);
  659. mp->mp_bh[i] = NULL;
  660. }
  661. i = branch_start;
  662. }
  663. if (n == 0)
  664. break;
  665. fallthrough; /* To branching from existing tree */
  666. case ALLOC_GROW_DEPTH:
  667. if (i > 1 && i < mp->mp_fheight)
  668. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  669. for (; i < mp->mp_fheight && n > 0; i++, n--)
  670. gfs2_indirect_init(mp, ip->i_gl, i,
  671. mp->mp_list[i-1], bn++);
  672. if (i == mp->mp_fheight)
  673. state = ALLOC_DATA;
  674. if (n == 0)
  675. break;
  676. fallthrough; /* To tree complete, adding data blocks */
  677. case ALLOC_DATA:
  678. BUG_ON(n > dblks);
  679. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  680. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  681. dblks = n;
  682. ptr = metapointer(end_of_metadata, mp);
  683. iomap->addr = bn << inode->i_blkbits;
  684. iomap->flags |= IOMAP_F_MERGED | IOMAP_F_NEW;
  685. while (n-- > 0)
  686. *ptr++ = cpu_to_be64(bn++);
  687. break;
  688. }
  689. } while (iomap->addr == IOMAP_NULL_ADDR);
  690. iomap->type = IOMAP_MAPPED;
  691. iomap->length = (u64)dblks << inode->i_blkbits;
  692. ip->i_height = mp->mp_fheight;
  693. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  694. gfs2_dinode_out(ip, dibh->b_data);
  695. out:
  696. up_write(&ip->i_rw_mutex);
  697. return ret;
  698. }
  699. #define IOMAP_F_GFS2_BOUNDARY IOMAP_F_PRIVATE
  700. /**
  701. * gfs2_alloc_size - Compute the maximum allocation size
  702. * @inode: The inode
  703. * @mp: The metapath
  704. * @size: Requested size in blocks
  705. *
  706. * Compute the maximum size of the next allocation at @mp.
  707. *
  708. * Returns: size in blocks
  709. */
  710. static u64 gfs2_alloc_size(struct inode *inode, struct metapath *mp, u64 size)
  711. {
  712. struct gfs2_inode *ip = GFS2_I(inode);
  713. struct gfs2_sbd *sdp = GFS2_SB(inode);
  714. const __be64 *first, *ptr, *end;
  715. /*
  716. * For writes to stuffed files, this function is called twice via
  717. * gfs2_iomap_get, before and after unstuffing. The size we return the
  718. * first time needs to be large enough to get the reservation and
  719. * allocation sizes right. The size we return the second time must
  720. * be exact or else gfs2_iomap_alloc won't do the right thing.
  721. */
  722. if (gfs2_is_stuffed(ip) || mp->mp_fheight != mp->mp_aheight) {
  723. unsigned int maxsize = mp->mp_fheight > 1 ?
  724. sdp->sd_inptrs : sdp->sd_diptrs;
  725. maxsize -= mp->mp_list[mp->mp_fheight - 1];
  726. if (size > maxsize)
  727. size = maxsize;
  728. return size;
  729. }
  730. first = metapointer(ip->i_height - 1, mp);
  731. end = metaend(ip->i_height - 1, mp);
  732. if (end - first > size)
  733. end = first + size;
  734. for (ptr = first; ptr < end; ptr++) {
  735. if (*ptr)
  736. break;
  737. }
  738. return ptr - first;
  739. }
  740. /**
  741. * gfs2_iomap_get - Map blocks from an inode to disk blocks
  742. * @inode: The inode
  743. * @pos: Starting position in bytes
  744. * @length: Length to map, in bytes
  745. * @flags: iomap flags
  746. * @iomap: The iomap structure
  747. * @mp: The metapath
  748. *
  749. * Returns: errno
  750. */
  751. static int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
  752. unsigned flags, struct iomap *iomap,
  753. struct metapath *mp)
  754. {
  755. struct gfs2_inode *ip = GFS2_I(inode);
  756. struct gfs2_sbd *sdp = GFS2_SB(inode);
  757. loff_t size = i_size_read(inode);
  758. __be64 *ptr;
  759. sector_t lblock;
  760. sector_t lblock_stop;
  761. int ret;
  762. int eob;
  763. u64 len;
  764. struct buffer_head *dibh = NULL, *bh;
  765. u8 height;
  766. if (!length)
  767. return -EINVAL;
  768. down_read(&ip->i_rw_mutex);
  769. ret = gfs2_meta_inode_buffer(ip, &dibh);
  770. if (ret)
  771. goto unlock;
  772. mp->mp_bh[0] = dibh;
  773. if (gfs2_is_stuffed(ip)) {
  774. if (flags & IOMAP_WRITE) {
  775. loff_t max_size = gfs2_max_stuffed_size(ip);
  776. if (pos + length > max_size)
  777. goto unstuff;
  778. iomap->length = max_size;
  779. } else {
  780. if (pos >= size) {
  781. if (flags & IOMAP_REPORT) {
  782. ret = -ENOENT;
  783. goto unlock;
  784. } else {
  785. iomap->offset = pos;
  786. iomap->length = length;
  787. goto hole_found;
  788. }
  789. }
  790. iomap->length = size;
  791. }
  792. iomap->addr = (ip->i_no_addr << inode->i_blkbits) +
  793. sizeof(struct gfs2_dinode);
  794. iomap->type = IOMAP_INLINE;
  795. iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode);
  796. goto out;
  797. }
  798. unstuff:
  799. lblock = pos >> inode->i_blkbits;
  800. iomap->offset = lblock << inode->i_blkbits;
  801. lblock_stop = (pos + length - 1) >> inode->i_blkbits;
  802. len = lblock_stop - lblock + 1;
  803. iomap->length = len << inode->i_blkbits;
  804. height = ip->i_height;
  805. while ((lblock + 1) * sdp->sd_sb.sb_bsize > sdp->sd_heightsize[height])
  806. height++;
  807. find_metapath(sdp, lblock, mp, height);
  808. if (height > ip->i_height || gfs2_is_stuffed(ip))
  809. goto do_alloc;
  810. ret = lookup_metapath(ip, mp);
  811. if (ret)
  812. goto unlock;
  813. if (mp->mp_aheight != ip->i_height)
  814. goto do_alloc;
  815. ptr = metapointer(ip->i_height - 1, mp);
  816. if (*ptr == 0)
  817. goto do_alloc;
  818. bh = mp->mp_bh[ip->i_height - 1];
  819. len = gfs2_extent_length(bh, ptr, len, &eob);
  820. iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits;
  821. iomap->length = len << inode->i_blkbits;
  822. iomap->type = IOMAP_MAPPED;
  823. iomap->flags |= IOMAP_F_MERGED;
  824. if (eob)
  825. iomap->flags |= IOMAP_F_GFS2_BOUNDARY;
  826. out:
  827. iomap->bdev = inode->i_sb->s_bdev;
  828. unlock:
  829. up_read(&ip->i_rw_mutex);
  830. return ret;
  831. do_alloc:
  832. if (flags & IOMAP_REPORT) {
  833. if (pos >= size)
  834. ret = -ENOENT;
  835. else if (height == ip->i_height)
  836. ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
  837. else
  838. iomap->length = size - iomap->offset;
  839. } else if (flags & IOMAP_WRITE) {
  840. u64 alloc_size;
  841. if (flags & IOMAP_DIRECT)
  842. goto out; /* (see gfs2_file_direct_write) */
  843. len = gfs2_alloc_size(inode, mp, len);
  844. alloc_size = len << inode->i_blkbits;
  845. if (alloc_size < iomap->length)
  846. iomap->length = alloc_size;
  847. } else {
  848. if (pos < size && height == ip->i_height)
  849. ret = gfs2_hole_size(inode, lblock, len, mp, iomap);
  850. }
  851. hole_found:
  852. iomap->addr = IOMAP_NULL_ADDR;
  853. iomap->type = IOMAP_HOLE;
  854. goto out;
  855. }
  856. /**
  857. * gfs2_lblk_to_dblk - convert logical block to disk block
  858. * @inode: the inode of the file we're mapping
  859. * @lblock: the block relative to the start of the file
  860. * @dblock: the returned dblock, if no error
  861. *
  862. * This function maps a single block from a file logical block (relative to
  863. * the start of the file) to a file system absolute block using iomap.
  864. *
  865. * Returns: the absolute file system block, or an error
  866. */
  867. int gfs2_lblk_to_dblk(struct inode *inode, u32 lblock, u64 *dblock)
  868. {
  869. struct iomap iomap = { };
  870. struct metapath mp = { .mp_aheight = 1, };
  871. loff_t pos = (loff_t)lblock << inode->i_blkbits;
  872. int ret;
  873. ret = gfs2_iomap_get(inode, pos, i_blocksize(inode), 0, &iomap, &mp);
  874. release_metapath(&mp);
  875. if (ret == 0)
  876. *dblock = iomap.addr >> inode->i_blkbits;
  877. return ret;
  878. }
  879. static int gfs2_write_lock(struct inode *inode)
  880. {
  881. struct gfs2_inode *ip = GFS2_I(inode);
  882. struct gfs2_sbd *sdp = GFS2_SB(inode);
  883. int error;
  884. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  885. error = gfs2_glock_nq(&ip->i_gh);
  886. if (error)
  887. goto out_uninit;
  888. if (&ip->i_inode == sdp->sd_rindex) {
  889. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  890. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
  891. GL_NOCACHE, &m_ip->i_gh);
  892. if (error)
  893. goto out_unlock;
  894. }
  895. return 0;
  896. out_unlock:
  897. gfs2_glock_dq(&ip->i_gh);
  898. out_uninit:
  899. gfs2_holder_uninit(&ip->i_gh);
  900. return error;
  901. }
  902. static void gfs2_write_unlock(struct inode *inode)
  903. {
  904. struct gfs2_inode *ip = GFS2_I(inode);
  905. struct gfs2_sbd *sdp = GFS2_SB(inode);
  906. if (&ip->i_inode == sdp->sd_rindex) {
  907. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  908. gfs2_glock_dq_uninit(&m_ip->i_gh);
  909. }
  910. gfs2_glock_dq_uninit(&ip->i_gh);
  911. }
  912. static int gfs2_iomap_page_prepare(struct inode *inode, loff_t pos,
  913. unsigned len, struct iomap *iomap)
  914. {
  915. unsigned int blockmask = i_blocksize(inode) - 1;
  916. struct gfs2_sbd *sdp = GFS2_SB(inode);
  917. unsigned int blocks;
  918. blocks = ((pos & blockmask) + len + blockmask) >> inode->i_blkbits;
  919. return gfs2_trans_begin(sdp, RES_DINODE + blocks, 0);
  920. }
  921. static void gfs2_iomap_page_done(struct inode *inode, loff_t pos,
  922. unsigned copied, struct page *page,
  923. struct iomap *iomap)
  924. {
  925. struct gfs2_trans *tr = current->journal_info;
  926. struct gfs2_inode *ip = GFS2_I(inode);
  927. struct gfs2_sbd *sdp = GFS2_SB(inode);
  928. if (page && !gfs2_is_stuffed(ip))
  929. gfs2_page_add_databufs(ip, page, offset_in_page(pos), copied);
  930. if (tr->tr_num_buf_new)
  931. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  932. gfs2_trans_end(sdp);
  933. }
  934. static const struct iomap_page_ops gfs2_iomap_page_ops = {
  935. .page_prepare = gfs2_iomap_page_prepare,
  936. .page_done = gfs2_iomap_page_done,
  937. };
  938. static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos,
  939. loff_t length, unsigned flags,
  940. struct iomap *iomap,
  941. struct metapath *mp)
  942. {
  943. struct gfs2_inode *ip = GFS2_I(inode);
  944. struct gfs2_sbd *sdp = GFS2_SB(inode);
  945. bool unstuff;
  946. int ret;
  947. unstuff = gfs2_is_stuffed(ip) &&
  948. pos + length > gfs2_max_stuffed_size(ip);
  949. if (unstuff || iomap->type == IOMAP_HOLE) {
  950. unsigned int data_blocks, ind_blocks;
  951. struct gfs2_alloc_parms ap = {};
  952. unsigned int rblocks;
  953. struct gfs2_trans *tr;
  954. gfs2_write_calc_reserv(ip, iomap->length, &data_blocks,
  955. &ind_blocks);
  956. ap.target = data_blocks + ind_blocks;
  957. ret = gfs2_quota_lock_check(ip, &ap);
  958. if (ret)
  959. return ret;
  960. ret = gfs2_inplace_reserve(ip, &ap);
  961. if (ret)
  962. goto out_qunlock;
  963. rblocks = RES_DINODE + ind_blocks;
  964. if (gfs2_is_jdata(ip))
  965. rblocks += data_blocks;
  966. if (ind_blocks || data_blocks)
  967. rblocks += RES_STATFS + RES_QUOTA;
  968. if (inode == sdp->sd_rindex)
  969. rblocks += 2 * RES_STATFS;
  970. rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
  971. ret = gfs2_trans_begin(sdp, rblocks,
  972. iomap->length >> inode->i_blkbits);
  973. if (ret)
  974. goto out_trans_fail;
  975. if (unstuff) {
  976. ret = gfs2_unstuff_dinode(ip, NULL);
  977. if (ret)
  978. goto out_trans_end;
  979. release_metapath(mp);
  980. ret = gfs2_iomap_get(inode, iomap->offset,
  981. iomap->length, flags, iomap, mp);
  982. if (ret)
  983. goto out_trans_end;
  984. }
  985. if (iomap->type == IOMAP_HOLE) {
  986. ret = gfs2_iomap_alloc(inode, iomap, mp);
  987. if (ret) {
  988. gfs2_trans_end(sdp);
  989. gfs2_inplace_release(ip);
  990. punch_hole(ip, iomap->offset, iomap->length);
  991. goto out_qunlock;
  992. }
  993. }
  994. tr = current->journal_info;
  995. if (tr->tr_num_buf_new)
  996. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  997. gfs2_trans_end(sdp);
  998. }
  999. if (gfs2_is_stuffed(ip) || gfs2_is_jdata(ip))
  1000. iomap->page_ops = &gfs2_iomap_page_ops;
  1001. return 0;
  1002. out_trans_end:
  1003. gfs2_trans_end(sdp);
  1004. out_trans_fail:
  1005. gfs2_inplace_release(ip);
  1006. out_qunlock:
  1007. gfs2_quota_unlock(ip);
  1008. return ret;
  1009. }
  1010. static inline bool gfs2_iomap_need_write_lock(unsigned flags)
  1011. {
  1012. return (flags & IOMAP_WRITE) && !(flags & IOMAP_DIRECT);
  1013. }
  1014. static int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
  1015. unsigned flags, struct iomap *iomap,
  1016. struct iomap *srcmap)
  1017. {
  1018. struct gfs2_inode *ip = GFS2_I(inode);
  1019. struct metapath mp = { .mp_aheight = 1, };
  1020. int ret;
  1021. if (gfs2_is_jdata(ip))
  1022. iomap->flags |= IOMAP_F_BUFFER_HEAD;
  1023. trace_gfs2_iomap_start(ip, pos, length, flags);
  1024. if (gfs2_iomap_need_write_lock(flags)) {
  1025. ret = gfs2_write_lock(inode);
  1026. if (ret)
  1027. goto out;
  1028. }
  1029. ret = gfs2_iomap_get(inode, pos, length, flags, iomap, &mp);
  1030. if (ret)
  1031. goto out_unlock;
  1032. switch(flags & (IOMAP_WRITE | IOMAP_ZERO)) {
  1033. case IOMAP_WRITE:
  1034. if (flags & IOMAP_DIRECT) {
  1035. /*
  1036. * Silently fall back to buffered I/O for stuffed files
  1037. * or if we've got a hole (see gfs2_file_direct_write).
  1038. */
  1039. if (iomap->type != IOMAP_MAPPED)
  1040. ret = -ENOTBLK;
  1041. goto out_unlock;
  1042. }
  1043. break;
  1044. case IOMAP_ZERO:
  1045. if (iomap->type == IOMAP_HOLE)
  1046. goto out_unlock;
  1047. break;
  1048. default:
  1049. goto out_unlock;
  1050. }
  1051. ret = gfs2_iomap_begin_write(inode, pos, length, flags, iomap, &mp);
  1052. out_unlock:
  1053. if (ret && gfs2_iomap_need_write_lock(flags))
  1054. gfs2_write_unlock(inode);
  1055. release_metapath(&mp);
  1056. out:
  1057. trace_gfs2_iomap_end(ip, iomap, ret);
  1058. return ret;
  1059. }
  1060. static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length,
  1061. ssize_t written, unsigned flags, struct iomap *iomap)
  1062. {
  1063. struct gfs2_inode *ip = GFS2_I(inode);
  1064. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1065. switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) {
  1066. case IOMAP_WRITE:
  1067. if (flags & IOMAP_DIRECT)
  1068. return 0;
  1069. break;
  1070. case IOMAP_ZERO:
  1071. if (iomap->type == IOMAP_HOLE)
  1072. return 0;
  1073. break;
  1074. default:
  1075. return 0;
  1076. }
  1077. if (!gfs2_is_stuffed(ip))
  1078. gfs2_ordered_add_inode(ip);
  1079. if (inode == sdp->sd_rindex)
  1080. adjust_fs_space(inode);
  1081. gfs2_inplace_release(ip);
  1082. if (ip->i_qadata && ip->i_qadata->qa_qd_num)
  1083. gfs2_quota_unlock(ip);
  1084. if (length != written && (iomap->flags & IOMAP_F_NEW)) {
  1085. /* Deallocate blocks that were just allocated. */
  1086. loff_t blockmask = i_blocksize(inode) - 1;
  1087. loff_t end = (pos + length) & ~blockmask;
  1088. pos = (pos + written + blockmask) & ~blockmask;
  1089. if (pos < end) {
  1090. truncate_pagecache_range(inode, pos, end - 1);
  1091. punch_hole(ip, pos, end - pos);
  1092. }
  1093. }
  1094. if (unlikely(!written))
  1095. goto out_unlock;
  1096. if (iomap->flags & IOMAP_F_SIZE_CHANGED)
  1097. mark_inode_dirty(inode);
  1098. set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
  1099. out_unlock:
  1100. if (gfs2_iomap_need_write_lock(flags))
  1101. gfs2_write_unlock(inode);
  1102. return 0;
  1103. }
  1104. const struct iomap_ops gfs2_iomap_ops = {
  1105. .iomap_begin = gfs2_iomap_begin,
  1106. .iomap_end = gfs2_iomap_end,
  1107. };
  1108. /**
  1109. * gfs2_block_map - Map one or more blocks of an inode to a disk block
  1110. * @inode: The inode
  1111. * @lblock: The logical block number
  1112. * @bh_map: The bh to be mapped
  1113. * @create: True if its ok to alloc blocks to satify the request
  1114. *
  1115. * The size of the requested mapping is defined in bh_map->b_size.
  1116. *
  1117. * Clears buffer_mapped(bh_map) and leaves bh_map->b_size unchanged
  1118. * when @lblock is not mapped. Sets buffer_mapped(bh_map) and
  1119. * bh_map->b_size to indicate the size of the mapping when @lblock and
  1120. * successive blocks are mapped, up to the requested size.
  1121. *
  1122. * Sets buffer_boundary() if a read of metadata will be required
  1123. * before the next block can be mapped. Sets buffer_new() if new
  1124. * blocks were allocated.
  1125. *
  1126. * Returns: errno
  1127. */
  1128. int gfs2_block_map(struct inode *inode, sector_t lblock,
  1129. struct buffer_head *bh_map, int create)
  1130. {
  1131. struct gfs2_inode *ip = GFS2_I(inode);
  1132. loff_t pos = (loff_t)lblock << inode->i_blkbits;
  1133. loff_t length = bh_map->b_size;
  1134. struct metapath mp = { .mp_aheight = 1, };
  1135. struct iomap iomap = { };
  1136. int flags = create ? IOMAP_WRITE : 0;
  1137. int ret;
  1138. clear_buffer_mapped(bh_map);
  1139. clear_buffer_new(bh_map);
  1140. clear_buffer_boundary(bh_map);
  1141. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  1142. ret = gfs2_iomap_get(inode, pos, length, flags, &iomap, &mp);
  1143. if (create && !ret && iomap.type == IOMAP_HOLE)
  1144. ret = gfs2_iomap_alloc(inode, &iomap, &mp);
  1145. release_metapath(&mp);
  1146. if (ret)
  1147. goto out;
  1148. if (iomap.length > bh_map->b_size) {
  1149. iomap.length = bh_map->b_size;
  1150. iomap.flags &= ~IOMAP_F_GFS2_BOUNDARY;
  1151. }
  1152. if (iomap.addr != IOMAP_NULL_ADDR)
  1153. map_bh(bh_map, inode->i_sb, iomap.addr >> inode->i_blkbits);
  1154. bh_map->b_size = iomap.length;
  1155. if (iomap.flags & IOMAP_F_GFS2_BOUNDARY)
  1156. set_buffer_boundary(bh_map);
  1157. if (iomap.flags & IOMAP_F_NEW)
  1158. set_buffer_new(bh_map);
  1159. out:
  1160. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  1161. return ret;
  1162. }
  1163. /*
  1164. * Deprecated: do not use in new code
  1165. */
  1166. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  1167. {
  1168. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  1169. int ret;
  1170. int create = *new;
  1171. BUG_ON(!extlen);
  1172. BUG_ON(!dblock);
  1173. BUG_ON(!new);
  1174. bh.b_size = BIT(inode->i_blkbits + (create ? 0 : 5));
  1175. ret = gfs2_block_map(inode, lblock, &bh, create);
  1176. *extlen = bh.b_size >> inode->i_blkbits;
  1177. *dblock = bh.b_blocknr;
  1178. if (buffer_new(&bh))
  1179. *new = 1;
  1180. else
  1181. *new = 0;
  1182. return ret;
  1183. }
  1184. /*
  1185. * NOTE: Never call gfs2_block_zero_range with an open transaction because it
  1186. * uses iomap write to perform its actions, which begin their own transactions
  1187. * (iomap_begin, page_prepare, etc.)
  1188. */
  1189. static int gfs2_block_zero_range(struct inode *inode, loff_t from,
  1190. unsigned int length)
  1191. {
  1192. BUG_ON(current->journal_info);
  1193. return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops);
  1194. }
  1195. #define GFS2_JTRUNC_REVOKES 8192
  1196. /**
  1197. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  1198. * @inode: The inode being truncated
  1199. * @oldsize: The original (larger) size
  1200. * @newsize: The new smaller size
  1201. *
  1202. * With jdata files, we have to journal a revoke for each block which is
  1203. * truncated. As a result, we need to split this into separate transactions
  1204. * if the number of pages being truncated gets too large.
  1205. */
  1206. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  1207. {
  1208. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1209. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  1210. u64 chunk;
  1211. int error;
  1212. while (oldsize != newsize) {
  1213. struct gfs2_trans *tr;
  1214. unsigned int offs;
  1215. chunk = oldsize - newsize;
  1216. if (chunk > max_chunk)
  1217. chunk = max_chunk;
  1218. offs = oldsize & ~PAGE_MASK;
  1219. if (offs && chunk > PAGE_SIZE)
  1220. chunk = offs + ((chunk - offs) & PAGE_MASK);
  1221. truncate_pagecache(inode, oldsize - chunk);
  1222. oldsize -= chunk;
  1223. tr = current->journal_info;
  1224. if (!test_bit(TR_TOUCHED, &tr->tr_flags))
  1225. continue;
  1226. gfs2_trans_end(sdp);
  1227. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  1228. if (error)
  1229. return error;
  1230. }
  1231. return 0;
  1232. }
  1233. static int trunc_start(struct inode *inode, u64 newsize)
  1234. {
  1235. struct gfs2_inode *ip = GFS2_I(inode);
  1236. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1237. struct buffer_head *dibh = NULL;
  1238. int journaled = gfs2_is_jdata(ip);
  1239. u64 oldsize = inode->i_size;
  1240. int error;
  1241. if (!gfs2_is_stuffed(ip)) {
  1242. unsigned int blocksize = i_blocksize(inode);
  1243. unsigned int offs = newsize & (blocksize - 1);
  1244. if (offs) {
  1245. error = gfs2_block_zero_range(inode, newsize,
  1246. blocksize - offs);
  1247. if (error)
  1248. return error;
  1249. }
  1250. }
  1251. if (journaled)
  1252. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  1253. else
  1254. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1255. if (error)
  1256. return error;
  1257. error = gfs2_meta_inode_buffer(ip, &dibh);
  1258. if (error)
  1259. goto out;
  1260. gfs2_trans_add_meta(ip->i_gl, dibh);
  1261. if (gfs2_is_stuffed(ip))
  1262. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  1263. else
  1264. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  1265. i_size_write(inode, newsize);
  1266. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1267. gfs2_dinode_out(ip, dibh->b_data);
  1268. if (journaled)
  1269. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  1270. else
  1271. truncate_pagecache(inode, newsize);
  1272. out:
  1273. brelse(dibh);
  1274. if (current->journal_info)
  1275. gfs2_trans_end(sdp);
  1276. return error;
  1277. }
  1278. int gfs2_iomap_get_alloc(struct inode *inode, loff_t pos, loff_t length,
  1279. struct iomap *iomap)
  1280. {
  1281. struct metapath mp = { .mp_aheight = 1, };
  1282. int ret;
  1283. ret = gfs2_iomap_get(inode, pos, length, IOMAP_WRITE, iomap, &mp);
  1284. if (!ret && iomap->type == IOMAP_HOLE)
  1285. ret = gfs2_iomap_alloc(inode, iomap, &mp);
  1286. release_metapath(&mp);
  1287. return ret;
  1288. }
  1289. /**
  1290. * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
  1291. * @ip: inode
  1292. * @rg_gh: holder of resource group glock
  1293. * @bh: buffer head to sweep
  1294. * @start: starting point in bh
  1295. * @end: end point in bh
  1296. * @meta: true if bh points to metadata (rather than data)
  1297. * @btotal: place to keep count of total blocks freed
  1298. *
  1299. * We sweep a metadata buffer (provided by the metapath) for blocks we need to
  1300. * free, and free them all. However, we do it one rgrp at a time. If this
  1301. * block has references to multiple rgrps, we break it into individual
  1302. * transactions. This allows other processes to use the rgrps while we're
  1303. * focused on a single one, for better concurrency / performance.
  1304. * At every transaction boundary, we rewrite the inode into the journal.
  1305. * That way the bitmaps are kept consistent with the inode and we can recover
  1306. * if we're interrupted by power-outages.
  1307. *
  1308. * Returns: 0, or return code if an error occurred.
  1309. * *btotal has the total number of blocks freed
  1310. */
  1311. static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
  1312. struct buffer_head *bh, __be64 *start, __be64 *end,
  1313. bool meta, u32 *btotal)
  1314. {
  1315. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1316. struct gfs2_rgrpd *rgd;
  1317. struct gfs2_trans *tr;
  1318. __be64 *p;
  1319. int blks_outside_rgrp;
  1320. u64 bn, bstart, isize_blks;
  1321. s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */
  1322. int ret = 0;
  1323. bool buf_in_tr = false; /* buffer was added to transaction */
  1324. more_rgrps:
  1325. rgd = NULL;
  1326. if (gfs2_holder_initialized(rd_gh)) {
  1327. rgd = gfs2_glock2rgrp(rd_gh->gh_gl);
  1328. gfs2_assert_withdraw(sdp,
  1329. gfs2_glock_is_locked_by_me(rd_gh->gh_gl));
  1330. }
  1331. blks_outside_rgrp = 0;
  1332. bstart = 0;
  1333. blen = 0;
  1334. for (p = start; p < end; p++) {
  1335. if (!*p)
  1336. continue;
  1337. bn = be64_to_cpu(*p);
  1338. if (rgd) {
  1339. if (!rgrp_contains_block(rgd, bn)) {
  1340. blks_outside_rgrp++;
  1341. continue;
  1342. }
  1343. } else {
  1344. rgd = gfs2_blk2rgrpd(sdp, bn, true);
  1345. if (unlikely(!rgd)) {
  1346. ret = -EIO;
  1347. goto out;
  1348. }
  1349. ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  1350. 0, rd_gh);
  1351. if (ret)
  1352. goto out;
  1353. /* Must be done with the rgrp glock held: */
  1354. if (gfs2_rs_active(&ip->i_res) &&
  1355. rgd == ip->i_res.rs_rbm.rgd)
  1356. gfs2_rs_deltree(&ip->i_res);
  1357. }
  1358. /* The size of our transactions will be unknown until we
  1359. actually process all the metadata blocks that relate to
  1360. the rgrp. So we estimate. We know it can't be more than
  1361. the dinode's i_blocks and we don't want to exceed the
  1362. journal flush threshold, sd_log_thresh2. */
  1363. if (current->journal_info == NULL) {
  1364. unsigned int jblocks_rqsted, revokes;
  1365. jblocks_rqsted = rgd->rd_length + RES_DINODE +
  1366. RES_INDIRECT;
  1367. isize_blks = gfs2_get_inode_blocks(&ip->i_inode);
  1368. if (isize_blks > atomic_read(&sdp->sd_log_thresh2))
  1369. jblocks_rqsted +=
  1370. atomic_read(&sdp->sd_log_thresh2);
  1371. else
  1372. jblocks_rqsted += isize_blks;
  1373. revokes = jblocks_rqsted;
  1374. if (meta)
  1375. revokes += end - start;
  1376. else if (ip->i_depth)
  1377. revokes += sdp->sd_inptrs;
  1378. ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
  1379. if (ret)
  1380. goto out_unlock;
  1381. down_write(&ip->i_rw_mutex);
  1382. }
  1383. /* check if we will exceed the transaction blocks requested */
  1384. tr = current->journal_info;
  1385. if (tr->tr_num_buf_new + RES_STATFS +
  1386. RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) {
  1387. /* We set blks_outside_rgrp to ensure the loop will
  1388. be repeated for the same rgrp, but with a new
  1389. transaction. */
  1390. blks_outside_rgrp++;
  1391. /* This next part is tricky. If the buffer was added
  1392. to the transaction, we've already set some block
  1393. pointers to 0, so we better follow through and free
  1394. them, or we will introduce corruption (so break).
  1395. This may be impossible, or at least rare, but I
  1396. decided to cover the case regardless.
  1397. If the buffer was not added to the transaction
  1398. (this call), doing so would exceed our transaction
  1399. size, so we need to end the transaction and start a
  1400. new one (so goto). */
  1401. if (buf_in_tr)
  1402. break;
  1403. goto out_unlock;
  1404. }
  1405. gfs2_trans_add_meta(ip->i_gl, bh);
  1406. buf_in_tr = true;
  1407. *p = 0;
  1408. if (bstart + blen == bn) {
  1409. blen++;
  1410. continue;
  1411. }
  1412. if (bstart) {
  1413. __gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta);
  1414. (*btotal) += blen;
  1415. gfs2_add_inode_blocks(&ip->i_inode, -blen);
  1416. }
  1417. bstart = bn;
  1418. blen = 1;
  1419. }
  1420. if (bstart) {
  1421. __gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta);
  1422. (*btotal) += blen;
  1423. gfs2_add_inode_blocks(&ip->i_inode, -blen);
  1424. }
  1425. out_unlock:
  1426. if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks
  1427. outside the rgrp we just processed,
  1428. do it all over again. */
  1429. if (current->journal_info) {
  1430. struct buffer_head *dibh;
  1431. ret = gfs2_meta_inode_buffer(ip, &dibh);
  1432. if (ret)
  1433. goto out;
  1434. /* Every transaction boundary, we rewrite the dinode
  1435. to keep its di_blocks current in case of failure. */
  1436. ip->i_inode.i_mtime = ip->i_inode.i_ctime =
  1437. current_time(&ip->i_inode);
  1438. gfs2_trans_add_meta(ip->i_gl, dibh);
  1439. gfs2_dinode_out(ip, dibh->b_data);
  1440. brelse(dibh);
  1441. up_write(&ip->i_rw_mutex);
  1442. gfs2_trans_end(sdp);
  1443. buf_in_tr = false;
  1444. }
  1445. gfs2_glock_dq_uninit(rd_gh);
  1446. cond_resched();
  1447. goto more_rgrps;
  1448. }
  1449. out:
  1450. return ret;
  1451. }
  1452. static bool mp_eq_to_hgt(struct metapath *mp, __u16 *list, unsigned int h)
  1453. {
  1454. if (memcmp(mp->mp_list, list, h * sizeof(mp->mp_list[0])))
  1455. return false;
  1456. return true;
  1457. }
  1458. /**
  1459. * find_nonnull_ptr - find a non-null pointer given a metapath and height
  1460. * @mp: starting metapath
  1461. * @h: desired height to search
  1462. *
  1463. * Assumes the metapath is valid (with buffers) out to height h.
  1464. * Returns: true if a non-null pointer was found in the metapath buffer
  1465. * false if all remaining pointers are NULL in the buffer
  1466. */
  1467. static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp,
  1468. unsigned int h,
  1469. __u16 *end_list, unsigned int end_aligned)
  1470. {
  1471. struct buffer_head *bh = mp->mp_bh[h];
  1472. __be64 *first, *ptr, *end;
  1473. first = metaptr1(h, mp);
  1474. ptr = first + mp->mp_list[h];
  1475. end = (__be64 *)(bh->b_data + bh->b_size);
  1476. if (end_list && mp_eq_to_hgt(mp, end_list, h)) {
  1477. bool keep_end = h < end_aligned;
  1478. end = first + end_list[h] + keep_end;
  1479. }
  1480. while (ptr < end) {
  1481. if (*ptr) { /* if we have a non-null pointer */
  1482. mp->mp_list[h] = ptr - first;
  1483. h++;
  1484. if (h < GFS2_MAX_META_HEIGHT)
  1485. mp->mp_list[h] = 0;
  1486. return true;
  1487. }
  1488. ptr++;
  1489. }
  1490. return false;
  1491. }
  1492. enum dealloc_states {
  1493. DEALLOC_MP_FULL = 0, /* Strip a metapath with all buffers read in */
  1494. DEALLOC_MP_LOWER = 1, /* lower the metapath strip height */
  1495. DEALLOC_FILL_MP = 2, /* Fill in the metapath to the given height. */
  1496. DEALLOC_DONE = 3, /* process complete */
  1497. };
  1498. static inline void
  1499. metapointer_range(struct metapath *mp, int height,
  1500. __u16 *start_list, unsigned int start_aligned,
  1501. __u16 *end_list, unsigned int end_aligned,
  1502. __be64 **start, __be64 **end)
  1503. {
  1504. struct buffer_head *bh = mp->mp_bh[height];
  1505. __be64 *first;
  1506. first = metaptr1(height, mp);
  1507. *start = first;
  1508. if (mp_eq_to_hgt(mp, start_list, height)) {
  1509. bool keep_start = height < start_aligned;
  1510. *start = first + start_list[height] + keep_start;
  1511. }
  1512. *end = (__be64 *)(bh->b_data + bh->b_size);
  1513. if (end_list && mp_eq_to_hgt(mp, end_list, height)) {
  1514. bool keep_end = height < end_aligned;
  1515. *end = first + end_list[height] + keep_end;
  1516. }
  1517. }
  1518. static inline bool walk_done(struct gfs2_sbd *sdp,
  1519. struct metapath *mp, int height,
  1520. __u16 *end_list, unsigned int end_aligned)
  1521. {
  1522. __u16 end;
  1523. if (end_list) {
  1524. bool keep_end = height < end_aligned;
  1525. if (!mp_eq_to_hgt(mp, end_list, height))
  1526. return false;
  1527. end = end_list[height] + keep_end;
  1528. } else
  1529. end = (height > 0) ? sdp->sd_inptrs : sdp->sd_diptrs;
  1530. return mp->mp_list[height] >= end;
  1531. }
  1532. /**
  1533. * punch_hole - deallocate blocks in a file
  1534. * @ip: inode to truncate
  1535. * @offset: the start of the hole
  1536. * @length: the size of the hole (or 0 for truncate)
  1537. *
  1538. * Punch a hole into a file or truncate a file at a given position. This
  1539. * function operates in whole blocks (@offset and @length are rounded
  1540. * accordingly); partially filled blocks must be cleared otherwise.
  1541. *
  1542. * This function works from the bottom up, and from the right to the left. In
  1543. * other words, it strips off the highest layer (data) before stripping any of
  1544. * the metadata. Doing it this way is best in case the operation is interrupted
  1545. * by power failure, etc. The dinode is rewritten in every transaction to
  1546. * guarantee integrity.
  1547. */
  1548. static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length)
  1549. {
  1550. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1551. u64 maxsize = sdp->sd_heightsize[ip->i_height];
  1552. struct metapath mp = {};
  1553. struct buffer_head *dibh, *bh;
  1554. struct gfs2_holder rd_gh;
  1555. unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift;
  1556. u64 lblock = (offset + (1 << bsize_shift) - 1) >> bsize_shift;
  1557. __u16 start_list[GFS2_MAX_META_HEIGHT];
  1558. __u16 __end_list[GFS2_MAX_META_HEIGHT], *end_list = NULL;
  1559. unsigned int start_aligned, end_aligned;
  1560. unsigned int strip_h = ip->i_height - 1;
  1561. u32 btotal = 0;
  1562. int ret, state;
  1563. int mp_h; /* metapath buffers are read in to this height */
  1564. u64 prev_bnr = 0;
  1565. __be64 *start, *end;
  1566. if (offset >= maxsize) {
  1567. /*
  1568. * The starting point lies beyond the allocated meta-data;
  1569. * there are no blocks do deallocate.
  1570. */
  1571. return 0;
  1572. }
  1573. /*
  1574. * The start position of the hole is defined by lblock, start_list, and
  1575. * start_aligned. The end position of the hole is defined by lend,
  1576. * end_list, and end_aligned.
  1577. *
  1578. * start_aligned and end_aligned define down to which height the start
  1579. * and end positions are aligned to the metadata tree (i.e., the
  1580. * position is a multiple of the metadata granularity at the height
  1581. * above). This determines at which heights additional meta pointers
  1582. * needs to be preserved for the remaining data.
  1583. */
  1584. if (length) {
  1585. u64 end_offset = offset + length;
  1586. u64 lend;
  1587. /*
  1588. * Clip the end at the maximum file size for the given height:
  1589. * that's how far the metadata goes; files bigger than that
  1590. * will have additional layers of indirection.
  1591. */
  1592. if (end_offset > maxsize)
  1593. end_offset = maxsize;
  1594. lend = end_offset >> bsize_shift;
  1595. if (lblock >= lend)
  1596. return 0;
  1597. find_metapath(sdp, lend, &mp, ip->i_height);
  1598. end_list = __end_list;
  1599. memcpy(end_list, mp.mp_list, sizeof(mp.mp_list));
  1600. for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) {
  1601. if (end_list[mp_h])
  1602. break;
  1603. }
  1604. end_aligned = mp_h;
  1605. }
  1606. find_metapath(sdp, lblock, &mp, ip->i_height);
  1607. memcpy(start_list, mp.mp_list, sizeof(start_list));
  1608. for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) {
  1609. if (start_list[mp_h])
  1610. break;
  1611. }
  1612. start_aligned = mp_h;
  1613. ret = gfs2_meta_inode_buffer(ip, &dibh);
  1614. if (ret)
  1615. return ret;
  1616. mp.mp_bh[0] = dibh;
  1617. ret = lookup_metapath(ip, &mp);
  1618. if (ret)
  1619. goto out_metapath;
  1620. /* issue read-ahead on metadata */
  1621. for (mp_h = 0; mp_h < mp.mp_aheight - 1; mp_h++) {
  1622. metapointer_range(&mp, mp_h, start_list, start_aligned,
  1623. end_list, end_aligned, &start, &end);
  1624. gfs2_metapath_ra(ip->i_gl, start, end);
  1625. }
  1626. if (mp.mp_aheight == ip->i_height)
  1627. state = DEALLOC_MP_FULL; /* We have a complete metapath */
  1628. else
  1629. state = DEALLOC_FILL_MP; /* deal with partial metapath */
  1630. ret = gfs2_rindex_update(sdp);
  1631. if (ret)
  1632. goto out_metapath;
  1633. ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1634. if (ret)
  1635. goto out_metapath;
  1636. gfs2_holder_mark_uninitialized(&rd_gh);
  1637. mp_h = strip_h;
  1638. while (state != DEALLOC_DONE) {
  1639. switch (state) {
  1640. /* Truncate a full metapath at the given strip height.
  1641. * Note that strip_h == mp_h in order to be in this state. */
  1642. case DEALLOC_MP_FULL:
  1643. bh = mp.mp_bh[mp_h];
  1644. gfs2_assert_withdraw(sdp, bh);
  1645. if (gfs2_assert_withdraw(sdp,
  1646. prev_bnr != bh->b_blocknr)) {
  1647. fs_emerg(sdp, "inode %llu, block:%llu, i_h:%u,"
  1648. "s_h:%u, mp_h:%u\n",
  1649. (unsigned long long)ip->i_no_addr,
  1650. prev_bnr, ip->i_height, strip_h, mp_h);
  1651. }
  1652. prev_bnr = bh->b_blocknr;
  1653. if (gfs2_metatype_check(sdp, bh,
  1654. (mp_h ? GFS2_METATYPE_IN :
  1655. GFS2_METATYPE_DI))) {
  1656. ret = -EIO;
  1657. goto out;
  1658. }
  1659. /*
  1660. * Below, passing end_aligned as 0 gives us the
  1661. * metapointer range excluding the end point: the end
  1662. * point is the first metapath we must not deallocate!
  1663. */
  1664. metapointer_range(&mp, mp_h, start_list, start_aligned,
  1665. end_list, 0 /* end_aligned */,
  1666. &start, &end);
  1667. ret = sweep_bh_for_rgrps(ip, &rd_gh, mp.mp_bh[mp_h],
  1668. start, end,
  1669. mp_h != ip->i_height - 1,
  1670. &btotal);
  1671. /* If we hit an error or just swept dinode buffer,
  1672. just exit. */
  1673. if (ret || !mp_h) {
  1674. state = DEALLOC_DONE;
  1675. break;
  1676. }
  1677. state = DEALLOC_MP_LOWER;
  1678. break;
  1679. /* lower the metapath strip height */
  1680. case DEALLOC_MP_LOWER:
  1681. /* We're done with the current buffer, so release it,
  1682. unless it's the dinode buffer. Then back up to the
  1683. previous pointer. */
  1684. if (mp_h) {
  1685. brelse(mp.mp_bh[mp_h]);
  1686. mp.mp_bh[mp_h] = NULL;
  1687. }
  1688. /* If we can't get any lower in height, we've stripped
  1689. off all we can. Next step is to back up and start
  1690. stripping the previous level of metadata. */
  1691. if (mp_h == 0) {
  1692. strip_h--;
  1693. memcpy(mp.mp_list, start_list, sizeof(start_list));
  1694. mp_h = strip_h;
  1695. state = DEALLOC_FILL_MP;
  1696. break;
  1697. }
  1698. mp.mp_list[mp_h] = 0;
  1699. mp_h--; /* search one metadata height down */
  1700. mp.mp_list[mp_h]++;
  1701. if (walk_done(sdp, &mp, mp_h, end_list, end_aligned))
  1702. break;
  1703. /* Here we've found a part of the metapath that is not
  1704. * allocated. We need to search at that height for the
  1705. * next non-null pointer. */
  1706. if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned)) {
  1707. state = DEALLOC_FILL_MP;
  1708. mp_h++;
  1709. }
  1710. /* No more non-null pointers at this height. Back up
  1711. to the previous height and try again. */
  1712. break; /* loop around in the same state */
  1713. /* Fill the metapath with buffers to the given height. */
  1714. case DEALLOC_FILL_MP:
  1715. /* Fill the buffers out to the current height. */
  1716. ret = fillup_metapath(ip, &mp, mp_h);
  1717. if (ret < 0)
  1718. goto out;
  1719. /* On the first pass, issue read-ahead on metadata. */
  1720. if (mp.mp_aheight > 1 && strip_h == ip->i_height - 1) {
  1721. unsigned int height = mp.mp_aheight - 1;
  1722. /* No read-ahead for data blocks. */
  1723. if (mp.mp_aheight - 1 == strip_h)
  1724. height--;
  1725. for (; height >= mp.mp_aheight - ret; height--) {
  1726. metapointer_range(&mp, height,
  1727. start_list, start_aligned,
  1728. end_list, end_aligned,
  1729. &start, &end);
  1730. gfs2_metapath_ra(ip->i_gl, start, end);
  1731. }
  1732. }
  1733. /* If buffers found for the entire strip height */
  1734. if (mp.mp_aheight - 1 == strip_h) {
  1735. state = DEALLOC_MP_FULL;
  1736. break;
  1737. }
  1738. if (mp.mp_aheight < ip->i_height) /* We have a partial height */
  1739. mp_h = mp.mp_aheight - 1;
  1740. /* If we find a non-null block pointer, crawl a bit
  1741. higher up in the metapath and try again, otherwise
  1742. we need to look lower for a new starting point. */
  1743. if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned))
  1744. mp_h++;
  1745. else
  1746. state = DEALLOC_MP_LOWER;
  1747. break;
  1748. }
  1749. }
  1750. if (btotal) {
  1751. if (current->journal_info == NULL) {
  1752. ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS +
  1753. RES_QUOTA, 0);
  1754. if (ret)
  1755. goto out;
  1756. down_write(&ip->i_rw_mutex);
  1757. }
  1758. gfs2_statfs_change(sdp, 0, +btotal, 0);
  1759. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  1760. ip->i_inode.i_gid);
  1761. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1762. gfs2_trans_add_meta(ip->i_gl, dibh);
  1763. gfs2_dinode_out(ip, dibh->b_data);
  1764. up_write(&ip->i_rw_mutex);
  1765. gfs2_trans_end(sdp);
  1766. }
  1767. out:
  1768. if (gfs2_holder_initialized(&rd_gh))
  1769. gfs2_glock_dq_uninit(&rd_gh);
  1770. if (current->journal_info) {
  1771. up_write(&ip->i_rw_mutex);
  1772. gfs2_trans_end(sdp);
  1773. cond_resched();
  1774. }
  1775. gfs2_quota_unhold(ip);
  1776. out_metapath:
  1777. release_metapath(&mp);
  1778. return ret;
  1779. }
  1780. static int trunc_end(struct gfs2_inode *ip)
  1781. {
  1782. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1783. struct buffer_head *dibh;
  1784. int error;
  1785. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1786. if (error)
  1787. return error;
  1788. down_write(&ip->i_rw_mutex);
  1789. error = gfs2_meta_inode_buffer(ip, &dibh);
  1790. if (error)
  1791. goto out;
  1792. if (!i_size_read(&ip->i_inode)) {
  1793. ip->i_height = 0;
  1794. ip->i_goal = ip->i_no_addr;
  1795. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  1796. gfs2_ordered_del_inode(ip);
  1797. }
  1798. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1799. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  1800. gfs2_trans_add_meta(ip->i_gl, dibh);
  1801. gfs2_dinode_out(ip, dibh->b_data);
  1802. brelse(dibh);
  1803. out:
  1804. up_write(&ip->i_rw_mutex);
  1805. gfs2_trans_end(sdp);
  1806. return error;
  1807. }
  1808. /**
  1809. * do_shrink - make a file smaller
  1810. * @inode: the inode
  1811. * @newsize: the size to make the file
  1812. *
  1813. * Called with an exclusive lock on @inode. The @size must
  1814. * be equal to or smaller than the current inode size.
  1815. *
  1816. * Returns: errno
  1817. */
  1818. static int do_shrink(struct inode *inode, u64 newsize)
  1819. {
  1820. struct gfs2_inode *ip = GFS2_I(inode);
  1821. int error;
  1822. error = trunc_start(inode, newsize);
  1823. if (error < 0)
  1824. return error;
  1825. if (gfs2_is_stuffed(ip))
  1826. return 0;
  1827. error = punch_hole(ip, newsize, 0);
  1828. if (error == 0)
  1829. error = trunc_end(ip);
  1830. return error;
  1831. }
  1832. void gfs2_trim_blocks(struct inode *inode)
  1833. {
  1834. int ret;
  1835. ret = do_shrink(inode, inode->i_size);
  1836. WARN_ON(ret != 0);
  1837. }
  1838. /**
  1839. * do_grow - Touch and update inode size
  1840. * @inode: The inode
  1841. * @size: The new size
  1842. *
  1843. * This function updates the timestamps on the inode and
  1844. * may also increase the size of the inode. This function
  1845. * must not be called with @size any smaller than the current
  1846. * inode size.
  1847. *
  1848. * Although it is not strictly required to unstuff files here,
  1849. * earlier versions of GFS2 have a bug in the stuffed file reading
  1850. * code which will result in a buffer overrun if the size is larger
  1851. * than the max stuffed file size. In order to prevent this from
  1852. * occurring, such files are unstuffed, but in other cases we can
  1853. * just update the inode size directly.
  1854. *
  1855. * Returns: 0 on success, or -ve on error
  1856. */
  1857. static int do_grow(struct inode *inode, u64 size)
  1858. {
  1859. struct gfs2_inode *ip = GFS2_I(inode);
  1860. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1861. struct gfs2_alloc_parms ap = { .target = 1, };
  1862. struct buffer_head *dibh;
  1863. int error;
  1864. int unstuff = 0;
  1865. if (gfs2_is_stuffed(ip) && size > gfs2_max_stuffed_size(ip)) {
  1866. error = gfs2_quota_lock_check(ip, &ap);
  1867. if (error)
  1868. return error;
  1869. error = gfs2_inplace_reserve(ip, &ap);
  1870. if (error)
  1871. goto do_grow_qunlock;
  1872. unstuff = 1;
  1873. }
  1874. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
  1875. (unstuff &&
  1876. gfs2_is_jdata(ip) ? RES_JDATA : 0) +
  1877. (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
  1878. 0 : RES_QUOTA), 0);
  1879. if (error)
  1880. goto do_grow_release;
  1881. if (unstuff) {
  1882. error = gfs2_unstuff_dinode(ip, NULL);
  1883. if (error)
  1884. goto do_end_trans;
  1885. }
  1886. error = gfs2_meta_inode_buffer(ip, &dibh);
  1887. if (error)
  1888. goto do_end_trans;
  1889. truncate_setsize(inode, size);
  1890. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1891. gfs2_trans_add_meta(ip->i_gl, dibh);
  1892. gfs2_dinode_out(ip, dibh->b_data);
  1893. brelse(dibh);
  1894. do_end_trans:
  1895. gfs2_trans_end(sdp);
  1896. do_grow_release:
  1897. if (unstuff) {
  1898. gfs2_inplace_release(ip);
  1899. do_grow_qunlock:
  1900. gfs2_quota_unlock(ip);
  1901. }
  1902. return error;
  1903. }
  1904. /**
  1905. * gfs2_setattr_size - make a file a given size
  1906. * @inode: the inode
  1907. * @newsize: the size to make the file
  1908. *
  1909. * The file size can grow, shrink, or stay the same size. This
  1910. * is called holding i_rwsem and an exclusive glock on the inode
  1911. * in question.
  1912. *
  1913. * Returns: errno
  1914. */
  1915. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1916. {
  1917. struct gfs2_inode *ip = GFS2_I(inode);
  1918. int ret;
  1919. BUG_ON(!S_ISREG(inode->i_mode));
  1920. ret = inode_newsize_ok(inode, newsize);
  1921. if (ret)
  1922. return ret;
  1923. inode_dio_wait(inode);
  1924. ret = gfs2_qa_get(ip);
  1925. if (ret)
  1926. goto out;
  1927. if (newsize >= inode->i_size) {
  1928. ret = do_grow(inode, newsize);
  1929. goto out;
  1930. }
  1931. ret = do_shrink(inode, newsize);
  1932. out:
  1933. gfs2_rs_delete(ip);
  1934. gfs2_qa_put(ip);
  1935. return ret;
  1936. }
  1937. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1938. {
  1939. int error;
  1940. error = punch_hole(ip, i_size_read(&ip->i_inode), 0);
  1941. if (!error)
  1942. error = trunc_end(ip);
  1943. return error;
  1944. }
  1945. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1946. {
  1947. return punch_hole(ip, 0, 0);
  1948. }
  1949. /**
  1950. * gfs2_free_journal_extents - Free cached journal bmap info
  1951. * @jd: The journal
  1952. *
  1953. */
  1954. void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
  1955. {
  1956. struct gfs2_journal_extent *jext;
  1957. while(!list_empty(&jd->extent_list)) {
  1958. jext = list_first_entry(&jd->extent_list, struct gfs2_journal_extent, list);
  1959. list_del(&jext->list);
  1960. kfree(jext);
  1961. }
  1962. }
  1963. /**
  1964. * gfs2_add_jextent - Add or merge a new extent to extent cache
  1965. * @jd: The journal descriptor
  1966. * @lblock: The logical block at start of new extent
  1967. * @dblock: The physical block at start of new extent
  1968. * @blocks: Size of extent in fs blocks
  1969. *
  1970. * Returns: 0 on success or -ENOMEM
  1971. */
  1972. static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
  1973. {
  1974. struct gfs2_journal_extent *jext;
  1975. if (!list_empty(&jd->extent_list)) {
  1976. jext = list_last_entry(&jd->extent_list, struct gfs2_journal_extent, list);
  1977. if ((jext->dblock + jext->blocks) == dblock) {
  1978. jext->blocks += blocks;
  1979. return 0;
  1980. }
  1981. }
  1982. jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
  1983. if (jext == NULL)
  1984. return -ENOMEM;
  1985. jext->dblock = dblock;
  1986. jext->lblock = lblock;
  1987. jext->blocks = blocks;
  1988. list_add_tail(&jext->list, &jd->extent_list);
  1989. jd->nr_extents++;
  1990. return 0;
  1991. }
  1992. /**
  1993. * gfs2_map_journal_extents - Cache journal bmap info
  1994. * @sdp: The super block
  1995. * @jd: The journal to map
  1996. *
  1997. * Create a reusable "extent" mapping from all logical
  1998. * blocks to all physical blocks for the given journal. This will save
  1999. * us time when writing journal blocks. Most journals will have only one
  2000. * extent that maps all their logical blocks. That's because gfs2.mkfs
  2001. * arranges the journal blocks sequentially to maximize performance.
  2002. * So the extent would map the first block for the entire file length.
  2003. * However, gfs2_jadd can happen while file activity is happening, so
  2004. * those journals may not be sequential. Less likely is the case where
  2005. * the users created their own journals by mounting the metafs and
  2006. * laying it out. But it's still possible. These journals might have
  2007. * several extents.
  2008. *
  2009. * Returns: 0 on success, or error on failure
  2010. */
  2011. int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
  2012. {
  2013. u64 lblock = 0;
  2014. u64 lblock_stop;
  2015. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  2016. struct buffer_head bh;
  2017. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  2018. u64 size;
  2019. int rc;
  2020. ktime_t start, end;
  2021. start = ktime_get();
  2022. lblock_stop = i_size_read(jd->jd_inode) >> shift;
  2023. size = (lblock_stop - lblock) << shift;
  2024. jd->nr_extents = 0;
  2025. WARN_ON(!list_empty(&jd->extent_list));
  2026. do {
  2027. bh.b_state = 0;
  2028. bh.b_blocknr = 0;
  2029. bh.b_size = size;
  2030. rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
  2031. if (rc || !buffer_mapped(&bh))
  2032. goto fail;
  2033. rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
  2034. if (rc)
  2035. goto fail;
  2036. size -= bh.b_size;
  2037. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  2038. } while(size > 0);
  2039. end = ktime_get();
  2040. fs_info(sdp, "journal %d mapped with %u extents in %lldms\n", jd->jd_jid,
  2041. jd->nr_extents, ktime_ms_delta(end, start));
  2042. return 0;
  2043. fail:
  2044. fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
  2045. rc, jd->jd_jid,
  2046. (unsigned long long)(i_size_read(jd->jd_inode) - size),
  2047. jd->nr_extents);
  2048. fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
  2049. rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
  2050. bh.b_state, (unsigned long long)bh.b_size);
  2051. gfs2_free_journal_extents(jd);
  2052. return rc;
  2053. }
  2054. /**
  2055. * gfs2_write_alloc_required - figure out if a write will require an allocation
  2056. * @ip: the file being written to
  2057. * @offset: the offset to write to
  2058. * @len: the number of bytes being written
  2059. *
  2060. * Returns: 1 if an alloc is required, 0 otherwise
  2061. */
  2062. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  2063. unsigned int len)
  2064. {
  2065. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  2066. struct buffer_head bh;
  2067. unsigned int shift;
  2068. u64 lblock, lblock_stop, size;
  2069. u64 end_of_file;
  2070. if (!len)
  2071. return 0;
  2072. if (gfs2_is_stuffed(ip)) {
  2073. if (offset + len > gfs2_max_stuffed_size(ip))
  2074. return 1;
  2075. return 0;
  2076. }
  2077. shift = sdp->sd_sb.sb_bsize_shift;
  2078. BUG_ON(gfs2_is_dir(ip));
  2079. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  2080. lblock = offset >> shift;
  2081. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  2082. if (lblock_stop > end_of_file && ip != GFS2_I(sdp->sd_rindex))
  2083. return 1;
  2084. size = (lblock_stop - lblock) << shift;
  2085. do {
  2086. bh.b_state = 0;
  2087. bh.b_size = size;
  2088. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  2089. if (!buffer_mapped(&bh))
  2090. return 1;
  2091. size -= bh.b_size;
  2092. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  2093. } while(size > 0);
  2094. return 0;
  2095. }
  2096. static int stuffed_zero_range(struct inode *inode, loff_t offset, loff_t length)
  2097. {
  2098. struct gfs2_inode *ip = GFS2_I(inode);
  2099. struct buffer_head *dibh;
  2100. int error;
  2101. if (offset >= inode->i_size)
  2102. return 0;
  2103. if (offset + length > inode->i_size)
  2104. length = inode->i_size - offset;
  2105. error = gfs2_meta_inode_buffer(ip, &dibh);
  2106. if (error)
  2107. return error;
  2108. gfs2_trans_add_meta(ip->i_gl, dibh);
  2109. memset(dibh->b_data + sizeof(struct gfs2_dinode) + offset, 0,
  2110. length);
  2111. brelse(dibh);
  2112. return 0;
  2113. }
  2114. static int gfs2_journaled_truncate_range(struct inode *inode, loff_t offset,
  2115. loff_t length)
  2116. {
  2117. struct gfs2_sbd *sdp = GFS2_SB(inode);
  2118. loff_t max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  2119. int error;
  2120. while (length) {
  2121. struct gfs2_trans *tr;
  2122. loff_t chunk;
  2123. unsigned int offs;
  2124. chunk = length;
  2125. if (chunk > max_chunk)
  2126. chunk = max_chunk;
  2127. offs = offset & ~PAGE_MASK;
  2128. if (offs && chunk > PAGE_SIZE)
  2129. chunk = offs + ((chunk - offs) & PAGE_MASK);
  2130. truncate_pagecache_range(inode, offset, chunk);
  2131. offset += chunk;
  2132. length -= chunk;
  2133. tr = current->journal_info;
  2134. if (!test_bit(TR_TOUCHED, &tr->tr_flags))
  2135. continue;
  2136. gfs2_trans_end(sdp);
  2137. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  2138. if (error)
  2139. return error;
  2140. }
  2141. return 0;
  2142. }
  2143. int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length)
  2144. {
  2145. struct inode *inode = file_inode(file);
  2146. struct gfs2_inode *ip = GFS2_I(inode);
  2147. struct gfs2_sbd *sdp = GFS2_SB(inode);
  2148. unsigned int blocksize = i_blocksize(inode);
  2149. loff_t start, end;
  2150. int error;
  2151. if (!gfs2_is_stuffed(ip)) {
  2152. unsigned int start_off, end_len;
  2153. start_off = offset & (blocksize - 1);
  2154. end_len = (offset + length) & (blocksize - 1);
  2155. if (start_off) {
  2156. unsigned int len = length;
  2157. if (length > blocksize - start_off)
  2158. len = blocksize - start_off;
  2159. error = gfs2_block_zero_range(inode, offset, len);
  2160. if (error)
  2161. goto out;
  2162. if (start_off + length < blocksize)
  2163. end_len = 0;
  2164. }
  2165. if (end_len) {
  2166. error = gfs2_block_zero_range(inode,
  2167. offset + length - end_len, end_len);
  2168. if (error)
  2169. goto out;
  2170. }
  2171. }
  2172. start = round_down(offset, blocksize);
  2173. end = round_up(offset + length, blocksize) - 1;
  2174. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  2175. if (error)
  2176. return error;
  2177. if (gfs2_is_jdata(ip))
  2178. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_JDATA,
  2179. GFS2_JTRUNC_REVOKES);
  2180. else
  2181. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  2182. if (error)
  2183. return error;
  2184. if (gfs2_is_stuffed(ip)) {
  2185. error = stuffed_zero_range(inode, offset, length);
  2186. if (error)
  2187. goto out;
  2188. }
  2189. if (gfs2_is_jdata(ip)) {
  2190. BUG_ON(!current->journal_info);
  2191. gfs2_journaled_truncate_range(inode, offset, length);
  2192. } else
  2193. truncate_pagecache_range(inode, offset, offset + length - 1);
  2194. file_update_time(file);
  2195. mark_inode_dirty(inode);
  2196. if (current->journal_info)
  2197. gfs2_trans_end(sdp);
  2198. if (!gfs2_is_stuffed(ip))
  2199. error = punch_hole(ip, offset, length);
  2200. out:
  2201. if (current->journal_info)
  2202. gfs2_trans_end(sdp);
  2203. return error;
  2204. }
  2205. static int gfs2_map_blocks(struct iomap_writepage_ctx *wpc, struct inode *inode,
  2206. loff_t offset)
  2207. {
  2208. struct metapath mp = { .mp_aheight = 1, };
  2209. int ret;
  2210. if (WARN_ON_ONCE(gfs2_is_stuffed(GFS2_I(inode))))
  2211. return -EIO;
  2212. if (offset >= wpc->iomap.offset &&
  2213. offset < wpc->iomap.offset + wpc->iomap.length)
  2214. return 0;
  2215. memset(&wpc->iomap, 0, sizeof(wpc->iomap));
  2216. ret = gfs2_iomap_get(inode, offset, INT_MAX, 0, &wpc->iomap, &mp);
  2217. release_metapath(&mp);
  2218. return ret;
  2219. }
  2220. const struct iomap_writeback_ops gfs2_writeback_ops = {
  2221. .map_blocks = gfs2_map_blocks,
  2222. };