inode.c 42 KB

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