bmap.c 31 KB

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