bmap.c 28 KB

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