inode.c 42 KB

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