inode.c 44 KB

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