bmap.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/crc32.h>
  15. #include <linux/lm_interface.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "bmap.h"
  19. #include "glock.h"
  20. #include "inode.h"
  21. #include "meta_io.h"
  22. #include "quota.h"
  23. #include "rgrp.h"
  24. #include "trans.h"
  25. #include "dir.h"
  26. #include "util.h"
  27. #include "ops_address.h"
  28. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  29. * block is 512, so __u16 is fine for that. It saves stack space to
  30. * keep it small.
  31. */
  32. struct metapath {
  33. struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
  34. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  35. };
  36. typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
  37. struct buffer_head *bh, __be64 *top,
  38. __be64 *bottom, unsigned int height,
  39. void *data);
  40. struct strip_mine {
  41. int sm_first;
  42. unsigned int sm_height;
  43. };
  44. /**
  45. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  46. * @ip: the inode
  47. * @dibh: the dinode buffer
  48. * @block: the block number that was allocated
  49. * @private: any locked page held by the caller process
  50. *
  51. * Returns: errno
  52. */
  53. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  54. u64 block, struct page *page)
  55. {
  56. struct inode *inode = &ip->i_inode;
  57. struct buffer_head *bh;
  58. int release = 0;
  59. if (!page || page->index) {
  60. page = grab_cache_page(inode->i_mapping, 0);
  61. if (!page)
  62. return -ENOMEM;
  63. release = 1;
  64. }
  65. if (!PageUptodate(page)) {
  66. void *kaddr = kmap(page);
  67. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  68. ip->i_di.di_size);
  69. memset(kaddr + ip->i_di.di_size, 0,
  70. PAGE_CACHE_SIZE - ip->i_di.di_size);
  71. kunmap(page);
  72. SetPageUptodate(page);
  73. }
  74. if (!page_has_buffers(page))
  75. create_empty_buffers(page, 1 << inode->i_blkbits,
  76. (1 << BH_Uptodate));
  77. bh = page_buffers(page);
  78. if (!buffer_mapped(bh))
  79. map_bh(bh, inode->i_sb, block);
  80. set_buffer_uptodate(bh);
  81. if (!gfs2_is_jdata(ip))
  82. mark_buffer_dirty(bh);
  83. if (!gfs2_is_writeback(ip))
  84. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  85. if (release) {
  86. unlock_page(page);
  87. page_cache_release(page);
  88. }
  89. return 0;
  90. }
  91. /**
  92. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  93. * @ip: The GFS2 inode to unstuff
  94. * @unstuffer: the routine that handles unstuffing a non-zero length file
  95. * @private: private data for the unstuffer
  96. *
  97. * This routine unstuffs a dinode and returns it to a "normal" state such
  98. * that the height can be grown in the traditional way.
  99. *
  100. * Returns: errno
  101. */
  102. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  103. {
  104. struct buffer_head *bh, *dibh;
  105. struct gfs2_dinode *di;
  106. u64 block = 0;
  107. int isdir = gfs2_is_dir(ip);
  108. int error;
  109. down_write(&ip->i_rw_mutex);
  110. error = gfs2_meta_inode_buffer(ip, &dibh);
  111. if (error)
  112. goto out;
  113. if (ip->i_di.di_size) {
  114. /* Get a free block, fill it with the stuffed data,
  115. and write it out to disk */
  116. unsigned int n = 1;
  117. block = gfs2_alloc_block(ip, &n);
  118. if (isdir) {
  119. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  120. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  121. if (error)
  122. goto out_brelse;
  123. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  124. dibh, sizeof(struct gfs2_dinode));
  125. brelse(bh);
  126. } else {
  127. error = gfs2_unstuffer_page(ip, dibh, block, page);
  128. if (error)
  129. goto out_brelse;
  130. }
  131. }
  132. /* Set up the pointer to the new block */
  133. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  134. di = (struct gfs2_dinode *)dibh->b_data;
  135. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  136. if (ip->i_di.di_size) {
  137. *(__be64 *)(di + 1) = cpu_to_be64(block);
  138. gfs2_add_inode_blocks(&ip->i_inode, 1);
  139. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  140. }
  141. ip->i_height = 1;
  142. di->di_height = cpu_to_be16(1);
  143. out_brelse:
  144. brelse(dibh);
  145. out:
  146. up_write(&ip->i_rw_mutex);
  147. return error;
  148. }
  149. /**
  150. * find_metapath - Find path through the metadata tree
  151. * @sdp: The superblock
  152. * @mp: The metapath to return the result in
  153. * @block: The disk block to look up
  154. * @height: The pre-calculated height of the metadata tree
  155. *
  156. * This routine returns a struct metapath structure that defines a path
  157. * through the metadata of inode "ip" to get to block "block".
  158. *
  159. * Example:
  160. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  161. * filesystem with a blocksize of 4096.
  162. *
  163. * find_metapath() would return a struct metapath structure set to:
  164. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  165. * and mp_list[2] = 165.
  166. *
  167. * That means that in order to get to the block containing the byte at
  168. * offset 101342453, we would load the indirect block pointed to by pointer
  169. * 0 in the dinode. We would then load the indirect block pointed to by
  170. * pointer 48 in that indirect block. We would then load the data block
  171. * pointed to by pointer 165 in that indirect block.
  172. *
  173. * ----------------------------------------
  174. * | Dinode | |
  175. * | | 4|
  176. * | |0 1 2 3 4 5 9|
  177. * | | 6|
  178. * ----------------------------------------
  179. * |
  180. * |
  181. * V
  182. * ----------------------------------------
  183. * | Indirect Block |
  184. * | 5|
  185. * | 4 4 4 4 4 5 5 1|
  186. * |0 5 6 7 8 9 0 1 2|
  187. * ----------------------------------------
  188. * |
  189. * |
  190. * V
  191. * ----------------------------------------
  192. * | Indirect Block |
  193. * | 1 1 1 1 1 5|
  194. * | 6 6 6 6 6 1|
  195. * |0 3 4 5 6 7 2|
  196. * ----------------------------------------
  197. * |
  198. * |
  199. * V
  200. * ----------------------------------------
  201. * | Data block containing offset |
  202. * | 101342453 |
  203. * | |
  204. * | |
  205. * ----------------------------------------
  206. *
  207. */
  208. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  209. struct metapath *mp, unsigned int height)
  210. {
  211. unsigned int i;
  212. for (i = height; i--;)
  213. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  214. }
  215. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  216. {
  217. if (mp->mp_list[0] == 0)
  218. return 2;
  219. return 1;
  220. }
  221. /**
  222. * metapointer - Return pointer to start of metadata in a buffer
  223. * @height: The metadata height (0 = dinode)
  224. * @mp: The metapath
  225. *
  226. * Return a pointer to the block number of the next height of the metadata
  227. * tree given a buffer containing the pointer to the current height of the
  228. * metadata tree.
  229. */
  230. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  231. {
  232. struct buffer_head *bh = mp->mp_bh[height];
  233. unsigned int head_size = (height > 0) ?
  234. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  235. return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  236. }
  237. /**
  238. * lookup_metapath - Walk the metadata tree to a specific point
  239. * @ip: The inode
  240. * @mp: The metapath
  241. *
  242. * Assumes that the inode's buffer has already been looked up and
  243. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  244. * by find_metapath().
  245. *
  246. * If this function encounters part of the tree which has not been
  247. * allocated, it returns the current height of the tree at the point
  248. * at which it found the unallocated block. Blocks which are found are
  249. * added to the mp->mp_bh[] list.
  250. *
  251. * Returns: error or height of metadata tree
  252. */
  253. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  254. {
  255. unsigned int end_of_metadata = ip->i_height - 1;
  256. unsigned int x;
  257. __be64 *ptr;
  258. u64 dblock;
  259. int ret;
  260. for (x = 0; x < end_of_metadata; x++) {
  261. ptr = metapointer(x, mp);
  262. dblock = be64_to_cpu(*ptr);
  263. if (!dblock)
  264. return x + 1;
  265. ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
  266. if (ret)
  267. return ret;
  268. }
  269. return ip->i_height;
  270. }
  271. static inline void release_metapath(struct metapath *mp)
  272. {
  273. int i;
  274. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  275. if (mp->mp_bh[i] == NULL)
  276. break;
  277. brelse(mp->mp_bh[i]);
  278. }
  279. }
  280. /**
  281. * gfs2_extent_length - Returns length of an extent of blocks
  282. * @start: Start of the buffer
  283. * @len: Length of the buffer in bytes
  284. * @ptr: Current position in the buffer
  285. * @limit: Max extent length to return (0 = unlimited)
  286. * @eob: Set to 1 if we hit "end of block"
  287. *
  288. * If the first block is zero (unallocated) it will return the number of
  289. * unallocated blocks in the extent, otherwise it will return the number
  290. * of contiguous blocks in the extent.
  291. *
  292. * Returns: The length of the extent (minimum of one block)
  293. */
  294. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
  295. {
  296. const __be64 *end = (start + len);
  297. const __be64 *first = ptr;
  298. u64 d = be64_to_cpu(*ptr);
  299. *eob = 0;
  300. do {
  301. ptr++;
  302. if (ptr >= end)
  303. break;
  304. if (limit && --limit == 0)
  305. break;
  306. if (d)
  307. d++;
  308. } while(be64_to_cpu(*ptr) == d);
  309. if (ptr >= end)
  310. *eob = 1;
  311. return (ptr - first);
  312. }
  313. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  314. {
  315. if (create)
  316. down_write(&ip->i_rw_mutex);
  317. else
  318. down_read(&ip->i_rw_mutex);
  319. }
  320. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  321. {
  322. if (create)
  323. up_write(&ip->i_rw_mutex);
  324. else
  325. up_read(&ip->i_rw_mutex);
  326. }
  327. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  328. struct gfs2_glock *gl, unsigned int i,
  329. unsigned offset, u64 bn)
  330. {
  331. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  332. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  333. sizeof(struct gfs2_dinode)));
  334. BUG_ON(i < 1);
  335. BUG_ON(mp->mp_bh[i] != NULL);
  336. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  337. gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
  338. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  339. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  340. ptr += offset;
  341. *ptr = cpu_to_be64(bn);
  342. return ptr;
  343. }
  344. enum alloc_state {
  345. ALLOC_DATA = 0,
  346. ALLOC_GROW_DEPTH = 1,
  347. ALLOC_GROW_HEIGHT = 2,
  348. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  349. };
  350. /**
  351. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  352. * @inode: The GFS2 inode
  353. * @lblock: The logical starting block of the extent
  354. * @bh_map: This is used to return the mapping details
  355. * @mp: The metapath
  356. * @sheight: The starting height (i.e. whats already mapped)
  357. * @height: The height to build to
  358. * @maxlen: The max number of data blocks to alloc
  359. *
  360. * In this routine we may have to alloc:
  361. * i) Indirect blocks to grow the metadata tree height
  362. * ii) Indirect blocks to fill in lower part of the metadata tree
  363. * iii) Data blocks
  364. *
  365. * The function is in two parts. The first part works out the total
  366. * number of blocks which we need. The second part does the actual
  367. * allocation asking for an extent at a time (if enough contiguous free
  368. * blocks are available, there will only be one request per bmap call)
  369. * and uses the state machine to initialise the blocks in order.
  370. *
  371. * Returns: errno on error
  372. */
  373. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  374. struct buffer_head *bh_map, struct metapath *mp,
  375. const unsigned int sheight,
  376. const unsigned int height,
  377. const unsigned int maxlen)
  378. {
  379. struct gfs2_inode *ip = GFS2_I(inode);
  380. struct gfs2_sbd *sdp = GFS2_SB(inode);
  381. struct buffer_head *dibh = mp->mp_bh[0];
  382. u64 bn, dblock = 0;
  383. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  384. unsigned dblks = 0;
  385. unsigned ptrs_per_blk;
  386. const unsigned end_of_metadata = height - 1;
  387. int eob = 0;
  388. enum alloc_state state;
  389. __be64 *ptr;
  390. __be64 zero_bn = 0;
  391. BUG_ON(sheight < 1);
  392. BUG_ON(dibh == NULL);
  393. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  394. if (height == sheight) {
  395. struct buffer_head *bh;
  396. /* Bottom indirect block exists, find unalloced extent size */
  397. ptr = metapointer(end_of_metadata, mp);
  398. bh = mp->mp_bh[end_of_metadata];
  399. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  400. &eob);
  401. BUG_ON(dblks < 1);
  402. state = ALLOC_DATA;
  403. } else {
  404. /* Need to allocate indirect blocks */
  405. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  406. dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
  407. if (height == ip->i_height) {
  408. /* Writing into existing tree, extend tree down */
  409. iblks = height - sheight;
  410. state = ALLOC_GROW_DEPTH;
  411. } else {
  412. /* Building up tree height */
  413. state = ALLOC_GROW_HEIGHT;
  414. iblks = height - ip->i_height;
  415. branch_start = metapath_branch_start(mp);
  416. iblks += (height - branch_start);
  417. }
  418. }
  419. /* start of the second part of the function (state machine) */
  420. blks = dblks + iblks;
  421. i = sheight;
  422. do {
  423. n = blks - alloced;
  424. bn = gfs2_alloc_block(ip, &n);
  425. alloced += n;
  426. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  427. gfs2_trans_add_unrevoke(sdp, bn, n);
  428. switch (state) {
  429. /* Growing height of tree */
  430. case ALLOC_GROW_HEIGHT:
  431. if (i == 1) {
  432. ptr = (__be64 *)(dibh->b_data +
  433. sizeof(struct gfs2_dinode));
  434. zero_bn = *ptr;
  435. }
  436. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  437. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  438. if (i - 1 == height - ip->i_height) {
  439. i--;
  440. gfs2_buffer_copy_tail(mp->mp_bh[i],
  441. sizeof(struct gfs2_meta_header),
  442. dibh, sizeof(struct gfs2_dinode));
  443. gfs2_buffer_clear_tail(dibh,
  444. sizeof(struct gfs2_dinode) +
  445. sizeof(__be64));
  446. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  447. sizeof(struct gfs2_meta_header));
  448. *ptr = zero_bn;
  449. state = ALLOC_GROW_DEPTH;
  450. for(i = branch_start; i < height; i++) {
  451. if (mp->mp_bh[i] == NULL)
  452. break;
  453. brelse(mp->mp_bh[i]);
  454. mp->mp_bh[i] = NULL;
  455. }
  456. i = branch_start;
  457. }
  458. if (n == 0)
  459. break;
  460. /* Branching from existing tree */
  461. case ALLOC_GROW_DEPTH:
  462. if (i > 1 && i < height)
  463. gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
  464. for (; i < height && n > 0; i++, n--)
  465. gfs2_indirect_init(mp, ip->i_gl, i,
  466. mp->mp_list[i-1], bn++);
  467. if (i == height)
  468. state = ALLOC_DATA;
  469. if (n == 0)
  470. break;
  471. /* Tree complete, adding data blocks */
  472. case ALLOC_DATA:
  473. BUG_ON(n > dblks);
  474. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  475. gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
  476. dblks = n;
  477. ptr = metapointer(end_of_metadata, mp);
  478. dblock = bn;
  479. while (n-- > 0)
  480. *ptr++ = cpu_to_be64(bn++);
  481. break;
  482. }
  483. } while (state != ALLOC_DATA);
  484. ip->i_height = height;
  485. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  486. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  487. map_bh(bh_map, inode->i_sb, dblock);
  488. bh_map->b_size = dblks << inode->i_blkbits;
  489. set_buffer_new(bh_map);
  490. return 0;
  491. }
  492. /**
  493. * gfs2_block_map - Map a block from an inode to a disk block
  494. * @inode: The inode
  495. * @lblock: The logical block number
  496. * @bh_map: The bh to be mapped
  497. * @create: True if its ok to alloc blocks to satify the request
  498. *
  499. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  500. * read of metadata will be required before the next block can be
  501. * mapped. Sets buffer_new() if new blocks were allocated.
  502. *
  503. * Returns: errno
  504. */
  505. int gfs2_block_map(struct inode *inode, sector_t lblock,
  506. struct buffer_head *bh_map, int create)
  507. {
  508. struct gfs2_inode *ip = GFS2_I(inode);
  509. struct gfs2_sbd *sdp = GFS2_SB(inode);
  510. unsigned int bsize = sdp->sd_sb.sb_bsize;
  511. const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  512. const u64 *arr = sdp->sd_heightsize;
  513. __be64 *ptr;
  514. u64 size;
  515. struct metapath mp;
  516. int ret;
  517. int eob;
  518. unsigned int len;
  519. struct buffer_head *bh;
  520. u8 height;
  521. BUG_ON(maxlen == 0);
  522. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  523. bmap_lock(ip, create);
  524. clear_buffer_mapped(bh_map);
  525. clear_buffer_new(bh_map);
  526. clear_buffer_boundary(bh_map);
  527. if (gfs2_is_dir(ip)) {
  528. bsize = sdp->sd_jbsize;
  529. arr = sdp->sd_jheightsize;
  530. }
  531. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  532. if (ret)
  533. goto out;
  534. height = ip->i_height;
  535. size = (lblock + 1) * bsize;
  536. while (size > arr[height])
  537. height++;
  538. find_metapath(sdp, lblock, &mp, height);
  539. ret = 1;
  540. if (height > ip->i_height || gfs2_is_stuffed(ip))
  541. goto do_alloc;
  542. ret = lookup_metapath(ip, &mp);
  543. if (ret < 0)
  544. goto out;
  545. if (ret != ip->i_height)
  546. goto do_alloc;
  547. ptr = metapointer(ip->i_height - 1, &mp);
  548. if (*ptr == 0)
  549. goto do_alloc;
  550. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  551. bh = mp.mp_bh[ip->i_height - 1];
  552. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  553. bh_map->b_size = (len << inode->i_blkbits);
  554. if (eob)
  555. set_buffer_boundary(bh_map);
  556. ret = 0;
  557. out:
  558. release_metapath(&mp);
  559. bmap_unlock(ip, create);
  560. return ret;
  561. do_alloc:
  562. /* All allocations are done here, firstly check create flag */
  563. if (!create) {
  564. BUG_ON(gfs2_is_stuffed(ip));
  565. ret = 0;
  566. goto out;
  567. }
  568. /* At this point ret is the tree depth of already allocated blocks */
  569. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  570. goto out;
  571. }
  572. /*
  573. * Deprecated: do not use in new code
  574. */
  575. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  576. {
  577. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  578. int ret;
  579. int create = *new;
  580. BUG_ON(!extlen);
  581. BUG_ON(!dblock);
  582. BUG_ON(!new);
  583. bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
  584. ret = gfs2_block_map(inode, lblock, &bh, create);
  585. *extlen = bh.b_size >> inode->i_blkbits;
  586. *dblock = bh.b_blocknr;
  587. if (buffer_new(&bh))
  588. *new = 1;
  589. else
  590. *new = 0;
  591. return ret;
  592. }
  593. /**
  594. * recursive_scan - recursively scan through the end of a file
  595. * @ip: the inode
  596. * @dibh: the dinode buffer
  597. * @mp: the path through the metadata to the point to start
  598. * @height: the height the recursion is at
  599. * @block: the indirect block to look at
  600. * @first: 1 if this is the first block
  601. * @bc: the call to make for each piece of metadata
  602. * @data: data opaque to this function to pass to @bc
  603. *
  604. * When this is first called @height and @block should be zero and
  605. * @first should be 1.
  606. *
  607. * Returns: errno
  608. */
  609. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  610. struct metapath *mp, unsigned int height,
  611. u64 block, int first, block_call_t bc,
  612. void *data)
  613. {
  614. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  615. struct buffer_head *bh = NULL;
  616. __be64 *top, *bottom;
  617. u64 bn;
  618. int error;
  619. int mh_size = sizeof(struct gfs2_meta_header);
  620. if (!height) {
  621. error = gfs2_meta_inode_buffer(ip, &bh);
  622. if (error)
  623. return error;
  624. dibh = bh;
  625. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  626. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  627. } else {
  628. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  629. if (error)
  630. return error;
  631. top = (__be64 *)(bh->b_data + mh_size) +
  632. (first ? mp->mp_list[height] : 0);
  633. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  634. }
  635. error = bc(ip, dibh, bh, top, bottom, height, data);
  636. if (error)
  637. goto out;
  638. if (height < ip->i_height - 1)
  639. for (; top < bottom; top++, first = 0) {
  640. if (!*top)
  641. continue;
  642. bn = be64_to_cpu(*top);
  643. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  644. first, bc, data);
  645. if (error)
  646. break;
  647. }
  648. out:
  649. brelse(bh);
  650. return error;
  651. }
  652. /**
  653. * do_strip - Look for a layer a particular layer of the file and strip it off
  654. * @ip: the inode
  655. * @dibh: the dinode buffer
  656. * @bh: A buffer of pointers
  657. * @top: The first pointer in the buffer
  658. * @bottom: One more than the last pointer
  659. * @height: the height this buffer is at
  660. * @data: a pointer to a struct strip_mine
  661. *
  662. * Returns: errno
  663. */
  664. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  665. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  666. unsigned int height, void *data)
  667. {
  668. struct strip_mine *sm = data;
  669. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  670. struct gfs2_rgrp_list rlist;
  671. u64 bn, bstart;
  672. u32 blen;
  673. __be64 *p;
  674. unsigned int rg_blocks = 0;
  675. int metadata;
  676. unsigned int revokes = 0;
  677. int x;
  678. int error;
  679. if (!*top)
  680. sm->sm_first = 0;
  681. if (height != sm->sm_height)
  682. return 0;
  683. if (sm->sm_first) {
  684. top++;
  685. sm->sm_first = 0;
  686. }
  687. metadata = (height != ip->i_height - 1);
  688. if (metadata)
  689. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  690. error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
  691. if (error)
  692. return error;
  693. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  694. bstart = 0;
  695. blen = 0;
  696. for (p = top; p < bottom; p++) {
  697. if (!*p)
  698. continue;
  699. bn = be64_to_cpu(*p);
  700. if (bstart + blen == bn)
  701. blen++;
  702. else {
  703. if (bstart)
  704. gfs2_rlist_add(sdp, &rlist, bstart);
  705. bstart = bn;
  706. blen = 1;
  707. }
  708. }
  709. if (bstart)
  710. gfs2_rlist_add(sdp, &rlist, bstart);
  711. else
  712. goto out; /* Nothing to do */
  713. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  714. for (x = 0; x < rlist.rl_rgrps; x++) {
  715. struct gfs2_rgrpd *rgd;
  716. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  717. rg_blocks += rgd->rd_length;
  718. }
  719. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  720. if (error)
  721. goto out_rlist;
  722. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  723. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  724. revokes);
  725. if (error)
  726. goto out_rg_gunlock;
  727. down_write(&ip->i_rw_mutex);
  728. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  729. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  730. bstart = 0;
  731. blen = 0;
  732. for (p = top; p < bottom; p++) {
  733. if (!*p)
  734. continue;
  735. bn = be64_to_cpu(*p);
  736. if (bstart + blen == bn)
  737. blen++;
  738. else {
  739. if (bstart) {
  740. if (metadata)
  741. gfs2_free_meta(ip, bstart, blen);
  742. else
  743. gfs2_free_data(ip, bstart, blen);
  744. }
  745. bstart = bn;
  746. blen = 1;
  747. }
  748. *p = 0;
  749. gfs2_add_inode_blocks(&ip->i_inode, -1);
  750. }
  751. if (bstart) {
  752. if (metadata)
  753. gfs2_free_meta(ip, bstart, blen);
  754. else
  755. gfs2_free_data(ip, bstart, blen);
  756. }
  757. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  758. gfs2_dinode_out(ip, dibh->b_data);
  759. up_write(&ip->i_rw_mutex);
  760. gfs2_trans_end(sdp);
  761. out_rg_gunlock:
  762. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  763. out_rlist:
  764. gfs2_rlist_free(&rlist);
  765. out:
  766. gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
  767. return error;
  768. }
  769. /**
  770. * do_grow - Make a file look bigger than it is
  771. * @ip: the inode
  772. * @size: the size to set the file to
  773. *
  774. * Called with an exclusive lock on @ip.
  775. *
  776. * Returns: errno
  777. */
  778. static int do_grow(struct gfs2_inode *ip, u64 size)
  779. {
  780. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  781. struct gfs2_alloc *al;
  782. struct buffer_head *dibh;
  783. int error;
  784. al = gfs2_alloc_get(ip);
  785. if (!al)
  786. return -ENOMEM;
  787. error = gfs2_quota_lock_check(ip);
  788. if (error)
  789. goto out;
  790. al->al_requested = sdp->sd_max_height + RES_DATA;
  791. error = gfs2_inplace_reserve(ip);
  792. if (error)
  793. goto out_gunlock_q;
  794. error = gfs2_trans_begin(sdp,
  795. sdp->sd_max_height + al->al_rgd->rd_length +
  796. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  797. if (error)
  798. goto out_ipres;
  799. error = gfs2_meta_inode_buffer(ip, &dibh);
  800. if (error)
  801. goto out_end_trans;
  802. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  803. if (gfs2_is_stuffed(ip)) {
  804. error = gfs2_unstuff_dinode(ip, NULL);
  805. if (error)
  806. goto out_brelse;
  807. }
  808. }
  809. ip->i_di.di_size = size;
  810. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  811. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  812. gfs2_dinode_out(ip, dibh->b_data);
  813. out_brelse:
  814. brelse(dibh);
  815. out_end_trans:
  816. gfs2_trans_end(sdp);
  817. out_ipres:
  818. gfs2_inplace_release(ip);
  819. out_gunlock_q:
  820. gfs2_quota_unlock(ip);
  821. out:
  822. gfs2_alloc_put(ip);
  823. return error;
  824. }
  825. /**
  826. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  827. *
  828. * This is partly borrowed from ext3.
  829. */
  830. static int gfs2_block_truncate_page(struct address_space *mapping)
  831. {
  832. struct inode *inode = mapping->host;
  833. struct gfs2_inode *ip = GFS2_I(inode);
  834. loff_t from = inode->i_size;
  835. unsigned long index = from >> PAGE_CACHE_SHIFT;
  836. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  837. unsigned blocksize, iblock, length, pos;
  838. struct buffer_head *bh;
  839. struct page *page;
  840. int err;
  841. page = grab_cache_page(mapping, index);
  842. if (!page)
  843. return 0;
  844. blocksize = inode->i_sb->s_blocksize;
  845. length = blocksize - (offset & (blocksize - 1));
  846. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  847. if (!page_has_buffers(page))
  848. create_empty_buffers(page, blocksize, 0);
  849. /* Find the buffer that contains "offset" */
  850. bh = page_buffers(page);
  851. pos = blocksize;
  852. while (offset >= pos) {
  853. bh = bh->b_this_page;
  854. iblock++;
  855. pos += blocksize;
  856. }
  857. err = 0;
  858. if (!buffer_mapped(bh)) {
  859. gfs2_block_map(inode, iblock, bh, 0);
  860. /* unmapped? It's a hole - nothing to do */
  861. if (!buffer_mapped(bh))
  862. goto unlock;
  863. }
  864. /* Ok, it's mapped. Make sure it's up-to-date */
  865. if (PageUptodate(page))
  866. set_buffer_uptodate(bh);
  867. if (!buffer_uptodate(bh)) {
  868. err = -EIO;
  869. ll_rw_block(READ, 1, &bh);
  870. wait_on_buffer(bh);
  871. /* Uhhuh. Read error. Complain and punt. */
  872. if (!buffer_uptodate(bh))
  873. goto unlock;
  874. err = 0;
  875. }
  876. if (!gfs2_is_writeback(ip))
  877. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  878. zero_user(page, offset, length);
  879. unlock:
  880. unlock_page(page);
  881. page_cache_release(page);
  882. return err;
  883. }
  884. static int trunc_start(struct gfs2_inode *ip, u64 size)
  885. {
  886. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  887. struct buffer_head *dibh;
  888. int journaled = gfs2_is_jdata(ip);
  889. int error;
  890. error = gfs2_trans_begin(sdp,
  891. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  892. if (error)
  893. return error;
  894. error = gfs2_meta_inode_buffer(ip, &dibh);
  895. if (error)
  896. goto out;
  897. if (gfs2_is_stuffed(ip)) {
  898. ip->i_di.di_size = size;
  899. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  900. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  901. gfs2_dinode_out(ip, dibh->b_data);
  902. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  903. error = 1;
  904. } else {
  905. if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
  906. error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
  907. if (!error) {
  908. ip->i_di.di_size = size;
  909. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  910. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  911. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  912. gfs2_dinode_out(ip, dibh->b_data);
  913. }
  914. }
  915. brelse(dibh);
  916. out:
  917. gfs2_trans_end(sdp);
  918. return error;
  919. }
  920. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  921. {
  922. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  923. unsigned int height = ip->i_height;
  924. u64 lblock;
  925. struct metapath mp;
  926. int error;
  927. if (!size)
  928. lblock = 0;
  929. else
  930. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  931. find_metapath(sdp, lblock, &mp, ip->i_height);
  932. if (!gfs2_alloc_get(ip))
  933. return -ENOMEM;
  934. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  935. if (error)
  936. goto out;
  937. while (height--) {
  938. struct strip_mine sm;
  939. sm.sm_first = !!size;
  940. sm.sm_height = height;
  941. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  942. if (error)
  943. break;
  944. }
  945. gfs2_quota_unhold(ip);
  946. out:
  947. gfs2_alloc_put(ip);
  948. return error;
  949. }
  950. static int trunc_end(struct gfs2_inode *ip)
  951. {
  952. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  953. struct buffer_head *dibh;
  954. int error;
  955. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  956. if (error)
  957. return error;
  958. down_write(&ip->i_rw_mutex);
  959. error = gfs2_meta_inode_buffer(ip, &dibh);
  960. if (error)
  961. goto out;
  962. if (!ip->i_di.di_size) {
  963. ip->i_height = 0;
  964. ip->i_goal = ip->i_no_addr;
  965. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  966. }
  967. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  968. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  969. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  970. gfs2_dinode_out(ip, dibh->b_data);
  971. brelse(dibh);
  972. out:
  973. up_write(&ip->i_rw_mutex);
  974. gfs2_trans_end(sdp);
  975. return error;
  976. }
  977. /**
  978. * do_shrink - make a file smaller
  979. * @ip: the inode
  980. * @size: the size to make the file
  981. * @truncator: function to truncate the last partial block
  982. *
  983. * Called with an exclusive lock on @ip.
  984. *
  985. * Returns: errno
  986. */
  987. static int do_shrink(struct gfs2_inode *ip, u64 size)
  988. {
  989. int error;
  990. error = trunc_start(ip, size);
  991. if (error < 0)
  992. return error;
  993. if (error > 0)
  994. return 0;
  995. error = trunc_dealloc(ip, size);
  996. if (!error)
  997. error = trunc_end(ip);
  998. return error;
  999. }
  1000. static int do_touch(struct gfs2_inode *ip, u64 size)
  1001. {
  1002. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1003. struct buffer_head *dibh;
  1004. int error;
  1005. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1006. if (error)
  1007. return error;
  1008. down_write(&ip->i_rw_mutex);
  1009. error = gfs2_meta_inode_buffer(ip, &dibh);
  1010. if (error)
  1011. goto do_touch_out;
  1012. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1013. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1014. gfs2_dinode_out(ip, dibh->b_data);
  1015. brelse(dibh);
  1016. do_touch_out:
  1017. up_write(&ip->i_rw_mutex);
  1018. gfs2_trans_end(sdp);
  1019. return error;
  1020. }
  1021. /**
  1022. * gfs2_truncatei - make a file a given size
  1023. * @ip: the inode
  1024. * @size: the size to make the file
  1025. * @truncator: function to truncate the last partial block
  1026. *
  1027. * The file size can grow, shrink, or stay the same size.
  1028. *
  1029. * Returns: errno
  1030. */
  1031. int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
  1032. {
  1033. int error;
  1034. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
  1035. return -EINVAL;
  1036. if (size > ip->i_di.di_size)
  1037. error = do_grow(ip, size);
  1038. else if (size < ip->i_di.di_size)
  1039. error = do_shrink(ip, size);
  1040. else
  1041. /* update time stamps */
  1042. error = do_touch(ip, size);
  1043. return error;
  1044. }
  1045. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1046. {
  1047. int error;
  1048. error = trunc_dealloc(ip, ip->i_di.di_size);
  1049. if (!error)
  1050. error = trunc_end(ip);
  1051. return error;
  1052. }
  1053. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1054. {
  1055. return trunc_dealloc(ip, 0);
  1056. }
  1057. /**
  1058. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  1059. * @ip: the file
  1060. * @len: the number of bytes to be written to the file
  1061. * @data_blocks: returns the number of data blocks required
  1062. * @ind_blocks: returns the number of indirect blocks required
  1063. *
  1064. */
  1065. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  1066. unsigned int *data_blocks, unsigned int *ind_blocks)
  1067. {
  1068. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1069. unsigned int tmp;
  1070. if (gfs2_is_dir(ip)) {
  1071. *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
  1072. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  1073. } else {
  1074. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  1075. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  1076. }
  1077. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  1078. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1079. *ind_blocks += tmp;
  1080. }
  1081. }
  1082. /**
  1083. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1084. * @ip: the file being written to
  1085. * @offset: the offset to write to
  1086. * @len: the number of bytes being written
  1087. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  1088. *
  1089. * Returns: errno
  1090. */
  1091. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1092. unsigned int len, int *alloc_required)
  1093. {
  1094. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1095. struct buffer_head bh;
  1096. unsigned int shift;
  1097. u64 lblock, lblock_stop, size;
  1098. *alloc_required = 0;
  1099. if (!len)
  1100. return 0;
  1101. if (gfs2_is_stuffed(ip)) {
  1102. if (offset + len >
  1103. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1104. *alloc_required = 1;
  1105. return 0;
  1106. }
  1107. *alloc_required = 1;
  1108. shift = sdp->sd_sb.sb_bsize_shift;
  1109. if (gfs2_is_dir(ip)) {
  1110. unsigned int bsize = sdp->sd_jbsize;
  1111. lblock = offset;
  1112. do_div(lblock, bsize);
  1113. lblock_stop = offset + len + bsize - 1;
  1114. do_div(lblock_stop, bsize);
  1115. } else {
  1116. u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
  1117. lblock = offset >> shift;
  1118. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1119. if (lblock_stop > end_of_file)
  1120. return 0;
  1121. }
  1122. size = (lblock_stop - lblock) << shift;
  1123. do {
  1124. bh.b_state = 0;
  1125. bh.b_size = size;
  1126. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1127. if (!buffer_mapped(&bh))
  1128. return 0;
  1129. size -= bh.b_size;
  1130. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1131. } while(size > 0);
  1132. *alloc_required = 0;
  1133. return 0;
  1134. }