bmap.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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, unsigned 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 unsigned int 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, ptrs_per_blk - mp->mp_list[end_of_metadata]);
  429. if (height == ip->i_height) {
  430. /* Writing into existing tree, extend tree down */
  431. iblks = height - sheight;
  432. state = ALLOC_GROW_DEPTH;
  433. } else {
  434. /* Building up tree height */
  435. state = ALLOC_GROW_HEIGHT;
  436. iblks = height - ip->i_height;
  437. branch_start = metapath_branch_start(mp);
  438. iblks += (height - branch_start);
  439. }
  440. }
  441. /* start of the second part of the function (state machine) */
  442. blks = dblks + iblks;
  443. i = sheight;
  444. do {
  445. int error;
  446. n = blks - alloced;
  447. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  448. if (error)
  449. return error;
  450. alloced += n;
  451. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  452. gfs2_trans_add_unrevoke(sdp, bn, n);
  453. switch (state) {
  454. /* Growing height of tree */
  455. case ALLOC_GROW_HEIGHT:
  456. if (i == 1) {
  457. ptr = (__be64 *)(dibh->b_data +
  458. sizeof(struct gfs2_dinode));
  459. zero_bn = *ptr;
  460. }
  461. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  462. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  463. if (i - 1 == height - ip->i_height) {
  464. i--;
  465. gfs2_buffer_copy_tail(mp->mp_bh[i],
  466. sizeof(struct gfs2_meta_header),
  467. dibh, sizeof(struct gfs2_dinode));
  468. gfs2_buffer_clear_tail(dibh,
  469. sizeof(struct gfs2_dinode) +
  470. sizeof(__be64));
  471. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  472. sizeof(struct gfs2_meta_header));
  473. *ptr = zero_bn;
  474. state = ALLOC_GROW_DEPTH;
  475. for(i = branch_start; i < height; i++) {
  476. if (mp->mp_bh[i] == NULL)
  477. break;
  478. brelse(mp->mp_bh[i]);
  479. mp->mp_bh[i] = NULL;
  480. }
  481. i = branch_start;
  482. }
  483. if (n == 0)
  484. break;
  485. /* Branching from existing tree */
  486. case ALLOC_GROW_DEPTH:
  487. if (i > 1 && i < height)
  488. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  489. for (; i < height && n > 0; i++, n--)
  490. gfs2_indirect_init(mp, ip->i_gl, i,
  491. mp->mp_list[i-1], bn++);
  492. if (i == height)
  493. state = ALLOC_DATA;
  494. if (n == 0)
  495. break;
  496. /* Tree complete, adding data blocks */
  497. case ALLOC_DATA:
  498. BUG_ON(n > dblks);
  499. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  500. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  501. dblks = n;
  502. ptr = metapointer(end_of_metadata, mp);
  503. dblock = bn;
  504. while (n-- > 0)
  505. *ptr++ = cpu_to_be64(bn++);
  506. if (buffer_zeronew(bh_map)) {
  507. ret = sb_issue_zeroout(sb, dblock, dblks,
  508. GFP_NOFS);
  509. if (ret) {
  510. fs_err(sdp,
  511. "Failed to zero data buffers\n");
  512. clear_buffer_zeronew(bh_map);
  513. }
  514. }
  515. break;
  516. }
  517. } while ((state != ALLOC_DATA) || !dblock);
  518. ip->i_height = height;
  519. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  520. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  521. map_bh(bh_map, inode->i_sb, dblock);
  522. bh_map->b_size = dblks << inode->i_blkbits;
  523. set_buffer_new(bh_map);
  524. return 0;
  525. }
  526. /**
  527. * gfs2_block_map - Map a block from an inode to a disk block
  528. * @inode: The inode
  529. * @lblock: The logical block number
  530. * @bh_map: The bh to be mapped
  531. * @create: True if its ok to alloc blocks to satify the request
  532. *
  533. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  534. * read of metadata will be required before the next block can be
  535. * mapped. Sets buffer_new() if new blocks were allocated.
  536. *
  537. * Returns: errno
  538. */
  539. int gfs2_block_map(struct inode *inode, sector_t lblock,
  540. struct buffer_head *bh_map, int create)
  541. {
  542. struct gfs2_inode *ip = GFS2_I(inode);
  543. struct gfs2_sbd *sdp = GFS2_SB(inode);
  544. unsigned int bsize = sdp->sd_sb.sb_bsize;
  545. const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  546. const u64 *arr = sdp->sd_heightsize;
  547. __be64 *ptr;
  548. u64 size;
  549. struct metapath mp;
  550. int ret;
  551. int eob;
  552. unsigned int len;
  553. struct buffer_head *bh;
  554. u8 height;
  555. BUG_ON(maxlen == 0);
  556. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  557. bmap_lock(ip, create);
  558. clear_buffer_mapped(bh_map);
  559. clear_buffer_new(bh_map);
  560. clear_buffer_boundary(bh_map);
  561. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  562. if (gfs2_is_dir(ip)) {
  563. bsize = sdp->sd_jbsize;
  564. arr = sdp->sd_jheightsize;
  565. }
  566. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  567. if (ret)
  568. goto out;
  569. height = ip->i_height;
  570. size = (lblock + 1) * bsize;
  571. while (size > arr[height])
  572. height++;
  573. find_metapath(sdp, lblock, &mp, height);
  574. ret = 1;
  575. if (height > ip->i_height || gfs2_is_stuffed(ip))
  576. goto do_alloc;
  577. ret = lookup_metapath(ip, &mp);
  578. if (ret < 0)
  579. goto out;
  580. if (ret != ip->i_height)
  581. goto do_alloc;
  582. ptr = metapointer(ip->i_height - 1, &mp);
  583. if (*ptr == 0)
  584. goto do_alloc;
  585. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  586. bh = mp.mp_bh[ip->i_height - 1];
  587. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  588. bh_map->b_size = (len << inode->i_blkbits);
  589. if (eob)
  590. set_buffer_boundary(bh_map);
  591. ret = 0;
  592. out:
  593. release_metapath(&mp);
  594. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  595. bmap_unlock(ip, create);
  596. return ret;
  597. do_alloc:
  598. /* All allocations are done here, firstly check create flag */
  599. if (!create) {
  600. BUG_ON(gfs2_is_stuffed(ip));
  601. ret = 0;
  602. goto out;
  603. }
  604. /* At this point ret is the tree depth of already allocated blocks */
  605. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  606. goto out;
  607. }
  608. /*
  609. * Deprecated: do not use in new code
  610. */
  611. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  612. {
  613. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  614. int ret;
  615. int create = *new;
  616. BUG_ON(!extlen);
  617. BUG_ON(!dblock);
  618. BUG_ON(!new);
  619. bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
  620. ret = gfs2_block_map(inode, lblock, &bh, create);
  621. *extlen = bh.b_size >> inode->i_blkbits;
  622. *dblock = bh.b_blocknr;
  623. if (buffer_new(&bh))
  624. *new = 1;
  625. else
  626. *new = 0;
  627. return ret;
  628. }
  629. /**
  630. * do_strip - Look for a layer a particular layer of the file and strip it off
  631. * @ip: the inode
  632. * @dibh: the dinode buffer
  633. * @bh: A buffer of pointers
  634. * @top: The first pointer in the buffer
  635. * @bottom: One more than the last pointer
  636. * @height: the height this buffer is at
  637. * @data: a pointer to a struct strip_mine
  638. *
  639. * Returns: errno
  640. */
  641. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  642. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  643. unsigned int height, struct strip_mine *sm)
  644. {
  645. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  646. struct gfs2_rgrp_list rlist;
  647. u64 bn, bstart;
  648. u32 blen, btotal;
  649. __be64 *p;
  650. unsigned int rg_blocks = 0;
  651. int metadata;
  652. unsigned int revokes = 0;
  653. int x;
  654. int error;
  655. error = gfs2_rindex_update(sdp);
  656. if (error)
  657. return error;
  658. if (!*top)
  659. sm->sm_first = 0;
  660. if (height != sm->sm_height)
  661. return 0;
  662. if (sm->sm_first) {
  663. top++;
  664. sm->sm_first = 0;
  665. }
  666. metadata = (height != ip->i_height - 1);
  667. if (metadata)
  668. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  669. else if (ip->i_depth)
  670. revokes = sdp->sd_inptrs;
  671. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  672. bstart = 0;
  673. blen = 0;
  674. for (p = top; p < bottom; p++) {
  675. if (!*p)
  676. continue;
  677. bn = be64_to_cpu(*p);
  678. if (bstart + blen == bn)
  679. blen++;
  680. else {
  681. if (bstart)
  682. gfs2_rlist_add(ip, &rlist, bstart);
  683. bstart = bn;
  684. blen = 1;
  685. }
  686. }
  687. if (bstart)
  688. gfs2_rlist_add(ip, &rlist, bstart);
  689. else
  690. goto out; /* Nothing to do */
  691. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  692. for (x = 0; x < rlist.rl_rgrps; x++) {
  693. struct gfs2_rgrpd *rgd;
  694. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  695. rg_blocks += rgd->rd_length;
  696. }
  697. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  698. if (error)
  699. goto out_rlist;
  700. if (gfs2_rs_active(ip->i_res)) /* needs to be done with the rgrp glock held */
  701. gfs2_rs_deltree(ip->i_res);
  702. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  703. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  704. revokes);
  705. if (error)
  706. goto out_rg_gunlock;
  707. down_write(&ip->i_rw_mutex);
  708. gfs2_trans_add_meta(ip->i_gl, dibh);
  709. gfs2_trans_add_meta(ip->i_gl, bh);
  710. bstart = 0;
  711. blen = 0;
  712. btotal = 0;
  713. for (p = top; p < bottom; p++) {
  714. if (!*p)
  715. continue;
  716. bn = be64_to_cpu(*p);
  717. if (bstart + blen == bn)
  718. blen++;
  719. else {
  720. if (bstart) {
  721. __gfs2_free_blocks(ip, bstart, blen, metadata);
  722. btotal += blen;
  723. }
  724. bstart = bn;
  725. blen = 1;
  726. }
  727. *p = 0;
  728. gfs2_add_inode_blocks(&ip->i_inode, -1);
  729. }
  730. if (bstart) {
  731. __gfs2_free_blocks(ip, bstart, blen, metadata);
  732. btotal += blen;
  733. }
  734. gfs2_statfs_change(sdp, 0, +btotal, 0);
  735. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  736. ip->i_inode.i_gid);
  737. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  738. gfs2_dinode_out(ip, dibh->b_data);
  739. up_write(&ip->i_rw_mutex);
  740. gfs2_trans_end(sdp);
  741. out_rg_gunlock:
  742. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  743. out_rlist:
  744. gfs2_rlist_free(&rlist);
  745. out:
  746. return error;
  747. }
  748. /**
  749. * recursive_scan - recursively scan through the end of a file
  750. * @ip: the inode
  751. * @dibh: the dinode buffer
  752. * @mp: the path through the metadata to the point to start
  753. * @height: the height the recursion is at
  754. * @block: the indirect block to look at
  755. * @first: 1 if this is the first block
  756. * @sm: data opaque to this function to pass to @bc
  757. *
  758. * When this is first called @height and @block should be zero and
  759. * @first should be 1.
  760. *
  761. * Returns: errno
  762. */
  763. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  764. struct metapath *mp, unsigned int height,
  765. u64 block, int first, struct strip_mine *sm)
  766. {
  767. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  768. struct buffer_head *bh = NULL;
  769. __be64 *top, *bottom;
  770. u64 bn;
  771. int error;
  772. int mh_size = sizeof(struct gfs2_meta_header);
  773. if (!height) {
  774. error = gfs2_meta_inode_buffer(ip, &bh);
  775. if (error)
  776. return error;
  777. dibh = bh;
  778. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  779. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  780. } else {
  781. error = gfs2_meta_indirect_buffer(ip, height, block, &bh);
  782. if (error)
  783. return error;
  784. top = (__be64 *)(bh->b_data + mh_size) +
  785. (first ? mp->mp_list[height] : 0);
  786. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  787. }
  788. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  789. if (error)
  790. goto out;
  791. if (height < ip->i_height - 1) {
  792. gfs2_metapath_ra(ip->i_gl, bh, top);
  793. for (; top < bottom; top++, first = 0) {
  794. if (!*top)
  795. continue;
  796. bn = be64_to_cpu(*top);
  797. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  798. first, sm);
  799. if (error)
  800. break;
  801. }
  802. }
  803. out:
  804. brelse(bh);
  805. return error;
  806. }
  807. /**
  808. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  809. *
  810. * This is partly borrowed from ext3.
  811. */
  812. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  813. {
  814. struct inode *inode = mapping->host;
  815. struct gfs2_inode *ip = GFS2_I(inode);
  816. unsigned long index = from >> PAGE_CACHE_SHIFT;
  817. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  818. unsigned blocksize, iblock, length, pos;
  819. struct buffer_head *bh;
  820. struct page *page;
  821. int err;
  822. page = find_or_create_page(mapping, index, GFP_NOFS);
  823. if (!page)
  824. return 0;
  825. blocksize = inode->i_sb->s_blocksize;
  826. length = blocksize - (offset & (blocksize - 1));
  827. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  828. if (!page_has_buffers(page))
  829. create_empty_buffers(page, blocksize, 0);
  830. /* Find the buffer that contains "offset" */
  831. bh = page_buffers(page);
  832. pos = blocksize;
  833. while (offset >= pos) {
  834. bh = bh->b_this_page;
  835. iblock++;
  836. pos += blocksize;
  837. }
  838. err = 0;
  839. if (!buffer_mapped(bh)) {
  840. gfs2_block_map(inode, iblock, bh, 0);
  841. /* unmapped? It's a hole - nothing to do */
  842. if (!buffer_mapped(bh))
  843. goto unlock;
  844. }
  845. /* Ok, it's mapped. Make sure it's up-to-date */
  846. if (PageUptodate(page))
  847. set_buffer_uptodate(bh);
  848. if (!buffer_uptodate(bh)) {
  849. err = -EIO;
  850. ll_rw_block(READ, 1, &bh);
  851. wait_on_buffer(bh);
  852. /* Uhhuh. Read error. Complain and punt. */
  853. if (!buffer_uptodate(bh))
  854. goto unlock;
  855. err = 0;
  856. }
  857. if (!gfs2_is_writeback(ip))
  858. gfs2_trans_add_data(ip->i_gl, bh);
  859. zero_user(page, offset, length);
  860. mark_buffer_dirty(bh);
  861. unlock:
  862. unlock_page(page);
  863. page_cache_release(page);
  864. return err;
  865. }
  866. /**
  867. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  868. * @inode: The inode being truncated
  869. * @oldsize: The original (larger) size
  870. * @newsize: The new smaller size
  871. *
  872. * With jdata files, we have to journal a revoke for each block which is
  873. * truncated. As a result, we need to split this into separate transactions
  874. * if the number of pages being truncated gets too large.
  875. */
  876. #define GFS2_JTRUNC_REVOKES 8192
  877. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  878. {
  879. struct gfs2_sbd *sdp = GFS2_SB(inode);
  880. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  881. u64 chunk;
  882. int error;
  883. while (oldsize != newsize) {
  884. chunk = oldsize - newsize;
  885. if (chunk > max_chunk)
  886. chunk = max_chunk;
  887. truncate_pagecache(inode, oldsize, oldsize - chunk);
  888. oldsize -= chunk;
  889. gfs2_trans_end(sdp);
  890. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  891. if (error)
  892. return error;
  893. }
  894. return 0;
  895. }
  896. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  897. {
  898. struct gfs2_inode *ip = GFS2_I(inode);
  899. struct gfs2_sbd *sdp = GFS2_SB(inode);
  900. struct address_space *mapping = inode->i_mapping;
  901. struct buffer_head *dibh;
  902. int journaled = gfs2_is_jdata(ip);
  903. int error;
  904. if (journaled)
  905. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  906. else
  907. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  908. if (error)
  909. return error;
  910. error = gfs2_meta_inode_buffer(ip, &dibh);
  911. if (error)
  912. goto out;
  913. gfs2_trans_add_meta(ip->i_gl, dibh);
  914. if (gfs2_is_stuffed(ip)) {
  915. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  916. } else {
  917. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  918. error = gfs2_block_truncate_page(mapping, newsize);
  919. if (error)
  920. goto out_brelse;
  921. }
  922. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  923. }
  924. i_size_write(inode, newsize);
  925. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  926. gfs2_dinode_out(ip, dibh->b_data);
  927. if (journaled)
  928. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  929. else
  930. truncate_pagecache(inode, oldsize, newsize);
  931. if (error) {
  932. brelse(dibh);
  933. return error;
  934. }
  935. out_brelse:
  936. brelse(dibh);
  937. out:
  938. gfs2_trans_end(sdp);
  939. return error;
  940. }
  941. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  942. {
  943. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  944. unsigned int height = ip->i_height;
  945. u64 lblock;
  946. struct metapath mp;
  947. int error;
  948. if (!size)
  949. lblock = 0;
  950. else
  951. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  952. find_metapath(sdp, lblock, &mp, ip->i_height);
  953. error = gfs2_rindex_update(sdp);
  954. if (error)
  955. return error;
  956. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  957. if (error)
  958. return error;
  959. while (height--) {
  960. struct strip_mine sm;
  961. sm.sm_first = !!size;
  962. sm.sm_height = height;
  963. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  964. if (error)
  965. break;
  966. }
  967. gfs2_quota_unhold(ip);
  968. return error;
  969. }
  970. static int trunc_end(struct gfs2_inode *ip)
  971. {
  972. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  973. struct buffer_head *dibh;
  974. int error;
  975. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  976. if (error)
  977. return error;
  978. down_write(&ip->i_rw_mutex);
  979. error = gfs2_meta_inode_buffer(ip, &dibh);
  980. if (error)
  981. goto out;
  982. if (!i_size_read(&ip->i_inode)) {
  983. ip->i_height = 0;
  984. ip->i_goal = ip->i_no_addr;
  985. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  986. gfs2_ordered_del_inode(ip);
  987. }
  988. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  989. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  990. gfs2_trans_add_meta(ip->i_gl, dibh);
  991. gfs2_dinode_out(ip, dibh->b_data);
  992. brelse(dibh);
  993. out:
  994. up_write(&ip->i_rw_mutex);
  995. gfs2_trans_end(sdp);
  996. return error;
  997. }
  998. /**
  999. * do_shrink - make a file smaller
  1000. * @inode: the inode
  1001. * @oldsize: the current inode size
  1002. * @newsize: the size to make the file
  1003. *
  1004. * Called with an exclusive lock on @inode. The @size must
  1005. * be equal to or smaller than the current inode size.
  1006. *
  1007. * Returns: errno
  1008. */
  1009. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  1010. {
  1011. struct gfs2_inode *ip = GFS2_I(inode);
  1012. int error;
  1013. error = trunc_start(inode, oldsize, newsize);
  1014. if (error < 0)
  1015. return error;
  1016. if (gfs2_is_stuffed(ip))
  1017. return 0;
  1018. error = trunc_dealloc(ip, newsize);
  1019. if (error == 0)
  1020. error = trunc_end(ip);
  1021. return error;
  1022. }
  1023. void gfs2_trim_blocks(struct inode *inode)
  1024. {
  1025. u64 size = inode->i_size;
  1026. int ret;
  1027. ret = do_shrink(inode, size, size);
  1028. WARN_ON(ret != 0);
  1029. }
  1030. /**
  1031. * do_grow - Touch and update inode size
  1032. * @inode: The inode
  1033. * @size: The new size
  1034. *
  1035. * This function updates the timestamps on the inode and
  1036. * may also increase the size of the inode. This function
  1037. * must not be called with @size any smaller than the current
  1038. * inode size.
  1039. *
  1040. * Although it is not strictly required to unstuff files here,
  1041. * earlier versions of GFS2 have a bug in the stuffed file reading
  1042. * code which will result in a buffer overrun if the size is larger
  1043. * than the max stuffed file size. In order to prevent this from
  1044. * occurring, such files are unstuffed, but in other cases we can
  1045. * just update the inode size directly.
  1046. *
  1047. * Returns: 0 on success, or -ve on error
  1048. */
  1049. static int do_grow(struct inode *inode, u64 size)
  1050. {
  1051. struct gfs2_inode *ip = GFS2_I(inode);
  1052. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1053. struct buffer_head *dibh;
  1054. int error;
  1055. int unstuff = 0;
  1056. if (gfs2_is_stuffed(ip) &&
  1057. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1058. error = gfs2_quota_lock_check(ip);
  1059. if (error)
  1060. return error;
  1061. error = gfs2_inplace_reserve(ip, 1, 0);
  1062. if (error)
  1063. goto do_grow_qunlock;
  1064. unstuff = 1;
  1065. }
  1066. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
  1067. (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
  1068. 0 : RES_QUOTA), 0);
  1069. if (error)
  1070. goto do_grow_release;
  1071. if (unstuff) {
  1072. error = gfs2_unstuff_dinode(ip, NULL);
  1073. if (error)
  1074. goto do_end_trans;
  1075. }
  1076. error = gfs2_meta_inode_buffer(ip, &dibh);
  1077. if (error)
  1078. goto do_end_trans;
  1079. i_size_write(inode, size);
  1080. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1081. gfs2_trans_add_meta(ip->i_gl, dibh);
  1082. gfs2_dinode_out(ip, dibh->b_data);
  1083. brelse(dibh);
  1084. do_end_trans:
  1085. gfs2_trans_end(sdp);
  1086. do_grow_release:
  1087. if (unstuff) {
  1088. gfs2_inplace_release(ip);
  1089. do_grow_qunlock:
  1090. gfs2_quota_unlock(ip);
  1091. }
  1092. return error;
  1093. }
  1094. /**
  1095. * gfs2_setattr_size - make a file a given size
  1096. * @inode: the inode
  1097. * @newsize: the size to make the file
  1098. *
  1099. * The file size can grow, shrink, or stay the same size. This
  1100. * is called holding i_mutex and an exclusive glock on the inode
  1101. * in question.
  1102. *
  1103. * Returns: errno
  1104. */
  1105. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1106. {
  1107. int ret;
  1108. u64 oldsize;
  1109. BUG_ON(!S_ISREG(inode->i_mode));
  1110. ret = inode_newsize_ok(inode, newsize);
  1111. if (ret)
  1112. return ret;
  1113. ret = get_write_access(inode);
  1114. if (ret)
  1115. return ret;
  1116. inode_dio_wait(inode);
  1117. ret = gfs2_rs_alloc(GFS2_I(inode));
  1118. if (ret)
  1119. goto out;
  1120. oldsize = inode->i_size;
  1121. if (newsize >= oldsize) {
  1122. ret = do_grow(inode, newsize);
  1123. goto out;
  1124. }
  1125. ret = do_shrink(inode, oldsize, newsize);
  1126. out:
  1127. put_write_access(inode);
  1128. return ret;
  1129. }
  1130. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1131. {
  1132. int error;
  1133. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1134. if (!error)
  1135. error = trunc_end(ip);
  1136. return error;
  1137. }
  1138. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1139. {
  1140. return trunc_dealloc(ip, 0);
  1141. }
  1142. /**
  1143. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1144. * @ip: the file being written to
  1145. * @offset: the offset to write to
  1146. * @len: the number of bytes being written
  1147. *
  1148. * Returns: 1 if an alloc is required, 0 otherwise
  1149. */
  1150. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1151. unsigned int len)
  1152. {
  1153. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1154. struct buffer_head bh;
  1155. unsigned int shift;
  1156. u64 lblock, lblock_stop, size;
  1157. u64 end_of_file;
  1158. if (!len)
  1159. return 0;
  1160. if (gfs2_is_stuffed(ip)) {
  1161. if (offset + len >
  1162. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1163. return 1;
  1164. return 0;
  1165. }
  1166. shift = sdp->sd_sb.sb_bsize_shift;
  1167. BUG_ON(gfs2_is_dir(ip));
  1168. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1169. lblock = offset >> shift;
  1170. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1171. if (lblock_stop > end_of_file)
  1172. return 1;
  1173. size = (lblock_stop - lblock) << shift;
  1174. do {
  1175. bh.b_state = 0;
  1176. bh.b_size = size;
  1177. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1178. if (!buffer_mapped(&bh))
  1179. return 1;
  1180. size -= bh.b_size;
  1181. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1182. } while(size > 0);
  1183. return 0;
  1184. }