bmap.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 "gfs2.h"
  16. #include "incore.h"
  17. #include "bmap.h"
  18. #include "glock.h"
  19. #include "inode.h"
  20. #include "meta_io.h"
  21. #include "quota.h"
  22. #include "rgrp.h"
  23. #include "log.h"
  24. #include "super.h"
  25. #include "trans.h"
  26. #include "dir.h"
  27. #include "util.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. };
  37. struct strip_mine {
  38. int sm_first;
  39. unsigned int sm_height;
  40. };
  41. /**
  42. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  43. * @ip: the inode
  44. * @dibh: the dinode buffer
  45. * @block: the block number that was allocated
  46. * @page: The (optional) page. This is looked up if @page is NULL
  47. *
  48. * Returns: errno
  49. */
  50. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  51. u64 block, struct page *page)
  52. {
  53. struct inode *inode = &ip->i_inode;
  54. struct buffer_head *bh;
  55. int release = 0;
  56. if (!page || page->index) {
  57. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  58. if (!page)
  59. return -ENOMEM;
  60. release = 1;
  61. }
  62. if (!PageUptodate(page)) {
  63. void *kaddr = kmap(page);
  64. u64 dsize = i_size_read(inode);
  65. if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
  66. dsize = dibh->b_size - sizeof(struct gfs2_dinode);
  67. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  68. memset(kaddr + dsize, 0, PAGE_CACHE_SIZE - dsize);
  69. kunmap(page);
  70. SetPageUptodate(page);
  71. }
  72. if (!page_has_buffers(page))
  73. create_empty_buffers(page, 1 << inode->i_blkbits,
  74. (1 << BH_Uptodate));
  75. bh = page_buffers(page);
  76. if (!buffer_mapped(bh))
  77. map_bh(bh, inode->i_sb, block);
  78. set_buffer_uptodate(bh);
  79. if (!gfs2_is_jdata(ip))
  80. mark_buffer_dirty(bh);
  81. if (!gfs2_is_writeback(ip))
  82. gfs2_trans_add_data(ip->i_gl, bh);
  83. if (release) {
  84. unlock_page(page);
  85. page_cache_release(page);
  86. }
  87. return 0;
  88. }
  89. /**
  90. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  91. * @ip: The GFS2 inode to unstuff
  92. * @page: The (optional) page. This is looked up if the @page is NULL
  93. *
  94. * This routine unstuffs a dinode and returns it to a "normal" state such
  95. * that the height can be grown in the traditional way.
  96. *
  97. * Returns: errno
  98. */
  99. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  100. {
  101. struct buffer_head *bh, *dibh;
  102. struct gfs2_dinode *di;
  103. u64 block = 0;
  104. int isdir = gfs2_is_dir(ip);
  105. int error;
  106. down_write(&ip->i_rw_mutex);
  107. error = gfs2_meta_inode_buffer(ip, &dibh);
  108. if (error)
  109. goto out;
  110. if (i_size_read(&ip->i_inode)) {
  111. /* Get a free block, fill it with the stuffed data,
  112. and write it out to disk */
  113. unsigned int n = 1;
  114. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  115. if (error)
  116. goto out_brelse;
  117. if (isdir) {
  118. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  119. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  120. if (error)
  121. goto out_brelse;
  122. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  123. dibh, sizeof(struct gfs2_dinode));
  124. brelse(bh);
  125. } else {
  126. error = gfs2_unstuffer_page(ip, dibh, block, page);
  127. if (error)
  128. goto out_brelse;
  129. }
  130. }
  131. /* Set up the pointer to the new block */
  132. gfs2_trans_add_meta(ip->i_gl, dibh);
  133. di = (struct gfs2_dinode *)dibh->b_data;
  134. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  135. if (i_size_read(&ip->i_inode)) {
  136. *(__be64 *)(di + 1) = cpu_to_be64(block);
  137. gfs2_add_inode_blocks(&ip->i_inode, 1);
  138. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  139. }
  140. ip->i_height = 1;
  141. di->di_height = cpu_to_be16(1);
  142. out_brelse:
  143. brelse(dibh);
  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. * @mp: The metapath to return the result in
  152. * @block: The disk block to look up
  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_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  164. * and mp_list[2] = 165.
  165. *
  166. * That means that in order to get to the block containing the byte at
  167. * offset 101342453, we would load the indirect block pointed to by pointer
  168. * 0 in the dinode. We would then load the indirect block pointed to by
  169. * pointer 48 in that indirect block. We would then load the data block
  170. * pointed to by pointer 165 in that indirect block.
  171. *
  172. * ----------------------------------------
  173. * | Dinode | |
  174. * | | 4|
  175. * | |0 1 2 3 4 5 9|
  176. * | | 6|
  177. * ----------------------------------------
  178. * |
  179. * |
  180. * V
  181. * ----------------------------------------
  182. * | Indirect Block |
  183. * | 5|
  184. * | 4 4 4 4 4 5 5 1|
  185. * |0 5 6 7 8 9 0 1 2|
  186. * ----------------------------------------
  187. * |
  188. * |
  189. * V
  190. * ----------------------------------------
  191. * | Indirect Block |
  192. * | 1 1 1 1 1 5|
  193. * | 6 6 6 6 6 1|
  194. * |0 3 4 5 6 7 2|
  195. * ----------------------------------------
  196. * |
  197. * |
  198. * V
  199. * ----------------------------------------
  200. * | Data block containing offset |
  201. * | 101342453 |
  202. * | |
  203. * | |
  204. * ----------------------------------------
  205. *
  206. */
  207. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  208. struct metapath *mp, unsigned int height)
  209. {
  210. unsigned int i;
  211. 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. * metapointer - Return pointer to start of metadata in a buffer
  222. * @height: The metadata height (0 = dinode)
  223. * @mp: The metapath
  224. *
  225. * Return a pointer to the block number of the next height of the metadata
  226. * tree given a buffer containing the pointer to the current height of the
  227. * metadata tree.
  228. */
  229. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  230. {
  231. struct buffer_head *bh = mp->mp_bh[height];
  232. unsigned int head_size = (height > 0) ?
  233. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  234. return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  235. }
  236. static void gfs2_metapath_ra(struct gfs2_glock *gl,
  237. const struct buffer_head *bh, const __be64 *pos)
  238. {
  239. struct buffer_head *rabh;
  240. const __be64 *endp = (const __be64 *)(bh->b_data + bh->b_size);
  241. const __be64 *t;
  242. for (t = pos; t < endp; t++) {
  243. if (!*t)
  244. continue;
  245. rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
  246. if (trylock_buffer(rabh)) {
  247. if (!buffer_uptodate(rabh)) {
  248. rabh->b_end_io = end_buffer_read_sync;
  249. submit_bh(READA | REQ_META, rabh);
  250. continue;
  251. }
  252. unlock_buffer(rabh);
  253. }
  254. brelse(rabh);
  255. }
  256. }
  257. /**
  258. * lookup_metapath - Walk the metadata tree to a specific point
  259. * @ip: The inode
  260. * @mp: The metapath
  261. *
  262. * Assumes that the inode's buffer has already been looked up and
  263. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  264. * by find_metapath().
  265. *
  266. * If this function encounters part of the tree which has not been
  267. * allocated, it returns the current height of the tree at the point
  268. * at which it found the unallocated block. Blocks which are found are
  269. * added to the mp->mp_bh[] list.
  270. *
  271. * Returns: error or height of metadata tree
  272. */
  273. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  274. {
  275. unsigned int end_of_metadata = ip->i_height - 1;
  276. unsigned int x;
  277. __be64 *ptr;
  278. u64 dblock;
  279. int ret;
  280. for (x = 0; x < end_of_metadata; x++) {
  281. ptr = metapointer(x, mp);
  282. dblock = be64_to_cpu(*ptr);
  283. if (!dblock)
  284. return x + 1;
  285. ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, &mp->mp_bh[x+1]);
  286. if (ret)
  287. return ret;
  288. }
  289. return ip->i_height;
  290. }
  291. static inline void release_metapath(struct metapath *mp)
  292. {
  293. int i;
  294. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  295. if (mp->mp_bh[i] == NULL)
  296. break;
  297. brelse(mp->mp_bh[i]);
  298. }
  299. }
  300. /**
  301. * gfs2_extent_length - Returns length of an extent of blocks
  302. * @start: Start of the buffer
  303. * @len: Length of the buffer in bytes
  304. * @ptr: Current position in the buffer
  305. * @limit: Max extent length to return (0 = unlimited)
  306. * @eob: Set to 1 if we hit "end of block"
  307. *
  308. * If the first block is zero (unallocated) it will return the number of
  309. * unallocated blocks in the extent, otherwise it will return the number
  310. * of contiguous blocks in the extent.
  311. *
  312. * Returns: The length of the extent (minimum of one block)
  313. */
  314. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, size_t limit, int *eob)
  315. {
  316. const __be64 *end = (start + len);
  317. const __be64 *first = ptr;
  318. u64 d = be64_to_cpu(*ptr);
  319. *eob = 0;
  320. do {
  321. ptr++;
  322. if (ptr >= end)
  323. break;
  324. if (limit && --limit == 0)
  325. break;
  326. if (d)
  327. d++;
  328. } while(be64_to_cpu(*ptr) == d);
  329. if (ptr >= end)
  330. *eob = 1;
  331. return (ptr - first);
  332. }
  333. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  334. {
  335. if (create)
  336. down_write(&ip->i_rw_mutex);
  337. else
  338. down_read(&ip->i_rw_mutex);
  339. }
  340. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  341. {
  342. if (create)
  343. up_write(&ip->i_rw_mutex);
  344. else
  345. up_read(&ip->i_rw_mutex);
  346. }
  347. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  348. struct gfs2_glock *gl, unsigned int i,
  349. unsigned offset, u64 bn)
  350. {
  351. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  352. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  353. sizeof(struct gfs2_dinode)));
  354. BUG_ON(i < 1);
  355. BUG_ON(mp->mp_bh[i] != NULL);
  356. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  357. gfs2_trans_add_meta(gl, mp->mp_bh[i]);
  358. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  359. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  360. ptr += offset;
  361. *ptr = cpu_to_be64(bn);
  362. return ptr;
  363. }
  364. enum alloc_state {
  365. ALLOC_DATA = 0,
  366. ALLOC_GROW_DEPTH = 1,
  367. ALLOC_GROW_HEIGHT = 2,
  368. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  369. };
  370. /**
  371. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  372. * @inode: The GFS2 inode
  373. * @lblock: The logical starting block of the extent
  374. * @bh_map: This is used to return the mapping details
  375. * @mp: The metapath
  376. * @sheight: The starting height (i.e. whats already mapped)
  377. * @height: The height to build to
  378. * @maxlen: The max number of data blocks to alloc
  379. *
  380. * In this routine we may have to alloc:
  381. * i) Indirect blocks to grow the metadata tree height
  382. * ii) Indirect blocks to fill in lower part of the metadata tree
  383. * iii) Data blocks
  384. *
  385. * The function is in two parts. The first part works out the total
  386. * number of blocks which we need. The second part does the actual
  387. * allocation asking for an extent at a time (if enough contiguous free
  388. * blocks are available, there will only be one request per bmap call)
  389. * and uses the state machine to initialise the blocks in order.
  390. *
  391. * Returns: errno on error
  392. */
  393. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  394. struct buffer_head *bh_map, struct metapath *mp,
  395. const unsigned int sheight,
  396. const unsigned int height,
  397. const size_t maxlen)
  398. {
  399. struct gfs2_inode *ip = GFS2_I(inode);
  400. struct gfs2_sbd *sdp = GFS2_SB(inode);
  401. struct super_block *sb = sdp->sd_vfs;
  402. struct buffer_head *dibh = mp->mp_bh[0];
  403. u64 bn, dblock = 0;
  404. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  405. unsigned dblks = 0;
  406. unsigned ptrs_per_blk;
  407. const unsigned end_of_metadata = height - 1;
  408. int ret;
  409. int eob = 0;
  410. enum alloc_state state;
  411. __be64 *ptr;
  412. __be64 zero_bn = 0;
  413. BUG_ON(sheight < 1);
  414. BUG_ON(dibh == NULL);
  415. gfs2_trans_add_meta(ip->i_gl, dibh);
  416. if (height == sheight) {
  417. struct buffer_head *bh;
  418. /* Bottom indirect block exists, find unalloced extent size */
  419. ptr = metapointer(end_of_metadata, mp);
  420. bh = mp->mp_bh[end_of_metadata];
  421. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  422. &eob);
  423. BUG_ON(dblks < 1);
  424. state = ALLOC_DATA;
  425. } else {
  426. /* Need to allocate indirect blocks */
  427. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  428. dblks = min(maxlen, (size_t)(ptrs_per_blk -
  429. mp->mp_list[end_of_metadata]));
  430. if (height == ip->i_height) {
  431. /* Writing into existing tree, extend tree down */
  432. iblks = height - sheight;
  433. state = ALLOC_GROW_DEPTH;
  434. } else {
  435. /* Building up tree height */
  436. state = ALLOC_GROW_HEIGHT;
  437. iblks = height - ip->i_height;
  438. branch_start = metapath_branch_start(mp);
  439. iblks += (height - branch_start);
  440. }
  441. }
  442. /* start of the second part of the function (state machine) */
  443. blks = dblks + iblks;
  444. i = sheight;
  445. do {
  446. int error;
  447. n = blks - alloced;
  448. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  449. if (error)
  450. return error;
  451. alloced += n;
  452. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  453. gfs2_trans_add_unrevoke(sdp, bn, n);
  454. switch (state) {
  455. /* Growing height of tree */
  456. case ALLOC_GROW_HEIGHT:
  457. if (i == 1) {
  458. ptr = (__be64 *)(dibh->b_data +
  459. sizeof(struct gfs2_dinode));
  460. zero_bn = *ptr;
  461. }
  462. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  463. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  464. if (i - 1 == height - ip->i_height) {
  465. i--;
  466. gfs2_buffer_copy_tail(mp->mp_bh[i],
  467. sizeof(struct gfs2_meta_header),
  468. dibh, sizeof(struct gfs2_dinode));
  469. gfs2_buffer_clear_tail(dibh,
  470. sizeof(struct gfs2_dinode) +
  471. sizeof(__be64));
  472. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  473. sizeof(struct gfs2_meta_header));
  474. *ptr = zero_bn;
  475. state = ALLOC_GROW_DEPTH;
  476. for(i = branch_start; i < height; i++) {
  477. if (mp->mp_bh[i] == NULL)
  478. break;
  479. brelse(mp->mp_bh[i]);
  480. mp->mp_bh[i] = NULL;
  481. }
  482. i = branch_start;
  483. }
  484. if (n == 0)
  485. break;
  486. /* Branching from existing tree */
  487. case ALLOC_GROW_DEPTH:
  488. if (i > 1 && i < height)
  489. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  490. for (; i < height && n > 0; i++, n--)
  491. gfs2_indirect_init(mp, ip->i_gl, i,
  492. mp->mp_list[i-1], bn++);
  493. if (i == height)
  494. state = ALLOC_DATA;
  495. if (n == 0)
  496. break;
  497. /* Tree complete, adding data blocks */
  498. case ALLOC_DATA:
  499. BUG_ON(n > dblks);
  500. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  501. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  502. dblks = n;
  503. ptr = metapointer(end_of_metadata, mp);
  504. dblock = bn;
  505. while (n-- > 0)
  506. *ptr++ = cpu_to_be64(bn++);
  507. if (buffer_zeronew(bh_map)) {
  508. ret = sb_issue_zeroout(sb, dblock, dblks,
  509. GFP_NOFS);
  510. if (ret) {
  511. fs_err(sdp,
  512. "Failed to zero data buffers\n");
  513. clear_buffer_zeronew(bh_map);
  514. }
  515. }
  516. break;
  517. }
  518. } while ((state != ALLOC_DATA) || !dblock);
  519. ip->i_height = height;
  520. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  521. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  522. map_bh(bh_map, inode->i_sb, dblock);
  523. bh_map->b_size = dblks << inode->i_blkbits;
  524. set_buffer_new(bh_map);
  525. return 0;
  526. }
  527. /**
  528. * gfs2_block_map - Map a block from an inode to a disk block
  529. * @inode: The inode
  530. * @lblock: The logical block number
  531. * @bh_map: The bh to be mapped
  532. * @create: True if its ok to alloc blocks to satify the request
  533. *
  534. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  535. * read of metadata will be required before the next block can be
  536. * mapped. Sets buffer_new() if new blocks were allocated.
  537. *
  538. * Returns: errno
  539. */
  540. int gfs2_block_map(struct inode *inode, sector_t lblock,
  541. struct buffer_head *bh_map, int create)
  542. {
  543. struct gfs2_inode *ip = GFS2_I(inode);
  544. struct gfs2_sbd *sdp = GFS2_SB(inode);
  545. unsigned int bsize = sdp->sd_sb.sb_bsize;
  546. const size_t maxlen = bh_map->b_size >> inode->i_blkbits;
  547. const u64 *arr = sdp->sd_heightsize;
  548. __be64 *ptr;
  549. u64 size;
  550. struct metapath mp;
  551. int ret;
  552. int eob;
  553. unsigned int len;
  554. struct buffer_head *bh;
  555. u8 height;
  556. BUG_ON(maxlen == 0);
  557. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  558. bmap_lock(ip, create);
  559. clear_buffer_mapped(bh_map);
  560. clear_buffer_new(bh_map);
  561. clear_buffer_boundary(bh_map);
  562. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  563. if (gfs2_is_dir(ip)) {
  564. bsize = sdp->sd_jbsize;
  565. arr = sdp->sd_jheightsize;
  566. }
  567. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  568. if (ret)
  569. goto out;
  570. height = ip->i_height;
  571. size = (lblock + 1) * bsize;
  572. while (size > arr[height])
  573. height++;
  574. find_metapath(sdp, lblock, &mp, height);
  575. ret = 1;
  576. if (height > ip->i_height || gfs2_is_stuffed(ip))
  577. goto do_alloc;
  578. ret = lookup_metapath(ip, &mp);
  579. if (ret < 0)
  580. goto out;
  581. if (ret != ip->i_height)
  582. goto do_alloc;
  583. ptr = metapointer(ip->i_height - 1, &mp);
  584. if (*ptr == 0)
  585. goto do_alloc;
  586. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  587. bh = mp.mp_bh[ip->i_height - 1];
  588. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  589. bh_map->b_size = (len << inode->i_blkbits);
  590. if (eob)
  591. set_buffer_boundary(bh_map);
  592. ret = 0;
  593. out:
  594. release_metapath(&mp);
  595. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  596. bmap_unlock(ip, create);
  597. return ret;
  598. do_alloc:
  599. /* All allocations are done here, firstly check create flag */
  600. if (!create) {
  601. BUG_ON(gfs2_is_stuffed(ip));
  602. ret = 0;
  603. goto out;
  604. }
  605. /* At this point ret is the tree depth of already allocated blocks */
  606. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  607. goto out;
  608. }
  609. /*
  610. * Deprecated: do not use in new code
  611. */
  612. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  613. {
  614. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  615. int ret;
  616. int create = *new;
  617. BUG_ON(!extlen);
  618. BUG_ON(!dblock);
  619. BUG_ON(!new);
  620. bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
  621. ret = gfs2_block_map(inode, lblock, &bh, create);
  622. *extlen = bh.b_size >> inode->i_blkbits;
  623. *dblock = bh.b_blocknr;
  624. if (buffer_new(&bh))
  625. *new = 1;
  626. else
  627. *new = 0;
  628. return ret;
  629. }
  630. /**
  631. * do_strip - Look for a layer a particular layer of the file and strip it off
  632. * @ip: the inode
  633. * @dibh: the dinode buffer
  634. * @bh: A buffer of pointers
  635. * @top: The first pointer in the buffer
  636. * @bottom: One more than the last pointer
  637. * @height: the height this buffer is at
  638. * @sm: a pointer to a struct strip_mine
  639. *
  640. * Returns: errno
  641. */
  642. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  643. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  644. unsigned int height, struct strip_mine *sm)
  645. {
  646. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  647. struct gfs2_rgrp_list rlist;
  648. u64 bn, bstart;
  649. u32 blen, btotal;
  650. __be64 *p;
  651. unsigned int rg_blocks = 0;
  652. int metadata;
  653. unsigned int revokes = 0;
  654. int x;
  655. int error;
  656. error = gfs2_rindex_update(sdp);
  657. if (error)
  658. return error;
  659. if (!*top)
  660. sm->sm_first = 0;
  661. if (height != sm->sm_height)
  662. return 0;
  663. if (sm->sm_first) {
  664. top++;
  665. sm->sm_first = 0;
  666. }
  667. metadata = (height != ip->i_height - 1);
  668. if (metadata)
  669. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  670. else if (ip->i_depth)
  671. revokes = sdp->sd_inptrs;
  672. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  673. bstart = 0;
  674. blen = 0;
  675. for (p = top; p < bottom; p++) {
  676. if (!*p)
  677. continue;
  678. bn = be64_to_cpu(*p);
  679. if (bstart + blen == bn)
  680. blen++;
  681. else {
  682. if (bstart)
  683. gfs2_rlist_add(ip, &rlist, bstart);
  684. bstart = bn;
  685. blen = 1;
  686. }
  687. }
  688. if (bstart)
  689. gfs2_rlist_add(ip, &rlist, bstart);
  690. else
  691. goto out; /* Nothing to do */
  692. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  693. for (x = 0; x < rlist.rl_rgrps; x++) {
  694. struct gfs2_rgrpd *rgd;
  695. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  696. rg_blocks += rgd->rd_length;
  697. }
  698. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  699. if (error)
  700. goto out_rlist;
  701. if (gfs2_rs_active(ip->i_res)) /* needs to be done with the rgrp glock held */
  702. gfs2_rs_deltree(ip->i_res);
  703. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  704. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  705. revokes);
  706. if (error)
  707. goto out_rg_gunlock;
  708. down_write(&ip->i_rw_mutex);
  709. gfs2_trans_add_meta(ip->i_gl, dibh);
  710. gfs2_trans_add_meta(ip->i_gl, bh);
  711. bstart = 0;
  712. blen = 0;
  713. btotal = 0;
  714. for (p = top; p < bottom; p++) {
  715. if (!*p)
  716. continue;
  717. bn = be64_to_cpu(*p);
  718. if (bstart + blen == bn)
  719. blen++;
  720. else {
  721. if (bstart) {
  722. __gfs2_free_blocks(ip, bstart, blen, metadata);
  723. btotal += blen;
  724. }
  725. bstart = bn;
  726. blen = 1;
  727. }
  728. *p = 0;
  729. gfs2_add_inode_blocks(&ip->i_inode, -1);
  730. }
  731. if (bstart) {
  732. __gfs2_free_blocks(ip, bstart, blen, metadata);
  733. btotal += blen;
  734. }
  735. gfs2_statfs_change(sdp, 0, +btotal, 0);
  736. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  737. ip->i_inode.i_gid);
  738. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  739. gfs2_dinode_out(ip, dibh->b_data);
  740. up_write(&ip->i_rw_mutex);
  741. gfs2_trans_end(sdp);
  742. out_rg_gunlock:
  743. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  744. out_rlist:
  745. gfs2_rlist_free(&rlist);
  746. out:
  747. return error;
  748. }
  749. /**
  750. * recursive_scan - recursively scan through the end of a file
  751. * @ip: the inode
  752. * @dibh: the dinode buffer
  753. * @mp: the path through the metadata to the point to start
  754. * @height: the height the recursion is at
  755. * @block: the indirect block to look at
  756. * @first: 1 if this is the first block
  757. * @sm: data opaque to this function to pass to @bc
  758. *
  759. * When this is first called @height and @block should be zero and
  760. * @first should be 1.
  761. *
  762. * Returns: errno
  763. */
  764. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  765. struct metapath *mp, unsigned int height,
  766. u64 block, int first, struct strip_mine *sm)
  767. {
  768. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  769. struct buffer_head *bh = NULL;
  770. __be64 *top, *bottom;
  771. u64 bn;
  772. int error;
  773. int mh_size = sizeof(struct gfs2_meta_header);
  774. if (!height) {
  775. error = gfs2_meta_inode_buffer(ip, &bh);
  776. if (error)
  777. return error;
  778. dibh = bh;
  779. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  780. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  781. } else {
  782. error = gfs2_meta_indirect_buffer(ip, height, block, &bh);
  783. if (error)
  784. return error;
  785. top = (__be64 *)(bh->b_data + mh_size) +
  786. (first ? mp->mp_list[height] : 0);
  787. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  788. }
  789. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  790. if (error)
  791. goto out;
  792. if (height < ip->i_height - 1) {
  793. gfs2_metapath_ra(ip->i_gl, bh, top);
  794. for (; top < bottom; top++, first = 0) {
  795. if (!*top)
  796. continue;
  797. bn = be64_to_cpu(*top);
  798. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  799. first, sm);
  800. if (error)
  801. break;
  802. }
  803. }
  804. out:
  805. brelse(bh);
  806. return error;
  807. }
  808. /**
  809. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  810. *
  811. * This is partly borrowed from ext3.
  812. */
  813. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  814. {
  815. struct inode *inode = mapping->host;
  816. struct gfs2_inode *ip = GFS2_I(inode);
  817. unsigned long index = from >> PAGE_CACHE_SHIFT;
  818. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  819. unsigned blocksize, iblock, length, pos;
  820. struct buffer_head *bh;
  821. struct page *page;
  822. int err;
  823. page = find_or_create_page(mapping, index, GFP_NOFS);
  824. if (!page)
  825. return 0;
  826. blocksize = inode->i_sb->s_blocksize;
  827. length = blocksize - (offset & (blocksize - 1));
  828. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  829. if (!page_has_buffers(page))
  830. create_empty_buffers(page, blocksize, 0);
  831. /* Find the buffer that contains "offset" */
  832. bh = page_buffers(page);
  833. pos = blocksize;
  834. while (offset >= pos) {
  835. bh = bh->b_this_page;
  836. iblock++;
  837. pos += blocksize;
  838. }
  839. err = 0;
  840. if (!buffer_mapped(bh)) {
  841. gfs2_block_map(inode, iblock, bh, 0);
  842. /* unmapped? It's a hole - nothing to do */
  843. if (!buffer_mapped(bh))
  844. goto unlock;
  845. }
  846. /* Ok, it's mapped. Make sure it's up-to-date */
  847. if (PageUptodate(page))
  848. set_buffer_uptodate(bh);
  849. if (!buffer_uptodate(bh)) {
  850. err = -EIO;
  851. ll_rw_block(READ, 1, &bh);
  852. wait_on_buffer(bh);
  853. /* Uhhuh. Read error. Complain and punt. */
  854. if (!buffer_uptodate(bh))
  855. goto unlock;
  856. err = 0;
  857. }
  858. if (!gfs2_is_writeback(ip))
  859. gfs2_trans_add_data(ip->i_gl, bh);
  860. zero_user(page, offset, length);
  861. mark_buffer_dirty(bh);
  862. unlock:
  863. unlock_page(page);
  864. page_cache_release(page);
  865. return err;
  866. }
  867. #define GFS2_JTRUNC_REVOKES 8192
  868. /**
  869. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  870. * @inode: The inode being truncated
  871. * @oldsize: The original (larger) size
  872. * @newsize: The new smaller size
  873. *
  874. * With jdata files, we have to journal a revoke for each block which is
  875. * truncated. As a result, we need to split this into separate transactions
  876. * if the number of pages being truncated gets too large.
  877. */
  878. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  879. {
  880. struct gfs2_sbd *sdp = GFS2_SB(inode);
  881. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  882. u64 chunk;
  883. int error;
  884. while (oldsize != newsize) {
  885. chunk = oldsize - newsize;
  886. if (chunk > max_chunk)
  887. chunk = max_chunk;
  888. truncate_pagecache(inode, oldsize - chunk);
  889. oldsize -= chunk;
  890. gfs2_trans_end(sdp);
  891. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  892. if (error)
  893. return error;
  894. }
  895. return 0;
  896. }
  897. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  898. {
  899. struct gfs2_inode *ip = GFS2_I(inode);
  900. struct gfs2_sbd *sdp = GFS2_SB(inode);
  901. struct address_space *mapping = inode->i_mapping;
  902. struct buffer_head *dibh;
  903. int journaled = gfs2_is_jdata(ip);
  904. int error;
  905. if (journaled)
  906. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  907. else
  908. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  909. if (error)
  910. return error;
  911. error = gfs2_meta_inode_buffer(ip, &dibh);
  912. if (error)
  913. goto out;
  914. gfs2_trans_add_meta(ip->i_gl, dibh);
  915. if (gfs2_is_stuffed(ip)) {
  916. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  917. } else {
  918. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  919. error = gfs2_block_truncate_page(mapping, newsize);
  920. if (error)
  921. goto out_brelse;
  922. }
  923. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  924. }
  925. i_size_write(inode, newsize);
  926. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  927. gfs2_dinode_out(ip, dibh->b_data);
  928. if (journaled)
  929. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  930. else
  931. truncate_pagecache(inode, newsize);
  932. if (error) {
  933. brelse(dibh);
  934. return error;
  935. }
  936. out_brelse:
  937. brelse(dibh);
  938. out:
  939. gfs2_trans_end(sdp);
  940. return error;
  941. }
  942. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  943. {
  944. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  945. unsigned int height = ip->i_height;
  946. u64 lblock;
  947. struct metapath mp;
  948. int error;
  949. if (!size)
  950. lblock = 0;
  951. else
  952. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  953. find_metapath(sdp, lblock, &mp, ip->i_height);
  954. error = gfs2_rindex_update(sdp);
  955. if (error)
  956. return error;
  957. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  958. if (error)
  959. return error;
  960. while (height--) {
  961. struct strip_mine sm;
  962. sm.sm_first = !!size;
  963. sm.sm_height = height;
  964. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  965. if (error)
  966. break;
  967. }
  968. gfs2_quota_unhold(ip);
  969. return error;
  970. }
  971. static int trunc_end(struct gfs2_inode *ip)
  972. {
  973. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  974. struct buffer_head *dibh;
  975. int error;
  976. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  977. if (error)
  978. return error;
  979. down_write(&ip->i_rw_mutex);
  980. error = gfs2_meta_inode_buffer(ip, &dibh);
  981. if (error)
  982. goto out;
  983. if (!i_size_read(&ip->i_inode)) {
  984. ip->i_height = 0;
  985. ip->i_goal = ip->i_no_addr;
  986. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  987. gfs2_ordered_del_inode(ip);
  988. }
  989. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  990. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  991. gfs2_trans_add_meta(ip->i_gl, dibh);
  992. gfs2_dinode_out(ip, dibh->b_data);
  993. brelse(dibh);
  994. out:
  995. up_write(&ip->i_rw_mutex);
  996. gfs2_trans_end(sdp);
  997. return error;
  998. }
  999. /**
  1000. * do_shrink - make a file smaller
  1001. * @inode: the inode
  1002. * @oldsize: the current inode size
  1003. * @newsize: the size to make the file
  1004. *
  1005. * Called with an exclusive lock on @inode. The @size must
  1006. * be equal to or smaller than the current inode size.
  1007. *
  1008. * Returns: errno
  1009. */
  1010. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  1011. {
  1012. struct gfs2_inode *ip = GFS2_I(inode);
  1013. int error;
  1014. error = trunc_start(inode, oldsize, newsize);
  1015. if (error < 0)
  1016. return error;
  1017. if (gfs2_is_stuffed(ip))
  1018. return 0;
  1019. error = trunc_dealloc(ip, newsize);
  1020. if (error == 0)
  1021. error = trunc_end(ip);
  1022. return error;
  1023. }
  1024. void gfs2_trim_blocks(struct inode *inode)
  1025. {
  1026. u64 size = inode->i_size;
  1027. int ret;
  1028. ret = do_shrink(inode, size, size);
  1029. WARN_ON(ret != 0);
  1030. }
  1031. /**
  1032. * do_grow - Touch and update inode size
  1033. * @inode: The inode
  1034. * @size: The new size
  1035. *
  1036. * This function updates the timestamps on the inode and
  1037. * may also increase the size of the inode. This function
  1038. * must not be called with @size any smaller than the current
  1039. * inode size.
  1040. *
  1041. * Although it is not strictly required to unstuff files here,
  1042. * earlier versions of GFS2 have a bug in the stuffed file reading
  1043. * code which will result in a buffer overrun if the size is larger
  1044. * than the max stuffed file size. In order to prevent this from
  1045. * occurring, such files are unstuffed, but in other cases we can
  1046. * just update the inode size directly.
  1047. *
  1048. * Returns: 0 on success, or -ve on error
  1049. */
  1050. static int do_grow(struct inode *inode, u64 size)
  1051. {
  1052. struct gfs2_inode *ip = GFS2_I(inode);
  1053. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1054. struct gfs2_alloc_parms ap = { .target = 1, };
  1055. struct buffer_head *dibh;
  1056. int error;
  1057. int unstuff = 0;
  1058. if (gfs2_is_stuffed(ip) &&
  1059. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1060. error = gfs2_quota_lock_check(ip, &ap);
  1061. if (error)
  1062. return error;
  1063. error = gfs2_inplace_reserve(ip, &ap);
  1064. if (error)
  1065. goto do_grow_qunlock;
  1066. unstuff = 1;
  1067. }
  1068. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
  1069. (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
  1070. 0 : RES_QUOTA), 0);
  1071. if (error)
  1072. goto do_grow_release;
  1073. if (unstuff) {
  1074. error = gfs2_unstuff_dinode(ip, NULL);
  1075. if (error)
  1076. goto do_end_trans;
  1077. }
  1078. error = gfs2_meta_inode_buffer(ip, &dibh);
  1079. if (error)
  1080. goto do_end_trans;
  1081. i_size_write(inode, size);
  1082. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1083. gfs2_trans_add_meta(ip->i_gl, dibh);
  1084. gfs2_dinode_out(ip, dibh->b_data);
  1085. brelse(dibh);
  1086. do_end_trans:
  1087. gfs2_trans_end(sdp);
  1088. do_grow_release:
  1089. if (unstuff) {
  1090. gfs2_inplace_release(ip);
  1091. do_grow_qunlock:
  1092. gfs2_quota_unlock(ip);
  1093. }
  1094. return error;
  1095. }
  1096. /**
  1097. * gfs2_setattr_size - make a file a given size
  1098. * @inode: the inode
  1099. * @newsize: the size to make the file
  1100. *
  1101. * The file size can grow, shrink, or stay the same size. This
  1102. * is called holding i_mutex and an exclusive glock on the inode
  1103. * in question.
  1104. *
  1105. * Returns: errno
  1106. */
  1107. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1108. {
  1109. struct gfs2_inode *ip = GFS2_I(inode);
  1110. int ret;
  1111. u64 oldsize;
  1112. BUG_ON(!S_ISREG(inode->i_mode));
  1113. ret = inode_newsize_ok(inode, newsize);
  1114. if (ret)
  1115. return ret;
  1116. ret = get_write_access(inode);
  1117. if (ret)
  1118. return ret;
  1119. inode_dio_wait(inode);
  1120. ret = gfs2_rs_alloc(ip);
  1121. if (ret)
  1122. goto out;
  1123. oldsize = inode->i_size;
  1124. if (newsize >= oldsize) {
  1125. ret = do_grow(inode, newsize);
  1126. goto out;
  1127. }
  1128. gfs2_rs_deltree(ip->i_res);
  1129. ret = do_shrink(inode, oldsize, newsize);
  1130. out:
  1131. put_write_access(inode);
  1132. return ret;
  1133. }
  1134. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1135. {
  1136. int error;
  1137. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1138. if (!error)
  1139. error = trunc_end(ip);
  1140. return error;
  1141. }
  1142. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1143. {
  1144. return trunc_dealloc(ip, 0);
  1145. }
  1146. /**
  1147. * gfs2_free_journal_extents - Free cached journal bmap info
  1148. * @jd: The journal
  1149. *
  1150. */
  1151. void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
  1152. {
  1153. struct gfs2_journal_extent *jext;
  1154. while(!list_empty(&jd->extent_list)) {
  1155. jext = list_entry(jd->extent_list.next, struct gfs2_journal_extent, list);
  1156. list_del(&jext->list);
  1157. kfree(jext);
  1158. }
  1159. }
  1160. /**
  1161. * gfs2_add_jextent - Add or merge a new extent to extent cache
  1162. * @jd: The journal descriptor
  1163. * @lblock: The logical block at start of new extent
  1164. * @dblock: The physical block at start of new extent
  1165. * @blocks: Size of extent in fs blocks
  1166. *
  1167. * Returns: 0 on success or -ENOMEM
  1168. */
  1169. static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
  1170. {
  1171. struct gfs2_journal_extent *jext;
  1172. if (!list_empty(&jd->extent_list)) {
  1173. jext = list_entry(jd->extent_list.prev, struct gfs2_journal_extent, list);
  1174. if ((jext->dblock + jext->blocks) == dblock) {
  1175. jext->blocks += blocks;
  1176. return 0;
  1177. }
  1178. }
  1179. jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
  1180. if (jext == NULL)
  1181. return -ENOMEM;
  1182. jext->dblock = dblock;
  1183. jext->lblock = lblock;
  1184. jext->blocks = blocks;
  1185. list_add_tail(&jext->list, &jd->extent_list);
  1186. jd->nr_extents++;
  1187. return 0;
  1188. }
  1189. /**
  1190. * gfs2_map_journal_extents - Cache journal bmap info
  1191. * @sdp: The super block
  1192. * @jd: The journal to map
  1193. *
  1194. * Create a reusable "extent" mapping from all logical
  1195. * blocks to all physical blocks for the given journal. This will save
  1196. * us time when writing journal blocks. Most journals will have only one
  1197. * extent that maps all their logical blocks. That's because gfs2.mkfs
  1198. * arranges the journal blocks sequentially to maximize performance.
  1199. * So the extent would map the first block for the entire file length.
  1200. * However, gfs2_jadd can happen while file activity is happening, so
  1201. * those journals may not be sequential. Less likely is the case where
  1202. * the users created their own journals by mounting the metafs and
  1203. * laying it out. But it's still possible. These journals might have
  1204. * several extents.
  1205. *
  1206. * Returns: 0 on success, or error on failure
  1207. */
  1208. int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
  1209. {
  1210. u64 lblock = 0;
  1211. u64 lblock_stop;
  1212. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  1213. struct buffer_head bh;
  1214. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  1215. u64 size;
  1216. int rc;
  1217. lblock_stop = i_size_read(jd->jd_inode) >> shift;
  1218. size = (lblock_stop - lblock) << shift;
  1219. jd->nr_extents = 0;
  1220. WARN_ON(!list_empty(&jd->extent_list));
  1221. do {
  1222. bh.b_state = 0;
  1223. bh.b_blocknr = 0;
  1224. bh.b_size = size;
  1225. rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
  1226. if (rc || !buffer_mapped(&bh))
  1227. goto fail;
  1228. rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
  1229. if (rc)
  1230. goto fail;
  1231. size -= bh.b_size;
  1232. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1233. } while(size > 0);
  1234. fs_info(sdp, "journal %d mapped with %u extents\n", jd->jd_jid,
  1235. jd->nr_extents);
  1236. return 0;
  1237. fail:
  1238. fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
  1239. rc, jd->jd_jid,
  1240. (unsigned long long)(i_size_read(jd->jd_inode) - size),
  1241. jd->nr_extents);
  1242. fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
  1243. rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
  1244. bh.b_state, (unsigned long long)bh.b_size);
  1245. gfs2_free_journal_extents(jd);
  1246. return rc;
  1247. }
  1248. /**
  1249. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1250. * @ip: the file being written to
  1251. * @offset: the offset to write to
  1252. * @len: the number of bytes being written
  1253. *
  1254. * Returns: 1 if an alloc is required, 0 otherwise
  1255. */
  1256. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1257. unsigned int len)
  1258. {
  1259. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1260. struct buffer_head bh;
  1261. unsigned int shift;
  1262. u64 lblock, lblock_stop, size;
  1263. u64 end_of_file;
  1264. if (!len)
  1265. return 0;
  1266. if (gfs2_is_stuffed(ip)) {
  1267. if (offset + len >
  1268. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1269. return 1;
  1270. return 0;
  1271. }
  1272. shift = sdp->sd_sb.sb_bsize_shift;
  1273. BUG_ON(gfs2_is_dir(ip));
  1274. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1275. lblock = offset >> shift;
  1276. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1277. if (lblock_stop > end_of_file)
  1278. return 1;
  1279. size = (lblock_stop - lblock) << shift;
  1280. do {
  1281. bh.b_state = 0;
  1282. bh.b_size = size;
  1283. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1284. if (!buffer_mapped(&bh))
  1285. return 1;
  1286. size -= bh.b_size;
  1287. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1288. } while(size > 0);
  1289. return 0;
  1290. }