inode.c 42 KB

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