inode.c 43 KB

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