bmap.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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/spinlock.h>
  10. #include <linux/completion.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/gfs2_ondisk.h>
  13. #include <linux/crc32.h>
  14. #include "gfs2.h"
  15. #include "incore.h"
  16. #include "bmap.h"
  17. #include "glock.h"
  18. #include "inode.h"
  19. #include "meta_io.h"
  20. #include "quota.h"
  21. #include "rgrp.h"
  22. #include "trans.h"
  23. #include "dir.h"
  24. #include "util.h"
  25. #include "trace_gfs2.h"
  26. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  27. * block is 512, so __u16 is fine for that. It saves stack space to
  28. * keep it small.
  29. */
  30. struct metapath {
  31. struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
  32. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  33. };
  34. typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
  35. struct buffer_head *bh, __be64 *top,
  36. __be64 *bottom, unsigned int height,
  37. void *data);
  38. struct strip_mine {
  39. int sm_first;
  40. unsigned int sm_height;
  41. };
  42. /**
  43. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  44. * @ip: the inode
  45. * @dibh: the dinode buffer
  46. * @block: the block number that was allocated
  47. * @page: The (optional) page. This is looked up if @page is NULL
  48. *
  49. * Returns: errno
  50. */
  51. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  52. u64 block, struct page *page)
  53. {
  54. struct inode *inode = &ip->i_inode;
  55. struct buffer_head *bh;
  56. int release = 0;
  57. if (!page || page->index) {
  58. page = grab_cache_page(inode->i_mapping, 0);
  59. if (!page)
  60. return -ENOMEM;
  61. release = 1;
  62. }
  63. if (!PageUptodate(page)) {
  64. void *kaddr = kmap(page);
  65. u64 dsize = i_size_read(inode);
  66. if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
  67. dsize = dibh->b_size - sizeof(struct gfs2_dinode);
  68. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  69. memset(kaddr + dsize, 0, PAGE_CACHE_SIZE - dsize);
  70. kunmap(page);
  71. SetPageUptodate(page);
  72. }
  73. if (!page_has_buffers(page))
  74. create_empty_buffers(page, 1 << inode->i_blkbits,
  75. (1 << BH_Uptodate));
  76. bh = page_buffers(page);
  77. if (!buffer_mapped(bh))
  78. map_bh(bh, inode->i_sb, block);
  79. set_buffer_uptodate(bh);
  80. if (!gfs2_is_jdata(ip))
  81. mark_buffer_dirty(bh);
  82. if (!gfs2_is_writeback(ip))
  83. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  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. * @page: The (optional) page. This is looked up if the @page is NULL
  94. *
  95. * This routine unstuffs a dinode and returns it to a "normal" state such
  96. * that the height can be grown in the traditional way.
  97. *
  98. * Returns: errno
  99. */
  100. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  101. {
  102. struct buffer_head *bh, *dibh;
  103. struct gfs2_dinode *di;
  104. u64 block = 0;
  105. int isdir = gfs2_is_dir(ip);
  106. int error;
  107. down_write(&ip->i_rw_mutex);
  108. error = gfs2_meta_inode_buffer(ip, &dibh);
  109. if (error)
  110. goto out;
  111. if (i_size_read(&ip->i_inode)) {
  112. /* Get a free block, fill it with the stuffed data,
  113. and write it out to disk */
  114. unsigned int n = 1;
  115. error = gfs2_alloc_block(ip, &block, &n);
  116. if (error)
  117. goto out_brelse;
  118. if (isdir) {
  119. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  120. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  121. if (error)
  122. goto out_brelse;
  123. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  124. dibh, sizeof(struct gfs2_dinode));
  125. brelse(bh);
  126. } else {
  127. error = gfs2_unstuffer_page(ip, dibh, block, page);
  128. if (error)
  129. goto out_brelse;
  130. }
  131. }
  132. /* Set up the pointer to the new block */
  133. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  134. di = (struct gfs2_dinode *)dibh->b_data;
  135. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  136. if (i_size_read(&ip->i_inode)) {
  137. *(__be64 *)(di + 1) = cpu_to_be64(block);
  138. gfs2_add_inode_blocks(&ip->i_inode, 1);
  139. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  140. }
  141. ip->i_height = 1;
  142. di->di_height = cpu_to_be16(1);
  143. out_brelse:
  144. brelse(dibh);
  145. out:
  146. up_write(&ip->i_rw_mutex);
  147. return error;
  148. }
  149. /**
  150. * find_metapath - Find path through the metadata tree
  151. * @sdp: The superblock
  152. * @mp: The metapath to return the result in
  153. * @block: The disk block to look up
  154. * @height: The pre-calculated height of the metadata tree
  155. *
  156. * This routine returns a struct metapath structure that defines a path
  157. * through the metadata of inode "ip" to get to block "block".
  158. *
  159. * Example:
  160. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  161. * filesystem with a blocksize of 4096.
  162. *
  163. * find_metapath() would return a struct metapath structure set to:
  164. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  165. * and mp_list[2] = 165.
  166. *
  167. * That means that in order to get to the block containing the byte at
  168. * offset 101342453, we would load the indirect block pointed to by pointer
  169. * 0 in the dinode. We would then load the indirect block pointed to by
  170. * pointer 48 in that indirect block. We would then load the data block
  171. * pointed to by pointer 165 in that indirect block.
  172. *
  173. * ----------------------------------------
  174. * | Dinode | |
  175. * | | 4|
  176. * | |0 1 2 3 4 5 9|
  177. * | | 6|
  178. * ----------------------------------------
  179. * |
  180. * |
  181. * V
  182. * ----------------------------------------
  183. * | Indirect Block |
  184. * | 5|
  185. * | 4 4 4 4 4 5 5 1|
  186. * |0 5 6 7 8 9 0 1 2|
  187. * ----------------------------------------
  188. * |
  189. * |
  190. * V
  191. * ----------------------------------------
  192. * | Indirect Block |
  193. * | 1 1 1 1 1 5|
  194. * | 6 6 6 6 6 1|
  195. * |0 3 4 5 6 7 2|
  196. * ----------------------------------------
  197. * |
  198. * |
  199. * V
  200. * ----------------------------------------
  201. * | Data block containing offset |
  202. * | 101342453 |
  203. * | |
  204. * | |
  205. * ----------------------------------------
  206. *
  207. */
  208. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  209. struct metapath *mp, unsigned int height)
  210. {
  211. unsigned int i;
  212. for (i = height; i--;)
  213. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  214. }
  215. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  216. {
  217. if (mp->mp_list[0] == 0)
  218. return 2;
  219. return 1;
  220. }
  221. /**
  222. * metapointer - Return pointer to start of metadata in a buffer
  223. * @height: The metadata height (0 = dinode)
  224. * @mp: The metapath
  225. *
  226. * Return a pointer to the block number of the next height of the metadata
  227. * tree given a buffer containing the pointer to the current height of the
  228. * metadata tree.
  229. */
  230. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  231. {
  232. struct buffer_head *bh = mp->mp_bh[height];
  233. unsigned int head_size = (height > 0) ?
  234. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  235. return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  236. }
  237. /**
  238. * lookup_metapath - Walk the metadata tree to a specific point
  239. * @ip: The inode
  240. * @mp: The metapath
  241. *
  242. * Assumes that the inode's buffer has already been looked up and
  243. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  244. * by find_metapath().
  245. *
  246. * If this function encounters part of the tree which has not been
  247. * allocated, it returns the current height of the tree at the point
  248. * at which it found the unallocated block. Blocks which are found are
  249. * added to the mp->mp_bh[] list.
  250. *
  251. * Returns: error or height of metadata tree
  252. */
  253. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  254. {
  255. unsigned int end_of_metadata = ip->i_height - 1;
  256. unsigned int x;
  257. __be64 *ptr;
  258. u64 dblock;
  259. int ret;
  260. for (x = 0; x < end_of_metadata; x++) {
  261. ptr = metapointer(x, mp);
  262. dblock = be64_to_cpu(*ptr);
  263. if (!dblock)
  264. return x + 1;
  265. ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
  266. if (ret)
  267. return ret;
  268. }
  269. return ip->i_height;
  270. }
  271. static inline void release_metapath(struct metapath *mp)
  272. {
  273. int i;
  274. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  275. if (mp->mp_bh[i] == NULL)
  276. break;
  277. brelse(mp->mp_bh[i]);
  278. }
  279. }
  280. /**
  281. * gfs2_extent_length - Returns length of an extent of blocks
  282. * @start: Start of the buffer
  283. * @len: Length of the buffer in bytes
  284. * @ptr: Current position in the buffer
  285. * @limit: Max extent length to return (0 = unlimited)
  286. * @eob: Set to 1 if we hit "end of block"
  287. *
  288. * If the first block is zero (unallocated) it will return the number of
  289. * unallocated blocks in the extent, otherwise it will return the number
  290. * of contiguous blocks in the extent.
  291. *
  292. * Returns: The length of the extent (minimum of one block)
  293. */
  294. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
  295. {
  296. const __be64 *end = (start + len);
  297. const __be64 *first = ptr;
  298. u64 d = be64_to_cpu(*ptr);
  299. *eob = 0;
  300. do {
  301. ptr++;
  302. if (ptr >= end)
  303. break;
  304. if (limit && --limit == 0)
  305. break;
  306. if (d)
  307. d++;
  308. } while(be64_to_cpu(*ptr) == d);
  309. if (ptr >= end)
  310. *eob = 1;
  311. return (ptr - first);
  312. }
  313. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  314. {
  315. if (create)
  316. down_write(&ip->i_rw_mutex);
  317. else
  318. down_read(&ip->i_rw_mutex);
  319. }
  320. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  321. {
  322. if (create)
  323. up_write(&ip->i_rw_mutex);
  324. else
  325. up_read(&ip->i_rw_mutex);
  326. }
  327. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  328. struct gfs2_glock *gl, unsigned int i,
  329. unsigned offset, u64 bn)
  330. {
  331. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  332. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  333. sizeof(struct gfs2_dinode)));
  334. BUG_ON(i < 1);
  335. BUG_ON(mp->mp_bh[i] != NULL);
  336. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  337. gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
  338. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  339. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  340. ptr += offset;
  341. *ptr = cpu_to_be64(bn);
  342. return ptr;
  343. }
  344. enum alloc_state {
  345. ALLOC_DATA = 0,
  346. ALLOC_GROW_DEPTH = 1,
  347. ALLOC_GROW_HEIGHT = 2,
  348. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  349. };
  350. /**
  351. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  352. * @inode: The GFS2 inode
  353. * @lblock: The logical starting block of the extent
  354. * @bh_map: This is used to return the mapping details
  355. * @mp: The metapath
  356. * @sheight: The starting height (i.e. whats already mapped)
  357. * @height: The height to build to
  358. * @maxlen: The max number of data blocks to alloc
  359. *
  360. * In this routine we may have to alloc:
  361. * i) Indirect blocks to grow the metadata tree height
  362. * ii) Indirect blocks to fill in lower part of the metadata tree
  363. * iii) Data blocks
  364. *
  365. * The function is in two parts. The first part works out the total
  366. * number of blocks which we need. The second part does the actual
  367. * allocation asking for an extent at a time (if enough contiguous free
  368. * blocks are available, there will only be one request per bmap call)
  369. * and uses the state machine to initialise the blocks in order.
  370. *
  371. * Returns: errno on error
  372. */
  373. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  374. struct buffer_head *bh_map, struct metapath *mp,
  375. const unsigned int sheight,
  376. const unsigned int height,
  377. const unsigned int maxlen)
  378. {
  379. struct gfs2_inode *ip = GFS2_I(inode);
  380. struct gfs2_sbd *sdp = GFS2_SB(inode);
  381. struct buffer_head *dibh = mp->mp_bh[0];
  382. u64 bn, dblock = 0;
  383. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  384. unsigned dblks = 0;
  385. unsigned ptrs_per_blk;
  386. const unsigned end_of_metadata = height - 1;
  387. int eob = 0;
  388. enum alloc_state state;
  389. __be64 *ptr;
  390. __be64 zero_bn = 0;
  391. BUG_ON(sheight < 1);
  392. BUG_ON(dibh == NULL);
  393. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  394. if (height == sheight) {
  395. struct buffer_head *bh;
  396. /* Bottom indirect block exists, find unalloced extent size */
  397. ptr = metapointer(end_of_metadata, mp);
  398. bh = mp->mp_bh[end_of_metadata];
  399. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  400. &eob);
  401. BUG_ON(dblks < 1);
  402. state = ALLOC_DATA;
  403. } else {
  404. /* Need to allocate indirect blocks */
  405. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  406. dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
  407. if (height == ip->i_height) {
  408. /* Writing into existing tree, extend tree down */
  409. iblks = height - sheight;
  410. state = ALLOC_GROW_DEPTH;
  411. } else {
  412. /* Building up tree height */
  413. state = ALLOC_GROW_HEIGHT;
  414. iblks = height - ip->i_height;
  415. branch_start = metapath_branch_start(mp);
  416. iblks += (height - branch_start);
  417. }
  418. }
  419. /* start of the second part of the function (state machine) */
  420. blks = dblks + iblks;
  421. i = sheight;
  422. do {
  423. int error;
  424. n = blks - alloced;
  425. error = gfs2_alloc_block(ip, &bn, &n);
  426. if (error)
  427. return error;
  428. alloced += n;
  429. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  430. gfs2_trans_add_unrevoke(sdp, bn, n);
  431. switch (state) {
  432. /* Growing height of tree */
  433. case ALLOC_GROW_HEIGHT:
  434. if (i == 1) {
  435. ptr = (__be64 *)(dibh->b_data +
  436. sizeof(struct gfs2_dinode));
  437. zero_bn = *ptr;
  438. }
  439. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  440. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  441. if (i - 1 == height - ip->i_height) {
  442. i--;
  443. gfs2_buffer_copy_tail(mp->mp_bh[i],
  444. sizeof(struct gfs2_meta_header),
  445. dibh, sizeof(struct gfs2_dinode));
  446. gfs2_buffer_clear_tail(dibh,
  447. sizeof(struct gfs2_dinode) +
  448. sizeof(__be64));
  449. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  450. sizeof(struct gfs2_meta_header));
  451. *ptr = zero_bn;
  452. state = ALLOC_GROW_DEPTH;
  453. for(i = branch_start; i < height; i++) {
  454. if (mp->mp_bh[i] == NULL)
  455. break;
  456. brelse(mp->mp_bh[i]);
  457. mp->mp_bh[i] = NULL;
  458. }
  459. i = branch_start;
  460. }
  461. if (n == 0)
  462. break;
  463. /* Branching from existing tree */
  464. case ALLOC_GROW_DEPTH:
  465. if (i > 1 && i < height)
  466. gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
  467. for (; i < height && n > 0; i++, n--)
  468. gfs2_indirect_init(mp, ip->i_gl, i,
  469. mp->mp_list[i-1], bn++);
  470. if (i == height)
  471. state = ALLOC_DATA;
  472. if (n == 0)
  473. break;
  474. /* Tree complete, adding data blocks */
  475. case ALLOC_DATA:
  476. BUG_ON(n > dblks);
  477. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  478. gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
  479. dblks = n;
  480. ptr = metapointer(end_of_metadata, mp);
  481. dblock = bn;
  482. while (n-- > 0)
  483. *ptr++ = cpu_to_be64(bn++);
  484. break;
  485. }
  486. } while ((state != ALLOC_DATA) || !dblock);
  487. ip->i_height = height;
  488. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  489. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  490. map_bh(bh_map, inode->i_sb, dblock);
  491. bh_map->b_size = dblks << inode->i_blkbits;
  492. set_buffer_new(bh_map);
  493. return 0;
  494. }
  495. /**
  496. * gfs2_block_map - Map a block from an inode to a disk block
  497. * @inode: The inode
  498. * @lblock: The logical block number
  499. * @bh_map: The bh to be mapped
  500. * @create: True if its ok to alloc blocks to satify the request
  501. *
  502. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  503. * read of metadata will be required before the next block can be
  504. * mapped. Sets buffer_new() if new blocks were allocated.
  505. *
  506. * Returns: errno
  507. */
  508. int gfs2_block_map(struct inode *inode, sector_t lblock,
  509. struct buffer_head *bh_map, int create)
  510. {
  511. struct gfs2_inode *ip = GFS2_I(inode);
  512. struct gfs2_sbd *sdp = GFS2_SB(inode);
  513. unsigned int bsize = sdp->sd_sb.sb_bsize;
  514. const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  515. const u64 *arr = sdp->sd_heightsize;
  516. __be64 *ptr;
  517. u64 size;
  518. struct metapath mp;
  519. int ret;
  520. int eob;
  521. unsigned int len;
  522. struct buffer_head *bh;
  523. u8 height;
  524. BUG_ON(maxlen == 0);
  525. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  526. bmap_lock(ip, create);
  527. clear_buffer_mapped(bh_map);
  528. clear_buffer_new(bh_map);
  529. clear_buffer_boundary(bh_map);
  530. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  531. if (gfs2_is_dir(ip)) {
  532. bsize = sdp->sd_jbsize;
  533. arr = sdp->sd_jheightsize;
  534. }
  535. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  536. if (ret)
  537. goto out;
  538. height = ip->i_height;
  539. size = (lblock + 1) * bsize;
  540. while (size > arr[height])
  541. height++;
  542. find_metapath(sdp, lblock, &mp, height);
  543. ret = 1;
  544. if (height > ip->i_height || gfs2_is_stuffed(ip))
  545. goto do_alloc;
  546. ret = lookup_metapath(ip, &mp);
  547. if (ret < 0)
  548. goto out;
  549. if (ret != ip->i_height)
  550. goto do_alloc;
  551. ptr = metapointer(ip->i_height - 1, &mp);
  552. if (*ptr == 0)
  553. goto do_alloc;
  554. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  555. bh = mp.mp_bh[ip->i_height - 1];
  556. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  557. bh_map->b_size = (len << inode->i_blkbits);
  558. if (eob)
  559. set_buffer_boundary(bh_map);
  560. ret = 0;
  561. out:
  562. release_metapath(&mp);
  563. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  564. bmap_unlock(ip, create);
  565. return ret;
  566. do_alloc:
  567. /* All allocations are done here, firstly check create flag */
  568. if (!create) {
  569. BUG_ON(gfs2_is_stuffed(ip));
  570. ret = 0;
  571. goto out;
  572. }
  573. /* At this point ret is the tree depth of already allocated blocks */
  574. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  575. goto out;
  576. }
  577. /*
  578. * Deprecated: do not use in new code
  579. */
  580. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  581. {
  582. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  583. int ret;
  584. int create = *new;
  585. BUG_ON(!extlen);
  586. BUG_ON(!dblock);
  587. BUG_ON(!new);
  588. bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
  589. ret = gfs2_block_map(inode, lblock, &bh, create);
  590. *extlen = bh.b_size >> inode->i_blkbits;
  591. *dblock = bh.b_blocknr;
  592. if (buffer_new(&bh))
  593. *new = 1;
  594. else
  595. *new = 0;
  596. return ret;
  597. }
  598. /**
  599. * recursive_scan - recursively scan through the end of a file
  600. * @ip: the inode
  601. * @dibh: the dinode buffer
  602. * @mp: the path through the metadata to the point to start
  603. * @height: the height the recursion is at
  604. * @block: the indirect block to look at
  605. * @first: 1 if this is the first block
  606. * @bc: the call to make for each piece of metadata
  607. * @data: data opaque to this function to pass to @bc
  608. *
  609. * When this is first called @height and @block should be zero and
  610. * @first should be 1.
  611. *
  612. * Returns: errno
  613. */
  614. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  615. struct metapath *mp, unsigned int height,
  616. u64 block, int first, block_call_t bc,
  617. void *data)
  618. {
  619. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  620. struct buffer_head *bh = NULL;
  621. __be64 *top, *bottom;
  622. u64 bn;
  623. int error;
  624. int mh_size = sizeof(struct gfs2_meta_header);
  625. if (!height) {
  626. error = gfs2_meta_inode_buffer(ip, &bh);
  627. if (error)
  628. return error;
  629. dibh = bh;
  630. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  631. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  632. } else {
  633. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  634. if (error)
  635. return error;
  636. top = (__be64 *)(bh->b_data + mh_size) +
  637. (first ? mp->mp_list[height] : 0);
  638. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  639. }
  640. error = bc(ip, dibh, bh, top, bottom, height, data);
  641. if (error)
  642. goto out;
  643. if (height < ip->i_height - 1)
  644. for (; top < bottom; top++, first = 0) {
  645. if (!*top)
  646. continue;
  647. bn = be64_to_cpu(*top);
  648. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  649. first, bc, data);
  650. if (error)
  651. break;
  652. }
  653. out:
  654. brelse(bh);
  655. return error;
  656. }
  657. /**
  658. * do_strip - Look for a layer a particular layer of the file and strip it off
  659. * @ip: the inode
  660. * @dibh: the dinode buffer
  661. * @bh: A buffer of pointers
  662. * @top: The first pointer in the buffer
  663. * @bottom: One more than the last pointer
  664. * @height: the height this buffer is at
  665. * @data: a pointer to a struct strip_mine
  666. *
  667. * Returns: errno
  668. */
  669. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  670. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  671. unsigned int height, void *data)
  672. {
  673. struct strip_mine *sm = data;
  674. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  675. struct gfs2_rgrp_list rlist;
  676. u64 bn, bstart;
  677. u32 blen;
  678. __be64 *p;
  679. unsigned int rg_blocks = 0;
  680. int metadata;
  681. unsigned int revokes = 0;
  682. int x;
  683. int error = 0;
  684. if (!*top)
  685. sm->sm_first = 0;
  686. if (height != sm->sm_height)
  687. return 0;
  688. if (sm->sm_first) {
  689. top++;
  690. sm->sm_first = 0;
  691. }
  692. metadata = (height != ip->i_height - 1);
  693. if (metadata)
  694. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  695. if (ip != GFS2_I(sdp->sd_rindex))
  696. error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
  697. else if (!sdp->sd_rgrps)
  698. error = gfs2_ri_update(ip);
  699. if (error)
  700. return error;
  701. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  702. bstart = 0;
  703. blen = 0;
  704. for (p = top; p < bottom; p++) {
  705. if (!*p)
  706. continue;
  707. bn = be64_to_cpu(*p);
  708. if (bstart + blen == bn)
  709. blen++;
  710. else {
  711. if (bstart)
  712. gfs2_rlist_add(sdp, &rlist, bstart);
  713. bstart = bn;
  714. blen = 1;
  715. }
  716. }
  717. if (bstart)
  718. gfs2_rlist_add(sdp, &rlist, bstart);
  719. else
  720. goto out; /* Nothing to do */
  721. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  722. for (x = 0; x < rlist.rl_rgrps; x++) {
  723. struct gfs2_rgrpd *rgd;
  724. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  725. rg_blocks += rgd->rd_length;
  726. }
  727. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  728. if (error)
  729. goto out_rlist;
  730. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  731. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  732. revokes);
  733. if (error)
  734. goto out_rg_gunlock;
  735. down_write(&ip->i_rw_mutex);
  736. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  737. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  738. bstart = 0;
  739. blen = 0;
  740. for (p = top; p < bottom; p++) {
  741. if (!*p)
  742. continue;
  743. bn = be64_to_cpu(*p);
  744. if (bstart + blen == bn)
  745. blen++;
  746. else {
  747. if (bstart) {
  748. if (metadata)
  749. gfs2_free_meta(ip, bstart, blen);
  750. else
  751. gfs2_free_data(ip, bstart, blen);
  752. }
  753. bstart = bn;
  754. blen = 1;
  755. }
  756. *p = 0;
  757. gfs2_add_inode_blocks(&ip->i_inode, -1);
  758. }
  759. if (bstart) {
  760. if (metadata)
  761. gfs2_free_meta(ip, bstart, blen);
  762. else
  763. gfs2_free_data(ip, bstart, blen);
  764. }
  765. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  766. gfs2_dinode_out(ip, dibh->b_data);
  767. up_write(&ip->i_rw_mutex);
  768. gfs2_trans_end(sdp);
  769. out_rg_gunlock:
  770. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  771. out_rlist:
  772. gfs2_rlist_free(&rlist);
  773. out:
  774. if (ip != GFS2_I(sdp->sd_rindex))
  775. gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
  776. return error;
  777. }
  778. /**
  779. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  780. *
  781. * This is partly borrowed from ext3.
  782. */
  783. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  784. {
  785. struct inode *inode = mapping->host;
  786. struct gfs2_inode *ip = GFS2_I(inode);
  787. unsigned long index = from >> PAGE_CACHE_SHIFT;
  788. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  789. unsigned blocksize, iblock, length, pos;
  790. struct buffer_head *bh;
  791. struct page *page;
  792. int err;
  793. page = grab_cache_page(mapping, index);
  794. if (!page)
  795. return 0;
  796. blocksize = inode->i_sb->s_blocksize;
  797. length = blocksize - (offset & (blocksize - 1));
  798. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  799. if (!page_has_buffers(page))
  800. create_empty_buffers(page, blocksize, 0);
  801. /* Find the buffer that contains "offset" */
  802. bh = page_buffers(page);
  803. pos = blocksize;
  804. while (offset >= pos) {
  805. bh = bh->b_this_page;
  806. iblock++;
  807. pos += blocksize;
  808. }
  809. err = 0;
  810. if (!buffer_mapped(bh)) {
  811. gfs2_block_map(inode, iblock, bh, 0);
  812. /* unmapped? It's a hole - nothing to do */
  813. if (!buffer_mapped(bh))
  814. goto unlock;
  815. }
  816. /* Ok, it's mapped. Make sure it's up-to-date */
  817. if (PageUptodate(page))
  818. set_buffer_uptodate(bh);
  819. if (!buffer_uptodate(bh)) {
  820. err = -EIO;
  821. ll_rw_block(READ, 1, &bh);
  822. wait_on_buffer(bh);
  823. /* Uhhuh. Read error. Complain and punt. */
  824. if (!buffer_uptodate(bh))
  825. goto unlock;
  826. err = 0;
  827. }
  828. if (!gfs2_is_writeback(ip))
  829. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  830. zero_user(page, offset, length);
  831. mark_buffer_dirty(bh);
  832. unlock:
  833. unlock_page(page);
  834. page_cache_release(page);
  835. return err;
  836. }
  837. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  838. {
  839. struct gfs2_inode *ip = GFS2_I(inode);
  840. struct gfs2_sbd *sdp = GFS2_SB(inode);
  841. struct address_space *mapping = inode->i_mapping;
  842. struct buffer_head *dibh;
  843. int journaled = gfs2_is_jdata(ip);
  844. int error;
  845. error = gfs2_trans_begin(sdp,
  846. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  847. if (error)
  848. return error;
  849. error = gfs2_meta_inode_buffer(ip, &dibh);
  850. if (error)
  851. goto out;
  852. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  853. if (gfs2_is_stuffed(ip)) {
  854. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  855. } else {
  856. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  857. error = gfs2_block_truncate_page(mapping, newsize);
  858. if (error)
  859. goto out_brelse;
  860. }
  861. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  862. }
  863. i_size_write(inode, newsize);
  864. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  865. gfs2_dinode_out(ip, dibh->b_data);
  866. truncate_pagecache(inode, oldsize, newsize);
  867. out_brelse:
  868. brelse(dibh);
  869. out:
  870. gfs2_trans_end(sdp);
  871. return error;
  872. }
  873. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  874. {
  875. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  876. unsigned int height = ip->i_height;
  877. u64 lblock;
  878. struct metapath mp;
  879. int error;
  880. if (!size)
  881. lblock = 0;
  882. else
  883. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  884. find_metapath(sdp, lblock, &mp, ip->i_height);
  885. if (!gfs2_alloc_get(ip))
  886. return -ENOMEM;
  887. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  888. if (error)
  889. goto out;
  890. while (height--) {
  891. struct strip_mine sm;
  892. sm.sm_first = !!size;
  893. sm.sm_height = height;
  894. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  895. if (error)
  896. break;
  897. }
  898. gfs2_quota_unhold(ip);
  899. out:
  900. gfs2_alloc_put(ip);
  901. return error;
  902. }
  903. static int trunc_end(struct gfs2_inode *ip)
  904. {
  905. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  906. struct buffer_head *dibh;
  907. int error;
  908. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  909. if (error)
  910. return error;
  911. down_write(&ip->i_rw_mutex);
  912. error = gfs2_meta_inode_buffer(ip, &dibh);
  913. if (error)
  914. goto out;
  915. if (!i_size_read(&ip->i_inode)) {
  916. ip->i_height = 0;
  917. ip->i_goal = ip->i_no_addr;
  918. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  919. }
  920. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  921. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  922. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  923. gfs2_dinode_out(ip, dibh->b_data);
  924. brelse(dibh);
  925. out:
  926. up_write(&ip->i_rw_mutex);
  927. gfs2_trans_end(sdp);
  928. return error;
  929. }
  930. /**
  931. * do_shrink - make a file smaller
  932. * @inode: the inode
  933. * @oldsize: the current inode size
  934. * @newsize: the size to make the file
  935. *
  936. * Called with an exclusive lock on @inode. The @size must
  937. * be equal to or smaller than the current inode size.
  938. *
  939. * Returns: errno
  940. */
  941. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  942. {
  943. struct gfs2_inode *ip = GFS2_I(inode);
  944. int error;
  945. error = trunc_start(inode, oldsize, newsize);
  946. if (error < 0)
  947. return error;
  948. if (gfs2_is_stuffed(ip))
  949. return 0;
  950. error = trunc_dealloc(ip, newsize);
  951. if (error == 0)
  952. error = trunc_end(ip);
  953. return error;
  954. }
  955. void gfs2_trim_blocks(struct inode *inode)
  956. {
  957. u64 size = inode->i_size;
  958. int ret;
  959. ret = do_shrink(inode, size, size);
  960. WARN_ON(ret != 0);
  961. }
  962. /**
  963. * do_grow - Touch and update inode size
  964. * @inode: The inode
  965. * @size: The new size
  966. *
  967. * This function updates the timestamps on the inode and
  968. * may also increase the size of the inode. This function
  969. * must not be called with @size any smaller than the current
  970. * inode size.
  971. *
  972. * Although it is not strictly required to unstuff files here,
  973. * earlier versions of GFS2 have a bug in the stuffed file reading
  974. * code which will result in a buffer overrun if the size is larger
  975. * than the max stuffed file size. In order to prevent this from
  976. * occuring, such files are unstuffed, but in other cases we can
  977. * just update the inode size directly.
  978. *
  979. * Returns: 0 on success, or -ve on error
  980. */
  981. static int do_grow(struct inode *inode, u64 size)
  982. {
  983. struct gfs2_inode *ip = GFS2_I(inode);
  984. struct gfs2_sbd *sdp = GFS2_SB(inode);
  985. struct buffer_head *dibh;
  986. struct gfs2_alloc *al = NULL;
  987. int error;
  988. if (gfs2_is_stuffed(ip) &&
  989. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  990. al = gfs2_alloc_get(ip);
  991. if (al == NULL)
  992. return -ENOMEM;
  993. error = gfs2_quota_lock_check(ip);
  994. if (error)
  995. goto do_grow_alloc_put;
  996. al->al_requested = 1;
  997. error = gfs2_inplace_reserve(ip);
  998. if (error)
  999. goto do_grow_qunlock;
  1000. }
  1001. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT, 0);
  1002. if (error)
  1003. goto do_grow_release;
  1004. if (al) {
  1005. error = gfs2_unstuff_dinode(ip, NULL);
  1006. if (error)
  1007. goto do_end_trans;
  1008. }
  1009. error = gfs2_meta_inode_buffer(ip, &dibh);
  1010. if (error)
  1011. goto do_end_trans;
  1012. i_size_write(inode, size);
  1013. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1014. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1015. gfs2_dinode_out(ip, dibh->b_data);
  1016. brelse(dibh);
  1017. do_end_trans:
  1018. gfs2_trans_end(sdp);
  1019. do_grow_release:
  1020. if (al) {
  1021. gfs2_inplace_release(ip);
  1022. do_grow_qunlock:
  1023. gfs2_quota_unlock(ip);
  1024. do_grow_alloc_put:
  1025. gfs2_alloc_put(ip);
  1026. }
  1027. return error;
  1028. }
  1029. /**
  1030. * gfs2_setattr_size - make a file a given size
  1031. * @inode: the inode
  1032. * @newsize: the size to make the file
  1033. *
  1034. * The file size can grow, shrink, or stay the same size. This
  1035. * is called holding i_mutex and an exclusive glock on the inode
  1036. * in question.
  1037. *
  1038. * Returns: errno
  1039. */
  1040. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1041. {
  1042. int ret;
  1043. u64 oldsize;
  1044. BUG_ON(!S_ISREG(inode->i_mode));
  1045. ret = inode_newsize_ok(inode, newsize);
  1046. if (ret)
  1047. return ret;
  1048. oldsize = inode->i_size;
  1049. if (newsize >= oldsize)
  1050. return do_grow(inode, newsize);
  1051. return do_shrink(inode, oldsize, newsize);
  1052. }
  1053. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1054. {
  1055. int error;
  1056. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1057. if (!error)
  1058. error = trunc_end(ip);
  1059. return error;
  1060. }
  1061. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1062. {
  1063. return trunc_dealloc(ip, 0);
  1064. }
  1065. /**
  1066. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1067. * @ip: the file being written to
  1068. * @offset: the offset to write to
  1069. * @len: the number of bytes being written
  1070. *
  1071. * Returns: 1 if an alloc is required, 0 otherwise
  1072. */
  1073. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1074. unsigned int len)
  1075. {
  1076. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1077. struct buffer_head bh;
  1078. unsigned int shift;
  1079. u64 lblock, lblock_stop, size;
  1080. u64 end_of_file;
  1081. if (!len)
  1082. return 0;
  1083. if (gfs2_is_stuffed(ip)) {
  1084. if (offset + len >
  1085. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1086. return 1;
  1087. return 0;
  1088. }
  1089. shift = sdp->sd_sb.sb_bsize_shift;
  1090. BUG_ON(gfs2_is_dir(ip));
  1091. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1092. lblock = offset >> shift;
  1093. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1094. if (lblock_stop > end_of_file)
  1095. return 1;
  1096. size = (lblock_stop - lblock) << shift;
  1097. do {
  1098. bh.b_state = 0;
  1099. bh.b_size = size;
  1100. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1101. if (!buffer_mapped(&bh))
  1102. return 1;
  1103. size -= bh.b_size;
  1104. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1105. } while(size > 0);
  1106. return 0;
  1107. }