inode.c 47 KB

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