bmap.c 29 KB

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