bmap.c 32 KB

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