inode.c 38 KB

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