bmap.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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/blkdev.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/crc32.h>
  15. #include "gfs2.h"
  16. #include "incore.h"
  17. #include "bmap.h"
  18. #include "glock.h"
  19. #include "inode.h"
  20. #include "meta_io.h"
  21. #include "quota.h"
  22. #include "rgrp.h"
  23. #include "log.h"
  24. #include "super.h"
  25. #include "trans.h"
  26. #include "dir.h"
  27. #include "util.h"
  28. #include "trace_gfs2.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. struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
  35. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  36. };
  37. struct strip_mine {
  38. int sm_first;
  39. unsigned int sm_height;
  40. };
  41. /**
  42. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  43. * @ip: the inode
  44. * @dibh: the dinode buffer
  45. * @block: the block number that was allocated
  46. * @page: The (optional) page. This is looked up if @page is NULL
  47. *
  48. * Returns: errno
  49. */
  50. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  51. u64 block, struct page *page)
  52. {
  53. struct inode *inode = &ip->i_inode;
  54. struct buffer_head *bh;
  55. int release = 0;
  56. if (!page || page->index) {
  57. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  58. if (!page)
  59. return -ENOMEM;
  60. release = 1;
  61. }
  62. if (!PageUptodate(page)) {
  63. void *kaddr = kmap(page);
  64. u64 dsize = i_size_read(inode);
  65. if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
  66. dsize = dibh->b_size - sizeof(struct gfs2_dinode);
  67. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  68. memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
  69. kunmap(page);
  70. SetPageUptodate(page);
  71. }
  72. if (!page_has_buffers(page))
  73. create_empty_buffers(page, BIT(inode->i_blkbits),
  74. BIT(BH_Uptodate));
  75. bh = page_buffers(page);
  76. if (!buffer_mapped(bh))
  77. map_bh(bh, inode->i_sb, block);
  78. set_buffer_uptodate(bh);
  79. if (!gfs2_is_jdata(ip))
  80. mark_buffer_dirty(bh);
  81. if (!gfs2_is_writeback(ip))
  82. gfs2_trans_add_data(ip->i_gl, bh);
  83. if (release) {
  84. unlock_page(page);
  85. put_page(page);
  86. }
  87. return 0;
  88. }
  89. /**
  90. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  91. * @ip: The GFS2 inode to unstuff
  92. * @page: The (optional) page. This is looked up if the @page is NULL
  93. *
  94. * This routine unstuffs a dinode and returns it to a "normal" state such
  95. * that the height can be grown in the traditional way.
  96. *
  97. * Returns: errno
  98. */
  99. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  100. {
  101. struct buffer_head *bh, *dibh;
  102. struct gfs2_dinode *di;
  103. u64 block = 0;
  104. int isdir = gfs2_is_dir(ip);
  105. int error;
  106. down_write(&ip->i_rw_mutex);
  107. error = gfs2_meta_inode_buffer(ip, &dibh);
  108. if (error)
  109. goto out;
  110. if (i_size_read(&ip->i_inode)) {
  111. /* Get a free block, fill it with the stuffed data,
  112. and write it out to disk */
  113. unsigned int n = 1;
  114. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  115. if (error)
  116. goto out_brelse;
  117. if (isdir) {
  118. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  119. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  120. if (error)
  121. goto out_brelse;
  122. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  123. dibh, sizeof(struct gfs2_dinode));
  124. brelse(bh);
  125. } else {
  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_meta(ip->i_gl, dibh);
  133. di = (struct gfs2_dinode *)dibh->b_data;
  134. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  135. if (i_size_read(&ip->i_inode)) {
  136. *(__be64 *)(di + 1) = cpu_to_be64(block);
  137. gfs2_add_inode_blocks(&ip->i_inode, 1);
  138. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  139. }
  140. ip->i_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. * find_metapath - Find path through the metadata tree
  150. * @sdp: The superblock
  151. * @mp: The metapath to return the result in
  152. * @block: The disk block to look up
  153. * @height: The pre-calculated height of the metadata tree
  154. *
  155. * This routine returns a struct metapath structure that defines a path
  156. * through the metadata of inode "ip" to get to block "block".
  157. *
  158. * Example:
  159. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  160. * filesystem with a blocksize of 4096.
  161. *
  162. * find_metapath() would return a struct metapath structure set to:
  163. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  164. * and mp_list[2] = 165.
  165. *
  166. * That means that in order to get to the block containing the byte at
  167. * offset 101342453, we would load the indirect block pointed to by pointer
  168. * 0 in the dinode. We would then load the indirect block pointed to by
  169. * pointer 48 in that indirect block. We would then load the data block
  170. * pointed to by pointer 165 in that indirect block.
  171. *
  172. * ----------------------------------------
  173. * | Dinode | |
  174. * | | 4|
  175. * | |0 1 2 3 4 5 9|
  176. * | | 6|
  177. * ----------------------------------------
  178. * |
  179. * |
  180. * V
  181. * ----------------------------------------
  182. * | Indirect Block |
  183. * | 5|
  184. * | 4 4 4 4 4 5 5 1|
  185. * |0 5 6 7 8 9 0 1 2|
  186. * ----------------------------------------
  187. * |
  188. * |
  189. * V
  190. * ----------------------------------------
  191. * | Indirect Block |
  192. * | 1 1 1 1 1 5|
  193. * | 6 6 6 6 6 1|
  194. * |0 3 4 5 6 7 2|
  195. * ----------------------------------------
  196. * |
  197. * |
  198. * V
  199. * ----------------------------------------
  200. * | Data block containing offset |
  201. * | 101342453 |
  202. * | |
  203. * | |
  204. * ----------------------------------------
  205. *
  206. */
  207. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  208. struct metapath *mp, unsigned int height)
  209. {
  210. unsigned int i;
  211. for (i = height; i--;)
  212. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  213. }
  214. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  215. {
  216. if (mp->mp_list[0] == 0)
  217. return 2;
  218. return 1;
  219. }
  220. /**
  221. * metapointer - Return pointer to start of metadata in a buffer
  222. * @height: The metadata height (0 = dinode)
  223. * @mp: The metapath
  224. *
  225. * Return a pointer to the block number of the next height of the metadata
  226. * tree given a buffer containing the pointer to the current height of the
  227. * metadata tree.
  228. */
  229. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  230. {
  231. struct buffer_head *bh = mp->mp_bh[height];
  232. unsigned int head_size = (height > 0) ?
  233. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  234. return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  235. }
  236. static void gfs2_metapath_ra(struct gfs2_glock *gl,
  237. const struct buffer_head *bh, const __be64 *pos)
  238. {
  239. struct buffer_head *rabh;
  240. const __be64 *endp = (const __be64 *)(bh->b_data + bh->b_size);
  241. const __be64 *t;
  242. for (t = pos; t < endp; t++) {
  243. if (!*t)
  244. continue;
  245. rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
  246. if (trylock_buffer(rabh)) {
  247. if (!buffer_uptodate(rabh)) {
  248. rabh->b_end_io = end_buffer_read_sync;
  249. submit_bh(REQ_OP_READ, REQ_RAHEAD | REQ_META,
  250. rabh);
  251. continue;
  252. }
  253. unlock_buffer(rabh);
  254. }
  255. brelse(rabh);
  256. }
  257. }
  258. /**
  259. * lookup_metapath - Walk the metadata tree to a specific point
  260. * @ip: The inode
  261. * @mp: The metapath
  262. *
  263. * Assumes that the inode's buffer has already been looked up and
  264. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  265. * by find_metapath().
  266. *
  267. * If this function encounters part of the tree which has not been
  268. * allocated, it returns the current height of the tree at the point
  269. * at which it found the unallocated block. Blocks which are found are
  270. * added to the mp->mp_bh[] list.
  271. *
  272. * Returns: error or height of metadata tree
  273. */
  274. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  275. {
  276. unsigned int end_of_metadata = ip->i_height - 1;
  277. unsigned int x;
  278. __be64 *ptr;
  279. u64 dblock;
  280. int ret;
  281. for (x = 0; x < end_of_metadata; x++) {
  282. ptr = metapointer(x, mp);
  283. dblock = be64_to_cpu(*ptr);
  284. if (!dblock)
  285. return x + 1;
  286. ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, &mp->mp_bh[x+1]);
  287. if (ret)
  288. return ret;
  289. }
  290. return ip->i_height;
  291. }
  292. static inline void release_metapath(struct metapath *mp)
  293. {
  294. int i;
  295. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  296. if (mp->mp_bh[i] == NULL)
  297. break;
  298. brelse(mp->mp_bh[i]);
  299. }
  300. }
  301. /**
  302. * gfs2_extent_length - Returns length of an extent of blocks
  303. * @start: Start of the buffer
  304. * @len: Length of the buffer in bytes
  305. * @ptr: Current position in the buffer
  306. * @limit: Max extent length to return (0 = unlimited)
  307. * @eob: Set to 1 if we hit "end of block"
  308. *
  309. * If the first block is zero (unallocated) it will return the number of
  310. * unallocated blocks in the extent, otherwise it will return the number
  311. * of contiguous blocks in the extent.
  312. *
  313. * Returns: The length of the extent (minimum of one block)
  314. */
  315. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, size_t limit, int *eob)
  316. {
  317. const __be64 *end = (start + len);
  318. const __be64 *first = ptr;
  319. u64 d = be64_to_cpu(*ptr);
  320. *eob = 0;
  321. do {
  322. ptr++;
  323. if (ptr >= end)
  324. break;
  325. if (limit && --limit == 0)
  326. break;
  327. if (d)
  328. d++;
  329. } while(be64_to_cpu(*ptr) == d);
  330. if (ptr >= end)
  331. *eob = 1;
  332. return (ptr - first);
  333. }
  334. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  335. {
  336. if (create)
  337. down_write(&ip->i_rw_mutex);
  338. else
  339. down_read(&ip->i_rw_mutex);
  340. }
  341. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  342. {
  343. if (create)
  344. up_write(&ip->i_rw_mutex);
  345. else
  346. up_read(&ip->i_rw_mutex);
  347. }
  348. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  349. struct gfs2_glock *gl, unsigned int i,
  350. unsigned offset, u64 bn)
  351. {
  352. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  353. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  354. sizeof(struct gfs2_dinode)));
  355. BUG_ON(i < 1);
  356. BUG_ON(mp->mp_bh[i] != NULL);
  357. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  358. gfs2_trans_add_meta(gl, mp->mp_bh[i]);
  359. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  360. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  361. ptr += offset;
  362. *ptr = cpu_to_be64(bn);
  363. return ptr;
  364. }
  365. enum alloc_state {
  366. ALLOC_DATA = 0,
  367. ALLOC_GROW_DEPTH = 1,
  368. ALLOC_GROW_HEIGHT = 2,
  369. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  370. };
  371. /**
  372. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  373. * @inode: The GFS2 inode
  374. * @lblock: The logical starting block of the extent
  375. * @bh_map: This is used to return the mapping details
  376. * @mp: The metapath
  377. * @sheight: The starting height (i.e. whats already mapped)
  378. * @height: The height to build to
  379. * @maxlen: The max number of data blocks to alloc
  380. *
  381. * In this routine we may have to alloc:
  382. * i) Indirect blocks to grow the metadata tree height
  383. * ii) Indirect blocks to fill in lower part of the metadata tree
  384. * iii) Data blocks
  385. *
  386. * The function is in two parts. The first part works out the total
  387. * number of blocks which we need. The second part does the actual
  388. * allocation asking for an extent at a time (if enough contiguous free
  389. * blocks are available, there will only be one request per bmap call)
  390. * and uses the state machine to initialise the blocks in order.
  391. *
  392. * Returns: errno on error
  393. */
  394. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  395. struct buffer_head *bh_map, struct metapath *mp,
  396. const unsigned int sheight,
  397. const unsigned int height,
  398. const size_t maxlen)
  399. {
  400. struct gfs2_inode *ip = GFS2_I(inode);
  401. struct gfs2_sbd *sdp = GFS2_SB(inode);
  402. struct super_block *sb = sdp->sd_vfs;
  403. struct buffer_head *dibh = mp->mp_bh[0];
  404. u64 bn, dblock = 0;
  405. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  406. unsigned dblks = 0;
  407. unsigned ptrs_per_blk;
  408. const unsigned end_of_metadata = height - 1;
  409. int ret;
  410. int eob = 0;
  411. enum alloc_state state;
  412. __be64 *ptr;
  413. __be64 zero_bn = 0;
  414. BUG_ON(sheight < 1);
  415. BUG_ON(dibh == NULL);
  416. gfs2_trans_add_meta(ip->i_gl, dibh);
  417. if (height == sheight) {
  418. struct buffer_head *bh;
  419. /* Bottom indirect block exists, find unalloced extent size */
  420. ptr = metapointer(end_of_metadata, mp);
  421. bh = mp->mp_bh[end_of_metadata];
  422. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  423. &eob);
  424. BUG_ON(dblks < 1);
  425. state = ALLOC_DATA;
  426. } else {
  427. /* Need to allocate indirect blocks */
  428. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  429. dblks = min(maxlen, (size_t)(ptrs_per_blk -
  430. mp->mp_list[end_of_metadata]));
  431. if (height == ip->i_height) {
  432. /* Writing into existing tree, extend tree down */
  433. iblks = height - sheight;
  434. state = ALLOC_GROW_DEPTH;
  435. } else {
  436. /* Building up tree height */
  437. state = ALLOC_GROW_HEIGHT;
  438. iblks = height - ip->i_height;
  439. branch_start = metapath_branch_start(mp);
  440. iblks += (height - branch_start);
  441. }
  442. }
  443. /* start of the second part of the function (state machine) */
  444. blks = dblks + iblks;
  445. i = sheight;
  446. do {
  447. int error;
  448. n = blks - alloced;
  449. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  450. if (error)
  451. return error;
  452. alloced += n;
  453. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  454. gfs2_trans_add_unrevoke(sdp, bn, n);
  455. switch (state) {
  456. /* Growing height of tree */
  457. case ALLOC_GROW_HEIGHT:
  458. if (i == 1) {
  459. ptr = (__be64 *)(dibh->b_data +
  460. sizeof(struct gfs2_dinode));
  461. zero_bn = *ptr;
  462. }
  463. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  464. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  465. if (i - 1 == height - ip->i_height) {
  466. i--;
  467. gfs2_buffer_copy_tail(mp->mp_bh[i],
  468. sizeof(struct gfs2_meta_header),
  469. dibh, sizeof(struct gfs2_dinode));
  470. gfs2_buffer_clear_tail(dibh,
  471. sizeof(struct gfs2_dinode) +
  472. sizeof(__be64));
  473. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  474. sizeof(struct gfs2_meta_header));
  475. *ptr = zero_bn;
  476. state = ALLOC_GROW_DEPTH;
  477. for(i = branch_start; i < height; i++) {
  478. if (mp->mp_bh[i] == NULL)
  479. break;
  480. brelse(mp->mp_bh[i]);
  481. mp->mp_bh[i] = NULL;
  482. }
  483. i = branch_start;
  484. }
  485. if (n == 0)
  486. break;
  487. /* Branching from existing tree */
  488. case ALLOC_GROW_DEPTH:
  489. if (i > 1 && i < height)
  490. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  491. for (; i < height && n > 0; i++, n--)
  492. gfs2_indirect_init(mp, ip->i_gl, i,
  493. mp->mp_list[i-1], bn++);
  494. if (i == height)
  495. state = ALLOC_DATA;
  496. if (n == 0)
  497. break;
  498. /* Tree complete, adding data blocks */
  499. case ALLOC_DATA:
  500. BUG_ON(n > dblks);
  501. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  502. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  503. dblks = n;
  504. ptr = metapointer(end_of_metadata, mp);
  505. dblock = bn;
  506. while (n-- > 0)
  507. *ptr++ = cpu_to_be64(bn++);
  508. if (buffer_zeronew(bh_map)) {
  509. ret = sb_issue_zeroout(sb, dblock, dblks,
  510. GFP_NOFS);
  511. if (ret) {
  512. fs_err(sdp,
  513. "Failed to zero data buffers\n");
  514. clear_buffer_zeronew(bh_map);
  515. }
  516. }
  517. break;
  518. }
  519. } while ((state != ALLOC_DATA) || !dblock);
  520. ip->i_height = height;
  521. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  522. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  523. map_bh(bh_map, inode->i_sb, dblock);
  524. bh_map->b_size = dblks << inode->i_blkbits;
  525. set_buffer_new(bh_map);
  526. return 0;
  527. }
  528. /**
  529. * gfs2_block_map - Map a block from an inode to a disk block
  530. * @inode: The inode
  531. * @lblock: The logical block number
  532. * @bh_map: The bh to be mapped
  533. * @create: True if its ok to alloc blocks to satify the request
  534. *
  535. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  536. * read of metadata will be required before the next block can be
  537. * mapped. Sets buffer_new() if new blocks were allocated.
  538. *
  539. * Returns: errno
  540. */
  541. int gfs2_block_map(struct inode *inode, sector_t lblock,
  542. struct buffer_head *bh_map, int create)
  543. {
  544. struct gfs2_inode *ip = GFS2_I(inode);
  545. struct gfs2_sbd *sdp = GFS2_SB(inode);
  546. unsigned int bsize = sdp->sd_sb.sb_bsize;
  547. const size_t maxlen = bh_map->b_size >> inode->i_blkbits;
  548. const u64 *arr = sdp->sd_heightsize;
  549. __be64 *ptr;
  550. u64 size;
  551. struct metapath mp;
  552. int ret;
  553. int eob;
  554. unsigned int len;
  555. struct buffer_head *bh;
  556. u8 height;
  557. BUG_ON(maxlen == 0);
  558. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  559. bmap_lock(ip, create);
  560. clear_buffer_mapped(bh_map);
  561. clear_buffer_new(bh_map);
  562. clear_buffer_boundary(bh_map);
  563. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  564. if (gfs2_is_dir(ip)) {
  565. bsize = sdp->sd_jbsize;
  566. arr = sdp->sd_jheightsize;
  567. }
  568. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  569. if (ret)
  570. goto out;
  571. height = ip->i_height;
  572. size = (lblock + 1) * bsize;
  573. while (size > arr[height])
  574. height++;
  575. find_metapath(sdp, lblock, &mp, height);
  576. ret = 1;
  577. if (height > ip->i_height || gfs2_is_stuffed(ip))
  578. goto do_alloc;
  579. ret = lookup_metapath(ip, &mp);
  580. if (ret < 0)
  581. goto out;
  582. if (ret != ip->i_height)
  583. goto do_alloc;
  584. ptr = metapointer(ip->i_height - 1, &mp);
  585. if (*ptr == 0)
  586. goto do_alloc;
  587. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  588. bh = mp.mp_bh[ip->i_height - 1];
  589. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  590. bh_map->b_size = (len << inode->i_blkbits);
  591. if (eob)
  592. set_buffer_boundary(bh_map);
  593. ret = 0;
  594. out:
  595. release_metapath(&mp);
  596. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  597. bmap_unlock(ip, create);
  598. return ret;
  599. do_alloc:
  600. /* All allocations are done here, firstly check create flag */
  601. if (!create) {
  602. BUG_ON(gfs2_is_stuffed(ip));
  603. ret = 0;
  604. goto out;
  605. }
  606. /* At this point ret is the tree depth of already allocated blocks */
  607. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  608. goto out;
  609. }
  610. /*
  611. * Deprecated: do not use in new code
  612. */
  613. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  614. {
  615. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  616. int ret;
  617. int create = *new;
  618. BUG_ON(!extlen);
  619. BUG_ON(!dblock);
  620. BUG_ON(!new);
  621. bh.b_size = BIT(inode->i_blkbits + (create ? 0 : 5));
  622. ret = gfs2_block_map(inode, lblock, &bh, create);
  623. *extlen = bh.b_size >> inode->i_blkbits;
  624. *dblock = bh.b_blocknr;
  625. if (buffer_new(&bh))
  626. *new = 1;
  627. else
  628. *new = 0;
  629. return ret;
  630. }
  631. /**
  632. * do_strip - Look for a layer a particular layer of the file and strip it off
  633. * @ip: the inode
  634. * @dibh: the dinode buffer
  635. * @bh: A buffer of pointers
  636. * @top: The first pointer in the buffer
  637. * @bottom: One more than the last pointer
  638. * @height: the height this buffer is at
  639. * @sm: a pointer to a struct strip_mine
  640. *
  641. * Returns: errno
  642. */
  643. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  644. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  645. unsigned int height, struct strip_mine *sm)
  646. {
  647. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  648. struct gfs2_rgrp_list rlist;
  649. u64 bn, bstart;
  650. u32 blen, btotal;
  651. __be64 *p;
  652. unsigned int rg_blocks = 0;
  653. int metadata;
  654. unsigned int revokes = 0;
  655. int x;
  656. int error;
  657. error = gfs2_rindex_update(sdp);
  658. if (error)
  659. return error;
  660. if (!*top)
  661. sm->sm_first = 0;
  662. if (height != sm->sm_height)
  663. return 0;
  664. if (sm->sm_first) {
  665. top++;
  666. sm->sm_first = 0;
  667. }
  668. metadata = (height != ip->i_height - 1);
  669. if (metadata)
  670. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  671. else if (ip->i_depth)
  672. revokes = sdp->sd_inptrs;
  673. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  674. bstart = 0;
  675. blen = 0;
  676. for (p = top; p < bottom; p++) {
  677. if (!*p)
  678. continue;
  679. bn = be64_to_cpu(*p);
  680. if (bstart + blen == bn)
  681. blen++;
  682. else {
  683. if (bstart)
  684. gfs2_rlist_add(ip, &rlist, bstart);
  685. bstart = bn;
  686. blen = 1;
  687. }
  688. }
  689. if (bstart)
  690. gfs2_rlist_add(ip, &rlist, bstart);
  691. else
  692. goto out; /* Nothing to do */
  693. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  694. for (x = 0; x < rlist.rl_rgrps; x++) {
  695. struct gfs2_rgrpd *rgd;
  696. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  697. rg_blocks += rgd->rd_length;
  698. }
  699. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  700. if (error)
  701. goto out_rlist;
  702. if (gfs2_rs_active(&ip->i_res)) /* needs to be done with the rgrp glock held */
  703. gfs2_rs_deltree(&ip->i_res);
  704. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  705. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  706. revokes);
  707. if (error)
  708. goto out_rg_gunlock;
  709. down_write(&ip->i_rw_mutex);
  710. gfs2_trans_add_meta(ip->i_gl, dibh);
  711. gfs2_trans_add_meta(ip->i_gl, bh);
  712. bstart = 0;
  713. blen = 0;
  714. btotal = 0;
  715. for (p = top; p < bottom; p++) {
  716. if (!*p)
  717. continue;
  718. bn = be64_to_cpu(*p);
  719. if (bstart + blen == bn)
  720. blen++;
  721. else {
  722. if (bstart) {
  723. __gfs2_free_blocks(ip, bstart, blen, metadata);
  724. btotal += blen;
  725. }
  726. bstart = bn;
  727. blen = 1;
  728. }
  729. *p = 0;
  730. gfs2_add_inode_blocks(&ip->i_inode, -1);
  731. }
  732. if (bstart) {
  733. __gfs2_free_blocks(ip, bstart, blen, metadata);
  734. btotal += blen;
  735. }
  736. gfs2_statfs_change(sdp, 0, +btotal, 0);
  737. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  738. ip->i_inode.i_gid);
  739. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  740. gfs2_dinode_out(ip, dibh->b_data);
  741. up_write(&ip->i_rw_mutex);
  742. gfs2_trans_end(sdp);
  743. out_rg_gunlock:
  744. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  745. out_rlist:
  746. gfs2_rlist_free(&rlist);
  747. out:
  748. return error;
  749. }
  750. /**
  751. * recursive_scan - recursively scan through the end of a file
  752. * @ip: the inode
  753. * @dibh: the dinode buffer
  754. * @mp: the path through the metadata to the point to start
  755. * @height: the height the recursion is at
  756. * @block: the indirect block to look at
  757. * @first: 1 if this is the first block
  758. * @sm: data opaque to this function to pass to @bc
  759. *
  760. * When this is first called @height and @block should be zero and
  761. * @first should be 1.
  762. *
  763. * Returns: errno
  764. */
  765. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  766. struct metapath *mp, unsigned int height,
  767. u64 block, int first, struct strip_mine *sm)
  768. {
  769. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  770. struct buffer_head *bh = NULL;
  771. __be64 *top, *bottom;
  772. u64 bn;
  773. int error;
  774. int mh_size = sizeof(struct gfs2_meta_header);
  775. if (!height) {
  776. error = gfs2_meta_inode_buffer(ip, &bh);
  777. if (error)
  778. return error;
  779. dibh = bh;
  780. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  781. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  782. } else {
  783. error = gfs2_meta_indirect_buffer(ip, height, block, &bh);
  784. if (error)
  785. return error;
  786. top = (__be64 *)(bh->b_data + mh_size) +
  787. (first ? mp->mp_list[height] : 0);
  788. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  789. }
  790. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  791. if (error)
  792. goto out;
  793. if (height < ip->i_height - 1) {
  794. gfs2_metapath_ra(ip->i_gl, bh, top);
  795. for (; top < bottom; top++, first = 0) {
  796. if (!*top)
  797. continue;
  798. bn = be64_to_cpu(*top);
  799. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  800. first, sm);
  801. if (error)
  802. break;
  803. }
  804. }
  805. out:
  806. brelse(bh);
  807. return error;
  808. }
  809. /**
  810. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  811. *
  812. * This is partly borrowed from ext3.
  813. */
  814. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  815. {
  816. struct inode *inode = mapping->host;
  817. struct gfs2_inode *ip = GFS2_I(inode);
  818. unsigned long index = from >> PAGE_SHIFT;
  819. unsigned offset = from & (PAGE_SIZE-1);
  820. unsigned blocksize, iblock, length, pos;
  821. struct buffer_head *bh;
  822. struct page *page;
  823. int err;
  824. page = find_or_create_page(mapping, index, GFP_NOFS);
  825. if (!page)
  826. return 0;
  827. blocksize = inode->i_sb->s_blocksize;
  828. length = blocksize - (offset & (blocksize - 1));
  829. iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
  830. if (!page_has_buffers(page))
  831. create_empty_buffers(page, blocksize, 0);
  832. /* Find the buffer that contains "offset" */
  833. bh = page_buffers(page);
  834. pos = blocksize;
  835. while (offset >= pos) {
  836. bh = bh->b_this_page;
  837. iblock++;
  838. pos += blocksize;
  839. }
  840. err = 0;
  841. if (!buffer_mapped(bh)) {
  842. gfs2_block_map(inode, iblock, bh, 0);
  843. /* unmapped? It's a hole - nothing to do */
  844. if (!buffer_mapped(bh))
  845. goto unlock;
  846. }
  847. /* Ok, it's mapped. Make sure it's up-to-date */
  848. if (PageUptodate(page))
  849. set_buffer_uptodate(bh);
  850. if (!buffer_uptodate(bh)) {
  851. err = -EIO;
  852. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  853. wait_on_buffer(bh);
  854. /* Uhhuh. Read error. Complain and punt. */
  855. if (!buffer_uptodate(bh))
  856. goto unlock;
  857. err = 0;
  858. }
  859. if (!gfs2_is_writeback(ip))
  860. gfs2_trans_add_data(ip->i_gl, bh);
  861. zero_user(page, offset, length);
  862. mark_buffer_dirty(bh);
  863. unlock:
  864. unlock_page(page);
  865. put_page(page);
  866. return err;
  867. }
  868. #define GFS2_JTRUNC_REVOKES 8192
  869. /**
  870. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  871. * @inode: The inode being truncated
  872. * @oldsize: The original (larger) size
  873. * @newsize: The new smaller size
  874. *
  875. * With jdata files, we have to journal a revoke for each block which is
  876. * truncated. As a result, we need to split this into separate transactions
  877. * if the number of pages being truncated gets too large.
  878. */
  879. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  880. {
  881. struct gfs2_sbd *sdp = GFS2_SB(inode);
  882. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  883. u64 chunk;
  884. int error;
  885. while (oldsize != newsize) {
  886. chunk = oldsize - newsize;
  887. if (chunk > max_chunk)
  888. chunk = max_chunk;
  889. truncate_pagecache(inode, oldsize - chunk);
  890. oldsize -= chunk;
  891. gfs2_trans_end(sdp);
  892. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  893. if (error)
  894. return error;
  895. }
  896. return 0;
  897. }
  898. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  899. {
  900. struct gfs2_inode *ip = GFS2_I(inode);
  901. struct gfs2_sbd *sdp = GFS2_SB(inode);
  902. struct address_space *mapping = inode->i_mapping;
  903. struct buffer_head *dibh;
  904. int journaled = gfs2_is_jdata(ip);
  905. int error;
  906. if (journaled)
  907. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  908. else
  909. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  910. if (error)
  911. return error;
  912. error = gfs2_meta_inode_buffer(ip, &dibh);
  913. if (error)
  914. goto out;
  915. gfs2_trans_add_meta(ip->i_gl, dibh);
  916. if (gfs2_is_stuffed(ip)) {
  917. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  918. } else {
  919. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  920. error = gfs2_block_truncate_page(mapping, newsize);
  921. if (error)
  922. goto out_brelse;
  923. }
  924. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  925. }
  926. i_size_write(inode, newsize);
  927. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  928. gfs2_dinode_out(ip, dibh->b_data);
  929. if (journaled)
  930. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  931. else
  932. truncate_pagecache(inode, newsize);
  933. if (error) {
  934. brelse(dibh);
  935. return error;
  936. }
  937. out_brelse:
  938. brelse(dibh);
  939. out:
  940. gfs2_trans_end(sdp);
  941. return error;
  942. }
  943. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  944. {
  945. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  946. unsigned int height = ip->i_height;
  947. u64 lblock;
  948. struct metapath mp;
  949. int error;
  950. if (!size)
  951. lblock = 0;
  952. else
  953. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  954. find_metapath(sdp, lblock, &mp, ip->i_height);
  955. error = gfs2_rindex_update(sdp);
  956. if (error)
  957. return error;
  958. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  959. if (error)
  960. return error;
  961. while (height--) {
  962. struct strip_mine sm;
  963. sm.sm_first = !!size;
  964. sm.sm_height = height;
  965. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  966. if (error)
  967. break;
  968. }
  969. gfs2_quota_unhold(ip);
  970. return error;
  971. }
  972. static int trunc_end(struct gfs2_inode *ip)
  973. {
  974. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  975. struct buffer_head *dibh;
  976. int error;
  977. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  978. if (error)
  979. return error;
  980. down_write(&ip->i_rw_mutex);
  981. error = gfs2_meta_inode_buffer(ip, &dibh);
  982. if (error)
  983. goto out;
  984. if (!i_size_read(&ip->i_inode)) {
  985. ip->i_height = 0;
  986. ip->i_goal = ip->i_no_addr;
  987. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  988. gfs2_ordered_del_inode(ip);
  989. }
  990. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  991. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  992. gfs2_trans_add_meta(ip->i_gl, dibh);
  993. gfs2_dinode_out(ip, dibh->b_data);
  994. brelse(dibh);
  995. out:
  996. up_write(&ip->i_rw_mutex);
  997. gfs2_trans_end(sdp);
  998. return error;
  999. }
  1000. /**
  1001. * do_shrink - make a file smaller
  1002. * @inode: the inode
  1003. * @oldsize: the current inode size
  1004. * @newsize: the size to make the file
  1005. *
  1006. * Called with an exclusive lock on @inode. The @size must
  1007. * be equal to or smaller than the current inode size.
  1008. *
  1009. * Returns: errno
  1010. */
  1011. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  1012. {
  1013. struct gfs2_inode *ip = GFS2_I(inode);
  1014. int error;
  1015. error = trunc_start(inode, oldsize, newsize);
  1016. if (error < 0)
  1017. return error;
  1018. if (gfs2_is_stuffed(ip))
  1019. return 0;
  1020. error = trunc_dealloc(ip, newsize);
  1021. if (error == 0)
  1022. error = trunc_end(ip);
  1023. return error;
  1024. }
  1025. void gfs2_trim_blocks(struct inode *inode)
  1026. {
  1027. u64 size = inode->i_size;
  1028. int ret;
  1029. ret = do_shrink(inode, size, size);
  1030. WARN_ON(ret != 0);
  1031. }
  1032. /**
  1033. * do_grow - Touch and update inode size
  1034. * @inode: The inode
  1035. * @size: The new size
  1036. *
  1037. * This function updates the timestamps on the inode and
  1038. * may also increase the size of the inode. This function
  1039. * must not be called with @size any smaller than the current
  1040. * inode size.
  1041. *
  1042. * Although it is not strictly required to unstuff files here,
  1043. * earlier versions of GFS2 have a bug in the stuffed file reading
  1044. * code which will result in a buffer overrun if the size is larger
  1045. * than the max stuffed file size. In order to prevent this from
  1046. * occurring, such files are unstuffed, but in other cases we can
  1047. * just update the inode size directly.
  1048. *
  1049. * Returns: 0 on success, or -ve on error
  1050. */
  1051. static int do_grow(struct inode *inode, u64 size)
  1052. {
  1053. struct gfs2_inode *ip = GFS2_I(inode);
  1054. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1055. struct gfs2_alloc_parms ap = { .target = 1, };
  1056. struct buffer_head *dibh;
  1057. int error;
  1058. int unstuff = 0;
  1059. if (gfs2_is_stuffed(ip) &&
  1060. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1061. error = gfs2_quota_lock_check(ip, &ap);
  1062. if (error)
  1063. return error;
  1064. error = gfs2_inplace_reserve(ip, &ap);
  1065. if (error)
  1066. goto do_grow_qunlock;
  1067. unstuff = 1;
  1068. }
  1069. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
  1070. (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
  1071. 0 : RES_QUOTA), 0);
  1072. if (error)
  1073. goto do_grow_release;
  1074. if (unstuff) {
  1075. error = gfs2_unstuff_dinode(ip, NULL);
  1076. if (error)
  1077. goto do_end_trans;
  1078. }
  1079. error = gfs2_meta_inode_buffer(ip, &dibh);
  1080. if (error)
  1081. goto do_end_trans;
  1082. i_size_write(inode, size);
  1083. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1084. gfs2_trans_add_meta(ip->i_gl, dibh);
  1085. gfs2_dinode_out(ip, dibh->b_data);
  1086. brelse(dibh);
  1087. do_end_trans:
  1088. gfs2_trans_end(sdp);
  1089. do_grow_release:
  1090. if (unstuff) {
  1091. gfs2_inplace_release(ip);
  1092. do_grow_qunlock:
  1093. gfs2_quota_unlock(ip);
  1094. }
  1095. return error;
  1096. }
  1097. /**
  1098. * gfs2_setattr_size - make a file a given size
  1099. * @inode: the inode
  1100. * @newsize: the size to make the file
  1101. *
  1102. * The file size can grow, shrink, or stay the same size. This
  1103. * is called holding i_mutex and an exclusive glock on the inode
  1104. * in question.
  1105. *
  1106. * Returns: errno
  1107. */
  1108. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1109. {
  1110. struct gfs2_inode *ip = GFS2_I(inode);
  1111. int ret;
  1112. u64 oldsize;
  1113. BUG_ON(!S_ISREG(inode->i_mode));
  1114. ret = inode_newsize_ok(inode, newsize);
  1115. if (ret)
  1116. return ret;
  1117. inode_dio_wait(inode);
  1118. ret = gfs2_rsqa_alloc(ip);
  1119. if (ret)
  1120. goto out;
  1121. oldsize = inode->i_size;
  1122. if (newsize >= oldsize) {
  1123. ret = do_grow(inode, newsize);
  1124. goto out;
  1125. }
  1126. ret = do_shrink(inode, oldsize, newsize);
  1127. out:
  1128. gfs2_rsqa_delete(ip, NULL);
  1129. return ret;
  1130. }
  1131. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1132. {
  1133. int error;
  1134. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1135. if (!error)
  1136. error = trunc_end(ip);
  1137. return error;
  1138. }
  1139. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1140. {
  1141. return trunc_dealloc(ip, 0);
  1142. }
  1143. /**
  1144. * gfs2_free_journal_extents - Free cached journal bmap info
  1145. * @jd: The journal
  1146. *
  1147. */
  1148. void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
  1149. {
  1150. struct gfs2_journal_extent *jext;
  1151. while(!list_empty(&jd->extent_list)) {
  1152. jext = list_entry(jd->extent_list.next, struct gfs2_journal_extent, list);
  1153. list_del(&jext->list);
  1154. kfree(jext);
  1155. }
  1156. }
  1157. /**
  1158. * gfs2_add_jextent - Add or merge a new extent to extent cache
  1159. * @jd: The journal descriptor
  1160. * @lblock: The logical block at start of new extent
  1161. * @dblock: The physical block at start of new extent
  1162. * @blocks: Size of extent in fs blocks
  1163. *
  1164. * Returns: 0 on success or -ENOMEM
  1165. */
  1166. static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
  1167. {
  1168. struct gfs2_journal_extent *jext;
  1169. if (!list_empty(&jd->extent_list)) {
  1170. jext = list_entry(jd->extent_list.prev, struct gfs2_journal_extent, list);
  1171. if ((jext->dblock + jext->blocks) == dblock) {
  1172. jext->blocks += blocks;
  1173. return 0;
  1174. }
  1175. }
  1176. jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
  1177. if (jext == NULL)
  1178. return -ENOMEM;
  1179. jext->dblock = dblock;
  1180. jext->lblock = lblock;
  1181. jext->blocks = blocks;
  1182. list_add_tail(&jext->list, &jd->extent_list);
  1183. jd->nr_extents++;
  1184. return 0;
  1185. }
  1186. /**
  1187. * gfs2_map_journal_extents - Cache journal bmap info
  1188. * @sdp: The super block
  1189. * @jd: The journal to map
  1190. *
  1191. * Create a reusable "extent" mapping from all logical
  1192. * blocks to all physical blocks for the given journal. This will save
  1193. * us time when writing journal blocks. Most journals will have only one
  1194. * extent that maps all their logical blocks. That's because gfs2.mkfs
  1195. * arranges the journal blocks sequentially to maximize performance.
  1196. * So the extent would map the first block for the entire file length.
  1197. * However, gfs2_jadd can happen while file activity is happening, so
  1198. * those journals may not be sequential. Less likely is the case where
  1199. * the users created their own journals by mounting the metafs and
  1200. * laying it out. But it's still possible. These journals might have
  1201. * several extents.
  1202. *
  1203. * Returns: 0 on success, or error on failure
  1204. */
  1205. int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
  1206. {
  1207. u64 lblock = 0;
  1208. u64 lblock_stop;
  1209. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  1210. struct buffer_head bh;
  1211. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  1212. u64 size;
  1213. int rc;
  1214. lblock_stop = i_size_read(jd->jd_inode) >> shift;
  1215. size = (lblock_stop - lblock) << shift;
  1216. jd->nr_extents = 0;
  1217. WARN_ON(!list_empty(&jd->extent_list));
  1218. do {
  1219. bh.b_state = 0;
  1220. bh.b_blocknr = 0;
  1221. bh.b_size = size;
  1222. rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
  1223. if (rc || !buffer_mapped(&bh))
  1224. goto fail;
  1225. rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
  1226. if (rc)
  1227. goto fail;
  1228. size -= bh.b_size;
  1229. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1230. } while(size > 0);
  1231. fs_info(sdp, "journal %d mapped with %u extents\n", jd->jd_jid,
  1232. jd->nr_extents);
  1233. return 0;
  1234. fail:
  1235. fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
  1236. rc, jd->jd_jid,
  1237. (unsigned long long)(i_size_read(jd->jd_inode) - size),
  1238. jd->nr_extents);
  1239. fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
  1240. rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
  1241. bh.b_state, (unsigned long long)bh.b_size);
  1242. gfs2_free_journal_extents(jd);
  1243. return rc;
  1244. }
  1245. /**
  1246. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1247. * @ip: the file being written to
  1248. * @offset: the offset to write to
  1249. * @len: the number of bytes being written
  1250. *
  1251. * Returns: 1 if an alloc is required, 0 otherwise
  1252. */
  1253. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1254. unsigned int len)
  1255. {
  1256. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1257. struct buffer_head bh;
  1258. unsigned int shift;
  1259. u64 lblock, lblock_stop, size;
  1260. u64 end_of_file;
  1261. if (!len)
  1262. return 0;
  1263. if (gfs2_is_stuffed(ip)) {
  1264. if (offset + len >
  1265. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1266. return 1;
  1267. return 0;
  1268. }
  1269. shift = sdp->sd_sb.sb_bsize_shift;
  1270. BUG_ON(gfs2_is_dir(ip));
  1271. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1272. lblock = offset >> shift;
  1273. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1274. if (lblock_stop > end_of_file)
  1275. return 1;
  1276. size = (lblock_stop - lblock) << shift;
  1277. do {
  1278. bh.b_state = 0;
  1279. bh.b_size = size;
  1280. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1281. if (!buffer_mapped(&bh))
  1282. return 1;
  1283. size -= bh.b_size;
  1284. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1285. } while(size > 0);
  1286. return 0;
  1287. }