bmap.c 32 KB

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