inode.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. lock_buffer(bh);
  410. memset(bh->b_data, 0, blocksize);
  411. branch[n].bh = bh;
  412. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  413. *branch[n].p = branch[n].key;
  414. set_buffer_uptodate(bh);
  415. unlock_buffer(bh);
  416. mark_buffer_dirty_inode(bh, inode);
  417. /* We used to sync bh here if IS_SYNC(inode).
  418. * But we now rely upon generic_osync_inode()
  419. * and b_inode_buffers. But not for directories.
  420. */
  421. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  422. sync_dirty_buffer(bh);
  423. parent = nr;
  424. }
  425. if (n == num)
  426. return 0;
  427. /* Allocation failed, free what we already allocated */
  428. for (i = 1; i < n; i++)
  429. bforget(branch[i].bh);
  430. for (i = 0; i < n; i++)
  431. ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
  432. return err;
  433. }
  434. /**
  435. * ext2_splice_branch - splice the allocated branch onto inode.
  436. * @inode: owner
  437. * @block: (logical) number of block we are adding
  438. * @chain: chain of indirect blocks (with a missing link - see
  439. * ext2_alloc_branch)
  440. * @where: location of missing link
  441. * @num: number of blocks we are adding
  442. *
  443. * This function verifies that chain (up to the missing link) had not
  444. * changed, fills the missing link and does all housekeeping needed in
  445. * inode (->i_blocks, etc.). In case of success we end up with the full
  446. * chain to new block and return 0. Otherwise (== chain had been changed)
  447. * we free the new blocks (forgetting their buffer_heads, indeed) and
  448. * return -EAGAIN.
  449. */
  450. static inline int ext2_splice_branch(struct inode *inode,
  451. long block,
  452. Indirect chain[4],
  453. Indirect *where,
  454. int num)
  455. {
  456. struct ext2_inode_info *ei = EXT2_I(inode);
  457. int i;
  458. /* Verify that place we are splicing to is still there and vacant */
  459. write_lock(&ei->i_meta_lock);
  460. if (!verify_chain(chain, where-1) || *where->p)
  461. goto changed;
  462. /* That's it */
  463. *where->p = where->key;
  464. ei->i_next_alloc_block = block;
  465. ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
  466. write_unlock(&ei->i_meta_lock);
  467. /* We are done with atomic stuff, now do the rest of housekeeping */
  468. inode->i_ctime = CURRENT_TIME_SEC;
  469. /* had we spliced it onto indirect block? */
  470. if (where->bh)
  471. mark_buffer_dirty_inode(where->bh, inode);
  472. mark_inode_dirty(inode);
  473. return 0;
  474. changed:
  475. write_unlock(&ei->i_meta_lock);
  476. for (i = 1; i < num; i++)
  477. bforget(where[i].bh);
  478. for (i = 0; i < num; i++)
  479. ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
  480. return -EAGAIN;
  481. }
  482. /*
  483. * Allocation strategy is simple: if we have to allocate something, we will
  484. * have to go the whole way to leaf. So let's do it before attaching anything
  485. * to tree, set linkage between the newborn blocks, write them if sync is
  486. * required, recheck the path, free and repeat if check fails, otherwise
  487. * set the last missing link (that will protect us from any truncate-generated
  488. * removals - all blocks on the path are immune now) and possibly force the
  489. * write on the parent block.
  490. * That has a nice additional property: no special recovery from the failed
  491. * allocations is needed - we simply release blocks and do not touch anything
  492. * reachable from inode.
  493. */
  494. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  495. {
  496. int err = -EIO;
  497. int offsets[4];
  498. Indirect chain[4];
  499. Indirect *partial;
  500. unsigned long goal;
  501. int left;
  502. int boundary = 0;
  503. int depth = ext2_block_to_path(inode, iblock, offsets, &boundary);
  504. if (depth == 0)
  505. goto out;
  506. reread:
  507. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  508. /* Simplest case - block found, no allocation needed */
  509. if (!partial) {
  510. got_it:
  511. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  512. if (boundary)
  513. set_buffer_boundary(bh_result);
  514. /* Clean up and exit */
  515. partial = chain+depth-1; /* the whole chain */
  516. goto cleanup;
  517. }
  518. /* Next simple case - plain lookup or failed read of indirect block */
  519. if (!create || err == -EIO) {
  520. cleanup:
  521. while (partial > chain) {
  522. brelse(partial->bh);
  523. partial--;
  524. }
  525. out:
  526. return err;
  527. }
  528. /*
  529. * Indirect block might be removed by truncate while we were
  530. * reading it. Handling of that case (forget what we've got and
  531. * reread) is taken out of the main path.
  532. */
  533. if (err == -EAGAIN)
  534. goto changed;
  535. goal = 0;
  536. if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
  537. goto changed;
  538. left = (chain + depth) - partial;
  539. err = ext2_alloc_branch(inode, left, goal,
  540. offsets+(partial-chain), partial);
  541. if (err)
  542. goto cleanup;
  543. if (ext2_use_xip(inode->i_sb)) {
  544. /*
  545. * we need to clear the block
  546. */
  547. err = ext2_clear_xip_target (inode,
  548. le32_to_cpu(chain[depth-1].key));
  549. if (err)
  550. goto cleanup;
  551. }
  552. if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
  553. goto changed;
  554. set_buffer_new(bh_result);
  555. goto got_it;
  556. changed:
  557. while (partial > chain) {
  558. brelse(partial->bh);
  559. partial--;
  560. }
  561. goto reread;
  562. }
  563. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  564. {
  565. return block_write_full_page(page, ext2_get_block, wbc);
  566. }
  567. static int ext2_readpage(struct file *file, struct page *page)
  568. {
  569. return mpage_readpage(page, ext2_get_block);
  570. }
  571. static int
  572. ext2_readpages(struct file *file, struct address_space *mapping,
  573. struct list_head *pages, unsigned nr_pages)
  574. {
  575. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  576. }
  577. static int
  578. ext2_prepare_write(struct file *file, struct page *page,
  579. unsigned from, unsigned to)
  580. {
  581. return block_prepare_write(page,from,to,ext2_get_block);
  582. }
  583. static int
  584. ext2_nobh_prepare_write(struct file *file, struct page *page,
  585. unsigned from, unsigned to)
  586. {
  587. return nobh_prepare_write(page,from,to,ext2_get_block);
  588. }
  589. static int ext2_nobh_writepage(struct page *page,
  590. struct writeback_control *wbc)
  591. {
  592. return nobh_writepage(page, ext2_get_block, wbc);
  593. }
  594. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  595. {
  596. return generic_block_bmap(mapping,block,ext2_get_block);
  597. }
  598. static int
  599. ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks,
  600. struct buffer_head *bh_result, int create)
  601. {
  602. int ret;
  603. ret = ext2_get_block(inode, iblock, bh_result, create);
  604. if (ret == 0)
  605. bh_result->b_size = (1 << inode->i_blkbits);
  606. return ret;
  607. }
  608. static ssize_t
  609. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  610. loff_t offset, unsigned long nr_segs)
  611. {
  612. struct file *file = iocb->ki_filp;
  613. struct inode *inode = file->f_mapping->host;
  614. return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  615. offset, nr_segs, ext2_get_blocks, NULL);
  616. }
  617. static int
  618. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  619. {
  620. return mpage_writepages(mapping, wbc, ext2_get_block);
  621. }
  622. struct address_space_operations ext2_aops = {
  623. .readpage = ext2_readpage,
  624. .readpages = ext2_readpages,
  625. .writepage = ext2_writepage,
  626. .sync_page = block_sync_page,
  627. .prepare_write = ext2_prepare_write,
  628. .commit_write = generic_commit_write,
  629. .bmap = ext2_bmap,
  630. .direct_IO = ext2_direct_IO,
  631. .writepages = ext2_writepages,
  632. };
  633. struct address_space_operations ext2_aops_xip = {
  634. .bmap = ext2_bmap,
  635. .get_xip_page = ext2_get_xip_page,
  636. };
  637. struct address_space_operations ext2_nobh_aops = {
  638. .readpage = ext2_readpage,
  639. .readpages = ext2_readpages,
  640. .writepage = ext2_nobh_writepage,
  641. .sync_page = block_sync_page,
  642. .prepare_write = ext2_nobh_prepare_write,
  643. .commit_write = nobh_commit_write,
  644. .bmap = ext2_bmap,
  645. .direct_IO = ext2_direct_IO,
  646. .writepages = ext2_writepages,
  647. };
  648. /*
  649. * Probably it should be a library function... search for first non-zero word
  650. * or memcmp with zero_page, whatever is better for particular architecture.
  651. * Linus?
  652. */
  653. static inline int all_zeroes(__le32 *p, __le32 *q)
  654. {
  655. while (p < q)
  656. if (*p++)
  657. return 0;
  658. return 1;
  659. }
  660. /**
  661. * ext2_find_shared - find the indirect blocks for partial truncation.
  662. * @inode: inode in question
  663. * @depth: depth of the affected branch
  664. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  665. * @chain: place to store the pointers to partial indirect blocks
  666. * @top: place to the (detached) top of branch
  667. *
  668. * This is a helper function used by ext2_truncate().
  669. *
  670. * When we do truncate() we may have to clean the ends of several indirect
  671. * blocks but leave the blocks themselves alive. Block is partially
  672. * truncated if some data below the new i_size is refered from it (and
  673. * it is on the path to the first completely truncated data block, indeed).
  674. * We have to free the top of that path along with everything to the right
  675. * of the path. Since no allocation past the truncation point is possible
  676. * until ext2_truncate() finishes, we may safely do the latter, but top
  677. * of branch may require special attention - pageout below the truncation
  678. * point might try to populate it.
  679. *
  680. * We atomically detach the top of branch from the tree, store the block
  681. * number of its root in *@top, pointers to buffer_heads of partially
  682. * truncated blocks - in @chain[].bh and pointers to their last elements
  683. * that should not be removed - in @chain[].p. Return value is the pointer
  684. * to last filled element of @chain.
  685. *
  686. * The work left to caller to do the actual freeing of subtrees:
  687. * a) free the subtree starting from *@top
  688. * b) free the subtrees whose roots are stored in
  689. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  690. * c) free the subtrees growing from the inode past the @chain[0].p
  691. * (no partially truncated stuff there).
  692. */
  693. static Indirect *ext2_find_shared(struct inode *inode,
  694. int depth,
  695. int offsets[4],
  696. Indirect chain[4],
  697. __le32 *top)
  698. {
  699. Indirect *partial, *p;
  700. int k, err;
  701. *top = 0;
  702. for (k = depth; k > 1 && !offsets[k-1]; k--)
  703. ;
  704. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  705. if (!partial)
  706. partial = chain + k-1;
  707. /*
  708. * If the branch acquired continuation since we've looked at it -
  709. * fine, it should all survive and (new) top doesn't belong to us.
  710. */
  711. write_lock(&EXT2_I(inode)->i_meta_lock);
  712. if (!partial->key && *partial->p) {
  713. write_unlock(&EXT2_I(inode)->i_meta_lock);
  714. goto no_top;
  715. }
  716. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  717. ;
  718. /*
  719. * OK, we've found the last block that must survive. The rest of our
  720. * branch should be detached before unlocking. However, if that rest
  721. * of branch is all ours and does not grow immediately from the inode
  722. * it's easier to cheat and just decrement partial->p.
  723. */
  724. if (p == chain + k - 1 && p > chain) {
  725. p->p--;
  726. } else {
  727. *top = *p->p;
  728. *p->p = 0;
  729. }
  730. write_unlock(&EXT2_I(inode)->i_meta_lock);
  731. while(partial > p)
  732. {
  733. brelse(partial->bh);
  734. partial--;
  735. }
  736. no_top:
  737. return partial;
  738. }
  739. /**
  740. * ext2_free_data - free a list of data blocks
  741. * @inode: inode we are dealing with
  742. * @p: array of block numbers
  743. * @q: points immediately past the end of array
  744. *
  745. * We are freeing all blocks refered from that array (numbers are
  746. * stored as little-endian 32-bit) and updating @inode->i_blocks
  747. * appropriately.
  748. */
  749. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  750. {
  751. unsigned long block_to_free = 0, count = 0;
  752. unsigned long nr;
  753. for ( ; p < q ; p++) {
  754. nr = le32_to_cpu(*p);
  755. if (nr) {
  756. *p = 0;
  757. /* accumulate blocks to free if they're contiguous */
  758. if (count == 0)
  759. goto free_this;
  760. else if (block_to_free == nr - count)
  761. count++;
  762. else {
  763. mark_inode_dirty(inode);
  764. ext2_free_blocks (inode, block_to_free, count);
  765. free_this:
  766. block_to_free = nr;
  767. count = 1;
  768. }
  769. }
  770. }
  771. if (count > 0) {
  772. mark_inode_dirty(inode);
  773. ext2_free_blocks (inode, block_to_free, count);
  774. }
  775. }
  776. /**
  777. * ext2_free_branches - free an array of branches
  778. * @inode: inode we are dealing with
  779. * @p: array of block numbers
  780. * @q: pointer immediately past the end of array
  781. * @depth: depth of the branches to free
  782. *
  783. * We are freeing all blocks refered from these branches (numbers are
  784. * stored as little-endian 32-bit) and updating @inode->i_blocks
  785. * appropriately.
  786. */
  787. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  788. {
  789. struct buffer_head * bh;
  790. unsigned long nr;
  791. if (depth--) {
  792. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  793. for ( ; p < q ; p++) {
  794. nr = le32_to_cpu(*p);
  795. if (!nr)
  796. continue;
  797. *p = 0;
  798. bh = sb_bread(inode->i_sb, nr);
  799. /*
  800. * A read failure? Report error and clear slot
  801. * (should be rare).
  802. */
  803. if (!bh) {
  804. ext2_error(inode->i_sb, "ext2_free_branches",
  805. "Read failure, inode=%ld, block=%ld",
  806. inode->i_ino, nr);
  807. continue;
  808. }
  809. ext2_free_branches(inode,
  810. (__le32*)bh->b_data,
  811. (__le32*)bh->b_data + addr_per_block,
  812. depth);
  813. bforget(bh);
  814. ext2_free_blocks(inode, nr, 1);
  815. mark_inode_dirty(inode);
  816. }
  817. } else
  818. ext2_free_data(inode, p, q);
  819. }
  820. void ext2_truncate (struct inode * inode)
  821. {
  822. __le32 *i_data = EXT2_I(inode)->i_data;
  823. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  824. int offsets[4];
  825. Indirect chain[4];
  826. Indirect *partial;
  827. __le32 nr = 0;
  828. int n;
  829. long iblock;
  830. unsigned blocksize;
  831. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  832. S_ISLNK(inode->i_mode)))
  833. return;
  834. if (ext2_inode_is_fast_symlink(inode))
  835. return;
  836. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  837. return;
  838. ext2_discard_prealloc(inode);
  839. blocksize = inode->i_sb->s_blocksize;
  840. iblock = (inode->i_size + blocksize-1)
  841. >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  842. if (mapping_is_xip(inode->i_mapping))
  843. xip_truncate_page(inode->i_mapping, inode->i_size);
  844. else if (test_opt(inode->i_sb, NOBH))
  845. nobh_truncate_page(inode->i_mapping, inode->i_size);
  846. else
  847. block_truncate_page(inode->i_mapping,
  848. inode->i_size, ext2_get_block);
  849. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  850. if (n == 0)
  851. return;
  852. if (n == 1) {
  853. ext2_free_data(inode, i_data+offsets[0],
  854. i_data + EXT2_NDIR_BLOCKS);
  855. goto do_indirects;
  856. }
  857. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  858. /* Kill the top of shared branch (already detached) */
  859. if (nr) {
  860. if (partial == chain)
  861. mark_inode_dirty(inode);
  862. else
  863. mark_buffer_dirty_inode(partial->bh, inode);
  864. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  865. }
  866. /* Clear the ends of indirect blocks on the shared branch */
  867. while (partial > chain) {
  868. ext2_free_branches(inode,
  869. partial->p + 1,
  870. (__le32*)partial->bh->b_data+addr_per_block,
  871. (chain+n-1) - partial);
  872. mark_buffer_dirty_inode(partial->bh, inode);
  873. brelse (partial->bh);
  874. partial--;
  875. }
  876. do_indirects:
  877. /* Kill the remaining (whole) subtrees */
  878. switch (offsets[0]) {
  879. default:
  880. nr = i_data[EXT2_IND_BLOCK];
  881. if (nr) {
  882. i_data[EXT2_IND_BLOCK] = 0;
  883. mark_inode_dirty(inode);
  884. ext2_free_branches(inode, &nr, &nr+1, 1);
  885. }
  886. case EXT2_IND_BLOCK:
  887. nr = i_data[EXT2_DIND_BLOCK];
  888. if (nr) {
  889. i_data[EXT2_DIND_BLOCK] = 0;
  890. mark_inode_dirty(inode);
  891. ext2_free_branches(inode, &nr, &nr+1, 2);
  892. }
  893. case EXT2_DIND_BLOCK:
  894. nr = i_data[EXT2_TIND_BLOCK];
  895. if (nr) {
  896. i_data[EXT2_TIND_BLOCK] = 0;
  897. mark_inode_dirty(inode);
  898. ext2_free_branches(inode, &nr, &nr+1, 3);
  899. }
  900. case EXT2_TIND_BLOCK:
  901. ;
  902. }
  903. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  904. if (inode_needs_sync(inode)) {
  905. sync_mapping_buffers(inode->i_mapping);
  906. ext2_sync_inode (inode);
  907. } else {
  908. mark_inode_dirty(inode);
  909. }
  910. }
  911. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  912. struct buffer_head **p)
  913. {
  914. struct buffer_head * bh;
  915. unsigned long block_group;
  916. unsigned long block;
  917. unsigned long offset;
  918. struct ext2_group_desc * gdp;
  919. *p = NULL;
  920. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  921. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  922. goto Einval;
  923. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  924. gdp = ext2_get_group_desc(sb, block_group, &bh);
  925. if (!gdp)
  926. goto Egdp;
  927. /*
  928. * Figure out the offset within the block group inode table
  929. */
  930. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  931. block = le32_to_cpu(gdp->bg_inode_table) +
  932. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  933. if (!(bh = sb_bread(sb, block)))
  934. goto Eio;
  935. *p = bh;
  936. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  937. return (struct ext2_inode *) (bh->b_data + offset);
  938. Einval:
  939. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  940. (unsigned long) ino);
  941. return ERR_PTR(-EINVAL);
  942. Eio:
  943. ext2_error(sb, "ext2_get_inode",
  944. "unable to read inode block - inode=%lu, block=%lu",
  945. (unsigned long) ino, block);
  946. Egdp:
  947. return ERR_PTR(-EIO);
  948. }
  949. void ext2_set_inode_flags(struct inode *inode)
  950. {
  951. unsigned int flags = EXT2_I(inode)->i_flags;
  952. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  953. if (flags & EXT2_SYNC_FL)
  954. inode->i_flags |= S_SYNC;
  955. if (flags & EXT2_APPEND_FL)
  956. inode->i_flags |= S_APPEND;
  957. if (flags & EXT2_IMMUTABLE_FL)
  958. inode->i_flags |= S_IMMUTABLE;
  959. if (flags & EXT2_NOATIME_FL)
  960. inode->i_flags |= S_NOATIME;
  961. if (flags & EXT2_DIRSYNC_FL)
  962. inode->i_flags |= S_DIRSYNC;
  963. }
  964. void ext2_read_inode (struct inode * inode)
  965. {
  966. struct ext2_inode_info *ei = EXT2_I(inode);
  967. ino_t ino = inode->i_ino;
  968. struct buffer_head * bh;
  969. struct ext2_inode * raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  970. int n;
  971. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  972. ei->i_acl = EXT2_ACL_NOT_CACHED;
  973. ei->i_default_acl = EXT2_ACL_NOT_CACHED;
  974. #endif
  975. if (IS_ERR(raw_inode))
  976. goto bad_inode;
  977. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  978. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  979. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  980. if (!(test_opt (inode->i_sb, NO_UID32))) {
  981. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  982. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  983. }
  984. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  985. inode->i_size = le32_to_cpu(raw_inode->i_size);
  986. inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
  987. inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
  988. inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
  989. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  990. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  991. /* We now have enough fields to check if the inode was active or not.
  992. * This is needed because nfsd might try to access dead inodes
  993. * the test is that same one that e2fsck uses
  994. * NeilBrown 1999oct15
  995. */
  996. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  997. /* this inode is deleted */
  998. brelse (bh);
  999. goto bad_inode;
  1000. }
  1001. inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
  1002. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1003. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1004. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1005. ei->i_frag_no = raw_inode->i_frag;
  1006. ei->i_frag_size = raw_inode->i_fsize;
  1007. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1008. ei->i_dir_acl = 0;
  1009. if (S_ISREG(inode->i_mode))
  1010. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1011. else
  1012. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1013. ei->i_dtime = 0;
  1014. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1015. ei->i_state = 0;
  1016. ei->i_next_alloc_block = 0;
  1017. ei->i_next_alloc_goal = 0;
  1018. ei->i_prealloc_count = 0;
  1019. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1020. ei->i_dir_start_lookup = 0;
  1021. /*
  1022. * NOTE! The in-memory inode i_data array is in little-endian order
  1023. * even on big-endian machines: we do NOT byteswap the block numbers!
  1024. */
  1025. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1026. ei->i_data[n] = raw_inode->i_block[n];
  1027. if (S_ISREG(inode->i_mode)) {
  1028. inode->i_op = &ext2_file_inode_operations;
  1029. if (ext2_use_xip(inode->i_sb)) {
  1030. inode->i_mapping->a_ops = &ext2_aops_xip;
  1031. inode->i_fop = &ext2_xip_file_operations;
  1032. } else if (test_opt(inode->i_sb, NOBH)) {
  1033. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1034. inode->i_fop = &ext2_file_operations;
  1035. } else {
  1036. inode->i_mapping->a_ops = &ext2_aops;
  1037. inode->i_fop = &ext2_file_operations;
  1038. }
  1039. } else if (S_ISDIR(inode->i_mode)) {
  1040. inode->i_op = &ext2_dir_inode_operations;
  1041. inode->i_fop = &ext2_dir_operations;
  1042. if (test_opt(inode->i_sb, NOBH))
  1043. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1044. else
  1045. inode->i_mapping->a_ops = &ext2_aops;
  1046. } else if (S_ISLNK(inode->i_mode)) {
  1047. if (ext2_inode_is_fast_symlink(inode))
  1048. inode->i_op = &ext2_fast_symlink_inode_operations;
  1049. else {
  1050. inode->i_op = &ext2_symlink_inode_operations;
  1051. if (test_opt(inode->i_sb, NOBH))
  1052. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1053. else
  1054. inode->i_mapping->a_ops = &ext2_aops;
  1055. }
  1056. } else {
  1057. inode->i_op = &ext2_special_inode_operations;
  1058. if (raw_inode->i_block[0])
  1059. init_special_inode(inode, inode->i_mode,
  1060. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1061. else
  1062. init_special_inode(inode, inode->i_mode,
  1063. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1064. }
  1065. brelse (bh);
  1066. ext2_set_inode_flags(inode);
  1067. return;
  1068. bad_inode:
  1069. make_bad_inode(inode);
  1070. return;
  1071. }
  1072. static int ext2_update_inode(struct inode * inode, int do_sync)
  1073. {
  1074. struct ext2_inode_info *ei = EXT2_I(inode);
  1075. struct super_block *sb = inode->i_sb;
  1076. ino_t ino = inode->i_ino;
  1077. uid_t uid = inode->i_uid;
  1078. gid_t gid = inode->i_gid;
  1079. struct buffer_head * bh;
  1080. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1081. int n;
  1082. int err = 0;
  1083. if (IS_ERR(raw_inode))
  1084. return -EIO;
  1085. /* For fields not not tracking in the in-memory inode,
  1086. * initialise them to zero for new inodes. */
  1087. if (ei->i_state & EXT2_STATE_NEW)
  1088. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1089. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1090. if (!(test_opt(sb, NO_UID32))) {
  1091. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1092. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1093. /*
  1094. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1095. * re-used with the upper 16 bits of the uid/gid intact
  1096. */
  1097. if (!ei->i_dtime) {
  1098. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1099. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1100. } else {
  1101. raw_inode->i_uid_high = 0;
  1102. raw_inode->i_gid_high = 0;
  1103. }
  1104. } else {
  1105. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1106. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1107. raw_inode->i_uid_high = 0;
  1108. raw_inode->i_gid_high = 0;
  1109. }
  1110. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1111. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1112. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1113. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1114. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1115. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1116. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1117. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1118. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1119. raw_inode->i_frag = ei->i_frag_no;
  1120. raw_inode->i_fsize = ei->i_frag_size;
  1121. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1122. if (!S_ISREG(inode->i_mode))
  1123. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1124. else {
  1125. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1126. if (inode->i_size > 0x7fffffffULL) {
  1127. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1128. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1129. EXT2_SB(sb)->s_es->s_rev_level ==
  1130. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1131. /* If this is the first large file
  1132. * created, add a flag to the superblock.
  1133. */
  1134. lock_kernel();
  1135. ext2_update_dynamic_rev(sb);
  1136. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1137. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1138. unlock_kernel();
  1139. ext2_write_super(sb);
  1140. }
  1141. }
  1142. }
  1143. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1144. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1145. if (old_valid_dev(inode->i_rdev)) {
  1146. raw_inode->i_block[0] =
  1147. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1148. raw_inode->i_block[1] = 0;
  1149. } else {
  1150. raw_inode->i_block[0] = 0;
  1151. raw_inode->i_block[1] =
  1152. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1153. raw_inode->i_block[2] = 0;
  1154. }
  1155. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1156. raw_inode->i_block[n] = ei->i_data[n];
  1157. mark_buffer_dirty(bh);
  1158. if (do_sync) {
  1159. sync_dirty_buffer(bh);
  1160. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1161. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1162. sb->s_id, (unsigned long) ino);
  1163. err = -EIO;
  1164. }
  1165. }
  1166. ei->i_state &= ~EXT2_STATE_NEW;
  1167. brelse (bh);
  1168. return err;
  1169. }
  1170. int ext2_write_inode(struct inode *inode, int wait)
  1171. {
  1172. return ext2_update_inode(inode, wait);
  1173. }
  1174. int ext2_sync_inode(struct inode *inode)
  1175. {
  1176. struct writeback_control wbc = {
  1177. .sync_mode = WB_SYNC_ALL,
  1178. .nr_to_write = 0, /* sys_fsync did this */
  1179. };
  1180. return sync_inode(inode, &wbc);
  1181. }
  1182. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1183. {
  1184. struct inode *inode = dentry->d_inode;
  1185. int error;
  1186. error = inode_change_ok(inode, iattr);
  1187. if (error)
  1188. return error;
  1189. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  1190. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  1191. error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0;
  1192. if (error)
  1193. return error;
  1194. }
  1195. error = inode_setattr(inode, iattr);
  1196. if (!error && (iattr->ia_valid & ATTR_MODE))
  1197. error = ext2_acl_chmod(inode);
  1198. return error;
  1199. }