bmap.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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;
  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. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  700. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  701. revokes);
  702. if (error)
  703. goto out_rg_gunlock;
  704. down_write(&ip->i_rw_mutex);
  705. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  706. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  707. bstart = 0;
  708. blen = 0;
  709. btotal = 0;
  710. for (p = top; p < bottom; p++) {
  711. if (!*p)
  712. continue;
  713. bn = be64_to_cpu(*p);
  714. if (bstart + blen == bn)
  715. blen++;
  716. else {
  717. if (bstart) {
  718. __gfs2_free_blocks(ip, bstart, blen, metadata);
  719. btotal += blen;
  720. }
  721. bstart = bn;
  722. blen = 1;
  723. }
  724. *p = 0;
  725. gfs2_add_inode_blocks(&ip->i_inode, -1);
  726. }
  727. if (bstart) {
  728. __gfs2_free_blocks(ip, bstart, blen, metadata);
  729. btotal += blen;
  730. }
  731. gfs2_statfs_change(sdp, 0, +btotal, 0);
  732. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  733. ip->i_inode.i_gid);
  734. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  735. gfs2_dinode_out(ip, dibh->b_data);
  736. up_write(&ip->i_rw_mutex);
  737. gfs2_trans_end(sdp);
  738. out_rg_gunlock:
  739. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  740. out_rlist:
  741. gfs2_rlist_free(&rlist);
  742. out:
  743. return error;
  744. }
  745. /**
  746. * recursive_scan - recursively scan through the end of a file
  747. * @ip: the inode
  748. * @dibh: the dinode buffer
  749. * @mp: the path through the metadata to the point to start
  750. * @height: the height the recursion is at
  751. * @block: the indirect block to look at
  752. * @first: 1 if this is the first block
  753. * @sm: data opaque to this function to pass to @bc
  754. *
  755. * When this is first called @height and @block should be zero and
  756. * @first should be 1.
  757. *
  758. * Returns: errno
  759. */
  760. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  761. struct metapath *mp, unsigned int height,
  762. u64 block, int first, struct strip_mine *sm)
  763. {
  764. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  765. struct buffer_head *bh = NULL;
  766. __be64 *top, *bottom;
  767. u64 bn;
  768. int error;
  769. int mh_size = sizeof(struct gfs2_meta_header);
  770. if (!height) {
  771. error = gfs2_meta_inode_buffer(ip, &bh);
  772. if (error)
  773. return error;
  774. dibh = bh;
  775. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  776. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  777. } else {
  778. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  779. if (error)
  780. return error;
  781. top = (__be64 *)(bh->b_data + mh_size) +
  782. (first ? mp->mp_list[height] : 0);
  783. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  784. }
  785. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  786. if (error)
  787. goto out;
  788. if (height < ip->i_height - 1) {
  789. gfs2_metapath_ra(ip->i_gl, bh, top);
  790. for (; top < bottom; top++, first = 0) {
  791. if (!*top)
  792. continue;
  793. bn = be64_to_cpu(*top);
  794. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  795. first, sm);
  796. if (error)
  797. break;
  798. }
  799. }
  800. out:
  801. brelse(bh);
  802. return error;
  803. }
  804. /**
  805. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  806. *
  807. * This is partly borrowed from ext3.
  808. */
  809. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  810. {
  811. struct inode *inode = mapping->host;
  812. struct gfs2_inode *ip = GFS2_I(inode);
  813. unsigned long index = from >> PAGE_CACHE_SHIFT;
  814. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  815. unsigned blocksize, iblock, length, pos;
  816. struct buffer_head *bh;
  817. struct page *page;
  818. int err;
  819. page = find_or_create_page(mapping, index, GFP_NOFS);
  820. if (!page)
  821. return 0;
  822. blocksize = inode->i_sb->s_blocksize;
  823. length = blocksize - (offset & (blocksize - 1));
  824. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  825. if (!page_has_buffers(page))
  826. create_empty_buffers(page, blocksize, 0);
  827. /* Find the buffer that contains "offset" */
  828. bh = page_buffers(page);
  829. pos = blocksize;
  830. while (offset >= pos) {
  831. bh = bh->b_this_page;
  832. iblock++;
  833. pos += blocksize;
  834. }
  835. err = 0;
  836. if (!buffer_mapped(bh)) {
  837. gfs2_block_map(inode, iblock, bh, 0);
  838. /* unmapped? It's a hole - nothing to do */
  839. if (!buffer_mapped(bh))
  840. goto unlock;
  841. }
  842. /* Ok, it's mapped. Make sure it's up-to-date */
  843. if (PageUptodate(page))
  844. set_buffer_uptodate(bh);
  845. if (!buffer_uptodate(bh)) {
  846. err = -EIO;
  847. ll_rw_block(READ, 1, &bh);
  848. wait_on_buffer(bh);
  849. /* Uhhuh. Read error. Complain and punt. */
  850. if (!buffer_uptodate(bh))
  851. goto unlock;
  852. err = 0;
  853. }
  854. if (!gfs2_is_writeback(ip))
  855. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  856. zero_user(page, offset, length);
  857. mark_buffer_dirty(bh);
  858. unlock:
  859. unlock_page(page);
  860. page_cache_release(page);
  861. return err;
  862. }
  863. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  864. {
  865. struct gfs2_inode *ip = GFS2_I(inode);
  866. struct gfs2_sbd *sdp = GFS2_SB(inode);
  867. struct address_space *mapping = inode->i_mapping;
  868. struct buffer_head *dibh;
  869. int journaled = gfs2_is_jdata(ip);
  870. int error;
  871. error = gfs2_trans_begin(sdp,
  872. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  873. if (error)
  874. return error;
  875. error = gfs2_meta_inode_buffer(ip, &dibh);
  876. if (error)
  877. goto out;
  878. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  879. if (gfs2_is_stuffed(ip)) {
  880. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  881. } else {
  882. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  883. error = gfs2_block_truncate_page(mapping, newsize);
  884. if (error)
  885. goto out_brelse;
  886. }
  887. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  888. }
  889. i_size_write(inode, newsize);
  890. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  891. gfs2_dinode_out(ip, dibh->b_data);
  892. truncate_pagecache(inode, oldsize, newsize);
  893. out_brelse:
  894. brelse(dibh);
  895. out:
  896. gfs2_trans_end(sdp);
  897. return error;
  898. }
  899. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  900. {
  901. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  902. unsigned int height = ip->i_height;
  903. u64 lblock;
  904. struct metapath mp;
  905. int error;
  906. if (!size)
  907. lblock = 0;
  908. else
  909. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  910. find_metapath(sdp, lblock, &mp, ip->i_height);
  911. if (!gfs2_qadata_get(ip))
  912. return -ENOMEM;
  913. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  914. if (error)
  915. goto out;
  916. while (height--) {
  917. struct strip_mine sm;
  918. sm.sm_first = !!size;
  919. sm.sm_height = height;
  920. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  921. if (error)
  922. break;
  923. }
  924. gfs2_quota_unhold(ip);
  925. out:
  926. gfs2_qadata_put(ip);
  927. return error;
  928. }
  929. static int trunc_end(struct gfs2_inode *ip)
  930. {
  931. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  932. struct buffer_head *dibh;
  933. int error;
  934. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  935. if (error)
  936. return error;
  937. down_write(&ip->i_rw_mutex);
  938. error = gfs2_meta_inode_buffer(ip, &dibh);
  939. if (error)
  940. goto out;
  941. if (!i_size_read(&ip->i_inode)) {
  942. ip->i_height = 0;
  943. ip->i_goal = ip->i_no_addr;
  944. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  945. }
  946. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  947. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  948. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  949. gfs2_dinode_out(ip, dibh->b_data);
  950. brelse(dibh);
  951. out:
  952. up_write(&ip->i_rw_mutex);
  953. gfs2_trans_end(sdp);
  954. return error;
  955. }
  956. /**
  957. * do_shrink - make a file smaller
  958. * @inode: the inode
  959. * @oldsize: the current inode size
  960. * @newsize: the size to make the file
  961. *
  962. * Called with an exclusive lock on @inode. The @size must
  963. * be equal to or smaller than the current inode size.
  964. *
  965. * Returns: errno
  966. */
  967. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  968. {
  969. struct gfs2_inode *ip = GFS2_I(inode);
  970. int error;
  971. error = trunc_start(inode, oldsize, newsize);
  972. if (error < 0)
  973. return error;
  974. if (gfs2_is_stuffed(ip))
  975. return 0;
  976. error = trunc_dealloc(ip, newsize);
  977. if (error == 0)
  978. error = trunc_end(ip);
  979. return error;
  980. }
  981. void gfs2_trim_blocks(struct inode *inode)
  982. {
  983. u64 size = inode->i_size;
  984. int ret;
  985. ret = do_shrink(inode, size, size);
  986. WARN_ON(ret != 0);
  987. }
  988. /**
  989. * do_grow - Touch and update inode size
  990. * @inode: The inode
  991. * @size: The new size
  992. *
  993. * This function updates the timestamps on the inode and
  994. * may also increase the size of the inode. This function
  995. * must not be called with @size any smaller than the current
  996. * inode size.
  997. *
  998. * Although it is not strictly required to unstuff files here,
  999. * earlier versions of GFS2 have a bug in the stuffed file reading
  1000. * code which will result in a buffer overrun if the size is larger
  1001. * than the max stuffed file size. In order to prevent this from
  1002. * occurring, such files are unstuffed, but in other cases we can
  1003. * just update the inode size directly.
  1004. *
  1005. * Returns: 0 on success, or -ve on error
  1006. */
  1007. static int do_grow(struct inode *inode, u64 size)
  1008. {
  1009. struct gfs2_inode *ip = GFS2_I(inode);
  1010. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1011. struct buffer_head *dibh;
  1012. struct gfs2_qadata *qa = NULL;
  1013. int error;
  1014. if (gfs2_is_stuffed(ip) &&
  1015. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1016. qa = gfs2_qadata_get(ip);
  1017. if (qa == NULL)
  1018. return -ENOMEM;
  1019. error = gfs2_quota_lock_check(ip);
  1020. if (error)
  1021. goto do_grow_alloc_put;
  1022. error = gfs2_inplace_reserve(ip, 1);
  1023. if (error)
  1024. goto do_grow_qunlock;
  1025. }
  1026. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT, 0);
  1027. if (error)
  1028. goto do_grow_release;
  1029. if (qa) {
  1030. error = gfs2_unstuff_dinode(ip, NULL);
  1031. if (error)
  1032. goto do_end_trans;
  1033. }
  1034. error = gfs2_meta_inode_buffer(ip, &dibh);
  1035. if (error)
  1036. goto do_end_trans;
  1037. i_size_write(inode, size);
  1038. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1039. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1040. gfs2_dinode_out(ip, dibh->b_data);
  1041. brelse(dibh);
  1042. do_end_trans:
  1043. gfs2_trans_end(sdp);
  1044. do_grow_release:
  1045. if (qa) {
  1046. gfs2_inplace_release(ip);
  1047. do_grow_qunlock:
  1048. gfs2_quota_unlock(ip);
  1049. do_grow_alloc_put:
  1050. gfs2_qadata_put(ip);
  1051. }
  1052. return error;
  1053. }
  1054. /**
  1055. * gfs2_setattr_size - make a file a given size
  1056. * @inode: the inode
  1057. * @newsize: the size to make the file
  1058. *
  1059. * The file size can grow, shrink, or stay the same size. This
  1060. * is called holding i_mutex and an exclusive glock on the inode
  1061. * in question.
  1062. *
  1063. * Returns: errno
  1064. */
  1065. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1066. {
  1067. int ret;
  1068. u64 oldsize;
  1069. BUG_ON(!S_ISREG(inode->i_mode));
  1070. ret = inode_newsize_ok(inode, newsize);
  1071. if (ret)
  1072. return ret;
  1073. inode_dio_wait(inode);
  1074. oldsize = inode->i_size;
  1075. if (newsize >= oldsize)
  1076. return do_grow(inode, newsize);
  1077. return do_shrink(inode, oldsize, newsize);
  1078. }
  1079. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1080. {
  1081. int error;
  1082. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1083. if (!error)
  1084. error = trunc_end(ip);
  1085. return error;
  1086. }
  1087. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1088. {
  1089. return trunc_dealloc(ip, 0);
  1090. }
  1091. /**
  1092. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1093. * @ip: the file being written to
  1094. * @offset: the offset to write to
  1095. * @len: the number of bytes being written
  1096. *
  1097. * Returns: 1 if an alloc is required, 0 otherwise
  1098. */
  1099. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1100. unsigned int len)
  1101. {
  1102. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1103. struct buffer_head bh;
  1104. unsigned int shift;
  1105. u64 lblock, lblock_stop, size;
  1106. u64 end_of_file;
  1107. if (!len)
  1108. return 0;
  1109. if (gfs2_is_stuffed(ip)) {
  1110. if (offset + len >
  1111. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1112. return 1;
  1113. return 0;
  1114. }
  1115. shift = sdp->sd_sb.sb_bsize_shift;
  1116. BUG_ON(gfs2_is_dir(ip));
  1117. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1118. lblock = offset >> shift;
  1119. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1120. if (lblock_stop > end_of_file)
  1121. return 1;
  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 1;
  1129. size -= bh.b_size;
  1130. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1131. } while(size > 0);
  1132. return 0;
  1133. }