PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ext2/inode.c

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