bmap.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/crc32.h>
  15. #include <linux/lm_interface.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "bmap.h"
  19. #include "glock.h"
  20. #include "inode.h"
  21. #include "meta_io.h"
  22. #include "quota.h"
  23. #include "rgrp.h"
  24. #include "trans.h"
  25. #include "dir.h"
  26. #include "util.h"
  27. #include "ops_address.h"
  28. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  29. * block is 512, so __u16 is fine for that. It saves stack space to
  30. * keep it small.
  31. */
  32. struct metapath {
  33. __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_di.di_size);
  68. memset(kaddr + ip->i_di.di_size, 0,
  69. PAGE_CACHE_SIZE - ip->i_di.di_size);
  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_di.di_size) {
  113. /* Get a free block, fill it with the stuffed data,
  114. and write it out to disk */
  115. if (isdir) {
  116. block = gfs2_alloc_meta(ip);
  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. block = gfs2_alloc_data(ip);
  125. error = gfs2_unstuffer_page(ip, dibh, block, page);
  126. if (error)
  127. goto out_brelse;
  128. }
  129. }
  130. /* Set up the pointer to the new block */
  131. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  132. di = (struct gfs2_dinode *)dibh->b_data;
  133. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  134. if (ip->i_di.di_size) {
  135. *(__be64 *)(di + 1) = cpu_to_be64(block);
  136. ip->i_di.di_blocks++;
  137. gfs2_set_inode_blocks(&ip->i_inode);
  138. di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
  139. }
  140. ip->i_di.di_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. * calc_tree_height - Calculate the height of a metadata tree
  150. * @ip: The GFS2 inode
  151. * @size: The proposed size of the file
  152. *
  153. * Work out how tall a metadata tree needs to be in order to accommodate a
  154. * file of a particular size. If size is less than the current size of
  155. * the inode, then the current size of the inode is used instead of the
  156. * supplied one.
  157. *
  158. * Returns: the height the tree should be
  159. */
  160. static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
  161. {
  162. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  163. u64 *arr;
  164. unsigned int max, height;
  165. if (ip->i_di.di_size > size)
  166. size = ip->i_di.di_size;
  167. if (gfs2_is_dir(ip)) {
  168. arr = sdp->sd_jheightsize;
  169. max = sdp->sd_max_jheight;
  170. } else {
  171. arr = sdp->sd_heightsize;
  172. max = sdp->sd_max_height;
  173. }
  174. for (height = 0; height < max; height++)
  175. if (arr[height] >= size)
  176. break;
  177. return height;
  178. }
  179. /**
  180. * build_height - Build a metadata tree of the requested height
  181. * @ip: The GFS2 inode
  182. * @height: The height to build to
  183. *
  184. *
  185. * Returns: errno
  186. */
  187. static int build_height(struct inode *inode, unsigned height)
  188. {
  189. struct gfs2_inode *ip = GFS2_I(inode);
  190. unsigned new_height = height - ip->i_di.di_height;
  191. struct buffer_head *dibh;
  192. struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
  193. struct gfs2_dinode *di;
  194. int error;
  195. __be64 *bp;
  196. u64 bn;
  197. unsigned n;
  198. if (height <= ip->i_di.di_height)
  199. return 0;
  200. error = gfs2_meta_inode_buffer(ip, &dibh);
  201. if (error)
  202. return error;
  203. for(n = 0; n < new_height; n++) {
  204. bn = gfs2_alloc_meta(ip);
  205. blocks[n] = gfs2_meta_new(ip->i_gl, bn);
  206. gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
  207. }
  208. n = 0;
  209. bn = blocks[0]->b_blocknr;
  210. if (new_height > 1) {
  211. for(; n < new_height-1; n++) {
  212. gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
  213. GFS2_FORMAT_IN);
  214. gfs2_buffer_clear_tail(blocks[n],
  215. sizeof(struct gfs2_meta_header));
  216. bp = (__be64 *)(blocks[n]->b_data +
  217. sizeof(struct gfs2_meta_header));
  218. *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
  219. brelse(blocks[n]);
  220. blocks[n] = NULL;
  221. }
  222. }
  223. gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  224. gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
  225. dibh, sizeof(struct gfs2_dinode));
  226. brelse(blocks[n]);
  227. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  228. di = (struct gfs2_dinode *)dibh->b_data;
  229. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  230. *(__be64 *)(di + 1) = cpu_to_be64(bn);
  231. ip->i_di.di_height += new_height;
  232. ip->i_di.di_blocks += new_height;
  233. gfs2_set_inode_blocks(&ip->i_inode);
  234. di->di_height = cpu_to_be16(ip->i_di.di_height);
  235. di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
  236. brelse(dibh);
  237. return error;
  238. }
  239. /**
  240. * find_metapath - Find path through the metadata tree
  241. * @ip: The inode pointer
  242. * @mp: The metapath to return the result in
  243. * @block: The disk block to look up
  244. *
  245. * This routine returns a struct metapath structure that defines a path
  246. * through the metadata of inode "ip" to get to block "block".
  247. *
  248. * Example:
  249. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  250. * filesystem with a blocksize of 4096.
  251. *
  252. * find_metapath() would return a struct metapath structure set to:
  253. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  254. * and mp_list[2] = 165.
  255. *
  256. * That means that in order to get to the block containing the byte at
  257. * offset 101342453, we would load the indirect block pointed to by pointer
  258. * 0 in the dinode. We would then load the indirect block pointed to by
  259. * pointer 48 in that indirect block. We would then load the data block
  260. * pointed to by pointer 165 in that indirect block.
  261. *
  262. * ----------------------------------------
  263. * | Dinode | |
  264. * | | 4|
  265. * | |0 1 2 3 4 5 9|
  266. * | | 6|
  267. * ----------------------------------------
  268. * |
  269. * |
  270. * V
  271. * ----------------------------------------
  272. * | Indirect Block |
  273. * | 5|
  274. * | 4 4 4 4 4 5 5 1|
  275. * |0 5 6 7 8 9 0 1 2|
  276. * ----------------------------------------
  277. * |
  278. * |
  279. * V
  280. * ----------------------------------------
  281. * | Indirect Block |
  282. * | 1 1 1 1 1 5|
  283. * | 6 6 6 6 6 1|
  284. * |0 3 4 5 6 7 2|
  285. * ----------------------------------------
  286. * |
  287. * |
  288. * V
  289. * ----------------------------------------
  290. * | Data block containing offset |
  291. * | 101342453 |
  292. * | |
  293. * | |
  294. * ----------------------------------------
  295. *
  296. */
  297. static void find_metapath(struct gfs2_inode *ip, u64 block,
  298. struct metapath *mp)
  299. {
  300. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  301. u64 b = block;
  302. unsigned int i;
  303. for (i = ip->i_di.di_height; i--;)
  304. mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
  305. }
  306. /**
  307. * metapointer - Return pointer to start of metadata in a buffer
  308. * @bh: The buffer
  309. * @height: The metadata height (0 = dinode)
  310. * @mp: The metapath
  311. *
  312. * Return a pointer to the block number of the next height of the metadata
  313. * tree given a buffer containing the pointer to the current height of the
  314. * metadata tree.
  315. */
  316. static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
  317. unsigned int height, const struct metapath *mp)
  318. {
  319. unsigned int head_size = (height > 0) ?
  320. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  321. __be64 *ptr;
  322. *boundary = 0;
  323. ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  324. if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
  325. *boundary = 1;
  326. return ptr;
  327. }
  328. /**
  329. * lookup_block - Get the next metadata block in metadata tree
  330. * @ip: The GFS2 inode
  331. * @bh: Buffer containing the pointers to metadata blocks
  332. * @height: The height of the tree (0 = dinode)
  333. * @mp: The metapath
  334. * @create: Non-zero if we may create a new meatdata block
  335. * @new: Used to indicate if we did create a new metadata block
  336. * @block: the returned disk block number
  337. *
  338. * Given a metatree, complete to a particular height, checks to see if the next
  339. * height of the tree exists. If not the next height of the tree is created.
  340. * The block number of the next height of the metadata tree is returned.
  341. *
  342. */
  343. static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  344. unsigned int height, struct metapath *mp, int create,
  345. int *new, u64 *block)
  346. {
  347. int boundary;
  348. __be64 *ptr = metapointer(bh, &boundary, height, mp);
  349. if (*ptr) {
  350. *block = be64_to_cpu(*ptr);
  351. return boundary;
  352. }
  353. *block = 0;
  354. if (!create)
  355. return 0;
  356. if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
  357. *block = gfs2_alloc_data(ip);
  358. else
  359. *block = gfs2_alloc_meta(ip);
  360. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  361. *ptr = cpu_to_be64(*block);
  362. ip->i_di.di_blocks++;
  363. gfs2_set_inode_blocks(&ip->i_inode);
  364. *new = 1;
  365. return 0;
  366. }
  367. static inline void bmap_lock(struct inode *inode, int create)
  368. {
  369. struct gfs2_inode *ip = GFS2_I(inode);
  370. if (create)
  371. down_write(&ip->i_rw_mutex);
  372. else
  373. down_read(&ip->i_rw_mutex);
  374. }
  375. static inline void bmap_unlock(struct inode *inode, int create)
  376. {
  377. struct gfs2_inode *ip = GFS2_I(inode);
  378. if (create)
  379. up_write(&ip->i_rw_mutex);
  380. else
  381. up_read(&ip->i_rw_mutex);
  382. }
  383. /**
  384. * gfs2_block_map - Map a block from an inode to a disk block
  385. * @inode: The inode
  386. * @lblock: The logical block number
  387. * @bh_map: The bh to be mapped
  388. *
  389. * Find the block number on the current device which corresponds to an
  390. * inode's block. If the block had to be created, "new" will be set.
  391. *
  392. * Returns: errno
  393. */
  394. int gfs2_block_map(struct inode *inode, sector_t lblock,
  395. struct buffer_head *bh_map, int create)
  396. {
  397. struct gfs2_inode *ip = GFS2_I(inode);
  398. struct gfs2_sbd *sdp = GFS2_SB(inode);
  399. struct buffer_head *bh;
  400. unsigned int bsize;
  401. unsigned int height;
  402. unsigned int end_of_metadata;
  403. unsigned int x;
  404. int error = 0;
  405. int new = 0;
  406. u64 dblock = 0;
  407. int boundary;
  408. unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  409. struct metapath mp;
  410. u64 size;
  411. struct buffer_head *dibh = NULL;
  412. BUG_ON(maxlen == 0);
  413. if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  414. return 0;
  415. bmap_lock(inode, create);
  416. clear_buffer_mapped(bh_map);
  417. clear_buffer_new(bh_map);
  418. clear_buffer_boundary(bh_map);
  419. bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
  420. size = (lblock + 1) * bsize;
  421. if (size > ip->i_di.di_size) {
  422. height = calc_tree_height(ip, size);
  423. if (ip->i_di.di_height < height) {
  424. if (!create)
  425. goto out_ok;
  426. error = build_height(inode, height);
  427. if (error)
  428. goto out_fail;
  429. }
  430. }
  431. find_metapath(ip, lblock, &mp);
  432. end_of_metadata = ip->i_di.di_height - 1;
  433. error = gfs2_meta_inode_buffer(ip, &bh);
  434. if (error)
  435. goto out_fail;
  436. dibh = bh;
  437. get_bh(dibh);
  438. for (x = 0; x < end_of_metadata; x++) {
  439. lookup_block(ip, bh, x, &mp, create, &new, &dblock);
  440. brelse(bh);
  441. if (!dblock)
  442. goto out_ok;
  443. error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
  444. if (error)
  445. goto out_fail;
  446. }
  447. boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
  448. if (dblock) {
  449. map_bh(bh_map, inode->i_sb, dblock);
  450. if (boundary)
  451. set_buffer_boundary(bh_map);
  452. if (new) {
  453. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  454. gfs2_dinode_out(ip, dibh->b_data);
  455. set_buffer_new(bh_map);
  456. goto out_brelse;
  457. }
  458. while(--maxlen && !buffer_boundary(bh_map)) {
  459. u64 eblock;
  460. mp.mp_list[end_of_metadata]++;
  461. boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
  462. if (eblock != ++dblock)
  463. break;
  464. bh_map->b_size += (1 << inode->i_blkbits);
  465. if (boundary)
  466. set_buffer_boundary(bh_map);
  467. }
  468. }
  469. out_brelse:
  470. brelse(bh);
  471. out_ok:
  472. error = 0;
  473. out_fail:
  474. if (dibh)
  475. brelse(dibh);
  476. bmap_unlock(inode, create);
  477. return error;
  478. }
  479. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  480. {
  481. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  482. int ret;
  483. int create = *new;
  484. BUG_ON(!extlen);
  485. BUG_ON(!dblock);
  486. BUG_ON(!new);
  487. bh.b_size = 1 << (inode->i_blkbits + 5);
  488. ret = gfs2_block_map(inode, lblock, &bh, create);
  489. *extlen = bh.b_size >> inode->i_blkbits;
  490. *dblock = bh.b_blocknr;
  491. if (buffer_new(&bh))
  492. *new = 1;
  493. else
  494. *new = 0;
  495. return ret;
  496. }
  497. /**
  498. * recursive_scan - recursively scan through the end of a file
  499. * @ip: the inode
  500. * @dibh: the dinode buffer
  501. * @mp: the path through the metadata to the point to start
  502. * @height: the height the recursion is at
  503. * @block: the indirect block to look at
  504. * @first: 1 if this is the first block
  505. * @bc: the call to make for each piece of metadata
  506. * @data: data opaque to this function to pass to @bc
  507. *
  508. * When this is first called @height and @block should be zero and
  509. * @first should be 1.
  510. *
  511. * Returns: errno
  512. */
  513. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  514. struct metapath *mp, unsigned int height,
  515. u64 block, int first, block_call_t bc,
  516. void *data)
  517. {
  518. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  519. struct buffer_head *bh = NULL;
  520. __be64 *top, *bottom;
  521. u64 bn;
  522. int error;
  523. int mh_size = sizeof(struct gfs2_meta_header);
  524. if (!height) {
  525. error = gfs2_meta_inode_buffer(ip, &bh);
  526. if (error)
  527. return error;
  528. dibh = bh;
  529. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  530. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  531. } else {
  532. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  533. if (error)
  534. return error;
  535. top = (__be64 *)(bh->b_data + mh_size) +
  536. (first ? mp->mp_list[height] : 0);
  537. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  538. }
  539. error = bc(ip, dibh, bh, top, bottom, height, data);
  540. if (error)
  541. goto out;
  542. if (height < ip->i_di.di_height - 1)
  543. for (; top < bottom; top++, first = 0) {
  544. if (!*top)
  545. continue;
  546. bn = be64_to_cpu(*top);
  547. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  548. first, bc, data);
  549. if (error)
  550. break;
  551. }
  552. out:
  553. brelse(bh);
  554. return error;
  555. }
  556. /**
  557. * do_strip - Look for a layer a particular layer of the file and strip it off
  558. * @ip: the inode
  559. * @dibh: the dinode buffer
  560. * @bh: A buffer of pointers
  561. * @top: The first pointer in the buffer
  562. * @bottom: One more than the last pointer
  563. * @height: the height this buffer is at
  564. * @data: a pointer to a struct strip_mine
  565. *
  566. * Returns: errno
  567. */
  568. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  569. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  570. unsigned int height, void *data)
  571. {
  572. struct strip_mine *sm = data;
  573. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  574. struct gfs2_rgrp_list rlist;
  575. u64 bn, bstart;
  576. u32 blen;
  577. __be64 *p;
  578. unsigned int rg_blocks = 0;
  579. int metadata;
  580. unsigned int revokes = 0;
  581. int x;
  582. int error;
  583. if (!*top)
  584. sm->sm_first = 0;
  585. if (height != sm->sm_height)
  586. return 0;
  587. if (sm->sm_first) {
  588. top++;
  589. sm->sm_first = 0;
  590. }
  591. metadata = (height != ip->i_di.di_height - 1);
  592. if (metadata)
  593. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  594. error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
  595. if (error)
  596. return error;
  597. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  598. bstart = 0;
  599. blen = 0;
  600. for (p = top; p < bottom; p++) {
  601. if (!*p)
  602. continue;
  603. bn = be64_to_cpu(*p);
  604. if (bstart + blen == bn)
  605. blen++;
  606. else {
  607. if (bstart)
  608. gfs2_rlist_add(sdp, &rlist, bstart);
  609. bstart = bn;
  610. blen = 1;
  611. }
  612. }
  613. if (bstart)
  614. gfs2_rlist_add(sdp, &rlist, bstart);
  615. else
  616. goto out; /* Nothing to do */
  617. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  618. for (x = 0; x < rlist.rl_rgrps; x++) {
  619. struct gfs2_rgrpd *rgd;
  620. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  621. rg_blocks += rgd->rd_length;
  622. }
  623. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  624. if (error)
  625. goto out_rlist;
  626. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  627. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  628. revokes);
  629. if (error)
  630. goto out_rg_gunlock;
  631. down_write(&ip->i_rw_mutex);
  632. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  633. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  634. bstart = 0;
  635. blen = 0;
  636. for (p = top; p < bottom; p++) {
  637. if (!*p)
  638. continue;
  639. bn = be64_to_cpu(*p);
  640. if (bstart + blen == bn)
  641. blen++;
  642. else {
  643. if (bstart) {
  644. if (metadata)
  645. gfs2_free_meta(ip, bstart, blen);
  646. else
  647. gfs2_free_data(ip, bstart, blen);
  648. }
  649. bstart = bn;
  650. blen = 1;
  651. }
  652. *p = 0;
  653. if (!ip->i_di.di_blocks)
  654. gfs2_consist_inode(ip);
  655. ip->i_di.di_blocks--;
  656. gfs2_set_inode_blocks(&ip->i_inode);
  657. }
  658. if (bstart) {
  659. if (metadata)
  660. gfs2_free_meta(ip, bstart, blen);
  661. else
  662. gfs2_free_data(ip, bstart, blen);
  663. }
  664. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  665. gfs2_dinode_out(ip, dibh->b_data);
  666. up_write(&ip->i_rw_mutex);
  667. gfs2_trans_end(sdp);
  668. out_rg_gunlock:
  669. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  670. out_rlist:
  671. gfs2_rlist_free(&rlist);
  672. out:
  673. gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
  674. return error;
  675. }
  676. /**
  677. * do_grow - Make a file look bigger than it is
  678. * @ip: the inode
  679. * @size: the size to set the file to
  680. *
  681. * Called with an exclusive lock on @ip.
  682. *
  683. * Returns: errno
  684. */
  685. static int do_grow(struct gfs2_inode *ip, u64 size)
  686. {
  687. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  688. struct gfs2_alloc *al;
  689. struct buffer_head *dibh;
  690. unsigned int h;
  691. int error;
  692. al = gfs2_alloc_get(ip);
  693. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  694. if (error)
  695. goto out;
  696. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  697. if (error)
  698. goto out_gunlock_q;
  699. al->al_requested = sdp->sd_max_height + RES_DATA;
  700. error = gfs2_inplace_reserve(ip);
  701. if (error)
  702. goto out_gunlock_q;
  703. error = gfs2_trans_begin(sdp,
  704. sdp->sd_max_height + al->al_rgd->rd_length +
  705. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  706. if (error)
  707. goto out_ipres;
  708. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  709. if (gfs2_is_stuffed(ip)) {
  710. error = gfs2_unstuff_dinode(ip, NULL);
  711. if (error)
  712. goto out_end_trans;
  713. }
  714. h = calc_tree_height(ip, size);
  715. if (ip->i_di.di_height < h) {
  716. down_write(&ip->i_rw_mutex);
  717. error = build_height(&ip->i_inode, h);
  718. up_write(&ip->i_rw_mutex);
  719. if (error)
  720. goto out_end_trans;
  721. }
  722. }
  723. ip->i_di.di_size = size;
  724. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  725. error = gfs2_meta_inode_buffer(ip, &dibh);
  726. if (error)
  727. goto out_end_trans;
  728. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  729. gfs2_dinode_out(ip, dibh->b_data);
  730. brelse(dibh);
  731. out_end_trans:
  732. gfs2_trans_end(sdp);
  733. out_ipres:
  734. gfs2_inplace_release(ip);
  735. out_gunlock_q:
  736. gfs2_quota_unlock(ip);
  737. out:
  738. gfs2_alloc_put(ip);
  739. return error;
  740. }
  741. /**
  742. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  743. *
  744. * This is partly borrowed from ext3.
  745. */
  746. static int gfs2_block_truncate_page(struct address_space *mapping)
  747. {
  748. struct inode *inode = mapping->host;
  749. struct gfs2_inode *ip = GFS2_I(inode);
  750. loff_t from = inode->i_size;
  751. unsigned long index = from >> PAGE_CACHE_SHIFT;
  752. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  753. unsigned blocksize, iblock, length, pos;
  754. struct buffer_head *bh;
  755. struct page *page;
  756. int err;
  757. page = grab_cache_page(mapping, index);
  758. if (!page)
  759. return 0;
  760. blocksize = inode->i_sb->s_blocksize;
  761. length = blocksize - (offset & (blocksize - 1));
  762. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  763. if (!page_has_buffers(page))
  764. create_empty_buffers(page, blocksize, 0);
  765. /* Find the buffer that contains "offset" */
  766. bh = page_buffers(page);
  767. pos = blocksize;
  768. while (offset >= pos) {
  769. bh = bh->b_this_page;
  770. iblock++;
  771. pos += blocksize;
  772. }
  773. err = 0;
  774. if (!buffer_mapped(bh)) {
  775. gfs2_block_map(inode, iblock, bh, 0);
  776. /* unmapped? It's a hole - nothing to do */
  777. if (!buffer_mapped(bh))
  778. goto unlock;
  779. }
  780. /* Ok, it's mapped. Make sure it's up-to-date */
  781. if (PageUptodate(page))
  782. set_buffer_uptodate(bh);
  783. if (!buffer_uptodate(bh)) {
  784. err = -EIO;
  785. ll_rw_block(READ, 1, &bh);
  786. wait_on_buffer(bh);
  787. /* Uhhuh. Read error. Complain and punt. */
  788. if (!buffer_uptodate(bh))
  789. goto unlock;
  790. err = 0;
  791. }
  792. if (!gfs2_is_writeback(ip))
  793. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  794. zero_user_page(page, offset, length, KM_USER0);
  795. unlock:
  796. unlock_page(page);
  797. page_cache_release(page);
  798. return err;
  799. }
  800. static int trunc_start(struct gfs2_inode *ip, u64 size)
  801. {
  802. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  803. struct buffer_head *dibh;
  804. int journaled = gfs2_is_jdata(ip);
  805. int error;
  806. error = gfs2_trans_begin(sdp,
  807. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  808. if (error)
  809. return error;
  810. error = gfs2_meta_inode_buffer(ip, &dibh);
  811. if (error)
  812. goto out;
  813. if (gfs2_is_stuffed(ip)) {
  814. ip->i_di.di_size = size;
  815. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  816. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  817. gfs2_dinode_out(ip, dibh->b_data);
  818. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  819. error = 1;
  820. } else {
  821. if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
  822. error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
  823. if (!error) {
  824. ip->i_di.di_size = size;
  825. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  826. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  827. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  828. gfs2_dinode_out(ip, dibh->b_data);
  829. }
  830. }
  831. brelse(dibh);
  832. out:
  833. gfs2_trans_end(sdp);
  834. return error;
  835. }
  836. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  837. {
  838. unsigned int height = ip->i_di.di_height;
  839. u64 lblock;
  840. struct metapath mp;
  841. int error;
  842. if (!size)
  843. lblock = 0;
  844. else
  845. lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
  846. find_metapath(ip, lblock, &mp);
  847. gfs2_alloc_get(ip);
  848. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  849. if (error)
  850. goto out;
  851. while (height--) {
  852. struct strip_mine sm;
  853. sm.sm_first = !!size;
  854. sm.sm_height = height;
  855. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  856. if (error)
  857. break;
  858. }
  859. gfs2_quota_unhold(ip);
  860. out:
  861. gfs2_alloc_put(ip);
  862. return error;
  863. }
  864. static int trunc_end(struct gfs2_inode *ip)
  865. {
  866. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  867. struct buffer_head *dibh;
  868. int error;
  869. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  870. if (error)
  871. return error;
  872. down_write(&ip->i_rw_mutex);
  873. error = gfs2_meta_inode_buffer(ip, &dibh);
  874. if (error)
  875. goto out;
  876. if (!ip->i_di.di_size) {
  877. ip->i_di.di_height = 0;
  878. ip->i_di.di_goal_meta =
  879. ip->i_di.di_goal_data =
  880. ip->i_no_addr;
  881. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  882. }
  883. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  884. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  885. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  886. gfs2_dinode_out(ip, dibh->b_data);
  887. brelse(dibh);
  888. out:
  889. up_write(&ip->i_rw_mutex);
  890. gfs2_trans_end(sdp);
  891. return error;
  892. }
  893. /**
  894. * do_shrink - make a file smaller
  895. * @ip: the inode
  896. * @size: the size to make the file
  897. * @truncator: function to truncate the last partial block
  898. *
  899. * Called with an exclusive lock on @ip.
  900. *
  901. * Returns: errno
  902. */
  903. static int do_shrink(struct gfs2_inode *ip, u64 size)
  904. {
  905. int error;
  906. error = trunc_start(ip, size);
  907. if (error < 0)
  908. return error;
  909. if (error > 0)
  910. return 0;
  911. error = trunc_dealloc(ip, size);
  912. if (!error)
  913. error = trunc_end(ip);
  914. return error;
  915. }
  916. static int do_touch(struct gfs2_inode *ip, u64 size)
  917. {
  918. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  919. struct buffer_head *dibh;
  920. int error;
  921. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  922. if (error)
  923. return error;
  924. down_write(&ip->i_rw_mutex);
  925. error = gfs2_meta_inode_buffer(ip, &dibh);
  926. if (error)
  927. goto do_touch_out;
  928. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  929. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  930. gfs2_dinode_out(ip, dibh->b_data);
  931. brelse(dibh);
  932. do_touch_out:
  933. up_write(&ip->i_rw_mutex);
  934. gfs2_trans_end(sdp);
  935. return error;
  936. }
  937. /**
  938. * gfs2_truncatei - make a file a given size
  939. * @ip: the inode
  940. * @size: the size to make the file
  941. * @truncator: function to truncate the last partial block
  942. *
  943. * The file size can grow, shrink, or stay the same size.
  944. *
  945. * Returns: errno
  946. */
  947. int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
  948. {
  949. int error;
  950. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
  951. return -EINVAL;
  952. if (size > ip->i_di.di_size)
  953. error = do_grow(ip, size);
  954. else if (size < ip->i_di.di_size)
  955. error = do_shrink(ip, size);
  956. else
  957. /* update time stamps */
  958. error = do_touch(ip, size);
  959. return error;
  960. }
  961. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  962. {
  963. int error;
  964. error = trunc_dealloc(ip, ip->i_di.di_size);
  965. if (!error)
  966. error = trunc_end(ip);
  967. return error;
  968. }
  969. int gfs2_file_dealloc(struct gfs2_inode *ip)
  970. {
  971. return trunc_dealloc(ip, 0);
  972. }
  973. /**
  974. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  975. * @ip: the file
  976. * @len: the number of bytes to be written to the file
  977. * @data_blocks: returns the number of data blocks required
  978. * @ind_blocks: returns the number of indirect blocks required
  979. *
  980. */
  981. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  982. unsigned int *data_blocks, unsigned int *ind_blocks)
  983. {
  984. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  985. unsigned int tmp;
  986. if (gfs2_is_dir(ip)) {
  987. *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
  988. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  989. } else {
  990. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  991. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  992. }
  993. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  994. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  995. *ind_blocks += tmp;
  996. }
  997. }
  998. /**
  999. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1000. * @ip: the file being written to
  1001. * @offset: the offset to write to
  1002. * @len: the number of bytes being written
  1003. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  1004. *
  1005. * Returns: errno
  1006. */
  1007. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1008. unsigned int len, int *alloc_required)
  1009. {
  1010. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1011. u64 lblock, lblock_stop, dblock;
  1012. u32 extlen;
  1013. int new = 0;
  1014. int error = 0;
  1015. *alloc_required = 0;
  1016. if (!len)
  1017. return 0;
  1018. if (gfs2_is_stuffed(ip)) {
  1019. if (offset + len >
  1020. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1021. *alloc_required = 1;
  1022. return 0;
  1023. }
  1024. if (gfs2_is_dir(ip)) {
  1025. unsigned int bsize = sdp->sd_jbsize;
  1026. lblock = offset;
  1027. do_div(lblock, bsize);
  1028. lblock_stop = offset + len + bsize - 1;
  1029. do_div(lblock_stop, bsize);
  1030. } else {
  1031. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  1032. u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
  1033. lblock = offset >> shift;
  1034. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1035. if (lblock_stop > end_of_file) {
  1036. *alloc_required = 1;
  1037. return 0;
  1038. }
  1039. }
  1040. for (; lblock < lblock_stop; lblock += extlen) {
  1041. error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
  1042. if (error)
  1043. return error;
  1044. if (!dblock) {
  1045. *alloc_required = 1;
  1046. return 0;
  1047. }
  1048. }
  1049. return 0;
  1050. }