inode.c 37 KB

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