bmap.c 31 KB

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