inode.c 37 KB

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