inode.c 42 KB

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