inode.c 38 KB

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