inode.c 42 KB

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