bmap.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/crc32.h>
  15. #include <linux/lm_interface.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "bmap.h"
  19. #include "glock.h"
  20. #include "inode.h"
  21. #include "meta_io.h"
  22. #include "quota.h"
  23. #include "rgrp.h"
  24. #include "trans.h"
  25. #include "dir.h"
  26. #include "util.h"
  27. #include "ops_address.h"
  28. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  29. * block is 512, so __u16 is fine for that. It saves stack space to
  30. * keep it small.
  31. */
  32. struct metapath {
  33. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  34. };
  35. typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
  36. struct buffer_head *bh, __be64 *top,
  37. __be64 *bottom, unsigned int height,
  38. void *data);
  39. struct strip_mine {
  40. int sm_first;
  41. unsigned int sm_height;
  42. };
  43. /**
  44. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  45. * @ip: the inode
  46. * @dibh: the dinode buffer
  47. * @block: the block number that was allocated
  48. * @private: any locked page held by the caller process
  49. *
  50. * Returns: errno
  51. */
  52. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  53. u64 block, struct page *page)
  54. {
  55. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  56. struct inode *inode = &ip->i_inode;
  57. struct buffer_head *bh;
  58. int release = 0;
  59. if (!page || page->index) {
  60. page = grab_cache_page(inode->i_mapping, 0);
  61. if (!page)
  62. return -ENOMEM;
  63. release = 1;
  64. }
  65. if (!PageUptodate(page)) {
  66. void *kaddr = kmap(page);
  67. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  68. ip->i_di.di_size);
  69. memset(kaddr + ip->i_di.di_size, 0,
  70. PAGE_CACHE_SIZE - ip->i_di.di_size);
  71. kunmap(page);
  72. SetPageUptodate(page);
  73. }
  74. if (!page_has_buffers(page))
  75. create_empty_buffers(page, 1 << inode->i_blkbits,
  76. (1 << BH_Uptodate));
  77. bh = page_buffers(page);
  78. if (!buffer_mapped(bh))
  79. map_bh(bh, inode->i_sb, block);
  80. set_buffer_uptodate(bh);
  81. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  82. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  83. mark_buffer_dirty(bh);
  84. if (release) {
  85. unlock_page(page);
  86. page_cache_release(page);
  87. }
  88. return 0;
  89. }
  90. /**
  91. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  92. * @ip: The GFS2 inode to unstuff
  93. * @unstuffer: the routine that handles unstuffing a non-zero length file
  94. * @private: private data for the unstuffer
  95. *
  96. * This routine unstuffs a dinode and returns it to a "normal" state such
  97. * that the height can be grown in the traditional way.
  98. *
  99. * Returns: errno
  100. */
  101. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  102. {
  103. struct buffer_head *bh, *dibh;
  104. struct gfs2_dinode *di;
  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, sizeof(struct gfs2_meta_header),
  121. dibh, sizeof(struct gfs2_dinode));
  122. brelse(bh);
  123. } else {
  124. block = gfs2_alloc_data(ip);
  125. error = gfs2_unstuffer_page(ip, dibh, block, page);
  126. if (error)
  127. goto out_brelse;
  128. }
  129. }
  130. /* Set up the pointer to the new block */
  131. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  132. di = (struct gfs2_dinode *)dibh->b_data;
  133. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  134. if (ip->i_di.di_size) {
  135. *(__be64 *)(di + 1) = cpu_to_be64(block);
  136. ip->i_di.di_blocks++;
  137. gfs2_set_inode_blocks(&ip->i_inode);
  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. __be64 *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 = (__be64 *)(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. gfs2_set_inode_blocks(&ip->i_inode);
  234. di->di_height = cpu_to_be16(ip->i_di.di_height);
  235. di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
  236. brelse(dibh);
  237. return error;
  238. }
  239. /**
  240. * find_metapath - Find path through the metadata tree
  241. * @ip: The inode pointer
  242. * @mp: The metapath to return the result in
  243. * @block: The disk block to look up
  244. *
  245. * This routine returns a struct metapath structure that defines a path
  246. * through the metadata of inode "ip" to get to block "block".
  247. *
  248. * Example:
  249. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  250. * filesystem with a blocksize of 4096.
  251. *
  252. * find_metapath() would return a struct metapath structure set to:
  253. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  254. * and mp_list[2] = 165.
  255. *
  256. * That means that in order to get to the block containing the byte at
  257. * offset 101342453, we would load the indirect block pointed to by pointer
  258. * 0 in the dinode. We would then load the indirect block pointed to by
  259. * pointer 48 in that indirect block. We would then load the data block
  260. * pointed to by pointer 165 in that indirect block.
  261. *
  262. * ----------------------------------------
  263. * | Dinode | |
  264. * | | 4|
  265. * | |0 1 2 3 4 5 9|
  266. * | | 6|
  267. * ----------------------------------------
  268. * |
  269. * |
  270. * V
  271. * ----------------------------------------
  272. * | Indirect Block |
  273. * | 5|
  274. * | 4 4 4 4 4 5 5 1|
  275. * |0 5 6 7 8 9 0 1 2|
  276. * ----------------------------------------
  277. * |
  278. * |
  279. * V
  280. * ----------------------------------------
  281. * | Indirect Block |
  282. * | 1 1 1 1 1 5|
  283. * | 6 6 6 6 6 1|
  284. * |0 3 4 5 6 7 2|
  285. * ----------------------------------------
  286. * |
  287. * |
  288. * V
  289. * ----------------------------------------
  290. * | Data block containing offset |
  291. * | 101342453 |
  292. * | |
  293. * | |
  294. * ----------------------------------------
  295. *
  296. */
  297. static void find_metapath(struct gfs2_inode *ip, u64 block,
  298. struct metapath *mp)
  299. {
  300. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  301. u64 b = block;
  302. unsigned int i;
  303. for (i = ip->i_di.di_height; i--;)
  304. mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
  305. }
  306. /**
  307. * metapointer - Return pointer to start of metadata in a buffer
  308. * @bh: The buffer
  309. * @height: The metadata height (0 = dinode)
  310. * @mp: The metapath
  311. *
  312. * Return a pointer to the block number of the next height of the metadata
  313. * tree given a buffer containing the pointer to the current height of the
  314. * metadata tree.
  315. */
  316. static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
  317. unsigned int height, const struct metapath *mp)
  318. {
  319. unsigned int head_size = (height > 0) ?
  320. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  321. __be64 *ptr;
  322. *boundary = 0;
  323. ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  324. if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
  325. *boundary = 1;
  326. return ptr;
  327. }
  328. /**
  329. * lookup_block - Get the next metadata block in metadata tree
  330. * @ip: The GFS2 inode
  331. * @bh: Buffer containing the pointers to metadata blocks
  332. * @height: The height of the tree (0 = dinode)
  333. * @mp: The metapath
  334. * @create: Non-zero if we may create a new meatdata block
  335. * @new: Used to indicate if we did create a new metadata block
  336. * @block: the returned disk block number
  337. *
  338. * Given a metatree, complete to a particular height, checks to see if the next
  339. * height of the tree exists. If not the next height of the tree is created.
  340. * The block number of the next height of the metadata tree is returned.
  341. *
  342. */
  343. static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  344. unsigned int height, struct metapath *mp, int create,
  345. int *new, u64 *block)
  346. {
  347. int boundary;
  348. __be64 *ptr = metapointer(bh, &boundary, height, mp);
  349. if (*ptr) {
  350. *block = be64_to_cpu(*ptr);
  351. return boundary;
  352. }
  353. *block = 0;
  354. if (!create)
  355. return 0;
  356. if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
  357. *block = gfs2_alloc_data(ip);
  358. else
  359. *block = gfs2_alloc_meta(ip);
  360. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  361. *ptr = cpu_to_be64(*block);
  362. ip->i_di.di_blocks++;
  363. gfs2_set_inode_blocks(&ip->i_inode);
  364. *new = 1;
  365. return 0;
  366. }
  367. static inline void bmap_lock(struct inode *inode, int create)
  368. {
  369. struct gfs2_inode *ip = GFS2_I(inode);
  370. if (create)
  371. down_write(&ip->i_rw_mutex);
  372. else
  373. down_read(&ip->i_rw_mutex);
  374. }
  375. static inline void bmap_unlock(struct inode *inode, int create)
  376. {
  377. struct gfs2_inode *ip = GFS2_I(inode);
  378. if (create)
  379. up_write(&ip->i_rw_mutex);
  380. else
  381. up_read(&ip->i_rw_mutex);
  382. }
  383. /**
  384. * gfs2_block_map - Map a block from an inode to a disk block
  385. * @inode: The inode
  386. * @lblock: The logical block number
  387. * @bh_map: The bh to be mapped
  388. *
  389. * Find the block number on the current device which corresponds to an
  390. * inode's block. If the block had to be created, "new" will be set.
  391. *
  392. * Returns: errno
  393. */
  394. int gfs2_block_map(struct inode *inode, u64 lblock, int create,
  395. struct buffer_head *bh_map)
  396. {
  397. struct gfs2_inode *ip = GFS2_I(inode);
  398. struct gfs2_sbd *sdp = GFS2_SB(inode);
  399. struct buffer_head *bh;
  400. unsigned int bsize;
  401. unsigned int height;
  402. unsigned int end_of_metadata;
  403. unsigned int x;
  404. int error = 0;
  405. int new = 0;
  406. u64 dblock = 0;
  407. int boundary;
  408. unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  409. struct metapath mp;
  410. u64 size;
  411. BUG_ON(maxlen == 0);
  412. if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  413. return 0;
  414. bmap_lock(inode, create);
  415. clear_buffer_mapped(bh_map);
  416. clear_buffer_new(bh_map);
  417. clear_buffer_boundary(bh_map);
  418. bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
  419. size = (lblock + 1) * bsize;
  420. if (size > ip->i_di.di_size) {
  421. height = calc_tree_height(ip, size);
  422. if (ip->i_di.di_height < height) {
  423. if (!create)
  424. goto out_ok;
  425. error = build_height(inode, height);
  426. if (error)
  427. goto out_fail;
  428. }
  429. }
  430. find_metapath(ip, lblock, &mp);
  431. end_of_metadata = ip->i_di.di_height - 1;
  432. error = gfs2_meta_inode_buffer(ip, &bh);
  433. if (error)
  434. goto out_fail;
  435. for (x = 0; x < end_of_metadata; x++) {
  436. lookup_block(ip, bh, x, &mp, create, &new, &dblock);
  437. brelse(bh);
  438. if (!dblock)
  439. goto out_ok;
  440. error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
  441. if (error)
  442. goto out_fail;
  443. }
  444. boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
  445. if (dblock) {
  446. map_bh(bh_map, inode->i_sb, dblock);
  447. if (boundary)
  448. set_buffer_boundary(bh_map);
  449. if (new) {
  450. struct buffer_head *dibh;
  451. error = gfs2_meta_inode_buffer(ip, &dibh);
  452. if (!error) {
  453. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  454. gfs2_dinode_out(ip, dibh->b_data);
  455. brelse(dibh);
  456. }
  457. set_buffer_new(bh_map);
  458. goto out_brelse;
  459. }
  460. while(--maxlen && !buffer_boundary(bh_map)) {
  461. u64 eblock;
  462. mp.mp_list[end_of_metadata]++;
  463. boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
  464. if (eblock != ++dblock)
  465. break;
  466. bh_map->b_size += (1 << inode->i_blkbits);
  467. if (boundary)
  468. set_buffer_boundary(bh_map);
  469. }
  470. }
  471. out_brelse:
  472. brelse(bh);
  473. out_ok:
  474. error = 0;
  475. out_fail:
  476. bmap_unlock(inode, create);
  477. return error;
  478. }
  479. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  480. {
  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. ret = gfs2_block_map(inode, lblock, create, &bh);
  489. *extlen = bh.b_size >> inode->i_blkbits;
  490. *dblock = bh.b_blocknr;
  491. if (buffer_new(&bh))
  492. *new = 1;
  493. else
  494. *new = 0;
  495. return ret;
  496. }
  497. /**
  498. * recursive_scan - recursively scan through the end of a file
  499. * @ip: the inode
  500. * @dibh: the dinode buffer
  501. * @mp: the path through the metadata to the point to start
  502. * @height: the height the recursion is at
  503. * @block: the indirect block to look at
  504. * @first: 1 if this is the first block
  505. * @bc: the call to make for each piece of metadata
  506. * @data: data opaque to this function to pass to @bc
  507. *
  508. * When this is first called @height and @block should be zero and
  509. * @first should be 1.
  510. *
  511. * Returns: errno
  512. */
  513. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  514. struct metapath *mp, unsigned int height,
  515. u64 block, int first, block_call_t bc,
  516. void *data)
  517. {
  518. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  519. struct buffer_head *bh = NULL;
  520. __be64 *top, *bottom;
  521. u64 bn;
  522. int error;
  523. int mh_size = sizeof(struct gfs2_meta_header);
  524. if (!height) {
  525. error = gfs2_meta_inode_buffer(ip, &bh);
  526. if (error)
  527. return error;
  528. dibh = bh;
  529. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  530. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  531. } else {
  532. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  533. if (error)
  534. return error;
  535. top = (__be64 *)(bh->b_data + mh_size) +
  536. (first ? mp->mp_list[height] : 0);
  537. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  538. }
  539. error = bc(ip, dibh, bh, top, bottom, height, data);
  540. if (error)
  541. goto out;
  542. if (height < ip->i_di.di_height - 1)
  543. for (; top < bottom; top++, first = 0) {
  544. if (!*top)
  545. continue;
  546. bn = be64_to_cpu(*top);
  547. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  548. first, bc, data);
  549. if (error)
  550. break;
  551. }
  552. out:
  553. brelse(bh);
  554. return error;
  555. }
  556. /**
  557. * do_strip - Look for a layer a particular layer of the file and strip it off
  558. * @ip: the inode
  559. * @dibh: the dinode buffer
  560. * @bh: A buffer of pointers
  561. * @top: The first pointer in the buffer
  562. * @bottom: One more than the last pointer
  563. * @height: the height this buffer is at
  564. * @data: a pointer to a struct strip_mine
  565. *
  566. * Returns: errno
  567. */
  568. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  569. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  570. unsigned int height, void *data)
  571. {
  572. struct strip_mine *sm = data;
  573. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  574. struct gfs2_rgrp_list rlist;
  575. u64 bn, bstart;
  576. u32 blen;
  577. __be64 *p;
  578. unsigned int rg_blocks = 0;
  579. int metadata;
  580. unsigned int revokes = 0;
  581. int x;
  582. int error;
  583. if (!*top)
  584. sm->sm_first = 0;
  585. if (height != sm->sm_height)
  586. return 0;
  587. if (sm->sm_first) {
  588. top++;
  589. sm->sm_first = 0;
  590. }
  591. metadata = (height != ip->i_di.di_height - 1);
  592. if (metadata)
  593. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  594. error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
  595. if (error)
  596. return error;
  597. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  598. bstart = 0;
  599. blen = 0;
  600. for (p = top; p < bottom; p++) {
  601. if (!*p)
  602. continue;
  603. bn = be64_to_cpu(*p);
  604. if (bstart + blen == bn)
  605. blen++;
  606. else {
  607. if (bstart)
  608. gfs2_rlist_add(sdp, &rlist, bstart);
  609. bstart = bn;
  610. blen = 1;
  611. }
  612. }
  613. if (bstart)
  614. gfs2_rlist_add(sdp, &rlist, bstart);
  615. else
  616. goto out; /* Nothing to do */
  617. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  618. for (x = 0; x < rlist.rl_rgrps; x++) {
  619. struct gfs2_rgrpd *rgd;
  620. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  621. rg_blocks += rgd->rd_length;
  622. }
  623. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  624. if (error)
  625. goto out_rlist;
  626. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  627. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  628. revokes);
  629. if (error)
  630. goto out_rg_gunlock;
  631. down_write(&ip->i_rw_mutex);
  632. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  633. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  634. bstart = 0;
  635. blen = 0;
  636. for (p = top; p < bottom; p++) {
  637. if (!*p)
  638. continue;
  639. bn = be64_to_cpu(*p);
  640. if (bstart + blen == bn)
  641. blen++;
  642. else {
  643. if (bstart) {
  644. if (metadata)
  645. gfs2_free_meta(ip, bstart, blen);
  646. else
  647. gfs2_free_data(ip, bstart, blen);
  648. }
  649. bstart = bn;
  650. blen = 1;
  651. }
  652. *p = 0;
  653. if (!ip->i_di.di_blocks)
  654. gfs2_consist_inode(ip);
  655. ip->i_di.di_blocks--;
  656. gfs2_set_inode_blocks(&ip->i_inode);
  657. }
  658. if (bstart) {
  659. if (metadata)
  660. gfs2_free_meta(ip, bstart, blen);
  661. else
  662. gfs2_free_data(ip, bstart, blen);
  663. }
  664. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  665. gfs2_dinode_out(ip, dibh->b_data);
  666. up_write(&ip->i_rw_mutex);
  667. gfs2_trans_end(sdp);
  668. out_rg_gunlock:
  669. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  670. out_rlist:
  671. gfs2_rlist_free(&rlist);
  672. out:
  673. gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
  674. return error;
  675. }
  676. /**
  677. * do_grow - Make a file look bigger than it is
  678. * @ip: the inode
  679. * @size: the size to set the file to
  680. *
  681. * Called with an exclusive lock on @ip.
  682. *
  683. * Returns: errno
  684. */
  685. static int do_grow(struct gfs2_inode *ip, u64 size)
  686. {
  687. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  688. struct gfs2_alloc *al;
  689. struct buffer_head *dibh;
  690. unsigned int h;
  691. int error;
  692. al = gfs2_alloc_get(ip);
  693. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  694. if (error)
  695. goto out;
  696. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  697. if (error)
  698. goto out_gunlock_q;
  699. al->al_requested = sdp->sd_max_height + RES_DATA;
  700. error = gfs2_inplace_reserve(ip);
  701. if (error)
  702. goto out_gunlock_q;
  703. error = gfs2_trans_begin(sdp,
  704. sdp->sd_max_height + al->al_rgd->rd_length +
  705. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  706. if (error)
  707. goto out_ipres;
  708. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  709. if (gfs2_is_stuffed(ip)) {
  710. error = gfs2_unstuff_dinode(ip, NULL);
  711. if (error)
  712. goto out_end_trans;
  713. }
  714. h = calc_tree_height(ip, size);
  715. if (ip->i_di.di_height < h) {
  716. down_write(&ip->i_rw_mutex);
  717. error = build_height(&ip->i_inode, h);
  718. up_write(&ip->i_rw_mutex);
  719. if (error)
  720. goto out_end_trans;
  721. }
  722. }
  723. ip->i_di.di_size = size;
  724. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  725. error = gfs2_meta_inode_buffer(ip, &dibh);
  726. if (error)
  727. goto out_end_trans;
  728. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  729. gfs2_dinode_out(ip, dibh->b_data);
  730. brelse(dibh);
  731. out_end_trans:
  732. gfs2_trans_end(sdp);
  733. out_ipres:
  734. gfs2_inplace_release(ip);
  735. out_gunlock_q:
  736. gfs2_quota_unlock(ip);
  737. out:
  738. gfs2_alloc_put(ip);
  739. return error;
  740. }
  741. /**
  742. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  743. *
  744. * This is partly borrowed from ext3.
  745. */
  746. static int gfs2_block_truncate_page(struct address_space *mapping)
  747. {
  748. struct inode *inode = mapping->host;
  749. struct gfs2_inode *ip = GFS2_I(inode);
  750. struct gfs2_sbd *sdp = GFS2_SB(inode);
  751. loff_t from = inode->i_size;
  752. unsigned long index = from >> PAGE_CACHE_SHIFT;
  753. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  754. unsigned blocksize, iblock, length, pos;
  755. struct buffer_head *bh;
  756. struct page *page;
  757. int err;
  758. page = grab_cache_page(mapping, index);
  759. if (!page)
  760. return 0;
  761. blocksize = inode->i_sb->s_blocksize;
  762. length = blocksize - (offset & (blocksize - 1));
  763. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  764. if (!page_has_buffers(page))
  765. create_empty_buffers(page, blocksize, 0);
  766. /* Find the buffer that contains "offset" */
  767. bh = page_buffers(page);
  768. pos = blocksize;
  769. while (offset >= pos) {
  770. bh = bh->b_this_page;
  771. iblock++;
  772. pos += blocksize;
  773. }
  774. err = 0;
  775. if (!buffer_mapped(bh)) {
  776. gfs2_get_block(inode, iblock, bh, 0);
  777. /* unmapped? It's a hole - nothing to do */
  778. if (!buffer_mapped(bh))
  779. goto unlock;
  780. }
  781. /* Ok, it's mapped. Make sure it's up-to-date */
  782. if (PageUptodate(page))
  783. set_buffer_uptodate(bh);
  784. if (!buffer_uptodate(bh)) {
  785. err = -EIO;
  786. ll_rw_block(READ, 1, &bh);
  787. wait_on_buffer(bh);
  788. /* Uhhuh. Read error. Complain and punt. */
  789. if (!buffer_uptodate(bh))
  790. goto unlock;
  791. err = 0;
  792. }
  793. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  794. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  795. zero_user_page(page, offset, length, 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_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  817. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  818. gfs2_dinode_out(ip, 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_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  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, 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_no_addr;
  882. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  883. }
  884. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  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, 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_inode.i_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. }