bmap.c 32 KB

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