inode.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. * linux/fs/ext2/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@dcs.ed.ac.uk), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/time.h>
  25. #include <linux/highuid.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/module.h>
  29. #include <linux/writeback.h>
  30. #include <linux/buffer_head.h>
  31. #include <linux/mpage.h>
  32. #include <linux/fiemap.h>
  33. #include <linux/namei.h>
  34. #include "ext2.h"
  35. #include "acl.h"
  36. #include "xip.h"
  37. MODULE_AUTHOR("Remy Card and others");
  38. MODULE_DESCRIPTION("Second Extended Filesystem");
  39. MODULE_LICENSE("GPL");
  40. static int __ext2_write_inode(struct inode *inode, int do_sync);
  41. /*
  42. * Test whether an inode is a fast symlink.
  43. */
  44. static inline int ext2_inode_is_fast_symlink(struct inode *inode)
  45. {
  46. int ea_blocks = EXT2_I(inode)->i_file_acl ?
  47. (inode->i_sb->s_blocksize >> 9) : 0;
  48. return (S_ISLNK(inode->i_mode) &&
  49. inode->i_blocks - ea_blocks == 0);
  50. }
  51. static void ext2_truncate_blocks(struct inode *inode, loff_t offset);
  52. static void ext2_write_failed(struct address_space *mapping, loff_t to)
  53. {
  54. struct inode *inode = mapping->host;
  55. if (to > inode->i_size) {
  56. truncate_pagecache(inode, to, inode->i_size);
  57. ext2_truncate_blocks(inode, inode->i_size);
  58. }
  59. }
  60. /*
  61. * Called at the last iput() if i_nlink is zero.
  62. */
  63. void ext2_delete_inode (struct inode * inode)
  64. {
  65. if (!is_bad_inode(inode))
  66. dquot_initialize(inode);
  67. truncate_inode_pages(&inode->i_data, 0);
  68. if (is_bad_inode(inode))
  69. goto no_delete;
  70. EXT2_I(inode)->i_dtime = get_seconds();
  71. mark_inode_dirty(inode);
  72. __ext2_write_inode(inode, inode_needs_sync(inode));
  73. inode->i_size = 0;
  74. if (inode->i_blocks)
  75. ext2_truncate_blocks(inode, 0);
  76. ext2_free_inode (inode);
  77. return;
  78. no_delete:
  79. clear_inode(inode); /* We must guarantee clearing of inode... */
  80. }
  81. typedef struct {
  82. __le32 *p;
  83. __le32 key;
  84. struct buffer_head *bh;
  85. } Indirect;
  86. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  87. {
  88. p->key = *(p->p = v);
  89. p->bh = bh;
  90. }
  91. static inline int verify_chain(Indirect *from, Indirect *to)
  92. {
  93. while (from <= to && from->key == *from->p)
  94. from++;
  95. return (from > to);
  96. }
  97. /**
  98. * ext2_block_to_path - parse the block number into array of offsets
  99. * @inode: inode in question (we are only interested in its superblock)
  100. * @i_block: block number to be parsed
  101. * @offsets: array to store the offsets in
  102. * @boundary: set this non-zero if the referred-to block is likely to be
  103. * followed (on disk) by an indirect block.
  104. * To store the locations of file's data ext2 uses a data structure common
  105. * for UNIX filesystems - tree of pointers anchored in the inode, with
  106. * data blocks at leaves and indirect blocks in intermediate nodes.
  107. * This function translates the block number into path in that tree -
  108. * return value is the path length and @offsets[n] is the offset of
  109. * pointer to (n+1)th node in the nth one. If @block is out of range
  110. * (negative or too large) warning is printed and zero returned.
  111. *
  112. * Note: function doesn't find node addresses, so no IO is needed. All
  113. * we need to know is the capacity of indirect blocks (taken from the
  114. * inode->i_sb).
  115. */
  116. /*
  117. * Portability note: the last comparison (check that we fit into triple
  118. * indirect block) is spelled differently, because otherwise on an
  119. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  120. * if our filesystem had 8Kb blocks. We might use long long, but that would
  121. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  122. * i_block would have to be negative in the very beginning, so we would not
  123. * get there at all.
  124. */
  125. static int ext2_block_to_path(struct inode *inode,
  126. long i_block, int offsets[4], int *boundary)
  127. {
  128. int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  129. int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
  130. const long direct_blocks = EXT2_NDIR_BLOCKS,
  131. indirect_blocks = ptrs,
  132. double_blocks = (1 << (ptrs_bits * 2));
  133. int n = 0;
  134. int final = 0;
  135. if (i_block < 0) {
  136. ext2_msg(inode->i_sb, KERN_WARNING,
  137. "warning: %s: block < 0", __func__);
  138. } else if (i_block < direct_blocks) {
  139. offsets[n++] = i_block;
  140. final = direct_blocks;
  141. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  142. offsets[n++] = EXT2_IND_BLOCK;
  143. offsets[n++] = i_block;
  144. final = ptrs;
  145. } else if ((i_block -= indirect_blocks) < double_blocks) {
  146. offsets[n++] = EXT2_DIND_BLOCK;
  147. offsets[n++] = i_block >> ptrs_bits;
  148. offsets[n++] = i_block & (ptrs - 1);
  149. final = ptrs;
  150. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  151. offsets[n++] = EXT2_TIND_BLOCK;
  152. offsets[n++] = i_block >> (ptrs_bits * 2);
  153. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  154. offsets[n++] = i_block & (ptrs - 1);
  155. final = ptrs;
  156. } else {
  157. ext2_msg(inode->i_sb, KERN_WARNING,
  158. "warning: %s: block is too big", __func__);
  159. }
  160. if (boundary)
  161. *boundary = final - 1 - (i_block & (ptrs - 1));
  162. return n;
  163. }
  164. /**
  165. * ext2_get_branch - read the chain of indirect blocks leading to data
  166. * @inode: inode in question
  167. * @depth: depth of the chain (1 - direct pointer, etc.)
  168. * @offsets: offsets of pointers in inode/indirect blocks
  169. * @chain: place to store the result
  170. * @err: here we store the error value
  171. *
  172. * Function fills the array of triples <key, p, bh> and returns %NULL
  173. * if everything went OK or the pointer to the last filled triple
  174. * (incomplete one) otherwise. Upon the return chain[i].key contains
  175. * the number of (i+1)-th block in the chain (as it is stored in memory,
  176. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  177. * number (it points into struct inode for i==0 and into the bh->b_data
  178. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  179. * block for i>0 and NULL for i==0. In other words, it holds the block
  180. * numbers of the chain, addresses they were taken from (and where we can
  181. * verify that chain did not change) and buffer_heads hosting these
  182. * numbers.
  183. *
  184. * Function stops when it stumbles upon zero pointer (absent block)
  185. * (pointer to last triple returned, *@err == 0)
  186. * or when it gets an IO error reading an indirect block
  187. * (ditto, *@err == -EIO)
  188. * or when it notices that chain had been changed while it was reading
  189. * (ditto, *@err == -EAGAIN)
  190. * or when it reads all @depth-1 indirect blocks successfully and finds
  191. * the whole chain, all way to the data (returns %NULL, *err == 0).
  192. */
  193. static Indirect *ext2_get_branch(struct inode *inode,
  194. int depth,
  195. int *offsets,
  196. Indirect chain[4],
  197. int *err)
  198. {
  199. struct super_block *sb = inode->i_sb;
  200. Indirect *p = chain;
  201. struct buffer_head *bh;
  202. *err = 0;
  203. /* i_data is not going away, no lock needed */
  204. add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
  205. if (!p->key)
  206. goto no_block;
  207. while (--depth) {
  208. bh = sb_bread(sb, le32_to_cpu(p->key));
  209. if (!bh)
  210. goto failure;
  211. read_lock(&EXT2_I(inode)->i_meta_lock);
  212. if (!verify_chain(chain, p))
  213. goto changed;
  214. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  215. read_unlock(&EXT2_I(inode)->i_meta_lock);
  216. if (!p->key)
  217. goto no_block;
  218. }
  219. return NULL;
  220. changed:
  221. read_unlock(&EXT2_I(inode)->i_meta_lock);
  222. brelse(bh);
  223. *err = -EAGAIN;
  224. goto no_block;
  225. failure:
  226. *err = -EIO;
  227. no_block:
  228. return p;
  229. }
  230. /**
  231. * ext2_find_near - find a place for allocation with sufficient locality
  232. * @inode: owner
  233. * @ind: descriptor of indirect block.
  234. *
  235. * This function returns the preferred place for block allocation.
  236. * It is used when heuristic for sequential allocation fails.
  237. * Rules are:
  238. * + if there is a block to the left of our position - allocate near it.
  239. * + if pointer will live in indirect block - allocate near that block.
  240. * + if pointer will live in inode - allocate in the same cylinder group.
  241. *
  242. * In the latter case we colour the starting block by the callers PID to
  243. * prevent it from clashing with concurrent allocations for a different inode
  244. * in the same block group. The PID is used here so that functionally related
  245. * files will be close-by on-disk.
  246. *
  247. * Caller must make sure that @ind is valid and will stay that way.
  248. */
  249. static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
  250. {
  251. struct ext2_inode_info *ei = EXT2_I(inode);
  252. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  253. __le32 *p;
  254. ext2_fsblk_t bg_start;
  255. ext2_fsblk_t colour;
  256. /* Try to find previous block */
  257. for (p = ind->p - 1; p >= start; p--)
  258. if (*p)
  259. return le32_to_cpu(*p);
  260. /* No such thing, so let's try location of indirect block */
  261. if (ind->bh)
  262. return ind->bh->b_blocknr;
  263. /*
  264. * It is going to be refered from inode itself? OK, just put it into
  265. * the same cylinder group then.
  266. */
  267. bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
  268. colour = (current->pid % 16) *
  269. (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  270. return bg_start + colour;
  271. }
  272. /**
  273. * ext2_find_goal - find a preferred place for allocation.
  274. * @inode: owner
  275. * @block: block we want
  276. * @partial: pointer to the last triple within a chain
  277. *
  278. * Returns preferred place for a block (the goal).
  279. */
  280. static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
  281. Indirect *partial)
  282. {
  283. struct ext2_block_alloc_info *block_i;
  284. block_i = EXT2_I(inode)->i_block_alloc_info;
  285. /*
  286. * try the heuristic for sequential allocation,
  287. * failing that at least try to get decent locality.
  288. */
  289. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  290. && (block_i->last_alloc_physical_block != 0)) {
  291. return block_i->last_alloc_physical_block + 1;
  292. }
  293. return ext2_find_near(inode, partial);
  294. }
  295. /**
  296. * ext2_blks_to_allocate: Look up the block map and count the number
  297. * of direct blocks need to be allocated for the given branch.
  298. *
  299. * @branch: chain of indirect blocks
  300. * @k: number of blocks need for indirect blocks
  301. * @blks: number of data blocks to be mapped.
  302. * @blocks_to_boundary: the offset in the indirect block
  303. *
  304. * return the total number of blocks to be allocate, including the
  305. * direct and indirect blocks.
  306. */
  307. static int
  308. ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
  309. int blocks_to_boundary)
  310. {
  311. unsigned long count = 0;
  312. /*
  313. * Simple case, [t,d]Indirect block(s) has not allocated yet
  314. * then it's clear blocks on that path have not allocated
  315. */
  316. if (k > 0) {
  317. /* right now don't hanel cross boundary allocation */
  318. if (blks < blocks_to_boundary + 1)
  319. count += blks;
  320. else
  321. count += blocks_to_boundary + 1;
  322. return count;
  323. }
  324. count++;
  325. while (count < blks && count <= blocks_to_boundary
  326. && le32_to_cpu(*(branch[0].p + count)) == 0) {
  327. count++;
  328. }
  329. return count;
  330. }
  331. /**
  332. * ext2_alloc_blocks: multiple allocate blocks needed for a branch
  333. * @indirect_blks: the number of blocks need to allocate for indirect
  334. * blocks
  335. *
  336. * @new_blocks: on return it will store the new block numbers for
  337. * the indirect blocks(if needed) and the first direct block,
  338. * @blks: on return it will store the total number of allocated
  339. * direct blocks
  340. */
  341. static int ext2_alloc_blocks(struct inode *inode,
  342. ext2_fsblk_t goal, int indirect_blks, int blks,
  343. ext2_fsblk_t new_blocks[4], int *err)
  344. {
  345. int target, i;
  346. unsigned long count = 0;
  347. int index = 0;
  348. ext2_fsblk_t current_block = 0;
  349. int ret = 0;
  350. /*
  351. * Here we try to allocate the requested multiple blocks at once,
  352. * on a best-effort basis.
  353. * To build a branch, we should allocate blocks for
  354. * the indirect blocks(if not allocated yet), and at least
  355. * the first direct block of this branch. That's the
  356. * minimum number of blocks need to allocate(required)
  357. */
  358. target = blks + indirect_blks;
  359. while (1) {
  360. count = target;
  361. /* allocating blocks for indirect blocks and direct blocks */
  362. current_block = ext2_new_blocks(inode,goal,&count,err);
  363. if (*err)
  364. goto failed_out;
  365. target -= count;
  366. /* allocate blocks for indirect blocks */
  367. while (index < indirect_blks && count) {
  368. new_blocks[index++] = current_block++;
  369. count--;
  370. }
  371. if (count > 0)
  372. break;
  373. }
  374. /* save the new block number for the first direct block */
  375. new_blocks[index] = current_block;
  376. /* total number of blocks allocated for direct blocks */
  377. ret = count;
  378. *err = 0;
  379. return ret;
  380. failed_out:
  381. for (i = 0; i <index; i++)
  382. ext2_free_blocks(inode, new_blocks[i], 1);
  383. return ret;
  384. }
  385. /**
  386. * ext2_alloc_branch - allocate and set up a chain of blocks.
  387. * @inode: owner
  388. * @num: depth of the chain (number of blocks to allocate)
  389. * @offsets: offsets (in the blocks) to store the pointers to next.
  390. * @branch: place to store the chain in.
  391. *
  392. * This function allocates @num blocks, zeroes out all but the last one,
  393. * links them into chain and (if we are synchronous) writes them to disk.
  394. * In other words, it prepares a branch that can be spliced onto the
  395. * inode. It stores the information about that chain in the branch[], in
  396. * the same format as ext2_get_branch() would do. We are calling it after
  397. * we had read the existing part of chain and partial points to the last
  398. * triple of that (one with zero ->key). Upon the exit we have the same
  399. * picture as after the successful ext2_get_block(), excpet that in one
  400. * place chain is disconnected - *branch->p is still zero (we did not
  401. * set the last link), but branch->key contains the number that should
  402. * be placed into *branch->p to fill that gap.
  403. *
  404. * If allocation fails we free all blocks we've allocated (and forget
  405. * their buffer_heads) and return the error value the from failed
  406. * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  407. * as described above and return 0.
  408. */
  409. static int ext2_alloc_branch(struct inode *inode,
  410. int indirect_blks, int *blks, ext2_fsblk_t goal,
  411. int *offsets, Indirect *branch)
  412. {
  413. int blocksize = inode->i_sb->s_blocksize;
  414. int i, n = 0;
  415. int err = 0;
  416. struct buffer_head *bh;
  417. int num;
  418. ext2_fsblk_t new_blocks[4];
  419. ext2_fsblk_t current_block;
  420. num = ext2_alloc_blocks(inode, goal, indirect_blks,
  421. *blks, new_blocks, &err);
  422. if (err)
  423. return err;
  424. branch[0].key = cpu_to_le32(new_blocks[0]);
  425. /*
  426. * metadata blocks and data blocks are allocated.
  427. */
  428. for (n = 1; n <= indirect_blks; n++) {
  429. /*
  430. * Get buffer_head for parent block, zero it out
  431. * and set the pointer to new one, then send
  432. * parent to disk.
  433. */
  434. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  435. branch[n].bh = bh;
  436. lock_buffer(bh);
  437. memset(bh->b_data, 0, blocksize);
  438. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  439. branch[n].key = cpu_to_le32(new_blocks[n]);
  440. *branch[n].p = branch[n].key;
  441. if ( n == indirect_blks) {
  442. current_block = new_blocks[n];
  443. /*
  444. * End of chain, update the last new metablock of
  445. * the chain to point to the new allocated
  446. * data blocks numbers
  447. */
  448. for (i=1; i < num; i++)
  449. *(branch[n].p + i) = cpu_to_le32(++current_block);
  450. }
  451. set_buffer_uptodate(bh);
  452. unlock_buffer(bh);
  453. mark_buffer_dirty_inode(bh, inode);
  454. /* We used to sync bh here if IS_SYNC(inode).
  455. * But we now rely upon generic_write_sync()
  456. * and b_inode_buffers. But not for directories.
  457. */
  458. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  459. sync_dirty_buffer(bh);
  460. }
  461. *blks = num;
  462. return err;
  463. }
  464. /**
  465. * ext2_splice_branch - splice the allocated branch onto inode.
  466. * @inode: owner
  467. * @block: (logical) number of block we are adding
  468. * @where: location of missing link
  469. * @num: number of indirect blocks we are adding
  470. * @blks: number of direct blocks we are adding
  471. *
  472. * This function fills the missing link and does all housekeeping needed in
  473. * inode (->i_blocks, etc.). In case of success we end up with the full
  474. * chain to new block and return 0.
  475. */
  476. static void ext2_splice_branch(struct inode *inode,
  477. long block, Indirect *where, int num, int blks)
  478. {
  479. int i;
  480. struct ext2_block_alloc_info *block_i;
  481. ext2_fsblk_t current_block;
  482. block_i = EXT2_I(inode)->i_block_alloc_info;
  483. /* XXX LOCKING probably should have i_meta_lock ?*/
  484. /* That's it */
  485. *where->p = where->key;
  486. /*
  487. * Update the host buffer_head or inode to point to more just allocated
  488. * direct blocks blocks
  489. */
  490. if (num == 0 && blks > 1) {
  491. current_block = le32_to_cpu(where->key) + 1;
  492. for (i = 1; i < blks; i++)
  493. *(where->p + i ) = cpu_to_le32(current_block++);
  494. }
  495. /*
  496. * update the most recently allocated logical & physical block
  497. * in i_block_alloc_info, to assist find the proper goal block for next
  498. * allocation
  499. */
  500. if (block_i) {
  501. block_i->last_alloc_logical_block = block + blks - 1;
  502. block_i->last_alloc_physical_block =
  503. le32_to_cpu(where[num].key) + blks - 1;
  504. }
  505. /* We are done with atomic stuff, now do the rest of housekeeping */
  506. /* had we spliced it onto indirect block? */
  507. if (where->bh)
  508. mark_buffer_dirty_inode(where->bh, inode);
  509. inode->i_ctime = CURRENT_TIME_SEC;
  510. mark_inode_dirty(inode);
  511. }
  512. /*
  513. * Allocation strategy is simple: if we have to allocate something, we will
  514. * have to go the whole way to leaf. So let's do it before attaching anything
  515. * to tree, set linkage between the newborn blocks, write them if sync is
  516. * required, recheck the path, free and repeat if check fails, otherwise
  517. * set the last missing link (that will protect us from any truncate-generated
  518. * removals - all blocks on the path are immune now) and possibly force the
  519. * write on the parent block.
  520. * That has a nice additional property: no special recovery from the failed
  521. * allocations is needed - we simply release blocks and do not touch anything
  522. * reachable from inode.
  523. *
  524. * `handle' can be NULL if create == 0.
  525. *
  526. * return > 0, # of blocks mapped or allocated.
  527. * return = 0, if plain lookup failed.
  528. * return < 0, error case.
  529. */
  530. static int ext2_get_blocks(struct inode *inode,
  531. sector_t iblock, unsigned long maxblocks,
  532. struct buffer_head *bh_result,
  533. int create)
  534. {
  535. int err = -EIO;
  536. int offsets[4];
  537. Indirect chain[4];
  538. Indirect *partial;
  539. ext2_fsblk_t goal;
  540. int indirect_blks;
  541. int blocks_to_boundary = 0;
  542. int depth;
  543. struct ext2_inode_info *ei = EXT2_I(inode);
  544. int count = 0;
  545. ext2_fsblk_t first_block = 0;
  546. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  547. if (depth == 0)
  548. return (err);
  549. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  550. /* Simplest case - block found, no allocation needed */
  551. if (!partial) {
  552. first_block = le32_to_cpu(chain[depth - 1].key);
  553. clear_buffer_new(bh_result); /* What's this do? */
  554. count++;
  555. /*map more blocks*/
  556. while (count < maxblocks && count <= blocks_to_boundary) {
  557. ext2_fsblk_t blk;
  558. if (!verify_chain(chain, chain + depth - 1)) {
  559. /*
  560. * Indirect block might be removed by
  561. * truncate while we were reading it.
  562. * Handling of that case: forget what we've
  563. * got now, go to reread.
  564. */
  565. err = -EAGAIN;
  566. count = 0;
  567. break;
  568. }
  569. blk = le32_to_cpu(*(chain[depth-1].p + count));
  570. if (blk == first_block + count)
  571. count++;
  572. else
  573. break;
  574. }
  575. if (err != -EAGAIN)
  576. goto got_it;
  577. }
  578. /* Next simple case - plain lookup or failed read of indirect block */
  579. if (!create || err == -EIO)
  580. goto cleanup;
  581. mutex_lock(&ei->truncate_mutex);
  582. /*
  583. * If the indirect block is missing while we are reading
  584. * the chain(ext3_get_branch() returns -EAGAIN err), or
  585. * if the chain has been changed after we grab the semaphore,
  586. * (either because another process truncated this branch, or
  587. * another get_block allocated this branch) re-grab the chain to see if
  588. * the request block has been allocated or not.
  589. *
  590. * Since we already block the truncate/other get_block
  591. * at this point, we will have the current copy of the chain when we
  592. * splice the branch into the tree.
  593. */
  594. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  595. while (partial > chain) {
  596. brelse(partial->bh);
  597. partial--;
  598. }
  599. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  600. if (!partial) {
  601. count++;
  602. mutex_unlock(&ei->truncate_mutex);
  603. if (err)
  604. goto cleanup;
  605. clear_buffer_new(bh_result);
  606. goto got_it;
  607. }
  608. }
  609. /*
  610. * Okay, we need to do block allocation. Lazily initialize the block
  611. * allocation info here if necessary
  612. */
  613. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  614. ext2_init_block_alloc_info(inode);
  615. goal = ext2_find_goal(inode, iblock, partial);
  616. /* the number of blocks need to allocate for [d,t]indirect blocks */
  617. indirect_blks = (chain + depth) - partial - 1;
  618. /*
  619. * Next look up the indirect map to count the totoal number of
  620. * direct blocks to allocate for this branch.
  621. */
  622. count = ext2_blks_to_allocate(partial, indirect_blks,
  623. maxblocks, blocks_to_boundary);
  624. /*
  625. * XXX ???? Block out ext2_truncate while we alter the tree
  626. */
  627. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  628. offsets + (partial - chain), partial);
  629. if (err) {
  630. mutex_unlock(&ei->truncate_mutex);
  631. goto cleanup;
  632. }
  633. if (ext2_use_xip(inode->i_sb)) {
  634. /*
  635. * we need to clear the block
  636. */
  637. err = ext2_clear_xip_target (inode,
  638. le32_to_cpu(chain[depth-1].key));
  639. if (err) {
  640. mutex_unlock(&ei->truncate_mutex);
  641. goto cleanup;
  642. }
  643. }
  644. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  645. mutex_unlock(&ei->truncate_mutex);
  646. set_buffer_new(bh_result);
  647. got_it:
  648. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  649. if (count > blocks_to_boundary)
  650. set_buffer_boundary(bh_result);
  651. err = count;
  652. /* Clean up and exit */
  653. partial = chain + depth - 1; /* the whole chain */
  654. cleanup:
  655. while (partial > chain) {
  656. brelse(partial->bh);
  657. partial--;
  658. }
  659. return err;
  660. }
  661. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  662. {
  663. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  664. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  665. bh_result, create);
  666. if (ret > 0) {
  667. bh_result->b_size = (ret << inode->i_blkbits);
  668. ret = 0;
  669. }
  670. return ret;
  671. }
  672. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  673. u64 start, u64 len)
  674. {
  675. return generic_block_fiemap(inode, fieinfo, start, len,
  676. ext2_get_block);
  677. }
  678. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  679. {
  680. return block_write_full_page(page, ext2_get_block, wbc);
  681. }
  682. static int ext2_readpage(struct file *file, struct page *page)
  683. {
  684. return mpage_readpage(page, ext2_get_block);
  685. }
  686. static int
  687. ext2_readpages(struct file *file, struct address_space *mapping,
  688. struct list_head *pages, unsigned nr_pages)
  689. {
  690. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  691. }
  692. int __ext2_write_begin(struct file *file, struct address_space *mapping,
  693. loff_t pos, unsigned len, unsigned flags,
  694. struct page **pagep, void **fsdata)
  695. {
  696. return block_write_begin_newtrunc(file, mapping, pos, len, flags,
  697. pagep, fsdata, ext2_get_block);
  698. }
  699. static int
  700. ext2_write_begin(struct file *file, struct address_space *mapping,
  701. loff_t pos, unsigned len, unsigned flags,
  702. struct page **pagep, void **fsdata)
  703. {
  704. int ret;
  705. *pagep = NULL;
  706. ret = __ext2_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
  707. if (ret < 0)
  708. ext2_write_failed(mapping, pos + len);
  709. return ret;
  710. }
  711. static int ext2_write_end(struct file *file, struct address_space *mapping,
  712. loff_t pos, unsigned len, unsigned copied,
  713. struct page *page, void *fsdata)
  714. {
  715. int ret;
  716. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  717. if (ret < len)
  718. ext2_write_failed(mapping, pos + len);
  719. return ret;
  720. }
  721. static int
  722. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  723. loff_t pos, unsigned len, unsigned flags,
  724. struct page **pagep, void **fsdata)
  725. {
  726. int ret;
  727. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  728. ext2_get_block);
  729. if (ret < 0)
  730. ext2_write_failed(mapping, pos + len);
  731. return ret;
  732. }
  733. static int ext2_nobh_writepage(struct page *page,
  734. struct writeback_control *wbc)
  735. {
  736. return nobh_writepage(page, ext2_get_block, wbc);
  737. }
  738. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  739. {
  740. return generic_block_bmap(mapping,block,ext2_get_block);
  741. }
  742. static ssize_t
  743. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  744. loff_t offset, unsigned long nr_segs)
  745. {
  746. struct file *file = iocb->ki_filp;
  747. struct address_space *mapping = file->f_mapping;
  748. struct inode *inode = mapping->host;
  749. ssize_t ret;
  750. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev,
  751. iov, offset, nr_segs, ext2_get_block, NULL);
  752. if (ret < 0 && (rw & WRITE))
  753. ext2_write_failed(mapping, offset + iov_length(iov, nr_segs));
  754. return ret;
  755. }
  756. static int
  757. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  758. {
  759. return mpage_writepages(mapping, wbc, ext2_get_block);
  760. }
  761. const struct address_space_operations ext2_aops = {
  762. .readpage = ext2_readpage,
  763. .readpages = ext2_readpages,
  764. .writepage = ext2_writepage,
  765. .sync_page = block_sync_page,
  766. .write_begin = ext2_write_begin,
  767. .write_end = ext2_write_end,
  768. .bmap = ext2_bmap,
  769. .direct_IO = ext2_direct_IO,
  770. .writepages = ext2_writepages,
  771. .migratepage = buffer_migrate_page,
  772. .is_partially_uptodate = block_is_partially_uptodate,
  773. .error_remove_page = generic_error_remove_page,
  774. };
  775. const struct address_space_operations ext2_aops_xip = {
  776. .bmap = ext2_bmap,
  777. .get_xip_mem = ext2_get_xip_mem,
  778. };
  779. const struct address_space_operations ext2_nobh_aops = {
  780. .readpage = ext2_readpage,
  781. .readpages = ext2_readpages,
  782. .writepage = ext2_nobh_writepage,
  783. .sync_page = block_sync_page,
  784. .write_begin = ext2_nobh_write_begin,
  785. .write_end = nobh_write_end,
  786. .bmap = ext2_bmap,
  787. .direct_IO = ext2_direct_IO,
  788. .writepages = ext2_writepages,
  789. .migratepage = buffer_migrate_page,
  790. .error_remove_page = generic_error_remove_page,
  791. };
  792. /*
  793. * Probably it should be a library function... search for first non-zero word
  794. * or memcmp with zero_page, whatever is better for particular architecture.
  795. * Linus?
  796. */
  797. static inline int all_zeroes(__le32 *p, __le32 *q)
  798. {
  799. while (p < q)
  800. if (*p++)
  801. return 0;
  802. return 1;
  803. }
  804. /**
  805. * ext2_find_shared - find the indirect blocks for partial truncation.
  806. * @inode: inode in question
  807. * @depth: depth of the affected branch
  808. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  809. * @chain: place to store the pointers to partial indirect blocks
  810. * @top: place to the (detached) top of branch
  811. *
  812. * This is a helper function used by ext2_truncate().
  813. *
  814. * When we do truncate() we may have to clean the ends of several indirect
  815. * blocks but leave the blocks themselves alive. Block is partially
  816. * truncated if some data below the new i_size is refered from it (and
  817. * it is on the path to the first completely truncated data block, indeed).
  818. * We have to free the top of that path along with everything to the right
  819. * of the path. Since no allocation past the truncation point is possible
  820. * until ext2_truncate() finishes, we may safely do the latter, but top
  821. * of branch may require special attention - pageout below the truncation
  822. * point might try to populate it.
  823. *
  824. * We atomically detach the top of branch from the tree, store the block
  825. * number of its root in *@top, pointers to buffer_heads of partially
  826. * truncated blocks - in @chain[].bh and pointers to their last elements
  827. * that should not be removed - in @chain[].p. Return value is the pointer
  828. * to last filled element of @chain.
  829. *
  830. * The work left to caller to do the actual freeing of subtrees:
  831. * a) free the subtree starting from *@top
  832. * b) free the subtrees whose roots are stored in
  833. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  834. * c) free the subtrees growing from the inode past the @chain[0].p
  835. * (no partially truncated stuff there).
  836. */
  837. static Indirect *ext2_find_shared(struct inode *inode,
  838. int depth,
  839. int offsets[4],
  840. Indirect chain[4],
  841. __le32 *top)
  842. {
  843. Indirect *partial, *p;
  844. int k, err;
  845. *top = 0;
  846. for (k = depth; k > 1 && !offsets[k-1]; k--)
  847. ;
  848. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  849. if (!partial)
  850. partial = chain + k-1;
  851. /*
  852. * If the branch acquired continuation since we've looked at it -
  853. * fine, it should all survive and (new) top doesn't belong to us.
  854. */
  855. write_lock(&EXT2_I(inode)->i_meta_lock);
  856. if (!partial->key && *partial->p) {
  857. write_unlock(&EXT2_I(inode)->i_meta_lock);
  858. goto no_top;
  859. }
  860. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  861. ;
  862. /*
  863. * OK, we've found the last block that must survive. The rest of our
  864. * branch should be detached before unlocking. However, if that rest
  865. * of branch is all ours and does not grow immediately from the inode
  866. * it's easier to cheat and just decrement partial->p.
  867. */
  868. if (p == chain + k - 1 && p > chain) {
  869. p->p--;
  870. } else {
  871. *top = *p->p;
  872. *p->p = 0;
  873. }
  874. write_unlock(&EXT2_I(inode)->i_meta_lock);
  875. while(partial > p)
  876. {
  877. brelse(partial->bh);
  878. partial--;
  879. }
  880. no_top:
  881. return partial;
  882. }
  883. /**
  884. * ext2_free_data - free a list of data blocks
  885. * @inode: inode we are dealing with
  886. * @p: array of block numbers
  887. * @q: points immediately past the end of array
  888. *
  889. * We are freeing all blocks refered from that array (numbers are
  890. * stored as little-endian 32-bit) and updating @inode->i_blocks
  891. * appropriately.
  892. */
  893. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  894. {
  895. unsigned long block_to_free = 0, count = 0;
  896. unsigned long nr;
  897. for ( ; p < q ; p++) {
  898. nr = le32_to_cpu(*p);
  899. if (nr) {
  900. *p = 0;
  901. /* accumulate blocks to free if they're contiguous */
  902. if (count == 0)
  903. goto free_this;
  904. else if (block_to_free == nr - count)
  905. count++;
  906. else {
  907. mark_inode_dirty(inode);
  908. ext2_free_blocks (inode, block_to_free, count);
  909. free_this:
  910. block_to_free = nr;
  911. count = 1;
  912. }
  913. }
  914. }
  915. if (count > 0) {
  916. mark_inode_dirty(inode);
  917. ext2_free_blocks (inode, block_to_free, count);
  918. }
  919. }
  920. /**
  921. * ext2_free_branches - free an array of branches
  922. * @inode: inode we are dealing with
  923. * @p: array of block numbers
  924. * @q: pointer immediately past the end of array
  925. * @depth: depth of the branches to free
  926. *
  927. * We are freeing all blocks refered from these branches (numbers are
  928. * stored as little-endian 32-bit) and updating @inode->i_blocks
  929. * appropriately.
  930. */
  931. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  932. {
  933. struct buffer_head * bh;
  934. unsigned long nr;
  935. if (depth--) {
  936. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  937. for ( ; p < q ; p++) {
  938. nr = le32_to_cpu(*p);
  939. if (!nr)
  940. continue;
  941. *p = 0;
  942. bh = sb_bread(inode->i_sb, nr);
  943. /*
  944. * A read failure? Report error and clear slot
  945. * (should be rare).
  946. */
  947. if (!bh) {
  948. ext2_error(inode->i_sb, "ext2_free_branches",
  949. "Read failure, inode=%ld, block=%ld",
  950. inode->i_ino, nr);
  951. continue;
  952. }
  953. ext2_free_branches(inode,
  954. (__le32*)bh->b_data,
  955. (__le32*)bh->b_data + addr_per_block,
  956. depth);
  957. bforget(bh);
  958. ext2_free_blocks(inode, nr, 1);
  959. mark_inode_dirty(inode);
  960. }
  961. } else
  962. ext2_free_data(inode, p, q);
  963. }
  964. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  965. {
  966. __le32 *i_data = EXT2_I(inode)->i_data;
  967. struct ext2_inode_info *ei = EXT2_I(inode);
  968. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  969. int offsets[4];
  970. Indirect chain[4];
  971. Indirect *partial;
  972. __le32 nr = 0;
  973. int n;
  974. long iblock;
  975. unsigned blocksize;
  976. blocksize = inode->i_sb->s_blocksize;
  977. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  978. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  979. if (n == 0)
  980. return;
  981. /*
  982. * From here we block out all ext2_get_block() callers who want to
  983. * modify the block allocation tree.
  984. */
  985. mutex_lock(&ei->truncate_mutex);
  986. if (n == 1) {
  987. ext2_free_data(inode, i_data+offsets[0],
  988. i_data + EXT2_NDIR_BLOCKS);
  989. goto do_indirects;
  990. }
  991. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  992. /* Kill the top of shared branch (already detached) */
  993. if (nr) {
  994. if (partial == chain)
  995. mark_inode_dirty(inode);
  996. else
  997. mark_buffer_dirty_inode(partial->bh, inode);
  998. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  999. }
  1000. /* Clear the ends of indirect blocks on the shared branch */
  1001. while (partial > chain) {
  1002. ext2_free_branches(inode,
  1003. partial->p + 1,
  1004. (__le32*)partial->bh->b_data+addr_per_block,
  1005. (chain+n-1) - partial);
  1006. mark_buffer_dirty_inode(partial->bh, inode);
  1007. brelse (partial->bh);
  1008. partial--;
  1009. }
  1010. do_indirects:
  1011. /* Kill the remaining (whole) subtrees */
  1012. switch (offsets[0]) {
  1013. default:
  1014. nr = i_data[EXT2_IND_BLOCK];
  1015. if (nr) {
  1016. i_data[EXT2_IND_BLOCK] = 0;
  1017. mark_inode_dirty(inode);
  1018. ext2_free_branches(inode, &nr, &nr+1, 1);
  1019. }
  1020. case EXT2_IND_BLOCK:
  1021. nr = i_data[EXT2_DIND_BLOCK];
  1022. if (nr) {
  1023. i_data[EXT2_DIND_BLOCK] = 0;
  1024. mark_inode_dirty(inode);
  1025. ext2_free_branches(inode, &nr, &nr+1, 2);
  1026. }
  1027. case EXT2_DIND_BLOCK:
  1028. nr = i_data[EXT2_TIND_BLOCK];
  1029. if (nr) {
  1030. i_data[EXT2_TIND_BLOCK] = 0;
  1031. mark_inode_dirty(inode);
  1032. ext2_free_branches(inode, &nr, &nr+1, 3);
  1033. }
  1034. case EXT2_TIND_BLOCK:
  1035. ;
  1036. }
  1037. ext2_discard_reservation(inode);
  1038. mutex_unlock(&ei->truncate_mutex);
  1039. }
  1040. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1041. {
  1042. /*
  1043. * XXX: it seems like a bug here that we don't allow
  1044. * IS_APPEND inode to have blocks-past-i_size trimmed off.
  1045. * review and fix this.
  1046. *
  1047. * Also would be nice to be able to handle IO errors and such,
  1048. * but that's probably too much to ask.
  1049. */
  1050. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1051. S_ISLNK(inode->i_mode)))
  1052. return;
  1053. if (ext2_inode_is_fast_symlink(inode))
  1054. return;
  1055. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1056. return;
  1057. __ext2_truncate_blocks(inode, offset);
  1058. }
  1059. int ext2_setsize(struct inode *inode, loff_t newsize)
  1060. {
  1061. loff_t oldsize;
  1062. int error;
  1063. error = inode_newsize_ok(inode, newsize);
  1064. if (error)
  1065. return error;
  1066. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1067. S_ISLNK(inode->i_mode)))
  1068. return -EINVAL;
  1069. if (ext2_inode_is_fast_symlink(inode))
  1070. return -EINVAL;
  1071. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1072. return -EPERM;
  1073. if (mapping_is_xip(inode->i_mapping))
  1074. error = xip_truncate_page(inode->i_mapping, newsize);
  1075. else if (test_opt(inode->i_sb, NOBH))
  1076. error = nobh_truncate_page(inode->i_mapping,
  1077. newsize, ext2_get_block);
  1078. else
  1079. error = block_truncate_page(inode->i_mapping,
  1080. newsize, ext2_get_block);
  1081. if (error)
  1082. return error;
  1083. oldsize = inode->i_size;
  1084. i_size_write(inode, newsize);
  1085. truncate_pagecache(inode, oldsize, newsize);
  1086. __ext2_truncate_blocks(inode, newsize);
  1087. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1088. if (inode_needs_sync(inode)) {
  1089. sync_mapping_buffers(inode->i_mapping);
  1090. ext2_sync_inode (inode);
  1091. } else {
  1092. mark_inode_dirty(inode);
  1093. }
  1094. return 0;
  1095. }
  1096. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1097. struct buffer_head **p)
  1098. {
  1099. struct buffer_head * bh;
  1100. unsigned long block_group;
  1101. unsigned long block;
  1102. unsigned long offset;
  1103. struct ext2_group_desc * gdp;
  1104. *p = NULL;
  1105. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1106. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1107. goto Einval;
  1108. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1109. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1110. if (!gdp)
  1111. goto Egdp;
  1112. /*
  1113. * Figure out the offset within the block group inode table
  1114. */
  1115. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1116. block = le32_to_cpu(gdp->bg_inode_table) +
  1117. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1118. if (!(bh = sb_bread(sb, block)))
  1119. goto Eio;
  1120. *p = bh;
  1121. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1122. return (struct ext2_inode *) (bh->b_data + offset);
  1123. Einval:
  1124. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1125. (unsigned long) ino);
  1126. return ERR_PTR(-EINVAL);
  1127. Eio:
  1128. ext2_error(sb, "ext2_get_inode",
  1129. "unable to read inode block - inode=%lu, block=%lu",
  1130. (unsigned long) ino, block);
  1131. Egdp:
  1132. return ERR_PTR(-EIO);
  1133. }
  1134. void ext2_set_inode_flags(struct inode *inode)
  1135. {
  1136. unsigned int flags = EXT2_I(inode)->i_flags;
  1137. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1138. if (flags & EXT2_SYNC_FL)
  1139. inode->i_flags |= S_SYNC;
  1140. if (flags & EXT2_APPEND_FL)
  1141. inode->i_flags |= S_APPEND;
  1142. if (flags & EXT2_IMMUTABLE_FL)
  1143. inode->i_flags |= S_IMMUTABLE;
  1144. if (flags & EXT2_NOATIME_FL)
  1145. inode->i_flags |= S_NOATIME;
  1146. if (flags & EXT2_DIRSYNC_FL)
  1147. inode->i_flags |= S_DIRSYNC;
  1148. }
  1149. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1150. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1151. {
  1152. unsigned int flags = ei->vfs_inode.i_flags;
  1153. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1154. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1155. if (flags & S_SYNC)
  1156. ei->i_flags |= EXT2_SYNC_FL;
  1157. if (flags & S_APPEND)
  1158. ei->i_flags |= EXT2_APPEND_FL;
  1159. if (flags & S_IMMUTABLE)
  1160. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1161. if (flags & S_NOATIME)
  1162. ei->i_flags |= EXT2_NOATIME_FL;
  1163. if (flags & S_DIRSYNC)
  1164. ei->i_flags |= EXT2_DIRSYNC_FL;
  1165. }
  1166. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1167. {
  1168. struct ext2_inode_info *ei;
  1169. struct buffer_head * bh;
  1170. struct ext2_inode *raw_inode;
  1171. struct inode *inode;
  1172. long ret = -EIO;
  1173. int n;
  1174. inode = iget_locked(sb, ino);
  1175. if (!inode)
  1176. return ERR_PTR(-ENOMEM);
  1177. if (!(inode->i_state & I_NEW))
  1178. return inode;
  1179. ei = EXT2_I(inode);
  1180. ei->i_block_alloc_info = NULL;
  1181. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1182. if (IS_ERR(raw_inode)) {
  1183. ret = PTR_ERR(raw_inode);
  1184. goto bad_inode;
  1185. }
  1186. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1187. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1188. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1189. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1190. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1191. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1192. }
  1193. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  1194. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1195. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1196. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1197. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1198. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1199. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1200. /* We now have enough fields to check if the inode was active or not.
  1201. * This is needed because nfsd might try to access dead inodes
  1202. * the test is that same one that e2fsck uses
  1203. * NeilBrown 1999oct15
  1204. */
  1205. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1206. /* this inode is deleted */
  1207. brelse (bh);
  1208. ret = -ESTALE;
  1209. goto bad_inode;
  1210. }
  1211. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1212. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1213. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1214. ei->i_frag_no = raw_inode->i_frag;
  1215. ei->i_frag_size = raw_inode->i_fsize;
  1216. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1217. ei->i_dir_acl = 0;
  1218. if (S_ISREG(inode->i_mode))
  1219. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1220. else
  1221. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1222. ei->i_dtime = 0;
  1223. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1224. ei->i_state = 0;
  1225. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1226. ei->i_dir_start_lookup = 0;
  1227. /*
  1228. * NOTE! The in-memory inode i_data array is in little-endian order
  1229. * even on big-endian machines: we do NOT byteswap the block numbers!
  1230. */
  1231. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1232. ei->i_data[n] = raw_inode->i_block[n];
  1233. if (S_ISREG(inode->i_mode)) {
  1234. inode->i_op = &ext2_file_inode_operations;
  1235. if (ext2_use_xip(inode->i_sb)) {
  1236. inode->i_mapping->a_ops = &ext2_aops_xip;
  1237. inode->i_fop = &ext2_xip_file_operations;
  1238. } else if (test_opt(inode->i_sb, NOBH)) {
  1239. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1240. inode->i_fop = &ext2_file_operations;
  1241. } else {
  1242. inode->i_mapping->a_ops = &ext2_aops;
  1243. inode->i_fop = &ext2_file_operations;
  1244. }
  1245. } else if (S_ISDIR(inode->i_mode)) {
  1246. inode->i_op = &ext2_dir_inode_operations;
  1247. inode->i_fop = &ext2_dir_operations;
  1248. if (test_opt(inode->i_sb, NOBH))
  1249. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1250. else
  1251. inode->i_mapping->a_ops = &ext2_aops;
  1252. } else if (S_ISLNK(inode->i_mode)) {
  1253. if (ext2_inode_is_fast_symlink(inode)) {
  1254. inode->i_op = &ext2_fast_symlink_inode_operations;
  1255. nd_terminate_link(ei->i_data, inode->i_size,
  1256. sizeof(ei->i_data) - 1);
  1257. } else {
  1258. inode->i_op = &ext2_symlink_inode_operations;
  1259. if (test_opt(inode->i_sb, NOBH))
  1260. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1261. else
  1262. inode->i_mapping->a_ops = &ext2_aops;
  1263. }
  1264. } else {
  1265. inode->i_op = &ext2_special_inode_operations;
  1266. if (raw_inode->i_block[0])
  1267. init_special_inode(inode, inode->i_mode,
  1268. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1269. else
  1270. init_special_inode(inode, inode->i_mode,
  1271. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1272. }
  1273. brelse (bh);
  1274. ext2_set_inode_flags(inode);
  1275. unlock_new_inode(inode);
  1276. return inode;
  1277. bad_inode:
  1278. iget_failed(inode);
  1279. return ERR_PTR(ret);
  1280. }
  1281. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1282. {
  1283. struct ext2_inode_info *ei = EXT2_I(inode);
  1284. struct super_block *sb = inode->i_sb;
  1285. ino_t ino = inode->i_ino;
  1286. uid_t uid = inode->i_uid;
  1287. gid_t gid = inode->i_gid;
  1288. struct buffer_head * bh;
  1289. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1290. int n;
  1291. int err = 0;
  1292. if (IS_ERR(raw_inode))
  1293. return -EIO;
  1294. /* For fields not not tracking in the in-memory inode,
  1295. * initialise them to zero for new inodes. */
  1296. if (ei->i_state & EXT2_STATE_NEW)
  1297. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1298. ext2_get_inode_flags(ei);
  1299. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1300. if (!(test_opt(sb, NO_UID32))) {
  1301. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1302. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1303. /*
  1304. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1305. * re-used with the upper 16 bits of the uid/gid intact
  1306. */
  1307. if (!ei->i_dtime) {
  1308. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1309. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1310. } else {
  1311. raw_inode->i_uid_high = 0;
  1312. raw_inode->i_gid_high = 0;
  1313. }
  1314. } else {
  1315. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1316. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1317. raw_inode->i_uid_high = 0;
  1318. raw_inode->i_gid_high = 0;
  1319. }
  1320. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1321. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1322. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1323. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1324. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1325. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1326. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1327. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1328. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1329. raw_inode->i_frag = ei->i_frag_no;
  1330. raw_inode->i_fsize = ei->i_frag_size;
  1331. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1332. if (!S_ISREG(inode->i_mode))
  1333. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1334. else {
  1335. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1336. if (inode->i_size > 0x7fffffffULL) {
  1337. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1338. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1339. EXT2_SB(sb)->s_es->s_rev_level ==
  1340. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1341. /* If this is the first large file
  1342. * created, add a flag to the superblock.
  1343. */
  1344. spin_lock(&EXT2_SB(sb)->s_lock);
  1345. ext2_update_dynamic_rev(sb);
  1346. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1347. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1348. spin_unlock(&EXT2_SB(sb)->s_lock);
  1349. ext2_write_super(sb);
  1350. }
  1351. }
  1352. }
  1353. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1354. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1355. if (old_valid_dev(inode->i_rdev)) {
  1356. raw_inode->i_block[0] =
  1357. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1358. raw_inode->i_block[1] = 0;
  1359. } else {
  1360. raw_inode->i_block[0] = 0;
  1361. raw_inode->i_block[1] =
  1362. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1363. raw_inode->i_block[2] = 0;
  1364. }
  1365. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1366. raw_inode->i_block[n] = ei->i_data[n];
  1367. mark_buffer_dirty(bh);
  1368. if (do_sync) {
  1369. sync_dirty_buffer(bh);
  1370. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1371. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1372. sb->s_id, (unsigned long) ino);
  1373. err = -EIO;
  1374. }
  1375. }
  1376. ei->i_state &= ~EXT2_STATE_NEW;
  1377. brelse (bh);
  1378. return err;
  1379. }
  1380. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1381. {
  1382. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1383. }
  1384. int ext2_sync_inode(struct inode *inode)
  1385. {
  1386. struct writeback_control wbc = {
  1387. .sync_mode = WB_SYNC_ALL,
  1388. .nr_to_write = 0, /* sys_fsync did this */
  1389. };
  1390. return sync_inode(inode, &wbc);
  1391. }
  1392. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1393. {
  1394. struct inode *inode = dentry->d_inode;
  1395. int error;
  1396. error = inode_change_ok(inode, iattr);
  1397. if (error)
  1398. return error;
  1399. if (is_quota_modification(inode, iattr))
  1400. dquot_initialize(inode);
  1401. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  1402. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  1403. error = dquot_transfer(inode, iattr);
  1404. if (error)
  1405. return error;
  1406. }
  1407. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1408. error = ext2_setsize(inode, iattr->ia_size);
  1409. if (error)
  1410. return error;
  1411. }
  1412. generic_setattr(inode, iattr);
  1413. if (iattr->ia_valid & ATTR_MODE)
  1414. error = ext2_acl_chmod(inode);
  1415. mark_inode_dirty(inode);
  1416. return error;
  1417. }