inode.c 42 KB

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