inode.c 38 KB

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