bmap.c 29 KB

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