bmap.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  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. /**
  38. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  39. * @ip: the inode
  40. * @dibh: the dinode buffer
  41. * @block: the block number that was allocated
  42. * @page: The (optional) page. This is looked up if @page is NULL
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  47. u64 block, struct page *page)
  48. {
  49. struct inode *inode = &ip->i_inode;
  50. struct buffer_head *bh;
  51. int release = 0;
  52. if (!page || page->index) {
  53. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  54. if (!page)
  55. return -ENOMEM;
  56. release = 1;
  57. }
  58. if (!PageUptodate(page)) {
  59. void *kaddr = kmap(page);
  60. u64 dsize = i_size_read(inode);
  61. if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
  62. dsize = dibh->b_size - sizeof(struct gfs2_dinode);
  63. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  64. memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
  65. kunmap(page);
  66. SetPageUptodate(page);
  67. }
  68. if (!page_has_buffers(page))
  69. create_empty_buffers(page, BIT(inode->i_blkbits),
  70. BIT(BH_Uptodate));
  71. bh = page_buffers(page);
  72. if (!buffer_mapped(bh))
  73. map_bh(bh, inode->i_sb, block);
  74. set_buffer_uptodate(bh);
  75. if (!gfs2_is_jdata(ip))
  76. mark_buffer_dirty(bh);
  77. if (!gfs2_is_writeback(ip))
  78. gfs2_trans_add_data(ip->i_gl, bh);
  79. if (release) {
  80. unlock_page(page);
  81. put_page(page);
  82. }
  83. return 0;
  84. }
  85. /**
  86. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  87. * @ip: The GFS2 inode to unstuff
  88. * @page: The (optional) page. This is looked up if the @page is NULL
  89. *
  90. * This routine unstuffs a dinode and returns it to a "normal" state such
  91. * that the height can be grown in the traditional way.
  92. *
  93. * Returns: errno
  94. */
  95. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  96. {
  97. struct buffer_head *bh, *dibh;
  98. struct gfs2_dinode *di;
  99. u64 block = 0;
  100. int isdir = gfs2_is_dir(ip);
  101. int error;
  102. down_write(&ip->i_rw_mutex);
  103. error = gfs2_meta_inode_buffer(ip, &dibh);
  104. if (error)
  105. goto out;
  106. if (i_size_read(&ip->i_inode)) {
  107. /* Get a free block, fill it with the stuffed data,
  108. and write it out to disk */
  109. unsigned int n = 1;
  110. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  111. if (error)
  112. goto out_brelse;
  113. if (isdir) {
  114. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  115. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  116. if (error)
  117. goto out_brelse;
  118. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  119. dibh, sizeof(struct gfs2_dinode));
  120. brelse(bh);
  121. } else {
  122. error = gfs2_unstuffer_page(ip, dibh, block, page);
  123. if (error)
  124. goto out_brelse;
  125. }
  126. }
  127. /* Set up the pointer to the new block */
  128. gfs2_trans_add_meta(ip->i_gl, dibh);
  129. di = (struct gfs2_dinode *)dibh->b_data;
  130. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  131. if (i_size_read(&ip->i_inode)) {
  132. *(__be64 *)(di + 1) = cpu_to_be64(block);
  133. gfs2_add_inode_blocks(&ip->i_inode, 1);
  134. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  135. }
  136. ip->i_height = 1;
  137. di->di_height = cpu_to_be16(1);
  138. out_brelse:
  139. brelse(dibh);
  140. out:
  141. up_write(&ip->i_rw_mutex);
  142. return error;
  143. }
  144. /**
  145. * find_metapath - Find path through the metadata tree
  146. * @sdp: The superblock
  147. * @mp: The metapath to return the result in
  148. * @block: The disk block to look up
  149. * @height: The pre-calculated height of the metadata tree
  150. *
  151. * This routine returns a struct metapath structure that defines a path
  152. * through the metadata of inode "ip" to get to block "block".
  153. *
  154. * Example:
  155. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  156. * filesystem with a blocksize of 4096.
  157. *
  158. * find_metapath() would return a struct metapath structure set to:
  159. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  160. * and mp_list[2] = 165.
  161. *
  162. * That means that in order to get to the block containing the byte at
  163. * offset 101342453, we would load the indirect block pointed to by pointer
  164. * 0 in the dinode. We would then load the indirect block pointed to by
  165. * pointer 48 in that indirect block. We would then load the data block
  166. * pointed to by pointer 165 in that indirect block.
  167. *
  168. * ----------------------------------------
  169. * | Dinode | |
  170. * | | 4|
  171. * | |0 1 2 3 4 5 9|
  172. * | | 6|
  173. * ----------------------------------------
  174. * |
  175. * |
  176. * V
  177. * ----------------------------------------
  178. * | Indirect Block |
  179. * | 5|
  180. * | 4 4 4 4 4 5 5 1|
  181. * |0 5 6 7 8 9 0 1 2|
  182. * ----------------------------------------
  183. * |
  184. * |
  185. * V
  186. * ----------------------------------------
  187. * | Indirect Block |
  188. * | 1 1 1 1 1 5|
  189. * | 6 6 6 6 6 1|
  190. * |0 3 4 5 6 7 2|
  191. * ----------------------------------------
  192. * |
  193. * |
  194. * V
  195. * ----------------------------------------
  196. * | Data block containing offset |
  197. * | 101342453 |
  198. * | |
  199. * | |
  200. * ----------------------------------------
  201. *
  202. */
  203. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  204. struct metapath *mp, unsigned int height)
  205. {
  206. unsigned int i;
  207. for (i = height; i--;)
  208. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  209. }
  210. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  211. {
  212. if (mp->mp_list[0] == 0)
  213. return 2;
  214. return 1;
  215. }
  216. /**
  217. * metaptr1 - Return the first possible metadata pointer in a metaath buffer
  218. * @height: The metadata height (0 = dinode)
  219. * @mp: The metapath
  220. */
  221. static inline __be64 *metaptr1(unsigned int height, const struct metapath *mp)
  222. {
  223. struct buffer_head *bh = mp->mp_bh[height];
  224. if (height == 0)
  225. return ((__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)));
  226. return ((__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)));
  227. }
  228. /**
  229. * metapointer - Return pointer to start of metadata in a buffer
  230. * @height: The metadata height (0 = dinode)
  231. * @mp: The metapath
  232. *
  233. * Return a pointer to the block number of the next height of the metadata
  234. * tree given a buffer containing the pointer to the current height of the
  235. * metadata tree.
  236. */
  237. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  238. {
  239. __be64 *p = metaptr1(height, mp);
  240. return p + mp->mp_list[height];
  241. }
  242. static void gfs2_metapath_ra(struct gfs2_glock *gl,
  243. const struct buffer_head *bh, const __be64 *pos)
  244. {
  245. struct buffer_head *rabh;
  246. const __be64 *endp = (const __be64 *)(bh->b_data + bh->b_size);
  247. const __be64 *t;
  248. for (t = pos; t < endp; t++) {
  249. if (!*t)
  250. continue;
  251. rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
  252. if (trylock_buffer(rabh)) {
  253. if (!buffer_uptodate(rabh)) {
  254. rabh->b_end_io = end_buffer_read_sync;
  255. submit_bh(REQ_OP_READ,
  256. REQ_RAHEAD | REQ_META | REQ_PRIO,
  257. rabh);
  258. continue;
  259. }
  260. unlock_buffer(rabh);
  261. }
  262. brelse(rabh);
  263. }
  264. }
  265. /**
  266. * lookup_mp_height - helper function for lookup_metapath
  267. * @ip: the inode
  268. * @mp: the metapath
  269. * @h: the height which needs looking up
  270. */
  271. static int lookup_mp_height(struct gfs2_inode *ip, struct metapath *mp, int h)
  272. {
  273. __be64 *ptr = metapointer(h, mp);
  274. u64 dblock = be64_to_cpu(*ptr);
  275. if (!dblock)
  276. return h + 1;
  277. return gfs2_meta_indirect_buffer(ip, h + 1, dblock, &mp->mp_bh[h + 1]);
  278. }
  279. /**
  280. * lookup_metapath - Walk the metadata tree to a specific point
  281. * @ip: The inode
  282. * @mp: The metapath
  283. *
  284. * Assumes that the inode's buffer has already been looked up and
  285. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  286. * by find_metapath().
  287. *
  288. * If this function encounters part of the tree which has not been
  289. * allocated, it returns the current height of the tree at the point
  290. * at which it found the unallocated block. Blocks which are found are
  291. * added to the mp->mp_bh[] list.
  292. *
  293. * Returns: error or height of metadata tree
  294. */
  295. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  296. {
  297. unsigned int end_of_metadata = ip->i_height - 1;
  298. unsigned int x;
  299. int ret;
  300. for (x = 0; x < end_of_metadata; x++) {
  301. ret = lookup_mp_height(ip, mp, x);
  302. if (ret)
  303. return ret;
  304. }
  305. return ip->i_height;
  306. }
  307. /**
  308. * fillup_metapath - fill up buffers for the metadata path to a specific height
  309. * @ip: The inode
  310. * @mp: The metapath
  311. * @h: The height to which it should be mapped
  312. *
  313. * Similar to lookup_metapath, but does lookups for a range of heights
  314. *
  315. * Returns: error or height of metadata tree
  316. */
  317. static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h)
  318. {
  319. unsigned int start_h = h - 1;
  320. int ret;
  321. if (h) {
  322. /* find the first buffer we need to look up. */
  323. while (start_h > 0 && mp->mp_bh[start_h] == NULL)
  324. start_h--;
  325. for (; start_h < h; start_h++) {
  326. ret = lookup_mp_height(ip, mp, start_h);
  327. if (ret)
  328. return ret;
  329. }
  330. }
  331. return ip->i_height;
  332. }
  333. static inline void release_metapath(struct metapath *mp)
  334. {
  335. int i;
  336. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  337. if (mp->mp_bh[i] == NULL)
  338. break;
  339. brelse(mp->mp_bh[i]);
  340. }
  341. }
  342. /**
  343. * gfs2_extent_length - Returns length of an extent of blocks
  344. * @start: Start of the buffer
  345. * @len: Length of the buffer in bytes
  346. * @ptr: Current position in the buffer
  347. * @limit: Max extent length to return (0 = unlimited)
  348. * @eob: Set to 1 if we hit "end of block"
  349. *
  350. * If the first block is zero (unallocated) it will return the number of
  351. * unallocated blocks in the extent, otherwise it will return the number
  352. * of contiguous blocks in the extent.
  353. *
  354. * Returns: The length of the extent (minimum of one block)
  355. */
  356. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, size_t limit, int *eob)
  357. {
  358. const __be64 *end = (start + len);
  359. const __be64 *first = ptr;
  360. u64 d = be64_to_cpu(*ptr);
  361. *eob = 0;
  362. do {
  363. ptr++;
  364. if (ptr >= end)
  365. break;
  366. if (limit && --limit == 0)
  367. break;
  368. if (d)
  369. d++;
  370. } while(be64_to_cpu(*ptr) == d);
  371. if (ptr >= end)
  372. *eob = 1;
  373. return (ptr - first);
  374. }
  375. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  376. {
  377. if (create)
  378. down_write(&ip->i_rw_mutex);
  379. else
  380. down_read(&ip->i_rw_mutex);
  381. }
  382. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  383. {
  384. if (create)
  385. up_write(&ip->i_rw_mutex);
  386. else
  387. up_read(&ip->i_rw_mutex);
  388. }
  389. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  390. struct gfs2_glock *gl, unsigned int i,
  391. unsigned offset, u64 bn)
  392. {
  393. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  394. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  395. sizeof(struct gfs2_dinode)));
  396. BUG_ON(i < 1);
  397. BUG_ON(mp->mp_bh[i] != NULL);
  398. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  399. gfs2_trans_add_meta(gl, mp->mp_bh[i]);
  400. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  401. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  402. ptr += offset;
  403. *ptr = cpu_to_be64(bn);
  404. return ptr;
  405. }
  406. enum alloc_state {
  407. ALLOC_DATA = 0,
  408. ALLOC_GROW_DEPTH = 1,
  409. ALLOC_GROW_HEIGHT = 2,
  410. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  411. };
  412. static inline unsigned int hptrs(struct gfs2_sbd *sdp, const unsigned int hgt)
  413. {
  414. if (hgt)
  415. return sdp->sd_inptrs;
  416. return sdp->sd_diptrs;
  417. }
  418. /**
  419. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  420. * @inode: The GFS2 inode
  421. * @lblock: The logical starting block of the extent
  422. * @bh_map: This is used to return the mapping details
  423. * @mp: The metapath
  424. * @sheight: The starting height (i.e. whats already mapped)
  425. * @height: The height to build to
  426. * @maxlen: The max number of data blocks to alloc
  427. *
  428. * In this routine we may have to alloc:
  429. * i) Indirect blocks to grow the metadata tree height
  430. * ii) Indirect blocks to fill in lower part of the metadata tree
  431. * iii) Data blocks
  432. *
  433. * The function is in two parts. The first part works out the total
  434. * number of blocks which we need. The second part does the actual
  435. * allocation asking for an extent at a time (if enough contiguous free
  436. * blocks are available, there will only be one request per bmap call)
  437. * and uses the state machine to initialise the blocks in order.
  438. *
  439. * Returns: errno on error
  440. */
  441. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  442. struct buffer_head *bh_map, struct metapath *mp,
  443. const unsigned int sheight,
  444. const unsigned int height,
  445. const size_t maxlen)
  446. {
  447. struct gfs2_inode *ip = GFS2_I(inode);
  448. struct gfs2_sbd *sdp = GFS2_SB(inode);
  449. struct super_block *sb = sdp->sd_vfs;
  450. struct buffer_head *dibh = mp->mp_bh[0];
  451. u64 bn, dblock = 0;
  452. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  453. unsigned dblks = 0;
  454. unsigned ptrs_per_blk;
  455. const unsigned end_of_metadata = height - 1;
  456. int ret;
  457. int eob = 0;
  458. enum alloc_state state;
  459. __be64 *ptr;
  460. __be64 zero_bn = 0;
  461. BUG_ON(sheight < 1);
  462. BUG_ON(dibh == NULL);
  463. gfs2_trans_add_meta(ip->i_gl, dibh);
  464. if (height == sheight) {
  465. struct buffer_head *bh;
  466. /* Bottom indirect block exists, find unalloced extent size */
  467. ptr = metapointer(end_of_metadata, mp);
  468. bh = mp->mp_bh[end_of_metadata];
  469. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  470. &eob);
  471. BUG_ON(dblks < 1);
  472. state = ALLOC_DATA;
  473. } else {
  474. /* Need to allocate indirect blocks */
  475. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  476. dblks = min(maxlen, (size_t)(ptrs_per_blk -
  477. mp->mp_list[end_of_metadata]));
  478. if (height == ip->i_height) {
  479. /* Writing into existing tree, extend tree down */
  480. iblks = height - sheight;
  481. state = ALLOC_GROW_DEPTH;
  482. } else {
  483. /* Building up tree height */
  484. state = ALLOC_GROW_HEIGHT;
  485. iblks = height - ip->i_height;
  486. branch_start = metapath_branch_start(mp);
  487. iblks += (height - branch_start);
  488. }
  489. }
  490. /* start of the second part of the function (state machine) */
  491. blks = dblks + iblks;
  492. i = sheight;
  493. do {
  494. int error;
  495. n = blks - alloced;
  496. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  497. if (error)
  498. return error;
  499. alloced += n;
  500. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  501. gfs2_trans_add_unrevoke(sdp, bn, n);
  502. switch (state) {
  503. /* Growing height of tree */
  504. case ALLOC_GROW_HEIGHT:
  505. if (i == 1) {
  506. ptr = (__be64 *)(dibh->b_data +
  507. sizeof(struct gfs2_dinode));
  508. zero_bn = *ptr;
  509. }
  510. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  511. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  512. if (i - 1 == height - ip->i_height) {
  513. i--;
  514. gfs2_buffer_copy_tail(mp->mp_bh[i],
  515. sizeof(struct gfs2_meta_header),
  516. dibh, sizeof(struct gfs2_dinode));
  517. gfs2_buffer_clear_tail(dibh,
  518. sizeof(struct gfs2_dinode) +
  519. sizeof(__be64));
  520. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  521. sizeof(struct gfs2_meta_header));
  522. *ptr = zero_bn;
  523. state = ALLOC_GROW_DEPTH;
  524. for(i = branch_start; i < height; i++) {
  525. if (mp->mp_bh[i] == NULL)
  526. break;
  527. brelse(mp->mp_bh[i]);
  528. mp->mp_bh[i] = NULL;
  529. }
  530. i = branch_start;
  531. }
  532. if (n == 0)
  533. break;
  534. /* Branching from existing tree */
  535. case ALLOC_GROW_DEPTH:
  536. if (i > 1 && i < height)
  537. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  538. for (; i < height && n > 0; i++, n--)
  539. gfs2_indirect_init(mp, ip->i_gl, i,
  540. mp->mp_list[i-1], bn++);
  541. if (i == height)
  542. state = ALLOC_DATA;
  543. if (n == 0)
  544. break;
  545. /* Tree complete, adding data blocks */
  546. case ALLOC_DATA:
  547. BUG_ON(n > dblks);
  548. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  549. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  550. dblks = n;
  551. ptr = metapointer(end_of_metadata, mp);
  552. dblock = bn;
  553. while (n-- > 0)
  554. *ptr++ = cpu_to_be64(bn++);
  555. if (buffer_zeronew(bh_map)) {
  556. ret = sb_issue_zeroout(sb, dblock, dblks,
  557. GFP_NOFS);
  558. if (ret) {
  559. fs_err(sdp,
  560. "Failed to zero data buffers\n");
  561. clear_buffer_zeronew(bh_map);
  562. }
  563. }
  564. break;
  565. }
  566. } while ((state != ALLOC_DATA) || !dblock);
  567. ip->i_height = height;
  568. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  569. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  570. map_bh(bh_map, inode->i_sb, dblock);
  571. bh_map->b_size = dblks << inode->i_blkbits;
  572. set_buffer_new(bh_map);
  573. return 0;
  574. }
  575. /**
  576. * gfs2_block_map - Map a block from an inode to a disk block
  577. * @inode: The inode
  578. * @lblock: The logical block number
  579. * @bh_map: The bh to be mapped
  580. * @create: True if its ok to alloc blocks to satify the request
  581. *
  582. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  583. * read of metadata will be required before the next block can be
  584. * mapped. Sets buffer_new() if new blocks were allocated.
  585. *
  586. * Returns: errno
  587. */
  588. int gfs2_block_map(struct inode *inode, sector_t lblock,
  589. struct buffer_head *bh_map, int create)
  590. {
  591. struct gfs2_inode *ip = GFS2_I(inode);
  592. struct gfs2_sbd *sdp = GFS2_SB(inode);
  593. unsigned int bsize = sdp->sd_sb.sb_bsize;
  594. const size_t maxlen = bh_map->b_size >> inode->i_blkbits;
  595. const u64 *arr = sdp->sd_heightsize;
  596. __be64 *ptr;
  597. u64 size;
  598. struct metapath mp;
  599. int ret;
  600. int eob;
  601. unsigned int len;
  602. struct buffer_head *bh;
  603. u8 height;
  604. BUG_ON(maxlen == 0);
  605. memset(&mp, 0, sizeof(mp));
  606. bmap_lock(ip, create);
  607. clear_buffer_mapped(bh_map);
  608. clear_buffer_new(bh_map);
  609. clear_buffer_boundary(bh_map);
  610. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  611. if (gfs2_is_dir(ip)) {
  612. bsize = sdp->sd_jbsize;
  613. arr = sdp->sd_jheightsize;
  614. }
  615. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  616. if (ret)
  617. goto out;
  618. height = ip->i_height;
  619. size = (lblock + 1) * bsize;
  620. while (size > arr[height])
  621. height++;
  622. find_metapath(sdp, lblock, &mp, height);
  623. ret = 1;
  624. if (height > ip->i_height || gfs2_is_stuffed(ip))
  625. goto do_alloc;
  626. ret = lookup_metapath(ip, &mp);
  627. if (ret < 0)
  628. goto out;
  629. if (ret != ip->i_height)
  630. goto do_alloc;
  631. ptr = metapointer(ip->i_height - 1, &mp);
  632. if (*ptr == 0)
  633. goto do_alloc;
  634. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  635. bh = mp.mp_bh[ip->i_height - 1];
  636. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  637. bh_map->b_size = (len << inode->i_blkbits);
  638. if (eob)
  639. set_buffer_boundary(bh_map);
  640. ret = 0;
  641. out:
  642. release_metapath(&mp);
  643. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  644. bmap_unlock(ip, create);
  645. return ret;
  646. do_alloc:
  647. /* All allocations are done here, firstly check create flag */
  648. if (!create) {
  649. BUG_ON(gfs2_is_stuffed(ip));
  650. ret = 0;
  651. goto out;
  652. }
  653. /* At this point ret is the tree depth of already allocated blocks */
  654. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  655. goto out;
  656. }
  657. /*
  658. * Deprecated: do not use in new code
  659. */
  660. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  661. {
  662. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  663. int ret;
  664. int create = *new;
  665. BUG_ON(!extlen);
  666. BUG_ON(!dblock);
  667. BUG_ON(!new);
  668. bh.b_size = BIT(inode->i_blkbits + (create ? 0 : 5));
  669. ret = gfs2_block_map(inode, lblock, &bh, create);
  670. *extlen = bh.b_size >> inode->i_blkbits;
  671. *dblock = bh.b_blocknr;
  672. if (buffer_new(&bh))
  673. *new = 1;
  674. else
  675. *new = 0;
  676. return ret;
  677. }
  678. /**
  679. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  680. *
  681. * This is partly borrowed from ext3.
  682. */
  683. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  684. {
  685. struct inode *inode = mapping->host;
  686. struct gfs2_inode *ip = GFS2_I(inode);
  687. unsigned long index = from >> PAGE_SHIFT;
  688. unsigned offset = from & (PAGE_SIZE-1);
  689. unsigned blocksize, iblock, length, pos;
  690. struct buffer_head *bh;
  691. struct page *page;
  692. int err;
  693. page = find_or_create_page(mapping, index, GFP_NOFS);
  694. if (!page)
  695. return 0;
  696. blocksize = inode->i_sb->s_blocksize;
  697. length = blocksize - (offset & (blocksize - 1));
  698. iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
  699. if (!page_has_buffers(page))
  700. create_empty_buffers(page, blocksize, 0);
  701. /* Find the buffer that contains "offset" */
  702. bh = page_buffers(page);
  703. pos = blocksize;
  704. while (offset >= pos) {
  705. bh = bh->b_this_page;
  706. iblock++;
  707. pos += blocksize;
  708. }
  709. err = 0;
  710. if (!buffer_mapped(bh)) {
  711. gfs2_block_map(inode, iblock, bh, 0);
  712. /* unmapped? It's a hole - nothing to do */
  713. if (!buffer_mapped(bh))
  714. goto unlock;
  715. }
  716. /* Ok, it's mapped. Make sure it's up-to-date */
  717. if (PageUptodate(page))
  718. set_buffer_uptodate(bh);
  719. if (!buffer_uptodate(bh)) {
  720. err = -EIO;
  721. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  722. wait_on_buffer(bh);
  723. /* Uhhuh. Read error. Complain and punt. */
  724. if (!buffer_uptodate(bh))
  725. goto unlock;
  726. err = 0;
  727. }
  728. if (!gfs2_is_writeback(ip))
  729. gfs2_trans_add_data(ip->i_gl, bh);
  730. zero_user(page, offset, length);
  731. mark_buffer_dirty(bh);
  732. unlock:
  733. unlock_page(page);
  734. put_page(page);
  735. return err;
  736. }
  737. #define GFS2_JTRUNC_REVOKES 8192
  738. /**
  739. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  740. * @inode: The inode being truncated
  741. * @oldsize: The original (larger) size
  742. * @newsize: The new smaller size
  743. *
  744. * With jdata files, we have to journal a revoke for each block which is
  745. * truncated. As a result, we need to split this into separate transactions
  746. * if the number of pages being truncated gets too large.
  747. */
  748. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  749. {
  750. struct gfs2_sbd *sdp = GFS2_SB(inode);
  751. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  752. u64 chunk;
  753. int error;
  754. while (oldsize != newsize) {
  755. chunk = oldsize - newsize;
  756. if (chunk > max_chunk)
  757. chunk = max_chunk;
  758. truncate_pagecache(inode, oldsize - chunk);
  759. oldsize -= chunk;
  760. gfs2_trans_end(sdp);
  761. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  762. if (error)
  763. return error;
  764. }
  765. return 0;
  766. }
  767. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  768. {
  769. struct gfs2_inode *ip = GFS2_I(inode);
  770. struct gfs2_sbd *sdp = GFS2_SB(inode);
  771. struct address_space *mapping = inode->i_mapping;
  772. struct buffer_head *dibh;
  773. int journaled = gfs2_is_jdata(ip);
  774. int error;
  775. if (journaled)
  776. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  777. else
  778. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  779. if (error)
  780. return error;
  781. error = gfs2_meta_inode_buffer(ip, &dibh);
  782. if (error)
  783. goto out;
  784. gfs2_trans_add_meta(ip->i_gl, dibh);
  785. if (gfs2_is_stuffed(ip)) {
  786. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  787. } else {
  788. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  789. error = gfs2_block_truncate_page(mapping, newsize);
  790. if (error)
  791. goto out_brelse;
  792. }
  793. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  794. }
  795. i_size_write(inode, newsize);
  796. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  797. gfs2_dinode_out(ip, dibh->b_data);
  798. if (journaled)
  799. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  800. else
  801. truncate_pagecache(inode, newsize);
  802. if (error) {
  803. brelse(dibh);
  804. return error;
  805. }
  806. out_brelse:
  807. brelse(dibh);
  808. out:
  809. gfs2_trans_end(sdp);
  810. return error;
  811. }
  812. /**
  813. * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein
  814. * @ip: inode
  815. * @rg_gh: holder of resource group glock
  816. * @mp: current metapath fully populated with buffers
  817. * @btotal: place to keep count of total blocks freed
  818. * @hgt: height we're processing
  819. * @first: true if this is the first call to this function for this height
  820. *
  821. * We sweep a metadata buffer (provided by the metapath) for blocks we need to
  822. * free, and free them all. However, we do it one rgrp at a time. If this
  823. * block has references to multiple rgrps, we break it into individual
  824. * transactions. This allows other processes to use the rgrps while we're
  825. * focused on a single one, for better concurrency / performance.
  826. * At every transaction boundary, we rewrite the inode into the journal.
  827. * That way the bitmaps are kept consistent with the inode and we can recover
  828. * if we're interrupted by power-outages.
  829. *
  830. * Returns: 0, or return code if an error occurred.
  831. * *btotal has the total number of blocks freed
  832. */
  833. static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
  834. const struct metapath *mp, u32 *btotal, int hgt,
  835. bool preserve1)
  836. {
  837. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  838. struct gfs2_rgrpd *rgd;
  839. struct gfs2_trans *tr;
  840. struct buffer_head *bh = mp->mp_bh[hgt];
  841. __be64 *top, *bottom, *p;
  842. int blks_outside_rgrp;
  843. u64 bn, bstart, isize_blks;
  844. s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */
  845. int meta = ((hgt != ip->i_height - 1) ? 1 : 0);
  846. int ret = 0;
  847. bool buf_in_tr = false; /* buffer was added to transaction */
  848. if (gfs2_metatype_check(sdp, bh,
  849. (hgt ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)))
  850. return -EIO;
  851. more_rgrps:
  852. blks_outside_rgrp = 0;
  853. bstart = 0;
  854. blen = 0;
  855. top = metapointer(hgt, mp); /* first ptr from metapath */
  856. /* If we're keeping some data at the truncation point, we've got to
  857. preserve the metadata tree by adding 1 to the starting metapath. */
  858. if (preserve1)
  859. top++;
  860. bottom = (__be64 *)(bh->b_data + bh->b_size);
  861. for (p = top; p < bottom; p++) {
  862. if (!*p)
  863. continue;
  864. bn = be64_to_cpu(*p);
  865. if (gfs2_holder_initialized(rd_gh)) {
  866. rgd = gfs2_glock2rgrp(rd_gh->gh_gl);
  867. gfs2_assert_withdraw(sdp,
  868. gfs2_glock_is_locked_by_me(rd_gh->gh_gl));
  869. } else {
  870. rgd = gfs2_blk2rgrpd(sdp, bn, false);
  871. ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  872. 0, rd_gh);
  873. if (ret)
  874. goto out;
  875. /* Must be done with the rgrp glock held: */
  876. if (gfs2_rs_active(&ip->i_res) &&
  877. rgd == ip->i_res.rs_rbm.rgd)
  878. gfs2_rs_deltree(&ip->i_res);
  879. }
  880. if (!rgrp_contains_block(rgd, bn)) {
  881. blks_outside_rgrp++;
  882. continue;
  883. }
  884. /* The size of our transactions will be unknown until we
  885. actually process all the metadata blocks that relate to
  886. the rgrp. So we estimate. We know it can't be more than
  887. the dinode's i_blocks and we don't want to exceed the
  888. journal flush threshold, sd_log_thresh2. */
  889. if (current->journal_info == NULL) {
  890. unsigned int jblocks_rqsted, revokes;
  891. jblocks_rqsted = rgd->rd_length + RES_DINODE +
  892. RES_INDIRECT;
  893. isize_blks = gfs2_get_inode_blocks(&ip->i_inode);
  894. if (isize_blks > atomic_read(&sdp->sd_log_thresh2))
  895. jblocks_rqsted +=
  896. atomic_read(&sdp->sd_log_thresh2);
  897. else
  898. jblocks_rqsted += isize_blks;
  899. revokes = jblocks_rqsted;
  900. if (meta)
  901. revokes += hptrs(sdp, hgt);
  902. else if (ip->i_depth)
  903. revokes += sdp->sd_inptrs;
  904. ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
  905. if (ret)
  906. goto out_unlock;
  907. down_write(&ip->i_rw_mutex);
  908. }
  909. /* check if we will exceed the transaction blocks requested */
  910. tr = current->journal_info;
  911. if (tr->tr_num_buf_new + RES_STATFS +
  912. RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) {
  913. /* We set blks_outside_rgrp to ensure the loop will
  914. be repeated for the same rgrp, but with a new
  915. transaction. */
  916. blks_outside_rgrp++;
  917. /* This next part is tricky. If the buffer was added
  918. to the transaction, we've already set some block
  919. pointers to 0, so we better follow through and free
  920. them, or we will introduce corruption (so break).
  921. This may be impossible, or at least rare, but I
  922. decided to cover the case regardless.
  923. If the buffer was not added to the transaction
  924. (this call), doing so would exceed our transaction
  925. size, so we need to end the transaction and start a
  926. new one (so goto). */
  927. if (buf_in_tr)
  928. break;
  929. goto out_unlock;
  930. }
  931. gfs2_trans_add_meta(ip->i_gl, bh);
  932. buf_in_tr = true;
  933. *p = 0;
  934. if (bstart + blen == bn) {
  935. blen++;
  936. continue;
  937. }
  938. if (bstart) {
  939. __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
  940. (*btotal) += blen;
  941. gfs2_add_inode_blocks(&ip->i_inode, -blen);
  942. }
  943. bstart = bn;
  944. blen = 1;
  945. }
  946. if (bstart) {
  947. __gfs2_free_blocks(ip, bstart, (u32)blen, meta);
  948. (*btotal) += blen;
  949. gfs2_add_inode_blocks(&ip->i_inode, -blen);
  950. }
  951. out_unlock:
  952. if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks
  953. outside the rgrp we just processed,
  954. do it all over again. */
  955. if (current->journal_info) {
  956. struct buffer_head *dibh = mp->mp_bh[0];
  957. /* Every transaction boundary, we rewrite the dinode
  958. to keep its di_blocks current in case of failure. */
  959. ip->i_inode.i_mtime = ip->i_inode.i_ctime =
  960. current_time(&ip->i_inode);
  961. gfs2_trans_add_meta(ip->i_gl, dibh);
  962. gfs2_dinode_out(ip, dibh->b_data);
  963. up_write(&ip->i_rw_mutex);
  964. gfs2_trans_end(sdp);
  965. }
  966. gfs2_glock_dq_uninit(rd_gh);
  967. cond_resched();
  968. goto more_rgrps;
  969. }
  970. out:
  971. return ret;
  972. }
  973. /**
  974. * find_nonnull_ptr - find a non-null pointer given a metapath and height
  975. * assumes the metapath is valid (with buffers) out to height h
  976. * @mp: starting metapath
  977. * @h: desired height to search
  978. *
  979. * Returns: true if a non-null pointer was found in the metapath buffer
  980. * false if all remaining pointers are NULL in the buffer
  981. */
  982. static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp,
  983. unsigned int h)
  984. {
  985. __be64 *ptr;
  986. unsigned int ptrs = hptrs(sdp, h) - 1;
  987. while (true) {
  988. ptr = metapointer(h, mp);
  989. if (*ptr) { /* if we have a non-null pointer */
  990. /* Now zero the metapath after the current height. */
  991. h++;
  992. if (h < GFS2_MAX_META_HEIGHT)
  993. memset(&mp->mp_list[h], 0,
  994. (GFS2_MAX_META_HEIGHT - h) *
  995. sizeof(mp->mp_list[0]));
  996. return true;
  997. }
  998. if (mp->mp_list[h] < ptrs)
  999. mp->mp_list[h]++;
  1000. else
  1001. return false; /* no more pointers in this buffer */
  1002. }
  1003. }
  1004. enum dealloc_states {
  1005. DEALLOC_MP_FULL = 0, /* Strip a metapath with all buffers read in */
  1006. DEALLOC_MP_LOWER = 1, /* lower the metapath strip height */
  1007. DEALLOC_FILL_MP = 2, /* Fill in the metapath to the given height. */
  1008. DEALLOC_DONE = 3, /* process complete */
  1009. };
  1010. static bool mp_eq_to_hgt(struct metapath *mp, __u16 *nbof, unsigned int h)
  1011. {
  1012. if (memcmp(mp->mp_list, nbof, h * sizeof(mp->mp_list[0])))
  1013. return false;
  1014. return true;
  1015. }
  1016. /**
  1017. * trunc_dealloc - truncate a file down to a desired size
  1018. * @ip: inode to truncate
  1019. * @newsize: The desired size of the file
  1020. *
  1021. * This function truncates a file to newsize. It works from the
  1022. * bottom up, and from the right to the left. In other words, it strips off
  1023. * the highest layer (data) before stripping any of the metadata. Doing it
  1024. * this way is best in case the operation is interrupted by power failure, etc.
  1025. * The dinode is rewritten in every transaction to guarantee integrity.
  1026. */
  1027. static int trunc_dealloc(struct gfs2_inode *ip, u64 newsize)
  1028. {
  1029. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1030. struct metapath mp;
  1031. struct buffer_head *dibh, *bh;
  1032. struct gfs2_holder rd_gh;
  1033. u64 lblock;
  1034. __u16 nbof[GFS2_MAX_META_HEIGHT]; /* new beginning of truncation */
  1035. unsigned int strip_h = ip->i_height - 1;
  1036. u32 btotal = 0;
  1037. int ret, state;
  1038. int mp_h; /* metapath buffers are read in to this height */
  1039. sector_t last_ra = 0;
  1040. u64 prev_bnr = 0;
  1041. bool preserve1; /* need to preserve the first meta pointer? */
  1042. if (!newsize)
  1043. lblock = 0;
  1044. else
  1045. lblock = (newsize - 1) >> sdp->sd_sb.sb_bsize_shift;
  1046. memset(&mp, 0, sizeof(mp));
  1047. find_metapath(sdp, lblock, &mp, ip->i_height);
  1048. memcpy(&nbof, &mp.mp_list, sizeof(nbof));
  1049. ret = gfs2_meta_inode_buffer(ip, &dibh);
  1050. if (ret)
  1051. return ret;
  1052. mp.mp_bh[0] = dibh;
  1053. ret = lookup_metapath(ip, &mp);
  1054. if (ret == ip->i_height)
  1055. state = DEALLOC_MP_FULL; /* We have a complete metapath */
  1056. else
  1057. state = DEALLOC_FILL_MP; /* deal with partial metapath */
  1058. ret = gfs2_rindex_update(sdp);
  1059. if (ret)
  1060. goto out_metapath;
  1061. ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  1062. if (ret)
  1063. goto out_metapath;
  1064. gfs2_holder_mark_uninitialized(&rd_gh);
  1065. mp_h = strip_h;
  1066. while (state != DEALLOC_DONE) {
  1067. switch (state) {
  1068. /* Truncate a full metapath at the given strip height.
  1069. * Note that strip_h == mp_h in order to be in this state. */
  1070. case DEALLOC_MP_FULL:
  1071. if (mp_h > 0) { /* issue read-ahead on metadata */
  1072. __be64 *top;
  1073. bh = mp.mp_bh[mp_h - 1];
  1074. if (bh->b_blocknr != last_ra) {
  1075. last_ra = bh->b_blocknr;
  1076. top = metaptr1(mp_h - 1, &mp);
  1077. gfs2_metapath_ra(ip->i_gl, bh, top);
  1078. }
  1079. }
  1080. /* If we're truncating to a non-zero size and the mp is
  1081. at the beginning of file for the strip height, we
  1082. need to preserve the first metadata pointer. */
  1083. preserve1 = (newsize && mp_eq_to_hgt(&mp, nbof, mp_h));
  1084. bh = mp.mp_bh[mp_h];
  1085. gfs2_assert_withdraw(sdp, bh);
  1086. if (gfs2_assert_withdraw(sdp,
  1087. prev_bnr != bh->b_blocknr)) {
  1088. printk(KERN_EMERG "GFS2: fsid=%s:inode %llu, "
  1089. "block:%llu, i_h:%u, s_h:%u, mp_h:%u\n",
  1090. sdp->sd_fsname,
  1091. (unsigned long long)ip->i_no_addr,
  1092. prev_bnr, ip->i_height, strip_h, mp_h);
  1093. }
  1094. prev_bnr = bh->b_blocknr;
  1095. ret = sweep_bh_for_rgrps(ip, &rd_gh, &mp, &btotal,
  1096. mp_h, preserve1);
  1097. /* If we hit an error or just swept dinode buffer,
  1098. just exit. */
  1099. if (ret || !mp_h) {
  1100. state = DEALLOC_DONE;
  1101. break;
  1102. }
  1103. state = DEALLOC_MP_LOWER;
  1104. break;
  1105. /* lower the metapath strip height */
  1106. case DEALLOC_MP_LOWER:
  1107. /* We're done with the current buffer, so release it,
  1108. unless it's the dinode buffer. Then back up to the
  1109. previous pointer. */
  1110. if (mp_h) {
  1111. brelse(mp.mp_bh[mp_h]);
  1112. mp.mp_bh[mp_h] = NULL;
  1113. }
  1114. /* If we can't get any lower in height, we've stripped
  1115. off all we can. Next step is to back up and start
  1116. stripping the previous level of metadata. */
  1117. if (mp_h == 0) {
  1118. strip_h--;
  1119. memcpy(&mp.mp_list, &nbof, sizeof(nbof));
  1120. mp_h = strip_h;
  1121. state = DEALLOC_FILL_MP;
  1122. break;
  1123. }
  1124. mp.mp_list[mp_h] = 0;
  1125. mp_h--; /* search one metadata height down */
  1126. if (mp.mp_list[mp_h] >= hptrs(sdp, mp_h) - 1)
  1127. break; /* loop around in the same state */
  1128. mp.mp_list[mp_h]++;
  1129. /* Here we've found a part of the metapath that is not
  1130. * allocated. We need to search at that height for the
  1131. * next non-null pointer. */
  1132. if (find_nonnull_ptr(sdp, &mp, mp_h)) {
  1133. state = DEALLOC_FILL_MP;
  1134. mp_h++;
  1135. }
  1136. /* No more non-null pointers at this height. Back up
  1137. to the previous height and try again. */
  1138. break; /* loop around in the same state */
  1139. /* Fill the metapath with buffers to the given height. */
  1140. case DEALLOC_FILL_MP:
  1141. /* Fill the buffers out to the current height. */
  1142. ret = fillup_metapath(ip, &mp, mp_h);
  1143. if (ret < 0)
  1144. goto out;
  1145. /* If buffers found for the entire strip height */
  1146. if ((ret == ip->i_height) && (mp_h == strip_h)) {
  1147. state = DEALLOC_MP_FULL;
  1148. break;
  1149. }
  1150. if (ret < ip->i_height) /* We have a partial height */
  1151. mp_h = ret - 1;
  1152. /* If we find a non-null block pointer, crawl a bit
  1153. higher up in the metapath and try again, otherwise
  1154. we need to look lower for a new starting point. */
  1155. if (find_nonnull_ptr(sdp, &mp, mp_h))
  1156. mp_h++;
  1157. else
  1158. state = DEALLOC_MP_LOWER;
  1159. break;
  1160. }
  1161. }
  1162. if (btotal) {
  1163. if (current->journal_info == NULL) {
  1164. ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS +
  1165. RES_QUOTA, 0);
  1166. if (ret)
  1167. goto out;
  1168. down_write(&ip->i_rw_mutex);
  1169. }
  1170. gfs2_statfs_change(sdp, 0, +btotal, 0);
  1171. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  1172. ip->i_inode.i_gid);
  1173. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1174. gfs2_trans_add_meta(ip->i_gl, dibh);
  1175. gfs2_dinode_out(ip, dibh->b_data);
  1176. up_write(&ip->i_rw_mutex);
  1177. gfs2_trans_end(sdp);
  1178. }
  1179. out:
  1180. if (gfs2_holder_initialized(&rd_gh))
  1181. gfs2_glock_dq_uninit(&rd_gh);
  1182. if (current->journal_info) {
  1183. up_write(&ip->i_rw_mutex);
  1184. gfs2_trans_end(sdp);
  1185. cond_resched();
  1186. }
  1187. gfs2_quota_unhold(ip);
  1188. out_metapath:
  1189. release_metapath(&mp);
  1190. return ret;
  1191. }
  1192. static int trunc_end(struct gfs2_inode *ip)
  1193. {
  1194. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1195. struct buffer_head *dibh;
  1196. int error;
  1197. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1198. if (error)
  1199. return error;
  1200. down_write(&ip->i_rw_mutex);
  1201. error = gfs2_meta_inode_buffer(ip, &dibh);
  1202. if (error)
  1203. goto out;
  1204. if (!i_size_read(&ip->i_inode)) {
  1205. ip->i_height = 0;
  1206. ip->i_goal = ip->i_no_addr;
  1207. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  1208. gfs2_ordered_del_inode(ip);
  1209. }
  1210. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1211. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  1212. gfs2_trans_add_meta(ip->i_gl, dibh);
  1213. gfs2_dinode_out(ip, dibh->b_data);
  1214. brelse(dibh);
  1215. out:
  1216. up_write(&ip->i_rw_mutex);
  1217. gfs2_trans_end(sdp);
  1218. return error;
  1219. }
  1220. /**
  1221. * do_shrink - make a file smaller
  1222. * @inode: the inode
  1223. * @oldsize: the current inode size
  1224. * @newsize: the size to make the file
  1225. *
  1226. * Called with an exclusive lock on @inode. The @size must
  1227. * be equal to or smaller than the current inode size.
  1228. *
  1229. * Returns: errno
  1230. */
  1231. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  1232. {
  1233. struct gfs2_inode *ip = GFS2_I(inode);
  1234. int error;
  1235. error = trunc_start(inode, oldsize, newsize);
  1236. if (error < 0)
  1237. return error;
  1238. if (gfs2_is_stuffed(ip))
  1239. return 0;
  1240. error = trunc_dealloc(ip, newsize);
  1241. if (error == 0)
  1242. error = trunc_end(ip);
  1243. return error;
  1244. }
  1245. void gfs2_trim_blocks(struct inode *inode)
  1246. {
  1247. u64 size = inode->i_size;
  1248. int ret;
  1249. ret = do_shrink(inode, size, size);
  1250. WARN_ON(ret != 0);
  1251. }
  1252. /**
  1253. * do_grow - Touch and update inode size
  1254. * @inode: The inode
  1255. * @size: The new size
  1256. *
  1257. * This function updates the timestamps on the inode and
  1258. * may also increase the size of the inode. This function
  1259. * must not be called with @size any smaller than the current
  1260. * inode size.
  1261. *
  1262. * Although it is not strictly required to unstuff files here,
  1263. * earlier versions of GFS2 have a bug in the stuffed file reading
  1264. * code which will result in a buffer overrun if the size is larger
  1265. * than the max stuffed file size. In order to prevent this from
  1266. * occurring, such files are unstuffed, but in other cases we can
  1267. * just update the inode size directly.
  1268. *
  1269. * Returns: 0 on success, or -ve on error
  1270. */
  1271. static int do_grow(struct inode *inode, u64 size)
  1272. {
  1273. struct gfs2_inode *ip = GFS2_I(inode);
  1274. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1275. struct gfs2_alloc_parms ap = { .target = 1, };
  1276. struct buffer_head *dibh;
  1277. int error;
  1278. int unstuff = 0;
  1279. if (gfs2_is_stuffed(ip) &&
  1280. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1281. error = gfs2_quota_lock_check(ip, &ap);
  1282. if (error)
  1283. return error;
  1284. error = gfs2_inplace_reserve(ip, &ap);
  1285. if (error)
  1286. goto do_grow_qunlock;
  1287. unstuff = 1;
  1288. }
  1289. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT +
  1290. (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ?
  1291. 0 : RES_QUOTA), 0);
  1292. if (error)
  1293. goto do_grow_release;
  1294. if (unstuff) {
  1295. error = gfs2_unstuff_dinode(ip, NULL);
  1296. if (error)
  1297. goto do_end_trans;
  1298. }
  1299. error = gfs2_meta_inode_buffer(ip, &dibh);
  1300. if (error)
  1301. goto do_end_trans;
  1302. i_size_write(inode, size);
  1303. ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
  1304. gfs2_trans_add_meta(ip->i_gl, dibh);
  1305. gfs2_dinode_out(ip, dibh->b_data);
  1306. brelse(dibh);
  1307. do_end_trans:
  1308. gfs2_trans_end(sdp);
  1309. do_grow_release:
  1310. if (unstuff) {
  1311. gfs2_inplace_release(ip);
  1312. do_grow_qunlock:
  1313. gfs2_quota_unlock(ip);
  1314. }
  1315. return error;
  1316. }
  1317. /**
  1318. * gfs2_setattr_size - make a file a given size
  1319. * @inode: the inode
  1320. * @newsize: the size to make the file
  1321. *
  1322. * The file size can grow, shrink, or stay the same size. This
  1323. * is called holding i_mutex and an exclusive glock on the inode
  1324. * in question.
  1325. *
  1326. * Returns: errno
  1327. */
  1328. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1329. {
  1330. struct gfs2_inode *ip = GFS2_I(inode);
  1331. int ret;
  1332. u64 oldsize;
  1333. BUG_ON(!S_ISREG(inode->i_mode));
  1334. ret = inode_newsize_ok(inode, newsize);
  1335. if (ret)
  1336. return ret;
  1337. inode_dio_wait(inode);
  1338. ret = gfs2_rsqa_alloc(ip);
  1339. if (ret)
  1340. goto out;
  1341. oldsize = inode->i_size;
  1342. if (newsize >= oldsize) {
  1343. ret = do_grow(inode, newsize);
  1344. goto out;
  1345. }
  1346. ret = do_shrink(inode, oldsize, newsize);
  1347. out:
  1348. gfs2_rsqa_delete(ip, NULL);
  1349. return ret;
  1350. }
  1351. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1352. {
  1353. int error;
  1354. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1355. if (!error)
  1356. error = trunc_end(ip);
  1357. return error;
  1358. }
  1359. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1360. {
  1361. return trunc_dealloc(ip, 0);
  1362. }
  1363. /**
  1364. * gfs2_free_journal_extents - Free cached journal bmap info
  1365. * @jd: The journal
  1366. *
  1367. */
  1368. void gfs2_free_journal_extents(struct gfs2_jdesc *jd)
  1369. {
  1370. struct gfs2_journal_extent *jext;
  1371. while(!list_empty(&jd->extent_list)) {
  1372. jext = list_entry(jd->extent_list.next, struct gfs2_journal_extent, list);
  1373. list_del(&jext->list);
  1374. kfree(jext);
  1375. }
  1376. }
  1377. /**
  1378. * gfs2_add_jextent - Add or merge a new extent to extent cache
  1379. * @jd: The journal descriptor
  1380. * @lblock: The logical block at start of new extent
  1381. * @dblock: The physical block at start of new extent
  1382. * @blocks: Size of extent in fs blocks
  1383. *
  1384. * Returns: 0 on success or -ENOMEM
  1385. */
  1386. static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks)
  1387. {
  1388. struct gfs2_journal_extent *jext;
  1389. if (!list_empty(&jd->extent_list)) {
  1390. jext = list_entry(jd->extent_list.prev, struct gfs2_journal_extent, list);
  1391. if ((jext->dblock + jext->blocks) == dblock) {
  1392. jext->blocks += blocks;
  1393. return 0;
  1394. }
  1395. }
  1396. jext = kzalloc(sizeof(struct gfs2_journal_extent), GFP_NOFS);
  1397. if (jext == NULL)
  1398. return -ENOMEM;
  1399. jext->dblock = dblock;
  1400. jext->lblock = lblock;
  1401. jext->blocks = blocks;
  1402. list_add_tail(&jext->list, &jd->extent_list);
  1403. jd->nr_extents++;
  1404. return 0;
  1405. }
  1406. /**
  1407. * gfs2_map_journal_extents - Cache journal bmap info
  1408. * @sdp: The super block
  1409. * @jd: The journal to map
  1410. *
  1411. * Create a reusable "extent" mapping from all logical
  1412. * blocks to all physical blocks for the given journal. This will save
  1413. * us time when writing journal blocks. Most journals will have only one
  1414. * extent that maps all their logical blocks. That's because gfs2.mkfs
  1415. * arranges the journal blocks sequentially to maximize performance.
  1416. * So the extent would map the first block for the entire file length.
  1417. * However, gfs2_jadd can happen while file activity is happening, so
  1418. * those journals may not be sequential. Less likely is the case where
  1419. * the users created their own journals by mounting the metafs and
  1420. * laying it out. But it's still possible. These journals might have
  1421. * several extents.
  1422. *
  1423. * Returns: 0 on success, or error on failure
  1424. */
  1425. int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd)
  1426. {
  1427. u64 lblock = 0;
  1428. u64 lblock_stop;
  1429. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  1430. struct buffer_head bh;
  1431. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  1432. u64 size;
  1433. int rc;
  1434. lblock_stop = i_size_read(jd->jd_inode) >> shift;
  1435. size = (lblock_stop - lblock) << shift;
  1436. jd->nr_extents = 0;
  1437. WARN_ON(!list_empty(&jd->extent_list));
  1438. do {
  1439. bh.b_state = 0;
  1440. bh.b_blocknr = 0;
  1441. bh.b_size = size;
  1442. rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0);
  1443. if (rc || !buffer_mapped(&bh))
  1444. goto fail;
  1445. rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift);
  1446. if (rc)
  1447. goto fail;
  1448. size -= bh.b_size;
  1449. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1450. } while(size > 0);
  1451. fs_info(sdp, "journal %d mapped with %u extents\n", jd->jd_jid,
  1452. jd->nr_extents);
  1453. return 0;
  1454. fail:
  1455. fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n",
  1456. rc, jd->jd_jid,
  1457. (unsigned long long)(i_size_read(jd->jd_inode) - size),
  1458. jd->nr_extents);
  1459. fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n",
  1460. rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr,
  1461. bh.b_state, (unsigned long long)bh.b_size);
  1462. gfs2_free_journal_extents(jd);
  1463. return rc;
  1464. }
  1465. /**
  1466. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1467. * @ip: the file being written to
  1468. * @offset: the offset to write to
  1469. * @len: the number of bytes being written
  1470. *
  1471. * Returns: 1 if an alloc is required, 0 otherwise
  1472. */
  1473. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1474. unsigned int len)
  1475. {
  1476. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1477. struct buffer_head bh;
  1478. unsigned int shift;
  1479. u64 lblock, lblock_stop, size;
  1480. u64 end_of_file;
  1481. if (!len)
  1482. return 0;
  1483. if (gfs2_is_stuffed(ip)) {
  1484. if (offset + len >
  1485. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1486. return 1;
  1487. return 0;
  1488. }
  1489. shift = sdp->sd_sb.sb_bsize_shift;
  1490. BUG_ON(gfs2_is_dir(ip));
  1491. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1492. lblock = offset >> shift;
  1493. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1494. if (lblock_stop > end_of_file)
  1495. return 1;
  1496. size = (lblock_stop - lblock) << shift;
  1497. do {
  1498. bh.b_state = 0;
  1499. bh.b_size = size;
  1500. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1501. if (!buffer_mapped(&bh))
  1502. return 1;
  1503. size -= bh.b_size;
  1504. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1505. } while(size > 0);
  1506. return 0;
  1507. }