bmap.c 29 KB

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