inode.c 45 KB

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