bmap.c 65 KB

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