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