bmap.c 25 KB

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