inode.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. branch[n].bh = bh;
  450. lock_buffer(bh);
  451. memset(bh->b_data, 0, blocksize);
  452. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  453. branch[n].key = cpu_to_le32(new_blocks[n]);
  454. *branch[n].p = branch[n].key;
  455. if ( n == indirect_blks) {
  456. current_block = new_blocks[n];
  457. /*
  458. * End of chain, update the last new metablock of
  459. * the chain to point to the new allocated
  460. * data blocks numbers
  461. */
  462. for (i=1; i < num; i++)
  463. *(branch[n].p + i) = cpu_to_le32(++current_block);
  464. }
  465. set_buffer_uptodate(bh);
  466. unlock_buffer(bh);
  467. mark_buffer_dirty_inode(bh, inode);
  468. /* We used to sync bh here if IS_SYNC(inode).
  469. * But we now rely upon generic_write_sync()
  470. * and b_inode_buffers. But not for directories.
  471. */
  472. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  473. sync_dirty_buffer(bh);
  474. }
  475. *blks = num;
  476. return err;
  477. }
  478. /**
  479. * ext2_splice_branch - splice the allocated branch onto inode.
  480. * @inode: owner
  481. * @block: (logical) number of block we are adding
  482. * @where: location of missing link
  483. * @num: number of indirect blocks we are adding
  484. * @blks: number of direct blocks we are adding
  485. *
  486. * This function fills the missing link and does all housekeeping needed in
  487. * inode (->i_blocks, etc.). In case of success we end up with the full
  488. * chain to new block and return 0.
  489. */
  490. static void ext2_splice_branch(struct inode *inode,
  491. long block, Indirect *where, int num, int blks)
  492. {
  493. int i;
  494. struct ext2_block_alloc_info *block_i;
  495. ext2_fsblk_t current_block;
  496. block_i = EXT2_I(inode)->i_block_alloc_info;
  497. /* XXX LOCKING probably should have i_meta_lock ?*/
  498. /* That's it */
  499. *where->p = where->key;
  500. /*
  501. * Update the host buffer_head or inode to point to more just allocated
  502. * direct blocks blocks
  503. */
  504. if (num == 0 && blks > 1) {
  505. current_block = le32_to_cpu(where->key) + 1;
  506. for (i = 1; i < blks; i++)
  507. *(where->p + i ) = cpu_to_le32(current_block++);
  508. }
  509. /*
  510. * update the most recently allocated logical & physical block
  511. * in i_block_alloc_info, to assist find the proper goal block for next
  512. * allocation
  513. */
  514. if (block_i) {
  515. block_i->last_alloc_logical_block = block + blks - 1;
  516. block_i->last_alloc_physical_block =
  517. le32_to_cpu(where[num].key) + blks - 1;
  518. }
  519. /* We are done with atomic stuff, now do the rest of housekeeping */
  520. /* had we spliced it onto indirect block? */
  521. if (where->bh)
  522. mark_buffer_dirty_inode(where->bh, inode);
  523. inode->i_ctime = CURRENT_TIME_SEC;
  524. mark_inode_dirty(inode);
  525. }
  526. /*
  527. * Allocation strategy is simple: if we have to allocate something, we will
  528. * have to go the whole way to leaf. So let's do it before attaching anything
  529. * to tree, set linkage between the newborn blocks, write them if sync is
  530. * required, recheck the path, free and repeat if check fails, otherwise
  531. * set the last missing link (that will protect us from any truncate-generated
  532. * removals - all blocks on the path are immune now) and possibly force the
  533. * write on the parent block.
  534. * That has a nice additional property: no special recovery from the failed
  535. * allocations is needed - we simply release blocks and do not touch anything
  536. * reachable from inode.
  537. *
  538. * `handle' can be NULL if create == 0.
  539. *
  540. * return > 0, # of blocks mapped or allocated.
  541. * return = 0, if plain lookup failed.
  542. * return < 0, error case.
  543. */
  544. static int ext2_get_blocks(struct inode *inode,
  545. sector_t iblock, unsigned long maxblocks,
  546. struct buffer_head *bh_result,
  547. int create)
  548. {
  549. int err = -EIO;
  550. int offsets[4];
  551. Indirect chain[4];
  552. Indirect *partial;
  553. ext2_fsblk_t goal;
  554. int indirect_blks;
  555. int blocks_to_boundary = 0;
  556. int depth;
  557. struct ext2_inode_info *ei = EXT2_I(inode);
  558. int count = 0;
  559. ext2_fsblk_t first_block = 0;
  560. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  561. if (depth == 0)
  562. return (err);
  563. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  564. /* Simplest case - block found, no allocation needed */
  565. if (!partial) {
  566. first_block = le32_to_cpu(chain[depth - 1].key);
  567. clear_buffer_new(bh_result); /* What's this do? */
  568. count++;
  569. /*map more blocks*/
  570. while (count < maxblocks && count <= blocks_to_boundary) {
  571. ext2_fsblk_t blk;
  572. if (!verify_chain(chain, chain + depth - 1)) {
  573. /*
  574. * Indirect block might be removed by
  575. * truncate while we were reading it.
  576. * Handling of that case: forget what we've
  577. * got now, go to reread.
  578. */
  579. err = -EAGAIN;
  580. count = 0;
  581. break;
  582. }
  583. blk = le32_to_cpu(*(chain[depth-1].p + count));
  584. if (blk == first_block + count)
  585. count++;
  586. else
  587. break;
  588. }
  589. if (err != -EAGAIN)
  590. goto got_it;
  591. }
  592. /* Next simple case - plain lookup or failed read of indirect block */
  593. if (!create || err == -EIO)
  594. goto cleanup;
  595. mutex_lock(&ei->truncate_mutex);
  596. /*
  597. * If the indirect block is missing while we are reading
  598. * the chain(ext2_get_branch() returns -EAGAIN err), or
  599. * if the chain has been changed after we grab the semaphore,
  600. * (either because another process truncated this branch, or
  601. * another get_block allocated this branch) re-grab the chain to see if
  602. * the request block has been allocated or not.
  603. *
  604. * Since we already block the truncate/other get_block
  605. * at this point, we will have the current copy of the chain when we
  606. * splice the branch into the tree.
  607. */
  608. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  609. while (partial > chain) {
  610. brelse(partial->bh);
  611. partial--;
  612. }
  613. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  614. if (!partial) {
  615. count++;
  616. mutex_unlock(&ei->truncate_mutex);
  617. if (err)
  618. goto cleanup;
  619. clear_buffer_new(bh_result);
  620. goto got_it;
  621. }
  622. }
  623. /*
  624. * Okay, we need to do block allocation. Lazily initialize the block
  625. * allocation info here if necessary
  626. */
  627. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  628. ext2_init_block_alloc_info(inode);
  629. goal = ext2_find_goal(inode, iblock, partial);
  630. /* the number of blocks need to allocate for [d,t]indirect blocks */
  631. indirect_blks = (chain + depth) - partial - 1;
  632. /*
  633. * Next look up the indirect map to count the totoal number of
  634. * direct blocks to allocate for this branch.
  635. */
  636. count = ext2_blks_to_allocate(partial, indirect_blks,
  637. maxblocks, blocks_to_boundary);
  638. /*
  639. * XXX ???? Block out ext2_truncate while we alter the tree
  640. */
  641. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  642. offsets + (partial - chain), partial);
  643. if (err) {
  644. mutex_unlock(&ei->truncate_mutex);
  645. goto cleanup;
  646. }
  647. if (ext2_use_xip(inode->i_sb)) {
  648. /*
  649. * we need to clear the block
  650. */
  651. err = ext2_clear_xip_target (inode,
  652. le32_to_cpu(chain[depth-1].key));
  653. if (err) {
  654. mutex_unlock(&ei->truncate_mutex);
  655. goto cleanup;
  656. }
  657. }
  658. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  659. mutex_unlock(&ei->truncate_mutex);
  660. set_buffer_new(bh_result);
  661. got_it:
  662. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  663. if (count > blocks_to_boundary)
  664. set_buffer_boundary(bh_result);
  665. err = count;
  666. /* Clean up and exit */
  667. partial = chain + depth - 1; /* the whole chain */
  668. cleanup:
  669. while (partial > chain) {
  670. brelse(partial->bh);
  671. partial--;
  672. }
  673. return err;
  674. }
  675. int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  676. {
  677. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  678. int ret = ext2_get_blocks(inode, iblock, max_blocks,
  679. bh_result, create);
  680. if (ret > 0) {
  681. bh_result->b_size = (ret << inode->i_blkbits);
  682. ret = 0;
  683. }
  684. return ret;
  685. }
  686. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  687. u64 start, u64 len)
  688. {
  689. return generic_block_fiemap(inode, fieinfo, start, len,
  690. ext2_get_block);
  691. }
  692. static int ext2_writepage(struct page *page, struct writeback_control *wbc)
  693. {
  694. return block_write_full_page(page, ext2_get_block, wbc);
  695. }
  696. static int ext2_readpage(struct file *file, struct page *page)
  697. {
  698. return mpage_readpage(page, ext2_get_block);
  699. }
  700. static int
  701. ext2_readpages(struct file *file, struct address_space *mapping,
  702. struct list_head *pages, unsigned nr_pages)
  703. {
  704. return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
  705. }
  706. static int
  707. ext2_write_begin(struct file *file, struct address_space *mapping,
  708. loff_t pos, unsigned len, unsigned flags,
  709. struct page **pagep, void **fsdata)
  710. {
  711. int ret;
  712. ret = block_write_begin(mapping, pos, len, flags, pagep,
  713. ext2_get_block);
  714. if (ret < 0)
  715. ext2_write_failed(mapping, pos + len);
  716. return ret;
  717. }
  718. static int ext2_write_end(struct file *file, struct address_space *mapping,
  719. loff_t pos, unsigned len, unsigned copied,
  720. struct page *page, void *fsdata)
  721. {
  722. int ret;
  723. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  724. if (ret < len)
  725. ext2_write_failed(mapping, pos + len);
  726. return ret;
  727. }
  728. static int
  729. ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
  730. loff_t pos, unsigned len, unsigned flags,
  731. struct page **pagep, void **fsdata)
  732. {
  733. int ret;
  734. ret = nobh_write_begin(mapping, pos, len, flags, pagep, fsdata,
  735. ext2_get_block);
  736. if (ret < 0)
  737. ext2_write_failed(mapping, pos + len);
  738. return ret;
  739. }
  740. static int ext2_nobh_writepage(struct page *page,
  741. struct writeback_control *wbc)
  742. {
  743. return nobh_writepage(page, ext2_get_block, wbc);
  744. }
  745. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  746. {
  747. return generic_block_bmap(mapping,block,ext2_get_block);
  748. }
  749. static ssize_t
  750. ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  751. loff_t offset, unsigned long nr_segs)
  752. {
  753. struct file *file = iocb->ki_filp;
  754. struct address_space *mapping = file->f_mapping;
  755. struct inode *inode = mapping->host;
  756. ssize_t ret;
  757. ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
  758. ext2_get_block);
  759. if (ret < 0 && (rw & WRITE))
  760. ext2_write_failed(mapping, offset + iov_length(iov, nr_segs));
  761. return ret;
  762. }
  763. static int
  764. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  765. {
  766. return mpage_writepages(mapping, wbc, ext2_get_block);
  767. }
  768. const struct address_space_operations ext2_aops = {
  769. .readpage = ext2_readpage,
  770. .readpages = ext2_readpages,
  771. .writepage = ext2_writepage,
  772. .write_begin = ext2_write_begin,
  773. .write_end = ext2_write_end,
  774. .bmap = ext2_bmap,
  775. .direct_IO = ext2_direct_IO,
  776. .writepages = ext2_writepages,
  777. .migratepage = buffer_migrate_page,
  778. .is_partially_uptodate = block_is_partially_uptodate,
  779. .error_remove_page = generic_error_remove_page,
  780. };
  781. const struct address_space_operations ext2_aops_xip = {
  782. .bmap = ext2_bmap,
  783. .get_xip_mem = ext2_get_xip_mem,
  784. };
  785. const struct address_space_operations ext2_nobh_aops = {
  786. .readpage = ext2_readpage,
  787. .readpages = ext2_readpages,
  788. .writepage = ext2_nobh_writepage,
  789. .write_begin = ext2_nobh_write_begin,
  790. .write_end = nobh_write_end,
  791. .bmap = ext2_bmap,
  792. .direct_IO = ext2_direct_IO,
  793. .writepages = ext2_writepages,
  794. .migratepage = buffer_migrate_page,
  795. .error_remove_page = generic_error_remove_page,
  796. };
  797. /*
  798. * Probably it should be a library function... search for first non-zero word
  799. * or memcmp with zero_page, whatever is better for particular architecture.
  800. * Linus?
  801. */
  802. static inline int all_zeroes(__le32 *p, __le32 *q)
  803. {
  804. while (p < q)
  805. if (*p++)
  806. return 0;
  807. return 1;
  808. }
  809. /**
  810. * ext2_find_shared - find the indirect blocks for partial truncation.
  811. * @inode: inode in question
  812. * @depth: depth of the affected branch
  813. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  814. * @chain: place to store the pointers to partial indirect blocks
  815. * @top: place to the (detached) top of branch
  816. *
  817. * This is a helper function used by ext2_truncate().
  818. *
  819. * When we do truncate() we may have to clean the ends of several indirect
  820. * blocks but leave the blocks themselves alive. Block is partially
  821. * truncated if some data below the new i_size is referred from it (and
  822. * it is on the path to the first completely truncated data block, indeed).
  823. * We have to free the top of that path along with everything to the right
  824. * of the path. Since no allocation past the truncation point is possible
  825. * until ext2_truncate() finishes, we may safely do the latter, but top
  826. * of branch may require special attention - pageout below the truncation
  827. * point might try to populate it.
  828. *
  829. * We atomically detach the top of branch from the tree, store the block
  830. * number of its root in *@top, pointers to buffer_heads of partially
  831. * truncated blocks - in @chain[].bh and pointers to their last elements
  832. * that should not be removed - in @chain[].p. Return value is the pointer
  833. * to last filled element of @chain.
  834. *
  835. * The work left to caller to do the actual freeing of subtrees:
  836. * a) free the subtree starting from *@top
  837. * b) free the subtrees whose roots are stored in
  838. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  839. * c) free the subtrees growing from the inode past the @chain[0].p
  840. * (no partially truncated stuff there).
  841. */
  842. static Indirect *ext2_find_shared(struct inode *inode,
  843. int depth,
  844. int offsets[4],
  845. Indirect chain[4],
  846. __le32 *top)
  847. {
  848. Indirect *partial, *p;
  849. int k, err;
  850. *top = 0;
  851. for (k = depth; k > 1 && !offsets[k-1]; k--)
  852. ;
  853. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  854. if (!partial)
  855. partial = chain + k-1;
  856. /*
  857. * If the branch acquired continuation since we've looked at it -
  858. * fine, it should all survive and (new) top doesn't belong to us.
  859. */
  860. write_lock(&EXT2_I(inode)->i_meta_lock);
  861. if (!partial->key && *partial->p) {
  862. write_unlock(&EXT2_I(inode)->i_meta_lock);
  863. goto no_top;
  864. }
  865. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  866. ;
  867. /*
  868. * OK, we've found the last block that must survive. The rest of our
  869. * branch should be detached before unlocking. However, if that rest
  870. * of branch is all ours and does not grow immediately from the inode
  871. * it's easier to cheat and just decrement partial->p.
  872. */
  873. if (p == chain + k - 1 && p > chain) {
  874. p->p--;
  875. } else {
  876. *top = *p->p;
  877. *p->p = 0;
  878. }
  879. write_unlock(&EXT2_I(inode)->i_meta_lock);
  880. while(partial > p)
  881. {
  882. brelse(partial->bh);
  883. partial--;
  884. }
  885. no_top:
  886. return partial;
  887. }
  888. /**
  889. * ext2_free_data - free a list of data blocks
  890. * @inode: inode we are dealing with
  891. * @p: array of block numbers
  892. * @q: points immediately past the end of array
  893. *
  894. * We are freeing all blocks referred from that array (numbers are
  895. * stored as little-endian 32-bit) and updating @inode->i_blocks
  896. * appropriately.
  897. */
  898. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  899. {
  900. unsigned long block_to_free = 0, count = 0;
  901. unsigned long nr;
  902. for ( ; p < q ; p++) {
  903. nr = le32_to_cpu(*p);
  904. if (nr) {
  905. *p = 0;
  906. /* accumulate blocks to free if they're contiguous */
  907. if (count == 0)
  908. goto free_this;
  909. else if (block_to_free == nr - count)
  910. count++;
  911. else {
  912. ext2_free_blocks (inode, block_to_free, count);
  913. mark_inode_dirty(inode);
  914. free_this:
  915. block_to_free = nr;
  916. count = 1;
  917. }
  918. }
  919. }
  920. if (count > 0) {
  921. ext2_free_blocks (inode, block_to_free, count);
  922. mark_inode_dirty(inode);
  923. }
  924. }
  925. /**
  926. * ext2_free_branches - free an array of branches
  927. * @inode: inode we are dealing with
  928. * @p: array of block numbers
  929. * @q: pointer immediately past the end of array
  930. * @depth: depth of the branches to free
  931. *
  932. * We are freeing all blocks referred from these branches (numbers are
  933. * stored as little-endian 32-bit) and updating @inode->i_blocks
  934. * appropriately.
  935. */
  936. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  937. {
  938. struct buffer_head * bh;
  939. unsigned long nr;
  940. if (depth--) {
  941. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  942. for ( ; p < q ; p++) {
  943. nr = le32_to_cpu(*p);
  944. if (!nr)
  945. continue;
  946. *p = 0;
  947. bh = sb_bread(inode->i_sb, nr);
  948. /*
  949. * A read failure? Report error and clear slot
  950. * (should be rare).
  951. */
  952. if (!bh) {
  953. ext2_error(inode->i_sb, "ext2_free_branches",
  954. "Read failure, inode=%ld, block=%ld",
  955. inode->i_ino, nr);
  956. continue;
  957. }
  958. ext2_free_branches(inode,
  959. (__le32*)bh->b_data,
  960. (__le32*)bh->b_data + addr_per_block,
  961. depth);
  962. bforget(bh);
  963. ext2_free_blocks(inode, nr, 1);
  964. mark_inode_dirty(inode);
  965. }
  966. } else
  967. ext2_free_data(inode, p, q);
  968. }
  969. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  970. {
  971. __le32 *i_data = EXT2_I(inode)->i_data;
  972. struct ext2_inode_info *ei = EXT2_I(inode);
  973. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  974. int offsets[4];
  975. Indirect chain[4];
  976. Indirect *partial;
  977. __le32 nr = 0;
  978. int n;
  979. long iblock;
  980. unsigned blocksize;
  981. blocksize = inode->i_sb->s_blocksize;
  982. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  983. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  984. if (n == 0)
  985. return;
  986. /*
  987. * From here we block out all ext2_get_block() callers who want to
  988. * modify the block allocation tree.
  989. */
  990. mutex_lock(&ei->truncate_mutex);
  991. if (n == 1) {
  992. ext2_free_data(inode, i_data+offsets[0],
  993. i_data + EXT2_NDIR_BLOCKS);
  994. goto do_indirects;
  995. }
  996. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  997. /* Kill the top of shared branch (already detached) */
  998. if (nr) {
  999. if (partial == chain)
  1000. mark_inode_dirty(inode);
  1001. else
  1002. mark_buffer_dirty_inode(partial->bh, inode);
  1003. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1004. }
  1005. /* Clear the ends of indirect blocks on the shared branch */
  1006. while (partial > chain) {
  1007. ext2_free_branches(inode,
  1008. partial->p + 1,
  1009. (__le32*)partial->bh->b_data+addr_per_block,
  1010. (chain+n-1) - partial);
  1011. mark_buffer_dirty_inode(partial->bh, inode);
  1012. brelse (partial->bh);
  1013. partial--;
  1014. }
  1015. do_indirects:
  1016. /* Kill the remaining (whole) subtrees */
  1017. switch (offsets[0]) {
  1018. default:
  1019. nr = i_data[EXT2_IND_BLOCK];
  1020. if (nr) {
  1021. i_data[EXT2_IND_BLOCK] = 0;
  1022. mark_inode_dirty(inode);
  1023. ext2_free_branches(inode, &nr, &nr+1, 1);
  1024. }
  1025. case EXT2_IND_BLOCK:
  1026. nr = i_data[EXT2_DIND_BLOCK];
  1027. if (nr) {
  1028. i_data[EXT2_DIND_BLOCK] = 0;
  1029. mark_inode_dirty(inode);
  1030. ext2_free_branches(inode, &nr, &nr+1, 2);
  1031. }
  1032. case EXT2_DIND_BLOCK:
  1033. nr = i_data[EXT2_TIND_BLOCK];
  1034. if (nr) {
  1035. i_data[EXT2_TIND_BLOCK] = 0;
  1036. mark_inode_dirty(inode);
  1037. ext2_free_branches(inode, &nr, &nr+1, 3);
  1038. }
  1039. case EXT2_TIND_BLOCK:
  1040. ;
  1041. }
  1042. ext2_discard_reservation(inode);
  1043. mutex_unlock(&ei->truncate_mutex);
  1044. }
  1045. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1046. {
  1047. /*
  1048. * XXX: it seems like a bug here that we don't allow
  1049. * IS_APPEND inode to have blocks-past-i_size trimmed off.
  1050. * review and fix this.
  1051. *
  1052. * Also would be nice to be able to handle IO errors and such,
  1053. * but that's probably too much to ask.
  1054. */
  1055. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1056. S_ISLNK(inode->i_mode)))
  1057. return;
  1058. if (ext2_inode_is_fast_symlink(inode))
  1059. return;
  1060. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1061. return;
  1062. __ext2_truncate_blocks(inode, offset);
  1063. }
  1064. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1065. {
  1066. int error;
  1067. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1068. S_ISLNK(inode->i_mode)))
  1069. return -EINVAL;
  1070. if (ext2_inode_is_fast_symlink(inode))
  1071. return -EINVAL;
  1072. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1073. return -EPERM;
  1074. inode_dio_wait(inode);
  1075. if (mapping_is_xip(inode->i_mapping))
  1076. error = xip_truncate_page(inode->i_mapping, newsize);
  1077. else if (test_opt(inode->i_sb, NOBH))
  1078. error = nobh_truncate_page(inode->i_mapping,
  1079. newsize, ext2_get_block);
  1080. else
  1081. error = block_truncate_page(inode->i_mapping,
  1082. newsize, ext2_get_block);
  1083. if (error)
  1084. return error;
  1085. truncate_setsize(inode, newsize);
  1086. __ext2_truncate_blocks(inode, newsize);
  1087. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  1088. if (inode_needs_sync(inode)) {
  1089. sync_mapping_buffers(inode->i_mapping);
  1090. sync_inode_metadata(inode, 1);
  1091. } else {
  1092. mark_inode_dirty(inode);
  1093. }
  1094. return 0;
  1095. }
  1096. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1097. struct buffer_head **p)
  1098. {
  1099. struct buffer_head * bh;
  1100. unsigned long block_group;
  1101. unsigned long block;
  1102. unsigned long offset;
  1103. struct ext2_group_desc * gdp;
  1104. *p = NULL;
  1105. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1106. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1107. goto Einval;
  1108. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1109. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1110. if (!gdp)
  1111. goto Egdp;
  1112. /*
  1113. * Figure out the offset within the block group inode table
  1114. */
  1115. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1116. block = le32_to_cpu(gdp->bg_inode_table) +
  1117. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1118. if (!(bh = sb_bread(sb, block)))
  1119. goto Eio;
  1120. *p = bh;
  1121. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1122. return (struct ext2_inode *) (bh->b_data + offset);
  1123. Einval:
  1124. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1125. (unsigned long) ino);
  1126. return ERR_PTR(-EINVAL);
  1127. Eio:
  1128. ext2_error(sb, "ext2_get_inode",
  1129. "unable to read inode block - inode=%lu, block=%lu",
  1130. (unsigned long) ino, block);
  1131. Egdp:
  1132. return ERR_PTR(-EIO);
  1133. }
  1134. void ext2_set_inode_flags(struct inode *inode)
  1135. {
  1136. unsigned int flags = EXT2_I(inode)->i_flags;
  1137. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  1138. if (flags & EXT2_SYNC_FL)
  1139. inode->i_flags |= S_SYNC;
  1140. if (flags & EXT2_APPEND_FL)
  1141. inode->i_flags |= S_APPEND;
  1142. if (flags & EXT2_IMMUTABLE_FL)
  1143. inode->i_flags |= S_IMMUTABLE;
  1144. if (flags & EXT2_NOATIME_FL)
  1145. inode->i_flags |= S_NOATIME;
  1146. if (flags & EXT2_DIRSYNC_FL)
  1147. inode->i_flags |= S_DIRSYNC;
  1148. }
  1149. /* Propagate flags from i_flags to EXT2_I(inode)->i_flags */
  1150. void ext2_get_inode_flags(struct ext2_inode_info *ei)
  1151. {
  1152. unsigned int flags = ei->vfs_inode.i_flags;
  1153. ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
  1154. EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
  1155. if (flags & S_SYNC)
  1156. ei->i_flags |= EXT2_SYNC_FL;
  1157. if (flags & S_APPEND)
  1158. ei->i_flags |= EXT2_APPEND_FL;
  1159. if (flags & S_IMMUTABLE)
  1160. ei->i_flags |= EXT2_IMMUTABLE_FL;
  1161. if (flags & S_NOATIME)
  1162. ei->i_flags |= EXT2_NOATIME_FL;
  1163. if (flags & S_DIRSYNC)
  1164. ei->i_flags |= EXT2_DIRSYNC_FL;
  1165. }
  1166. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1167. {
  1168. struct ext2_inode_info *ei;
  1169. struct buffer_head * bh;
  1170. struct ext2_inode *raw_inode;
  1171. struct inode *inode;
  1172. long ret = -EIO;
  1173. int n;
  1174. uid_t i_uid;
  1175. gid_t i_gid;
  1176. inode = iget_locked(sb, ino);
  1177. if (!inode)
  1178. return ERR_PTR(-ENOMEM);
  1179. if (!(inode->i_state & I_NEW))
  1180. return inode;
  1181. ei = EXT2_I(inode);
  1182. ei->i_block_alloc_info = NULL;
  1183. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1184. if (IS_ERR(raw_inode)) {
  1185. ret = PTR_ERR(raw_inode);
  1186. goto bad_inode;
  1187. }
  1188. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1189. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1190. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1191. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1192. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1193. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1194. }
  1195. i_uid_write(inode, i_uid);
  1196. i_gid_write(inode, i_gid);
  1197. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1198. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1199. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  1200. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  1201. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  1202. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  1203. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1204. /* We now have enough fields to check if the inode was active or not.
  1205. * This is needed because nfsd might try to access dead inodes
  1206. * the test is that same one that e2fsck uses
  1207. * NeilBrown 1999oct15
  1208. */
  1209. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1210. /* this inode is deleted */
  1211. brelse (bh);
  1212. ret = -ESTALE;
  1213. goto bad_inode;
  1214. }
  1215. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1216. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1217. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1218. ei->i_frag_no = raw_inode->i_frag;
  1219. ei->i_frag_size = raw_inode->i_fsize;
  1220. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1221. ei->i_dir_acl = 0;
  1222. if (S_ISREG(inode->i_mode))
  1223. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1224. else
  1225. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1226. ei->i_dtime = 0;
  1227. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1228. ei->i_state = 0;
  1229. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1230. ei->i_dir_start_lookup = 0;
  1231. /*
  1232. * NOTE! The in-memory inode i_data array is in little-endian order
  1233. * even on big-endian machines: we do NOT byteswap the block numbers!
  1234. */
  1235. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1236. ei->i_data[n] = raw_inode->i_block[n];
  1237. if (S_ISREG(inode->i_mode)) {
  1238. inode->i_op = &ext2_file_inode_operations;
  1239. if (ext2_use_xip(inode->i_sb)) {
  1240. inode->i_mapping->a_ops = &ext2_aops_xip;
  1241. inode->i_fop = &ext2_xip_file_operations;
  1242. } else if (test_opt(inode->i_sb, NOBH)) {
  1243. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1244. inode->i_fop = &ext2_file_operations;
  1245. } else {
  1246. inode->i_mapping->a_ops = &ext2_aops;
  1247. inode->i_fop = &ext2_file_operations;
  1248. }
  1249. } else if (S_ISDIR(inode->i_mode)) {
  1250. inode->i_op = &ext2_dir_inode_operations;
  1251. inode->i_fop = &ext2_dir_operations;
  1252. if (test_opt(inode->i_sb, NOBH))
  1253. inode->i_mapping->a_ops = &ext2_nobh_aops;
  1254. else
  1255. inode->i_mapping->a_ops = &ext2_aops;
  1256. } else if (S_ISLNK(inode->i_mode)) {
  1257. if (ext2_inode_is_fast_symlink(inode)) {
  1258. inode->i_op = &ext2_fast_symlink_inode_operations;
  1259. nd_terminate_link(ei->i_data, inode->i_size,
  1260. sizeof(ei->i_data) - 1);
  1261. } else {
  1262. inode->i_op = &ext2_symlink_inode_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. }
  1268. } else {
  1269. inode->i_op = &ext2_special_inode_operations;
  1270. if (raw_inode->i_block[0])
  1271. init_special_inode(inode, inode->i_mode,
  1272. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1273. else
  1274. init_special_inode(inode, inode->i_mode,
  1275. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1276. }
  1277. brelse (bh);
  1278. ext2_set_inode_flags(inode);
  1279. unlock_new_inode(inode);
  1280. return inode;
  1281. bad_inode:
  1282. iget_failed(inode);
  1283. return ERR_PTR(ret);
  1284. }
  1285. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1286. {
  1287. struct ext2_inode_info *ei = EXT2_I(inode);
  1288. struct super_block *sb = inode->i_sb;
  1289. ino_t ino = inode->i_ino;
  1290. uid_t uid = i_uid_read(inode);
  1291. gid_t gid = i_gid_read(inode);
  1292. struct buffer_head * bh;
  1293. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1294. int n;
  1295. int err = 0;
  1296. if (IS_ERR(raw_inode))
  1297. return -EIO;
  1298. /* For fields not not tracking in the in-memory inode,
  1299. * initialise them to zero for new inodes. */
  1300. if (ei->i_state & EXT2_STATE_NEW)
  1301. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1302. ext2_get_inode_flags(ei);
  1303. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1304. if (!(test_opt(sb, NO_UID32))) {
  1305. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1306. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1307. /*
  1308. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1309. * re-used with the upper 16 bits of the uid/gid intact
  1310. */
  1311. if (!ei->i_dtime) {
  1312. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1313. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1314. } else {
  1315. raw_inode->i_uid_high = 0;
  1316. raw_inode->i_gid_high = 0;
  1317. }
  1318. } else {
  1319. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1320. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1321. raw_inode->i_uid_high = 0;
  1322. raw_inode->i_gid_high = 0;
  1323. }
  1324. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1325. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1326. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1327. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1328. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1329. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1330. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1331. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1332. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1333. raw_inode->i_frag = ei->i_frag_no;
  1334. raw_inode->i_fsize = ei->i_frag_size;
  1335. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1336. if (!S_ISREG(inode->i_mode))
  1337. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1338. else {
  1339. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1340. if (inode->i_size > 0x7fffffffULL) {
  1341. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1342. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1343. EXT2_SB(sb)->s_es->s_rev_level ==
  1344. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1345. /* If this is the first large file
  1346. * created, add a flag to the superblock.
  1347. */
  1348. spin_lock(&EXT2_SB(sb)->s_lock);
  1349. ext2_update_dynamic_rev(sb);
  1350. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1351. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1352. spin_unlock(&EXT2_SB(sb)->s_lock);
  1353. ext2_write_super(sb);
  1354. }
  1355. }
  1356. }
  1357. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1358. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1359. if (old_valid_dev(inode->i_rdev)) {
  1360. raw_inode->i_block[0] =
  1361. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1362. raw_inode->i_block[1] = 0;
  1363. } else {
  1364. raw_inode->i_block[0] = 0;
  1365. raw_inode->i_block[1] =
  1366. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1367. raw_inode->i_block[2] = 0;
  1368. }
  1369. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1370. raw_inode->i_block[n] = ei->i_data[n];
  1371. mark_buffer_dirty(bh);
  1372. if (do_sync) {
  1373. sync_dirty_buffer(bh);
  1374. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1375. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1376. sb->s_id, (unsigned long) ino);
  1377. err = -EIO;
  1378. }
  1379. }
  1380. ei->i_state &= ~EXT2_STATE_NEW;
  1381. brelse (bh);
  1382. return err;
  1383. }
  1384. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1385. {
  1386. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1387. }
  1388. int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
  1389. {
  1390. struct inode *inode = dentry->d_inode;
  1391. int error;
  1392. error = inode_change_ok(inode, iattr);
  1393. if (error)
  1394. return error;
  1395. if (is_quota_modification(inode, iattr))
  1396. dquot_initialize(inode);
  1397. if ((iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)) ||
  1398. (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))) {
  1399. error = dquot_transfer(inode, iattr);
  1400. if (error)
  1401. return error;
  1402. }
  1403. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1404. error = ext2_setsize(inode, iattr->ia_size);
  1405. if (error)
  1406. return error;
  1407. }
  1408. setattr_copy(inode, iattr);
  1409. if (iattr->ia_valid & ATTR_MODE)
  1410. error = ext2_acl_chmod(inode);
  1411. mark_inode_dirty(inode);
  1412. return error;
  1413. }