inode.c 43 KB

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