bmap.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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, 0, &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 = 0;
  654. if (!*top)
  655. sm->sm_first = 0;
  656. if (height != sm->sm_height)
  657. return 0;
  658. if (sm->sm_first) {
  659. top++;
  660. sm->sm_first = 0;
  661. }
  662. metadata = (height != ip->i_height - 1);
  663. if (metadata)
  664. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  665. else if (ip->i_depth)
  666. revokes = sdp->sd_inptrs;
  667. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  668. bstart = 0;
  669. blen = 0;
  670. for (p = top; p < bottom; p++) {
  671. if (!*p)
  672. continue;
  673. bn = be64_to_cpu(*p);
  674. if (bstart + blen == bn)
  675. blen++;
  676. else {
  677. if (bstart)
  678. gfs2_rlist_add(ip, &rlist, bstart);
  679. bstart = bn;
  680. blen = 1;
  681. }
  682. }
  683. if (bstart)
  684. gfs2_rlist_add(ip, &rlist, bstart);
  685. else
  686. goto out; /* Nothing to do */
  687. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  688. for (x = 0; x < rlist.rl_rgrps; x++) {
  689. struct gfs2_rgrpd *rgd;
  690. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  691. rg_blocks += rgd->rd_length;
  692. }
  693. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  694. if (error)
  695. goto out_rlist;
  696. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  697. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  698. revokes);
  699. if (error)
  700. goto out_rg_gunlock;
  701. down_write(&ip->i_rw_mutex);
  702. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  703. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  704. bstart = 0;
  705. blen = 0;
  706. btotal = 0;
  707. for (p = top; p < bottom; p++) {
  708. if (!*p)
  709. continue;
  710. bn = be64_to_cpu(*p);
  711. if (bstart + blen == bn)
  712. blen++;
  713. else {
  714. if (bstart) {
  715. __gfs2_free_blocks(ip, bstart, blen, metadata);
  716. btotal += blen;
  717. }
  718. bstart = bn;
  719. blen = 1;
  720. }
  721. *p = 0;
  722. gfs2_add_inode_blocks(&ip->i_inode, -1);
  723. }
  724. if (bstart) {
  725. __gfs2_free_blocks(ip, bstart, blen, metadata);
  726. btotal += blen;
  727. }
  728. gfs2_statfs_change(sdp, 0, +btotal, 0);
  729. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  730. ip->i_inode.i_gid);
  731. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  732. gfs2_dinode_out(ip, dibh->b_data);
  733. up_write(&ip->i_rw_mutex);
  734. gfs2_trans_end(sdp);
  735. out_rg_gunlock:
  736. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  737. out_rlist:
  738. gfs2_rlist_free(&rlist);
  739. out:
  740. return error;
  741. }
  742. /**
  743. * recursive_scan - recursively scan through the end of a file
  744. * @ip: the inode
  745. * @dibh: the dinode buffer
  746. * @mp: the path through the metadata to the point to start
  747. * @height: the height the recursion is at
  748. * @block: the indirect block to look at
  749. * @first: 1 if this is the first block
  750. * @sm: data opaque to this function to pass to @bc
  751. *
  752. * When this is first called @height and @block should be zero and
  753. * @first should be 1.
  754. *
  755. * Returns: errno
  756. */
  757. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  758. struct metapath *mp, unsigned int height,
  759. u64 block, int first, struct strip_mine *sm)
  760. {
  761. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  762. struct buffer_head *bh = NULL;
  763. __be64 *top, *bottom;
  764. u64 bn;
  765. int error;
  766. int mh_size = sizeof(struct gfs2_meta_header);
  767. if (!height) {
  768. error = gfs2_meta_inode_buffer(ip, &bh);
  769. if (error)
  770. return error;
  771. dibh = bh;
  772. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  773. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  774. } else {
  775. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  776. if (error)
  777. return error;
  778. top = (__be64 *)(bh->b_data + mh_size) +
  779. (first ? mp->mp_list[height] : 0);
  780. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  781. }
  782. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  783. if (error)
  784. goto out;
  785. if (height < ip->i_height - 1) {
  786. gfs2_metapath_ra(ip->i_gl, bh, top);
  787. for (; top < bottom; top++, first = 0) {
  788. if (!*top)
  789. continue;
  790. bn = be64_to_cpu(*top);
  791. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  792. first, sm);
  793. if (error)
  794. break;
  795. }
  796. }
  797. out:
  798. brelse(bh);
  799. return error;
  800. }
  801. /**
  802. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  803. *
  804. * This is partly borrowed from ext3.
  805. */
  806. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  807. {
  808. struct inode *inode = mapping->host;
  809. struct gfs2_inode *ip = GFS2_I(inode);
  810. unsigned long index = from >> PAGE_CACHE_SHIFT;
  811. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  812. unsigned blocksize, iblock, length, pos;
  813. struct buffer_head *bh;
  814. struct page *page;
  815. int err;
  816. page = find_or_create_page(mapping, index, GFP_NOFS);
  817. if (!page)
  818. return 0;
  819. blocksize = inode->i_sb->s_blocksize;
  820. length = blocksize - (offset & (blocksize - 1));
  821. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  822. if (!page_has_buffers(page))
  823. create_empty_buffers(page, blocksize, 0);
  824. /* Find the buffer that contains "offset" */
  825. bh = page_buffers(page);
  826. pos = blocksize;
  827. while (offset >= pos) {
  828. bh = bh->b_this_page;
  829. iblock++;
  830. pos += blocksize;
  831. }
  832. err = 0;
  833. if (!buffer_mapped(bh)) {
  834. gfs2_block_map(inode, iblock, bh, 0);
  835. /* unmapped? It's a hole - nothing to do */
  836. if (!buffer_mapped(bh))
  837. goto unlock;
  838. }
  839. /* Ok, it's mapped. Make sure it's up-to-date */
  840. if (PageUptodate(page))
  841. set_buffer_uptodate(bh);
  842. if (!buffer_uptodate(bh)) {
  843. err = -EIO;
  844. ll_rw_block(READ, 1, &bh);
  845. wait_on_buffer(bh);
  846. /* Uhhuh. Read error. Complain and punt. */
  847. if (!buffer_uptodate(bh))
  848. goto unlock;
  849. err = 0;
  850. }
  851. if (!gfs2_is_writeback(ip))
  852. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  853. zero_user(page, offset, length);
  854. mark_buffer_dirty(bh);
  855. unlock:
  856. unlock_page(page);
  857. page_cache_release(page);
  858. return err;
  859. }
  860. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  861. {
  862. struct gfs2_inode *ip = GFS2_I(inode);
  863. struct gfs2_sbd *sdp = GFS2_SB(inode);
  864. struct address_space *mapping = inode->i_mapping;
  865. struct buffer_head *dibh;
  866. int journaled = gfs2_is_jdata(ip);
  867. int error;
  868. error = gfs2_trans_begin(sdp,
  869. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  870. if (error)
  871. return error;
  872. error = gfs2_meta_inode_buffer(ip, &dibh);
  873. if (error)
  874. goto out;
  875. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  876. if (gfs2_is_stuffed(ip)) {
  877. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  878. } else {
  879. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  880. error = gfs2_block_truncate_page(mapping, newsize);
  881. if (error)
  882. goto out_brelse;
  883. }
  884. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  885. }
  886. i_size_write(inode, newsize);
  887. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  888. gfs2_dinode_out(ip, dibh->b_data);
  889. truncate_pagecache(inode, oldsize, newsize);
  890. out_brelse:
  891. brelse(dibh);
  892. out:
  893. gfs2_trans_end(sdp);
  894. return error;
  895. }
  896. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  897. {
  898. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  899. unsigned int height = ip->i_height;
  900. u64 lblock;
  901. struct metapath mp;
  902. int error;
  903. if (!size)
  904. lblock = 0;
  905. else
  906. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  907. find_metapath(sdp, lblock, &mp, ip->i_height);
  908. if (!gfs2_qadata_get(ip))
  909. return -ENOMEM;
  910. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  911. if (error)
  912. goto out;
  913. while (height--) {
  914. struct strip_mine sm;
  915. sm.sm_first = !!size;
  916. sm.sm_height = height;
  917. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  918. if (error)
  919. break;
  920. }
  921. gfs2_quota_unhold(ip);
  922. out:
  923. gfs2_qadata_put(ip);
  924. return error;
  925. }
  926. static int trunc_end(struct gfs2_inode *ip)
  927. {
  928. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  929. struct buffer_head *dibh;
  930. int error;
  931. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  932. if (error)
  933. return error;
  934. down_write(&ip->i_rw_mutex);
  935. error = gfs2_meta_inode_buffer(ip, &dibh);
  936. if (error)
  937. goto out;
  938. if (!i_size_read(&ip->i_inode)) {
  939. ip->i_height = 0;
  940. ip->i_goal = ip->i_no_addr;
  941. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  942. }
  943. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  944. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  945. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  946. gfs2_dinode_out(ip, dibh->b_data);
  947. brelse(dibh);
  948. out:
  949. up_write(&ip->i_rw_mutex);
  950. gfs2_trans_end(sdp);
  951. return error;
  952. }
  953. /**
  954. * do_shrink - make a file smaller
  955. * @inode: the inode
  956. * @oldsize: the current inode size
  957. * @newsize: the size to make the file
  958. *
  959. * Called with an exclusive lock on @inode. The @size must
  960. * be equal to or smaller than the current inode size.
  961. *
  962. * Returns: errno
  963. */
  964. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  965. {
  966. struct gfs2_inode *ip = GFS2_I(inode);
  967. int error;
  968. error = trunc_start(inode, oldsize, newsize);
  969. if (error < 0)
  970. return error;
  971. if (gfs2_is_stuffed(ip))
  972. return 0;
  973. error = trunc_dealloc(ip, newsize);
  974. if (error == 0)
  975. error = trunc_end(ip);
  976. return error;
  977. }
  978. void gfs2_trim_blocks(struct inode *inode)
  979. {
  980. u64 size = inode->i_size;
  981. int ret;
  982. ret = do_shrink(inode, size, size);
  983. WARN_ON(ret != 0);
  984. }
  985. /**
  986. * do_grow - Touch and update inode size
  987. * @inode: The inode
  988. * @size: The new size
  989. *
  990. * This function updates the timestamps on the inode and
  991. * may also increase the size of the inode. This function
  992. * must not be called with @size any smaller than the current
  993. * inode size.
  994. *
  995. * Although it is not strictly required to unstuff files here,
  996. * earlier versions of GFS2 have a bug in the stuffed file reading
  997. * code which will result in a buffer overrun if the size is larger
  998. * than the max stuffed file size. In order to prevent this from
  999. * occurring, such files are unstuffed, but in other cases we can
  1000. * just update the inode size directly.
  1001. *
  1002. * Returns: 0 on success, or -ve on error
  1003. */
  1004. static int do_grow(struct inode *inode, u64 size)
  1005. {
  1006. struct gfs2_inode *ip = GFS2_I(inode);
  1007. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1008. struct buffer_head *dibh;
  1009. struct gfs2_qadata *qa = NULL;
  1010. int error;
  1011. if (gfs2_is_stuffed(ip) &&
  1012. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1013. qa = gfs2_qadata_get(ip);
  1014. if (qa == NULL)
  1015. return -ENOMEM;
  1016. error = gfs2_quota_lock_check(ip);
  1017. if (error)
  1018. goto do_grow_alloc_put;
  1019. error = gfs2_inplace_reserve(ip, 1);
  1020. if (error)
  1021. goto do_grow_qunlock;
  1022. }
  1023. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT, 0);
  1024. if (error)
  1025. goto do_grow_release;
  1026. if (qa) {
  1027. error = gfs2_unstuff_dinode(ip, NULL);
  1028. if (error)
  1029. goto do_end_trans;
  1030. }
  1031. error = gfs2_meta_inode_buffer(ip, &dibh);
  1032. if (error)
  1033. goto do_end_trans;
  1034. i_size_write(inode, size);
  1035. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1036. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1037. gfs2_dinode_out(ip, dibh->b_data);
  1038. brelse(dibh);
  1039. do_end_trans:
  1040. gfs2_trans_end(sdp);
  1041. do_grow_release:
  1042. if (qa) {
  1043. gfs2_inplace_release(ip);
  1044. do_grow_qunlock:
  1045. gfs2_quota_unlock(ip);
  1046. do_grow_alloc_put:
  1047. gfs2_qadata_put(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. }