bmap.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <asm/semaphore.h>
  15. #include "gfs2.h"
  16. #include "bmap.h"
  17. #include "glock.h"
  18. #include "inode.h"
  19. #include "jdata.h"
  20. #include "meta_io.h"
  21. #include "page.h"
  22. #include "quota.h"
  23. #include "rgrp.h"
  24. #include "trans.h"
  25. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  26. * block is 512, so __u16 is fine for that. It saves stack space to
  27. * keep it small.
  28. */
  29. struct metapath {
  30. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  31. };
  32. typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
  33. struct buffer_head *bh, uint64_t *top,
  34. uint64_t *bottom, unsigned int height,
  35. void *data);
  36. struct strip_mine {
  37. int sm_first;
  38. unsigned int sm_height;
  39. };
  40. /**
  41. * @gfs2_unstuffer_sync - Synchronously unstuff a dinode
  42. * @ip:
  43. * @dibh:
  44. * @block:
  45. * @private:
  46. *
  47. * Cheat and use a metadata buffer instead of a data page.
  48. *
  49. * Returns: errno
  50. */
  51. int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
  52. uint64_t block, void *private)
  53. {
  54. struct buffer_head *bh;
  55. int error;
  56. bh = gfs2_meta_new(ip->i_gl, block);
  57. gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode));
  58. set_buffer_dirty(bh);
  59. error = sync_dirty_buffer(bh);
  60. brelse(bh);
  61. return error;
  62. }
  63. /**
  64. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  65. * @ip: The GFS2 inode to unstuff
  66. * @unstuffer: the routine that handles unstuffing a non-zero length file
  67. * @private: private data for the unstuffer
  68. *
  69. * This routine unstuffs a dinode and returns it to a "normal" state such
  70. * that the height can be grown in the traditional way.
  71. *
  72. * Returns: errno
  73. */
  74. int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
  75. void *private)
  76. {
  77. struct buffer_head *bh, *dibh;
  78. uint64_t block = 0;
  79. int journaled = gfs2_is_jdata(ip);
  80. int error;
  81. down_write(&ip->i_rw_mutex);
  82. error = gfs2_meta_inode_buffer(ip, &dibh);
  83. if (error)
  84. goto out;
  85. if (ip->i_di.di_size) {
  86. /* Get a free block, fill it with the stuffed data,
  87. and write it out to disk */
  88. if (journaled) {
  89. block = gfs2_alloc_meta(ip);
  90. error = gfs2_jdata_get_buffer(ip, block, 1, &bh);
  91. if (error)
  92. goto out_brelse;
  93. gfs2_buffer_copy_tail(bh,
  94. sizeof(struct gfs2_meta_header),
  95. dibh, sizeof(struct gfs2_dinode));
  96. brelse(bh);
  97. } else {
  98. block = gfs2_alloc_data(ip);
  99. error = unstuffer(ip, dibh, block, private);
  100. if (error)
  101. goto out_brelse;
  102. }
  103. }
  104. /* Set up the pointer to the new block */
  105. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  106. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  107. if (ip->i_di.di_size) {
  108. *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
  109. ip->i_di.di_blocks++;
  110. }
  111. ip->i_di.di_height = 1;
  112. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  113. out_brelse:
  114. brelse(dibh);
  115. out:
  116. up_write(&ip->i_rw_mutex);
  117. return error;
  118. }
  119. /**
  120. * calc_tree_height - Calculate the height of a metadata tree
  121. * @ip: The GFS2 inode
  122. * @size: The proposed size of the file
  123. *
  124. * Work out how tall a metadata tree needs to be in order to accommodate a
  125. * file of a particular size. If size is less than the current size of
  126. * the inode, then the current size of the inode is used instead of the
  127. * supplied one.
  128. *
  129. * Returns: the height the tree should be
  130. */
  131. static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
  132. {
  133. struct gfs2_sbd *sdp = ip->i_sbd;
  134. uint64_t *arr;
  135. unsigned int max, height;
  136. if (ip->i_di.di_size > size)
  137. size = ip->i_di.di_size;
  138. if (gfs2_is_jdata(ip)) {
  139. arr = sdp->sd_jheightsize;
  140. max = sdp->sd_max_jheight;
  141. } else {
  142. arr = sdp->sd_heightsize;
  143. max = sdp->sd_max_height;
  144. }
  145. for (height = 0; height < max; height++)
  146. if (arr[height] >= size)
  147. break;
  148. return height;
  149. }
  150. /**
  151. * build_height - Build a metadata tree of the requested height
  152. * @ip: The GFS2 inode
  153. * @height: The height to build to
  154. *
  155. * This routine makes sure that the metadata tree is tall enough to hold
  156. * "size" bytes of data.
  157. *
  158. * Returns: errno
  159. */
  160. static int build_height(struct gfs2_inode *ip, int height)
  161. {
  162. struct gfs2_sbd *sdp = ip->i_sbd;
  163. struct buffer_head *bh, *dibh;
  164. uint64_t block = 0, *bp;
  165. unsigned int x;
  166. int new_block;
  167. int error;
  168. while (ip->i_di.di_height < height) {
  169. error = gfs2_meta_inode_buffer(ip, &dibh);
  170. if (error)
  171. return error;
  172. new_block = 0;
  173. bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
  174. for (x = 0; x < sdp->sd_diptrs; x++, bp++)
  175. if (*bp) {
  176. new_block = 1;
  177. break;
  178. }
  179. if (new_block) {
  180. /* Get a new block, fill it with the old direct
  181. pointers, and write it out */
  182. block = gfs2_alloc_meta(ip);
  183. bh = gfs2_meta_new(ip->i_gl, block);
  184. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  185. gfs2_metatype_set(bh,
  186. GFS2_METATYPE_IN,
  187. GFS2_FORMAT_IN);
  188. gfs2_buffer_copy_tail(bh,
  189. sizeof(struct gfs2_meta_header),
  190. dibh, sizeof(struct gfs2_dinode));
  191. brelse(bh);
  192. }
  193. /* Set up the new direct pointer and write it out to disk */
  194. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  195. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  196. if (new_block) {
  197. *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
  198. ip->i_di.di_blocks++;
  199. }
  200. ip->i_di.di_height++;
  201. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  202. brelse(dibh);
  203. }
  204. return 0;
  205. }
  206. /**
  207. * find_metapath - Find path through the metadata tree
  208. * @ip: The inode pointer
  209. * @mp: The metapath to return the result in
  210. * @block: The disk block to look up
  211. *
  212. * This routine returns a struct metapath structure that defines a path
  213. * through the metadata of inode "ip" to get to block "block".
  214. *
  215. * Example:
  216. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  217. * filesystem with a blocksize of 4096.
  218. *
  219. * find_metapath() would return a struct metapath structure set to:
  220. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  221. * and mp_list[2] = 165.
  222. *
  223. * That means that in order to get to the block containing the byte at
  224. * offset 101342453, we would load the indirect block pointed to by pointer
  225. * 0 in the dinode. We would then load the indirect block pointed to by
  226. * pointer 48 in that indirect block. We would then load the data block
  227. * pointed to by pointer 165 in that indirect block.
  228. *
  229. * ----------------------------------------
  230. * | Dinode | |
  231. * | | 4|
  232. * | |0 1 2 3 4 5 9|
  233. * | | 6|
  234. * ----------------------------------------
  235. * |
  236. * |
  237. * V
  238. * ----------------------------------------
  239. * | Indirect Block |
  240. * | 5|
  241. * | 4 4 4 4 4 5 5 1|
  242. * |0 5 6 7 8 9 0 1 2|
  243. * ----------------------------------------
  244. * |
  245. * |
  246. * V
  247. * ----------------------------------------
  248. * | Indirect Block |
  249. * | 1 1 1 1 1 5|
  250. * | 6 6 6 6 6 1|
  251. * |0 3 4 5 6 7 2|
  252. * ----------------------------------------
  253. * |
  254. * |
  255. * V
  256. * ----------------------------------------
  257. * | Data block containing offset |
  258. * | 101342453 |
  259. * | |
  260. * | |
  261. * ----------------------------------------
  262. *
  263. */
  264. static void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath *mp)
  265. {
  266. struct gfs2_sbd *sdp = ip->i_sbd;
  267. uint64_t b = block;
  268. unsigned int i;
  269. for (i = ip->i_di.di_height; i--;)
  270. mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
  271. }
  272. /**
  273. * metapointer - Return pointer to start of metadata in a buffer
  274. * @bh: The buffer
  275. * @height: The metadata height (0 = dinode)
  276. * @mp: The metapath
  277. *
  278. * Return a pointer to the block number of the next height of the metadata
  279. * tree given a buffer containing the pointer to the current height of the
  280. * metadata tree.
  281. */
  282. static inline uint64_t *metapointer(struct buffer_head *bh,
  283. unsigned int height, struct metapath *mp)
  284. {
  285. unsigned int head_size = (height > 0) ?
  286. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  287. return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
  288. }
  289. /**
  290. * lookup_block - Get the next metadata block in metadata tree
  291. * @ip: The GFS2 inode
  292. * @bh: Buffer containing the pointers to metadata blocks
  293. * @height: The height of the tree (0 = dinode)
  294. * @mp: The metapath
  295. * @create: Non-zero if we may create a new meatdata block
  296. * @new: Used to indicate if we did create a new metadata block
  297. * @block: the returned disk block number
  298. *
  299. * Given a metatree, complete to a particular height, checks to see if the next
  300. * height of the tree exists. If not the next height of the tree is created.
  301. * The block number of the next height of the metadata tree is returned.
  302. *
  303. */
  304. static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  305. unsigned int height, struct metapath *mp, int create,
  306. int *new, uint64_t *block)
  307. {
  308. uint64_t *ptr = metapointer(bh, height, mp);
  309. if (*ptr) {
  310. *block = be64_to_cpu(*ptr);
  311. return;
  312. }
  313. *block = 0;
  314. if (!create)
  315. return;
  316. if (height == ip->i_di.di_height - 1 &&
  317. !gfs2_is_jdata(ip))
  318. *block = gfs2_alloc_data(ip);
  319. else
  320. *block = gfs2_alloc_meta(ip);
  321. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  322. *ptr = cpu_to_be64(*block);
  323. ip->i_di.di_blocks++;
  324. *new = 1;
  325. }
  326. /**
  327. * gfs2_block_map - Map a block from an inode to a disk block
  328. * @ip: The GFS2 inode
  329. * @lblock: The logical block number
  330. * @new: Value/Result argument (1 = may create/did create new blocks)
  331. * @dblock: the disk block number of the start of an extent
  332. * @extlen: the size of the extent
  333. *
  334. * Find the block number on the current device which corresponds to an
  335. * inode's block. If the block had to be created, "new" will be set.
  336. *
  337. * Returns: errno
  338. */
  339. int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
  340. uint64_t *dblock, uint32_t *extlen)
  341. {
  342. struct gfs2_sbd *sdp = ip->i_sbd;
  343. struct buffer_head *bh;
  344. struct metapath mp;
  345. int create = *new;
  346. unsigned int bsize;
  347. unsigned int height;
  348. unsigned int end_of_metadata;
  349. unsigned int x;
  350. int error = 0;
  351. *new = 0;
  352. *dblock = 0;
  353. if (extlen)
  354. *extlen = 0;
  355. if (create)
  356. down_write(&ip->i_rw_mutex);
  357. else
  358. down_read(&ip->i_rw_mutex);
  359. if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  360. goto out;
  361. bsize = (gfs2_is_jdata(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
  362. height = calc_tree_height(ip, (lblock + 1) * bsize);
  363. if (ip->i_di.di_height < height) {
  364. if (!create)
  365. goto out;
  366. error = build_height(ip, height);
  367. if (error)
  368. goto out;
  369. }
  370. find_metapath(ip, lblock, &mp);
  371. end_of_metadata = ip->i_di.di_height - 1;
  372. error = gfs2_meta_inode_buffer(ip, &bh);
  373. if (error)
  374. goto out;
  375. for (x = 0; x < end_of_metadata; x++) {
  376. lookup_block(ip, bh, x, &mp, create, new, dblock);
  377. brelse(bh);
  378. if (!*dblock)
  379. goto out;
  380. error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
  381. if (error)
  382. goto out;
  383. }
  384. lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
  385. if (extlen && *dblock) {
  386. *extlen = 1;
  387. if (!*new) {
  388. uint64_t tmp_dblock;
  389. int tmp_new;
  390. unsigned int nptrs;
  391. nptrs = (end_of_metadata) ? sdp->sd_inptrs :
  392. sdp->sd_diptrs;
  393. while (++mp.mp_list[end_of_metadata] < nptrs) {
  394. lookup_block(ip, bh, end_of_metadata, &mp,
  395. 0, &tmp_new, &tmp_dblock);
  396. if (*dblock + *extlen != tmp_dblock)
  397. break;
  398. (*extlen)++;
  399. }
  400. }
  401. }
  402. brelse(bh);
  403. if (*new) {
  404. error = gfs2_meta_inode_buffer(ip, &bh);
  405. if (!error) {
  406. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  407. gfs2_dinode_out(&ip->i_di, bh->b_data);
  408. brelse(bh);
  409. }
  410. }
  411. out:
  412. if (create)
  413. up_write(&ip->i_rw_mutex);
  414. else
  415. up_read(&ip->i_rw_mutex);
  416. return error;
  417. }
  418. /**
  419. * recursive_scan - recursively scan through the end of a file
  420. * @ip: the inode
  421. * @dibh: the dinode buffer
  422. * @mp: the path through the metadata to the point to start
  423. * @height: the height the recursion is at
  424. * @block: the indirect block to look at
  425. * @first: 1 if this is the first block
  426. * @bc: the call to make for each piece of metadata
  427. * @data: data opaque to this function to pass to @bc
  428. *
  429. * When this is first called @height and @block should be zero and
  430. * @first should be 1.
  431. *
  432. * Returns: errno
  433. */
  434. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  435. struct metapath *mp, unsigned int height,
  436. uint64_t block, int first, block_call_t bc,
  437. void *data)
  438. {
  439. struct gfs2_sbd *sdp = ip->i_sbd;
  440. struct buffer_head *bh = NULL;
  441. uint64_t *top, *bottom;
  442. uint64_t bn;
  443. int error;
  444. int mh_size = sizeof(struct gfs2_meta_header);
  445. if (!height) {
  446. error = gfs2_meta_inode_buffer(ip, &bh);
  447. if (error)
  448. return error;
  449. dibh = bh;
  450. top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  451. mp->mp_list[0];
  452. bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  453. sdp->sd_diptrs;
  454. } else {
  455. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  456. if (error)
  457. return error;
  458. top = (uint64_t *)(bh->b_data + mh_size) +
  459. ((first) ? mp->mp_list[height] : 0);
  460. bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  461. }
  462. error = bc(ip, dibh, bh, top, bottom, height, data);
  463. if (error)
  464. goto out;
  465. if (height < ip->i_di.di_height - 1)
  466. for (; top < bottom; top++, first = 0) {
  467. if (!*top)
  468. continue;
  469. bn = be64_to_cpu(*top);
  470. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  471. first, bc, data);
  472. if (error)
  473. break;
  474. }
  475. out:
  476. brelse(bh);
  477. return error;
  478. }
  479. /**
  480. * do_strip - Look for a layer a particular layer of the file and strip it off
  481. * @ip: the inode
  482. * @dibh: the dinode buffer
  483. * @bh: A buffer of pointers
  484. * @top: The first pointer in the buffer
  485. * @bottom: One more than the last pointer
  486. * @height: the height this buffer is at
  487. * @data: a pointer to a struct strip_mine
  488. *
  489. * Returns: errno
  490. */
  491. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  492. struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
  493. unsigned int height, void *data)
  494. {
  495. struct strip_mine *sm = (struct strip_mine *)data;
  496. struct gfs2_sbd *sdp = ip->i_sbd;
  497. struct gfs2_rgrp_list rlist;
  498. uint64_t bn, bstart;
  499. uint32_t blen;
  500. uint64_t *p;
  501. unsigned int rg_blocks = 0;
  502. int metadata;
  503. unsigned int revokes = 0;
  504. int x;
  505. int error;
  506. if (!*top)
  507. sm->sm_first = 0;
  508. if (height != sm->sm_height)
  509. return 0;
  510. if (sm->sm_first) {
  511. top++;
  512. sm->sm_first = 0;
  513. }
  514. metadata = (height != ip->i_di.di_height - 1) || gfs2_is_jdata(ip);
  515. if (metadata)
  516. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  517. error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
  518. if (error)
  519. return error;
  520. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  521. bstart = 0;
  522. blen = 0;
  523. for (p = top; p < bottom; p++) {
  524. if (!*p)
  525. continue;
  526. bn = be64_to_cpu(*p);
  527. if (bstart + blen == bn)
  528. blen++;
  529. else {
  530. if (bstart)
  531. gfs2_rlist_add(sdp, &rlist, bstart);
  532. bstart = bn;
  533. blen = 1;
  534. }
  535. }
  536. if (bstart)
  537. gfs2_rlist_add(sdp, &rlist, bstart);
  538. else
  539. goto out; /* Nothing to do */
  540. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  541. for (x = 0; x < rlist.rl_rgrps; x++) {
  542. struct gfs2_rgrpd *rgd;
  543. rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
  544. rg_blocks += rgd->rd_ri.ri_length;
  545. }
  546. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  547. if (error)
  548. goto out_rlist;
  549. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  550. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  551. revokes);
  552. if (error)
  553. goto out_rg_gunlock;
  554. down_write(&ip->i_rw_mutex);
  555. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  556. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  557. bstart = 0;
  558. blen = 0;
  559. for (p = top; p < bottom; p++) {
  560. if (!*p)
  561. continue;
  562. bn = be64_to_cpu(*p);
  563. if (bstart + blen == bn)
  564. blen++;
  565. else {
  566. if (bstart) {
  567. if (metadata)
  568. gfs2_free_meta(ip, bstart, blen);
  569. else
  570. gfs2_free_data(ip, bstart, blen);
  571. }
  572. bstart = bn;
  573. blen = 1;
  574. }
  575. *p = 0;
  576. if (!ip->i_di.di_blocks)
  577. gfs2_consist_inode(ip);
  578. ip->i_di.di_blocks--;
  579. }
  580. if (bstart) {
  581. if (metadata)
  582. gfs2_free_meta(ip, bstart, blen);
  583. else
  584. gfs2_free_data(ip, bstart, blen);
  585. }
  586. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  587. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  588. up_write(&ip->i_rw_mutex);
  589. gfs2_trans_end(sdp);
  590. out_rg_gunlock:
  591. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  592. out_rlist:
  593. gfs2_rlist_free(&rlist);
  594. out:
  595. gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
  596. return error;
  597. }
  598. /**
  599. * do_grow - Make a file look bigger than it is
  600. * @ip: the inode
  601. * @size: the size to set the file to
  602. *
  603. * Called with an exclusive lock on @ip.
  604. *
  605. * Returns: errno
  606. */
  607. static int do_grow(struct gfs2_inode *ip, uint64_t size)
  608. {
  609. struct gfs2_sbd *sdp = ip->i_sbd;
  610. struct gfs2_alloc *al;
  611. struct buffer_head *dibh;
  612. unsigned int h;
  613. int error;
  614. al = gfs2_alloc_get(ip);
  615. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  616. if (error)
  617. goto out;
  618. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  619. if (error)
  620. goto out_gunlock_q;
  621. al->al_requested = sdp->sd_max_height + RES_DATA;
  622. error = gfs2_inplace_reserve(ip);
  623. if (error)
  624. goto out_gunlock_q;
  625. error = gfs2_trans_begin(sdp,
  626. sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
  627. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  628. if (error)
  629. goto out_ipres;
  630. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  631. if (gfs2_is_stuffed(ip)) {
  632. error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
  633. NULL);
  634. if (error)
  635. goto out_end_trans;
  636. }
  637. h = calc_tree_height(ip, size);
  638. if (ip->i_di.di_height < h) {
  639. down_write(&ip->i_rw_mutex);
  640. error = build_height(ip, h);
  641. up_write(&ip->i_rw_mutex);
  642. if (error)
  643. goto out_end_trans;
  644. }
  645. }
  646. ip->i_di.di_size = size;
  647. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  648. error = gfs2_meta_inode_buffer(ip, &dibh);
  649. if (error)
  650. goto out_end_trans;
  651. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  652. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  653. brelse(dibh);
  654. out_end_trans:
  655. gfs2_trans_end(sdp);
  656. out_ipres:
  657. gfs2_inplace_release(ip);
  658. out_gunlock_q:
  659. gfs2_quota_unlock(ip);
  660. out:
  661. gfs2_alloc_put(ip);
  662. return error;
  663. }
  664. static int truncator_journaled(struct gfs2_inode *ip, uint64_t size)
  665. {
  666. uint64_t lbn, dbn;
  667. uint32_t off;
  668. struct buffer_head *bh;
  669. int new = 0;
  670. int error;
  671. lbn = size;
  672. off = do_div(lbn, ip->i_sbd->sd_jbsize);
  673. error = gfs2_block_map(ip, lbn, &new, &dbn, NULL);
  674. if (error || !dbn)
  675. return error;
  676. error = gfs2_jdata_get_buffer(ip, dbn, 0, &bh);
  677. if (error)
  678. return error;
  679. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  680. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header) + off);
  681. brelse(bh);
  682. return 0;
  683. }
  684. static int trunc_start(struct gfs2_inode *ip, uint64_t size)
  685. {
  686. struct gfs2_sbd *sdp = ip->i_sbd;
  687. struct buffer_head *dibh;
  688. int journaled = gfs2_is_jdata(ip);
  689. int error;
  690. error = gfs2_trans_begin(sdp,
  691. RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
  692. if (error)
  693. return error;
  694. error = gfs2_meta_inode_buffer(ip, &dibh);
  695. if (error)
  696. goto out;
  697. if (gfs2_is_stuffed(ip)) {
  698. ip->i_di.di_size = size;
  699. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  700. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  701. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  702. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  703. error = 1;
  704. } else {
  705. if (journaled) {
  706. uint64_t junk = size;
  707. /* we're just interested in the modulus */
  708. if (do_div(junk, sdp->sd_jbsize))
  709. error = truncator_journaled(ip, size);
  710. } else if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
  711. error = gfs2_truncator_page(ip, size);
  712. if (!error) {
  713. ip->i_di.di_size = size;
  714. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  715. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  716. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  717. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  718. }
  719. }
  720. brelse(dibh);
  721. out:
  722. gfs2_trans_end(sdp);
  723. return error;
  724. }
  725. static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
  726. {
  727. unsigned int height = ip->i_di.di_height;
  728. uint64_t lblock;
  729. struct metapath mp;
  730. int error;
  731. if (!size)
  732. lblock = 0;
  733. else if (gfs2_is_jdata(ip)) {
  734. lblock = size - 1;
  735. do_div(lblock, ip->i_sbd->sd_jbsize);
  736. } else
  737. lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
  738. find_metapath(ip, lblock, &mp);
  739. gfs2_alloc_get(ip);
  740. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  741. if (error)
  742. goto out;
  743. while (height--) {
  744. struct strip_mine sm;
  745. sm.sm_first = !!size;
  746. sm.sm_height = height;
  747. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  748. if (error)
  749. break;
  750. }
  751. gfs2_quota_unhold(ip);
  752. out:
  753. gfs2_alloc_put(ip);
  754. return error;
  755. }
  756. static int trunc_end(struct gfs2_inode *ip)
  757. {
  758. struct gfs2_sbd *sdp = ip->i_sbd;
  759. struct buffer_head *dibh;
  760. int error;
  761. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  762. if (error)
  763. return error;
  764. down_write(&ip->i_rw_mutex);
  765. error = gfs2_meta_inode_buffer(ip, &dibh);
  766. if (error)
  767. goto out;
  768. if (!ip->i_di.di_size) {
  769. ip->i_di.di_height = 0;
  770. ip->i_di.di_goal_meta =
  771. ip->i_di.di_goal_data =
  772. ip->i_num.no_addr;
  773. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  774. }
  775. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  776. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  777. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  778. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  779. brelse(dibh);
  780. out:
  781. up_write(&ip->i_rw_mutex);
  782. gfs2_trans_end(sdp);
  783. return error;
  784. }
  785. /**
  786. * do_shrink - make a file smaller
  787. * @ip: the inode
  788. * @size: the size to make the file
  789. * @truncator: function to truncate the last partial block
  790. *
  791. * Called with an exclusive lock on @ip.
  792. *
  793. * Returns: errno
  794. */
  795. static int do_shrink(struct gfs2_inode *ip, uint64_t size)
  796. {
  797. int error;
  798. error = trunc_start(ip, size);
  799. if (error < 0)
  800. return error;
  801. if (error > 0)
  802. return 0;
  803. error = trunc_dealloc(ip, size);
  804. if (!error)
  805. error = trunc_end(ip);
  806. return error;
  807. }
  808. /**
  809. * gfs2_truncatei - make a file a given size
  810. * @ip: the inode
  811. * @size: the size to make the file
  812. * @truncator: function to truncate the last partial block
  813. *
  814. * The file size can grow, shrink, or stay the same size.
  815. *
  816. * Returns: errno
  817. */
  818. int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
  819. {
  820. int error;
  821. if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
  822. return -EINVAL;
  823. if (size > ip->i_di.di_size)
  824. error = do_grow(ip, size);
  825. else
  826. error = do_shrink(ip, size);
  827. return error;
  828. }
  829. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  830. {
  831. int error;
  832. error = trunc_dealloc(ip, ip->i_di.di_size);
  833. if (!error)
  834. error = trunc_end(ip);
  835. return error;
  836. }
  837. int gfs2_file_dealloc(struct gfs2_inode *ip)
  838. {
  839. return trunc_dealloc(ip, 0);
  840. }
  841. /**
  842. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  843. * @ip: the file
  844. * @len: the number of bytes to be written to the file
  845. * @data_blocks: returns the number of data blocks required
  846. * @ind_blocks: returns the number of indirect blocks required
  847. *
  848. */
  849. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  850. unsigned int *data_blocks, unsigned int *ind_blocks)
  851. {
  852. struct gfs2_sbd *sdp = ip->i_sbd;
  853. unsigned int tmp;
  854. if (gfs2_is_jdata(ip)) {
  855. *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2;
  856. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  857. } else {
  858. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  859. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  860. }
  861. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  862. tmp = DIV_RU(tmp, sdp->sd_inptrs);
  863. *ind_blocks += tmp;
  864. }
  865. }
  866. /**
  867. * gfs2_write_alloc_required - figure out if a write will require an allocation
  868. * @ip: the file being written to
  869. * @offset: the offset to write to
  870. * @len: the number of bytes being written
  871. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  872. *
  873. * Returns: errno
  874. */
  875. int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
  876. unsigned int len, int *alloc_required)
  877. {
  878. struct gfs2_sbd *sdp = ip->i_sbd;
  879. uint64_t lblock, lblock_stop, dblock;
  880. uint32_t extlen;
  881. int new = 0;
  882. int error = 0;
  883. *alloc_required = 0;
  884. if (!len)
  885. return 0;
  886. if (gfs2_is_stuffed(ip)) {
  887. if (offset + len >
  888. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  889. *alloc_required = 1;
  890. return 0;
  891. }
  892. if (gfs2_is_jdata(ip)) {
  893. unsigned int bsize = sdp->sd_jbsize;
  894. lblock = offset;
  895. do_div(lblock, bsize);
  896. lblock_stop = offset + len + bsize - 1;
  897. do_div(lblock_stop, bsize);
  898. } else {
  899. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  900. lblock = offset >> shift;
  901. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  902. }
  903. for (; lblock < lblock_stop; lblock += extlen) {
  904. error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
  905. if (error)
  906. return error;
  907. if (!dblock) {
  908. *alloc_required = 1;
  909. return 0;
  910. }
  911. }
  912. return 0;
  913. }