bmap.c 31 KB

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