bmap.c 30 KB

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