inode.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. /*
  728. * Dir-in-pagecache still uses ext2_write_begin. Would have to rework
  729. * directory handling code to pass around offsets rather than struct
  730. * pages in order to make this work easily.
  731. */
  732. ret = nobh_write_begin_newtrunc(file, mapping, pos, len, flags, pagep,
  733. fsdata, ext2_get_block);
  734. if (ret < 0)
  735. ext2_write_failed(mapping, pos + len);
  736. return ret;
  737. }
  738. static int ext2_nobh_writepage(struct page *page,
  739. struct writeback_control *wbc)
  740. {
  741. return nobh_writepage(page, ext2_get_block, wbc);
  742. }
  743. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  744. {
  745. return generic_block_bmap(mapping,block,ext2_get_block);
  746. }
  747. static ssize_t
  748. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  749. loff_t offset, unsigned long nr_segs)
  750. {
  751. struct file *file = iocb->ki_filp;
  752. struct address_space *mapping = file->f_mapping;
  753. struct inode *inode = mapping->host;
  754. ssize_t ret;
  755. ret = blockdev_direct_IO_newtrunc(rw, iocb, inode, inode->i_sb->s_bdev,
  756. iov, offset, nr_segs, ext2_get_block, NULL);
  757. if (ret < 0 && (rw & WRITE))
  758. ext2_write_failed(mapping, offset + iov_length(iov, nr_segs));
  759. return ret;
  760. }
  761. static int
  762. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  763. {
  764. return mpage_writepages(mapping, wbc, ext2_get_block);
  765. }
  766. const struct address_space_operations ext2_aops = {
  767. .readpage = ext2_readpage,
  768. .readpages = ext2_readpages,
  769. .writepage = ext2_writepage,
  770. .sync_page = block_sync_page,
  771. .write_begin = ext2_write_begin,
  772. .write_end = ext2_write_end,
  773. .bmap = ext2_bmap,
  774. .direct_IO = ext2_direct_IO,
  775. .writepages = ext2_writepages,
  776. .migratepage = buffer_migrate_page,
  777. .is_partially_uptodate = block_is_partially_uptodate,
  778. .error_remove_page = generic_error_remove_page,
  779. };
  780. const struct address_space_operations ext2_aops_xip = {
  781. .bmap = ext2_bmap,
  782. .get_xip_mem = ext2_get_xip_mem,
  783. };
  784. const struct address_space_operations ext2_nobh_aops = {
  785. .readpage = ext2_readpage,
  786. .readpages = ext2_readpages,
  787. .writepage = ext2_nobh_writepage,
  788. .sync_page = block_sync_page,
  789. .write_begin = ext2_nobh_write_begin,
  790. .write_end = nobh_write_end,
  791. .bmap = ext2_bmap,
  792. .direct_IO = ext2_direct_IO,
  793. .writepages = ext2_writepages,
  794. .migratepage = buffer_migrate_page,
  795. .error_remove_page = generic_error_remove_page,
  796. };
  797. /*
  798. * Probably it should be a library function... search for first non-zero word
  799. * or memcmp with zero_page, whatever is better for particular architecture.
  800. * Linus?
  801. */
  802. static inline int all_zeroes(__le32 *p, __le32 *q)
  803. {
  804. while (p < q)
  805. if (*p++)
  806. return 0;
  807. return 1;
  808. }
  809. /**
  810. * ext2_find_shared - find the indirect blocks for partial truncation.
  811. * @inode: inode in question
  812. * @depth: depth of the affected branch
  813. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  814. * @chain: place to store the pointers to partial indirect blocks
  815. * @top: place to the (detached) top of branch
  816. *
  817. * This is a helper function used by ext2_truncate().
  818. *
  819. * When we do truncate() we may have to clean the ends of several indirect
  820. * blocks but leave the blocks themselves alive. Block is partially
  821. * truncated if some data below the new i_size is refered from it (and
  822. * it is on the path to the first completely truncated data block, indeed).
  823. * We have to free the top of that path along with everything to the right
  824. * of the path. Since no allocation past the truncation point is possible
  825. * until ext2_truncate() finishes, we may safely do the latter, but top
  826. * of branch may require special attention - pageout below the truncation
  827. * point might try to populate it.
  828. *
  829. * We atomically detach the top of branch from the tree, store the block
  830. * number of its root in *@top, pointers to buffer_heads of partially
  831. * truncated blocks - in @chain[].bh and pointers to their last elements
  832. * that should not be removed - in @chain[].p. Return value is the pointer
  833. * to last filled element of @chain.
  834. *
  835. * The work left to caller to do the actual freeing of subtrees:
  836. * a) free the subtree starting from *@top
  837. * b) free the subtrees whose roots are stored in
  838. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  839. * c) free the subtrees growing from the inode past the @chain[0].p
  840. * (no partially truncated stuff there).
  841. */
  842. static Indirect *ext2_find_shared(struct inode *inode,
  843. int depth,
  844. int offsets[4],
  845. Indirect chain[4],
  846. __le32 *top)
  847. {
  848. Indirect *partial, *p;
  849. int k, err;
  850. *top = 0;
  851. for (k = depth; k > 1 && !offsets[k-1]; k--)
  852. ;
  853. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  854. if (!partial)
  855. partial = chain + k-1;
  856. /*
  857. * If the branch acquired continuation since we've looked at it -
  858. * fine, it should all survive and (new) top doesn't belong to us.
  859. */
  860. write_lock(&EXT2_I(inode)->i_meta_lock);
  861. if (!partial->key && *partial->p) {
  862. write_unlock(&EXT2_I(inode)->i_meta_lock);
  863. goto no_top;
  864. }
  865. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  866. ;
  867. /*
  868. * OK, we've found the last block that must survive. The rest of our
  869. * branch should be detached before unlocking. However, if that rest
  870. * of branch is all ours and does not grow immediately from the inode
  871. * it's easier to cheat and just decrement partial->p.
  872. */
  873. if (p == chain + k - 1 && p > chain) {
  874. p->p--;
  875. } else {
  876. *top = *p->p;
  877. *p->p = 0;
  878. }
  879. write_unlock(&EXT2_I(inode)->i_meta_lock);
  880. while(partial > p)
  881. {
  882. brelse(partial->bh);
  883. partial--;
  884. }
  885. no_top:
  886. return partial;
  887. }
  888. /**
  889. * ext2_free_data - free a list of data blocks
  890. * @inode: inode we are dealing with
  891. * @p: array of block numbers
  892. * @q: points immediately past the end of array
  893. *
  894. * We are freeing all blocks refered from that array (numbers are
  895. * stored as little-endian 32-bit) and updating @inode->i_blocks
  896. * appropriately.
  897. */
  898. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  899. {
  900. unsigned long block_to_free = 0, count = 0;
  901. unsigned long nr;
  902. for ( ; p < q ; p++) {
  903. nr = le32_to_cpu(*p);
  904. if (nr) {
  905. *p = 0;
  906. /* accumulate blocks to free if they're contiguous */
  907. if (count == 0)
  908. goto free_this;
  909. else if (block_to_free == nr - count)
  910. count++;
  911. else {
  912. mark_inode_dirty(inode);
  913. ext2_free_blocks (inode, block_to_free, count);
  914. free_this:
  915. block_to_free = nr;
  916. count = 1;
  917. }
  918. }
  919. }
  920. if (count > 0) {
  921. mark_inode_dirty(inode);
  922. ext2_free_blocks (inode, block_to_free, count);
  923. }
  924. }
  925. /**
  926. * ext2_free_branches - free an array of branches
  927. * @inode: inode we are dealing with
  928. * @p: array of block numbers
  929. * @q: pointer immediately past the end of array
  930. * @depth: depth of the branches to free
  931. *
  932. * We are freeing all blocks refered from these branches (numbers are
  933. * stored as little-endian 32-bit) and updating @inode->i_blocks
  934. * appropriately.
  935. */
  936. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  937. {
  938. struct buffer_head * bh;
  939. unsigned long nr;
  940. if (depth--) {
  941. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  942. for ( ; p < q ; p++) {
  943. nr = le32_to_cpu(*p);
  944. if (!nr)
  945. continue;
  946. *p = 0;
  947. bh = sb_bread(inode->i_sb, nr);
  948. /*
  949. * A read failure? Report error and clear slot
  950. * (should be rare).
  951. */
  952. if (!bh) {
  953. ext2_error(inode->i_sb, "ext2_free_branches",
  954. "Read failure, inode=%ld, block=%ld",
  955. inode->i_ino, nr);
  956. continue;
  957. }
  958. ext2_free_branches(inode,
  959. (__le32*)bh->b_data,
  960. (__le32*)bh->b_data + addr_per_block,
  961. depth);
  962. bforget(bh);
  963. ext2_free_blocks(inode, nr, 1);
  964. mark_inode_dirty(inode);
  965. }
  966. } else
  967. ext2_free_data(inode, p, q);
  968. }
  969. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  970. {
  971. __le32 *i_data = EXT2_I(inode)->i_data;
  972. struct ext2_inode_info *ei = EXT2_I(inode);
  973. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  974. int offsets[4];
  975. Indirect chain[4];
  976. Indirect *partial;
  977. __le32 nr = 0;
  978. int n;
  979. long iblock;
  980. unsigned blocksize;
  981. blocksize = inode->i_sb->s_blocksize;
  982. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  983. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  984. if (n == 0)
  985. return;
  986. /*
  987. * From here we block out all ext2_get_block() callers who want to
  988. * modify the block allocation tree.
  989. */
  990. mutex_lock(&ei->truncate_mutex);
  991. if (n == 1) {
  992. ext2_free_data(inode, i_data+offsets[0],
  993. i_data + EXT2_NDIR_BLOCKS);
  994. goto do_indirects;
  995. }
  996. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  997. /* Kill the top of shared branch (already detached) */
  998. if (nr) {
  999. if (partial == chain)
  1000. mark_inode_dirty(inode);
  1001. else
  1002. mark_buffer_dirty_inode(partial->bh, inode);
  1003. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1004. }
  1005. /* Clear the ends of indirect blocks on the shared branch */
  1006. while (partial > chain) {
  1007. ext2_free_branches(inode,
  1008. partial->p + 1,
  1009. (__le32*)partial->bh->b_data+addr_per_block,
  1010. (chain+n-1) - partial);
  1011. mark_buffer_dirty_inode(partial->bh, inode);
  1012. brelse (partial->bh);
  1013. partial--;
  1014. }
  1015. do_indirects:
  1016. /* Kill the remaining (whole) subtrees */
  1017. switch (offsets[0]) {
  1018. default:
  1019. nr = i_data[EXT2_IND_BLOCK];
  1020. if (nr) {
  1021. i_data[EXT2_IND_BLOCK] = 0;
  1022. mark_inode_dirty(inode);
  1023. ext2_free_branches(inode, &nr, &nr+1, 1);
  1024. }
  1025. case EXT2_IND_BLOCK:
  1026. nr = i_data[EXT2_DIND_BLOCK];
  1027. if (nr) {
  1028. i_data[EXT2_DIND_BLOCK] = 0;
  1029. mark_inode_dirty(inode);
  1030. ext2_free_branches(inode, &nr, &nr+1, 2);
  1031. }
  1032. case EXT2_DIND_BLOCK:
  1033. nr = i_data[EXT2_TIND_BLOCK];
  1034. if (nr) {
  1035. i_data[EXT2_TIND_BLOCK] = 0;
  1036. mark_inode_dirty(inode);
  1037. ext2_free_branches(inode, &nr, &nr+1, 3);
  1038. }
  1039. case EXT2_TIND_BLOCK:
  1040. ;
  1041. }
  1042. ext2_discard_reservation(inode);
  1043. mutex_unlock(&ei->truncate_mutex);
  1044. }
  1045. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1046. {
  1047. /*
  1048. * XXX: it seems like a bug here that we don't allow
  1049. * IS_APPEND inode to have blocks-past-i_size trimmed off.
  1050. * review and fix this.
  1051. *
  1052. * Also would be nice to be able to handle IO errors and such,
  1053. * but that's probably too much to ask.
  1054. */
  1055. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1056. S_ISLNK(inode->i_mode)))
  1057. return;
  1058. if (ext2_inode_is_fast_symlink(inode))
  1059. return;
  1060. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1061. return;
  1062. __ext2_truncate_blocks(inode, offset);
  1063. }
  1064. int ext2_setsize(struct inode *inode, loff_t newsize)
  1065. {
  1066. loff_t oldsize;
  1067. int error;
  1068. error = inode_newsize_ok(inode, newsize);
  1069. if (error)
  1070. return error;
  1071. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1072. S_ISLNK(inode->i_mode)))
  1073. return -EINVAL;
  1074. if (ext2_inode_is_fast_symlink(inode))
  1075. return -EINVAL;
  1076. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1077. return -EPERM;
  1078. if (mapping_is_xip(inode->i_mapping))
  1079. error = xip_truncate_page(inode->i_mapping, newsize);
  1080. else if (test_opt(inode->i_sb, NOBH))
  1081. error = nobh_truncate_page(inode->i_mapping,
  1082. newsize, ext2_get_block);
  1083. else
  1084. error = block_truncate_page(inode->i_mapping,
  1085. newsize, ext2_get_block);
  1086. if (error)
  1087. return error;
  1088. oldsize = inode->i_size;
  1089. i_size_write(inode, newsize);
  1090. truncate_pagecache(inode, oldsize, newsize);
  1091. __ext2_truncate_blocks(inode, newsize);
  1092. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1093. if (inode_needs_sync(inode)) {
  1094. sync_mapping_buffers(inode->i_mapping);
  1095. ext2_sync_inode (inode);
  1096. } else {
  1097. mark_inode_dirty(inode);
  1098. }
  1099. return 0;
  1100. }
  1101. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1102. struct buffer_head **p)
  1103. {
  1104. struct buffer_head * bh;
  1105. unsigned long block_group;
  1106. unsigned long block;
  1107. unsigned long offset;
  1108. struct ext2_group_desc * gdp;
  1109. *p = NULL;
  1110. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1111. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1112. goto Einval;
  1113. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1114. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1115. if (!gdp)
  1116. goto Egdp;
  1117. /*
  1118. * Figure out the offset within the block group inode table
  1119. */
  1120. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1121. block = le32_to_cpu(gdp->bg_inode_table) +
  1122. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1123. if (!(bh = sb_bread(sb, block)))
  1124. goto Eio;
  1125. *p = bh;
  1126. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1127. return (struct ext2_inode *) (bh->b_data + offset);
  1128. Einval:
  1129. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1130. (unsigned long) ino);
  1131. return ERR_PTR(-EINVAL);
  1132. Eio:
  1133. ext2_error(sb, "ext2_get_inode",
  1134. "unable to read inode block - inode=%lu, block=%lu",
  1135. (unsigned long) ino, block);
  1136. Egdp:
  1137. return ERR_PTR(-EIO);
  1138. }
  1139. void ext2_set_inode_flags(struct inode *inode)
  1140. {
  1141. unsigned int flags = EXT2_I(inode)->i_flags;
  1142. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1143. if (flags & EXT2_SYNC_FL)
  1144. inode->i_flags |= S_SYNC;
  1145. if (flags & EXT2_APPEND_FL)
  1146. inode->i_flags |= S_APPEND;
  1147. if (flags & EXT2_IMMUTABLE_FL)
  1148. inode->i_flags |= S_IMMUTABLE;
  1149. if (flags & EXT2_NOATIME_FL)
  1150. inode->i_flags |= S_NOATIME;
  1151. if (flags & EXT2_DIRSYNC_FL)
  1152. inode->i_flags |= S_DIRSYNC;
  1153. }
  1154. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1155. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1156. {
  1157. unsigned int flags = ei->vfs_inode.i_flags;
  1158. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1159. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1160. if (flags & S_SYNC)
  1161. ei->i_flags |= EXT2_SYNC_FL;
  1162. if (flags & S_APPEND)
  1163. ei->i_flags |= EXT2_APPEND_FL;
  1164. if (flags & S_IMMUTABLE)
  1165. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1166. if (flags & S_NOATIME)
  1167. ei->i_flags |= EXT2_NOATIME_FL;
  1168. if (flags & S_DIRSYNC)
  1169. ei->i_flags |= EXT2_DIRSYNC_FL;
  1170. }
  1171. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1172. {
  1173. struct ext2_inode_info *ei;
  1174. struct buffer_head * bh;
  1175. struct ext2_inode *raw_inode;
  1176. struct inode *inode;
  1177. long ret = -EIO;
  1178. int n;
  1179. inode = iget_locked(sb, ino);
  1180. if (!inode)
  1181. return ERR_PTR(-ENOMEM);
  1182. if (!(inode->i_state & I_NEW))
  1183. return inode;
  1184. ei = EXT2_I(inode);
  1185. ei->i_block_alloc_info = NULL;
  1186. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1187. if (IS_ERR(raw_inode)) {
  1188. ret = PTR_ERR(raw_inode);
  1189. goto bad_inode;
  1190. }
  1191. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1192. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1193. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1194. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1195. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1196. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1197. }
  1198. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  1199. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1200. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1201. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1202. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1203. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1204. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1205. /* We now have enough fields to check if the inode was active or not.
  1206. * This is needed because nfsd might try to access dead inodes
  1207. * the test is that same one that e2fsck uses
  1208. * NeilBrown 1999oct15
  1209. */
  1210. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1211. /* this inode is deleted */
  1212. brelse (bh);
  1213. ret = -ESTALE;
  1214. goto bad_inode;
  1215. }
  1216. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1217. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1218. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1219. ei->i_frag_no = raw_inode->i_frag;
  1220. ei->i_frag_size = raw_inode->i_fsize;
  1221. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1222. ei->i_dir_acl = 0;
  1223. if (S_ISREG(inode->i_mode))
  1224. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1225. else
  1226. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1227. ei->i_dtime = 0;
  1228. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1229. ei->i_state = 0;
  1230. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1231. ei->i_dir_start_lookup = 0;
  1232. /*
  1233. * NOTE! The in-memory inode i_data array is in little-endian order
  1234. * even on big-endian machines: we do NOT byteswap the block numbers!
  1235. */
  1236. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1237. ei->i_data[n] = raw_inode->i_block[n];
  1238. if (S_ISREG(inode->i_mode)) {
  1239. inode->i_op = &ext2_file_inode_operations;
  1240. if (ext2_use_xip(inode->i_sb)) {
  1241. inode->i_mapping->a_ops = &ext2_aops_xip;
  1242. inode->i_fop = &ext2_xip_file_operations;
  1243. } else if (test_opt(inode->i_sb, NOBH)) {
  1244. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1245. inode->i_fop = &ext2_file_operations;
  1246. } else {
  1247. inode->i_mapping->a_ops = &ext2_aops;
  1248. inode->i_fop = &ext2_file_operations;
  1249. }
  1250. } else if (S_ISDIR(inode->i_mode)) {
  1251. inode->i_op = &ext2_dir_inode_operations;
  1252. inode->i_fop = &ext2_dir_operations;
  1253. if (test_opt(inode->i_sb, NOBH))
  1254. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1255. else
  1256. inode->i_mapping->a_ops = &ext2_aops;
  1257. } else if (S_ISLNK(inode->i_mode)) {
  1258. if (ext2_inode_is_fast_symlink(inode)) {
  1259. inode->i_op = &ext2_fast_symlink_inode_operations;
  1260. nd_terminate_link(ei->i_data, inode->i_size,
  1261. sizeof(ei->i_data) - 1);
  1262. } else {
  1263. inode->i_op = &ext2_symlink_inode_operations;
  1264. if (test_opt(inode->i_sb, NOBH))
  1265. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1266. else
  1267. inode->i_mapping->a_ops = &ext2_aops;
  1268. }
  1269. } else {
  1270. inode->i_op = &ext2_special_inode_operations;
  1271. if (raw_inode->i_block[0])
  1272. init_special_inode(inode, inode->i_mode,
  1273. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1274. else
  1275. init_special_inode(inode, inode->i_mode,
  1276. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1277. }
  1278. brelse (bh);
  1279. ext2_set_inode_flags(inode);
  1280. unlock_new_inode(inode);
  1281. return inode;
  1282. bad_inode:
  1283. iget_failed(inode);
  1284. return ERR_PTR(ret);
  1285. }
  1286. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1287. {
  1288. struct ext2_inode_info *ei = EXT2_I(inode);
  1289. struct super_block *sb = inode->i_sb;
  1290. ino_t ino = inode->i_ino;
  1291. uid_t uid = inode->i_uid;
  1292. gid_t gid = inode->i_gid;
  1293. struct buffer_head * bh;
  1294. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1295. int n;
  1296. int err = 0;
  1297. if (IS_ERR(raw_inode))
  1298. return -EIO;
  1299. /* For fields not not tracking in the in-memory inode,
  1300. * initialise them to zero for new inodes. */
  1301. if (ei->i_state & EXT2_STATE_NEW)
  1302. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1303. ext2_get_inode_flags(ei);
  1304. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1305. if (!(test_opt(sb, NO_UID32))) {
  1306. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1307. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1308. /*
  1309. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1310. * re-used with the upper 16 bits of the uid/gid intact
  1311. */
  1312. if (!ei->i_dtime) {
  1313. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1314. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1315. } else {
  1316. raw_inode->i_uid_high = 0;
  1317. raw_inode->i_gid_high = 0;
  1318. }
  1319. } else {
  1320. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1321. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1322. raw_inode->i_uid_high = 0;
  1323. raw_inode->i_gid_high = 0;
  1324. }
  1325. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1326. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1327. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1328. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1329. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1330. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1331. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1332. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1333. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1334. raw_inode->i_frag = ei->i_frag_no;
  1335. raw_inode->i_fsize = ei->i_frag_size;
  1336. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1337. if (!S_ISREG(inode->i_mode))
  1338. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1339. else {
  1340. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1341. if (inode->i_size > 0x7fffffffULL) {
  1342. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1343. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1344. EXT2_SB(sb)->s_es->s_rev_level ==
  1345. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1346. /* If this is the first large file
  1347. * created, add a flag to the superblock.
  1348. */
  1349. spin_lock(&EXT2_SB(sb)->s_lock);
  1350. ext2_update_dynamic_rev(sb);
  1351. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1352. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1353. spin_unlock(&EXT2_SB(sb)->s_lock);
  1354. ext2_write_super(sb);
  1355. }
  1356. }
  1357. }
  1358. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1359. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1360. if (old_valid_dev(inode->i_rdev)) {
  1361. raw_inode->i_block[0] =
  1362. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1363. raw_inode->i_block[1] = 0;
  1364. } else {
  1365. raw_inode->i_block[0] = 0;
  1366. raw_inode->i_block[1] =
  1367. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1368. raw_inode->i_block[2] = 0;
  1369. }
  1370. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1371. raw_inode->i_block[n] = ei->i_data[n];
  1372. mark_buffer_dirty(bh);
  1373. if (do_sync) {
  1374. sync_dirty_buffer(bh);
  1375. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1376. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1377. sb->s_id, (unsigned long) ino);
  1378. err = -EIO;
  1379. }
  1380. }
  1381. ei->i_state &= ~EXT2_STATE_NEW;
  1382. brelse (bh);
  1383. return err;
  1384. }
  1385. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1386. {
  1387. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1388. }
  1389. int ext2_sync_inode(struct inode *inode)
  1390. {
  1391. struct writeback_control wbc = {
  1392. .sync_mode = WB_SYNC_ALL,
  1393. .nr_to_write = 0, /* sys_fsync did this */
  1394. };
  1395. return sync_inode(inode, &wbc);
  1396. }
  1397. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1398. {
  1399. struct inode *inode = dentry->d_inode;
  1400. int error;
  1401. error = inode_change_ok(inode, iattr);
  1402. if (error)
  1403. return error;
  1404. if (is_quota_modification(inode, iattr))
  1405. dquot_initialize(inode);
  1406. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  1407. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  1408. error = dquot_transfer(inode, iattr);
  1409. if (error)
  1410. return error;
  1411. }
  1412. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1413. error = ext2_setsize(inode, iattr->ia_size);
  1414. if (error)
  1415. return error;
  1416. }
  1417. generic_setattr(inode, iattr);
  1418. if (iattr->ia_valid & ATTR_MODE)
  1419. error = ext2_acl_chmod(inode);
  1420. mark_inode_dirty(inode);
  1421. return error;
  1422. }