bmap.c 65 KB

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