inode.c 47 KB

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