inode.c 45 KB

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