inode.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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. 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 number of direct blocks to allocate.
  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: Allocate multiple blocks needed for a branch.
  350. * @inode: Owner.
  351. * @goal: Preferred place for allocation.
  352. * @indirect_blks: The number of blocks needed to allocate for indirect blocks.
  353. * @blks: The number of blocks need to allocate for direct blocks.
  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. * @err: Error pointer.
  357. *
  358. * Return: Number of blocks allocated.
  359. */
  360. static int ext2_alloc_blocks(struct inode *inode,
  361. ext2_fsblk_t goal, int indirect_blks, int blks,
  362. ext2_fsblk_t new_blocks[4], int *err)
  363. {
  364. int target, i;
  365. unsigned long count = 0;
  366. int index = 0;
  367. ext2_fsblk_t current_block = 0;
  368. int ret = 0;
  369. /*
  370. * Here we try to allocate the requested multiple blocks at once,
  371. * on a best-effort basis.
  372. * To build a branch, we should allocate blocks for
  373. * the indirect blocks(if not allocated yet), and at least
  374. * the first direct block of this branch. That's the
  375. * minimum number of blocks need to allocate(required)
  376. */
  377. target = blks + indirect_blks;
  378. while (1) {
  379. count = target;
  380. /* allocating blocks for indirect blocks and direct blocks */
  381. current_block = ext2_new_blocks(inode, goal, &count, err, 0);
  382. if (*err)
  383. goto failed_out;
  384. target -= count;
  385. /* allocate blocks for indirect blocks */
  386. while (index < indirect_blks && count) {
  387. new_blocks[index++] = current_block++;
  388. count--;
  389. }
  390. if (count > 0)
  391. break;
  392. }
  393. /* save the new block number for the first direct block */
  394. new_blocks[index] = current_block;
  395. /* total number of blocks allocated for direct blocks */
  396. ret = count;
  397. *err = 0;
  398. return ret;
  399. failed_out:
  400. for (i = 0; i <index; i++)
  401. ext2_free_blocks(inode, new_blocks[i], 1);
  402. if (index)
  403. mark_inode_dirty(inode);
  404. return ret;
  405. }
  406. /**
  407. * ext2_alloc_branch - allocate and set up a chain of blocks.
  408. * @inode: owner
  409. * @indirect_blks: depth of the chain (number of blocks to allocate)
  410. * @blks: number of allocated direct blocks
  411. * @goal: preferred place for allocation
  412. * @offsets: offsets (in the blocks) to store the pointers to next.
  413. * @branch: place to store the chain in.
  414. *
  415. * This function allocates @num blocks, zeroes out all but the last one,
  416. * links them into chain and (if we are synchronous) writes them to disk.
  417. * In other words, it prepares a branch that can be spliced onto the
  418. * inode. It stores the information about that chain in the branch[], in
  419. * the same format as ext2_get_branch() would do. We are calling it after
  420. * we had read the existing part of chain and partial points to the last
  421. * triple of that (one with zero ->key). Upon the exit we have the same
  422. * picture as after the successful ext2_get_block(), except that in one
  423. * place chain is disconnected - *branch->p is still zero (we did not
  424. * set the last link), but branch->key contains the number that should
  425. * be placed into *branch->p to fill that gap.
  426. *
  427. * If allocation fails we free all blocks we've allocated (and forget
  428. * their buffer_heads) and return the error value the from failed
  429. * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  430. * as described above and return 0.
  431. */
  432. static int ext2_alloc_branch(struct inode *inode,
  433. int indirect_blks, int *blks, ext2_fsblk_t goal,
  434. int *offsets, Indirect *branch)
  435. {
  436. int blocksize = inode->i_sb->s_blocksize;
  437. int i, n = 0;
  438. int err = 0;
  439. struct buffer_head *bh;
  440. int num;
  441. ext2_fsblk_t new_blocks[4];
  442. ext2_fsblk_t current_block;
  443. num = ext2_alloc_blocks(inode, goal, indirect_blks,
  444. *blks, new_blocks, &err);
  445. if (err)
  446. return err;
  447. branch[0].key = cpu_to_le32(new_blocks[0]);
  448. /*
  449. * metadata blocks and data blocks are allocated.
  450. */
  451. for (n = 1; n <= indirect_blks; n++) {
  452. /*
  453. * Get buffer_head for parent block, zero it out
  454. * and set the pointer to new one, then send
  455. * parent to disk.
  456. */
  457. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  458. if (unlikely(!bh)) {
  459. err = -ENOMEM;
  460. goto failed;
  461. }
  462. branch[n].bh = bh;
  463. lock_buffer(bh);
  464. memset(bh->b_data, 0, blocksize);
  465. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  466. branch[n].key = cpu_to_le32(new_blocks[n]);
  467. *branch[n].p = branch[n].key;
  468. if ( n == indirect_blks) {
  469. current_block = new_blocks[n];
  470. /*
  471. * End of chain, update the last new metablock of
  472. * the chain to point to the new allocated
  473. * data blocks numbers
  474. */
  475. for (i=1; i < num; i++)
  476. *(branch[n].p + i) = cpu_to_le32(++current_block);
  477. }
  478. set_buffer_uptodate(bh);
  479. unlock_buffer(bh);
  480. mark_buffer_dirty_inode(bh, inode);
  481. /* We used to sync bh here if IS_SYNC(inode).
  482. * But we now rely upon generic_write_sync()
  483. * and b_inode_buffers. But not for directories.
  484. */
  485. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  486. sync_dirty_buffer(bh);
  487. }
  488. *blks = num;
  489. return err;
  490. failed:
  491. for (i = 1; i < n; i++)
  492. bforget(branch[i].bh);
  493. for (i = 0; i < indirect_blks; i++)
  494. ext2_free_blocks(inode, new_blocks[i], 1);
  495. ext2_free_blocks(inode, new_blocks[i], num);
  496. return err;
  497. }
  498. /**
  499. * ext2_splice_branch - splice the allocated branch onto inode.
  500. * @inode: owner
  501. * @block: (logical) number of block we are adding
  502. * @where: location of missing link
  503. * @num: number of indirect blocks we are adding
  504. * @blks: number of direct blocks we are adding
  505. *
  506. * This function fills the missing link and does all housekeeping needed in
  507. * inode (->i_blocks, etc.). In case of success we end up with the full
  508. * chain to new block and return 0.
  509. */
  510. static void ext2_splice_branch(struct inode *inode,
  511. long block, Indirect *where, int num, int blks)
  512. {
  513. int i;
  514. struct ext2_block_alloc_info *block_i;
  515. ext2_fsblk_t current_block;
  516. block_i = EXT2_I(inode)->i_block_alloc_info;
  517. /* XXX LOCKING probably should have i_meta_lock ?*/
  518. /* That's it */
  519. *where->p = where->key;
  520. /*
  521. * Update the host buffer_head or inode to point to more just allocated
  522. * direct blocks blocks
  523. */
  524. if (num == 0 && blks > 1) {
  525. current_block = le32_to_cpu(where->key) + 1;
  526. for (i = 1; i < blks; i++)
  527. *(where->p + i ) = cpu_to_le32(current_block++);
  528. }
  529. /*
  530. * update the most recently allocated logical & physical block
  531. * in i_block_alloc_info, to assist find the proper goal block for next
  532. * allocation
  533. */
  534. if (block_i) {
  535. block_i->last_alloc_logical_block = block + blks - 1;
  536. block_i->last_alloc_physical_block =
  537. le32_to_cpu(where[num].key) + blks - 1;
  538. }
  539. /* We are done with atomic stuff, now do the rest of housekeeping */
  540. /* had we spliced it onto indirect block? */
  541. if (where->bh)
  542. mark_buffer_dirty_inode(where->bh, inode);
  543. inode_set_ctime_current(inode);
  544. mark_inode_dirty(inode);
  545. }
  546. /*
  547. * Allocation strategy is simple: if we have to allocate something, we will
  548. * have to go the whole way to leaf. So let's do it before attaching anything
  549. * to tree, set linkage between the newborn blocks, write them if sync is
  550. * required, recheck the path, free and repeat if check fails, otherwise
  551. * set the last missing link (that will protect us from any truncate-generated
  552. * removals - all blocks on the path are immune now) and possibly force the
  553. * write on the parent block.
  554. * That has a nice additional property: no special recovery from the failed
  555. * allocations is needed - we simply release blocks and do not touch anything
  556. * reachable from inode.
  557. *
  558. * `handle' can be NULL if create == 0.
  559. *
  560. * return > 0, # of blocks mapped or allocated.
  561. * return = 0, if plain lookup failed.
  562. * return < 0, error case.
  563. */
  564. static int ext2_get_blocks(struct inode *inode,
  565. sector_t iblock, unsigned long maxblocks,
  566. u32 *bno, bool *new, bool *boundary,
  567. int create)
  568. {
  569. int err;
  570. int offsets[4];
  571. Indirect chain[4];
  572. Indirect *partial;
  573. ext2_fsblk_t goal;
  574. int indirect_blks;
  575. int blocks_to_boundary = 0;
  576. int depth;
  577. struct ext2_inode_info *ei = EXT2_I(inode);
  578. int count = 0;
  579. ext2_fsblk_t first_block = 0;
  580. BUG_ON(maxblocks == 0);
  581. depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  582. if (depth == 0)
  583. return -EIO;
  584. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  585. /* Simplest case - block found, no allocation needed */
  586. if (!partial) {
  587. first_block = le32_to_cpu(chain[depth - 1].key);
  588. count++;
  589. /*map more blocks*/
  590. while (count < maxblocks && count <= blocks_to_boundary) {
  591. ext2_fsblk_t blk;
  592. if (!verify_chain(chain, chain + depth - 1)) {
  593. /*
  594. * Indirect block might be removed by
  595. * truncate while we were reading it.
  596. * Handling of that case: forget what we've
  597. * got now, go to reread.
  598. */
  599. err = -EAGAIN;
  600. count = 0;
  601. partial = chain + depth - 1;
  602. break;
  603. }
  604. blk = le32_to_cpu(*(chain[depth-1].p + count));
  605. if (blk == first_block + count)
  606. count++;
  607. else
  608. break;
  609. }
  610. if (err != -EAGAIN)
  611. goto got_it;
  612. }
  613. /* Next simple case - plain lookup or failed read of indirect block */
  614. if (!create || err == -EIO)
  615. goto cleanup;
  616. mutex_lock(&ei->truncate_mutex);
  617. /*
  618. * If the indirect block is missing while we are reading
  619. * the chain(ext2_get_branch() returns -EAGAIN err), or
  620. * if the chain has been changed after we grab the semaphore,
  621. * (either because another process truncated this branch, or
  622. * another get_block allocated this branch) re-grab the chain to see if
  623. * the request block has been allocated or not.
  624. *
  625. * Since we already block the truncate/other get_block
  626. * at this point, we will have the current copy of the chain when we
  627. * splice the branch into the tree.
  628. */
  629. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  630. while (partial > chain) {
  631. brelse(partial->bh);
  632. partial--;
  633. }
  634. partial = ext2_get_branch(inode, depth, offsets, chain, &err);
  635. if (!partial) {
  636. count++;
  637. mutex_unlock(&ei->truncate_mutex);
  638. goto got_it;
  639. }
  640. if (err) {
  641. mutex_unlock(&ei->truncate_mutex);
  642. goto cleanup;
  643. }
  644. }
  645. /*
  646. * Okay, we need to do block allocation. Lazily initialize the block
  647. * allocation info here if necessary
  648. */
  649. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  650. ext2_init_block_alloc_info(inode);
  651. goal = ext2_find_goal(inode, iblock, partial);
  652. /* the number of blocks need to allocate for [d,t]indirect blocks */
  653. indirect_blks = (chain + depth) - partial - 1;
  654. /*
  655. * Next look up the indirect map to count the total number of
  656. * direct blocks to allocate for this branch.
  657. */
  658. count = ext2_blks_to_allocate(partial, indirect_blks,
  659. maxblocks, blocks_to_boundary);
  660. /*
  661. * XXX ???? Block out ext2_truncate while we alter the tree
  662. */
  663. err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
  664. offsets + (partial - chain), partial);
  665. if (err) {
  666. mutex_unlock(&ei->truncate_mutex);
  667. goto cleanup;
  668. }
  669. if (IS_DAX(inode)) {
  670. /*
  671. * We must unmap blocks before zeroing so that writeback cannot
  672. * overwrite zeros with stale data from block device page cache.
  673. */
  674. clean_bdev_aliases(inode->i_sb->s_bdev,
  675. le32_to_cpu(chain[depth-1].key),
  676. count);
  677. /*
  678. * block must be initialised before we put it in the tree
  679. * so that it's not found by another thread before it's
  680. * initialised
  681. */
  682. err = sb_issue_zeroout(inode->i_sb,
  683. le32_to_cpu(chain[depth-1].key), count,
  684. GFP_KERNEL);
  685. if (err) {
  686. mutex_unlock(&ei->truncate_mutex);
  687. goto cleanup;
  688. }
  689. }
  690. *new = true;
  691. ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
  692. mutex_unlock(&ei->truncate_mutex);
  693. got_it:
  694. if (count > blocks_to_boundary)
  695. *boundary = true;
  696. err = count;
  697. /* Clean up and exit */
  698. partial = chain + depth - 1; /* the whole chain */
  699. cleanup:
  700. while (partial > chain) {
  701. brelse(partial->bh);
  702. partial--;
  703. }
  704. if (err > 0)
  705. *bno = le32_to_cpu(chain[depth-1].key);
  706. return err;
  707. }
  708. int ext2_get_block(struct inode *inode, sector_t iblock,
  709. struct buffer_head *bh_result, int create)
  710. {
  711. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  712. bool new = false, boundary = false;
  713. u32 bno;
  714. int ret;
  715. ret = ext2_get_blocks(inode, iblock, max_blocks, &bno, &new, &boundary,
  716. create);
  717. if (ret <= 0)
  718. return ret;
  719. map_bh(bh_result, inode->i_sb, bno);
  720. bh_result->b_size = (ret << inode->i_blkbits);
  721. if (new)
  722. set_buffer_new(bh_result);
  723. if (boundary)
  724. set_buffer_boundary(bh_result);
  725. return 0;
  726. }
  727. static int ext2_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
  728. unsigned flags, struct iomap *iomap, struct iomap *srcmap)
  729. {
  730. unsigned int blkbits = inode->i_blkbits;
  731. unsigned long first_block = offset >> blkbits;
  732. unsigned long max_blocks = (length + (1 << blkbits) - 1) >> blkbits;
  733. struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
  734. bool new = false, boundary = false;
  735. u32 bno;
  736. int ret;
  737. bool create = flags & IOMAP_WRITE;
  738. /*
  739. * For writes that could fill holes inside i_size on a
  740. * DIO_SKIP_HOLES filesystem we forbid block creations: only
  741. * overwrites are permitted.
  742. */
  743. if ((flags & IOMAP_DIRECT) &&
  744. (first_block << blkbits) < i_size_read(inode))
  745. create = 0;
  746. /*
  747. * Writes that span EOF might trigger an IO size update on completion,
  748. * so consider them to be dirty for the purposes of O_DSYNC even if
  749. * there is no other metadata changes pending or have been made here.
  750. */
  751. if ((flags & IOMAP_WRITE) && offset + length > i_size_read(inode))
  752. iomap->flags |= IOMAP_F_DIRTY;
  753. ret = ext2_get_blocks(inode, first_block, max_blocks,
  754. &bno, &new, &boundary, create);
  755. if (ret < 0)
  756. return ret;
  757. iomap->flags = 0;
  758. iomap->offset = (u64)first_block << blkbits;
  759. if (flags & IOMAP_DAX)
  760. iomap->dax_dev = sbi->s_daxdev;
  761. else
  762. iomap->bdev = inode->i_sb->s_bdev;
  763. if (ret == 0) {
  764. /*
  765. * Switch to buffered-io for writing to holes in a non-extent
  766. * based filesystem to avoid stale data exposure problem.
  767. */
  768. if (!create && (flags & IOMAP_WRITE) && (flags & IOMAP_DIRECT))
  769. return -ENOTBLK;
  770. iomap->type = IOMAP_HOLE;
  771. iomap->addr = IOMAP_NULL_ADDR;
  772. iomap->length = 1 << blkbits;
  773. } else {
  774. iomap->type = IOMAP_MAPPED;
  775. iomap->addr = (u64)bno << blkbits;
  776. if (flags & IOMAP_DAX)
  777. iomap->addr += sbi->s_dax_part_off;
  778. iomap->length = (u64)ret << blkbits;
  779. iomap->flags |= IOMAP_F_MERGED;
  780. }
  781. if (new)
  782. iomap->flags |= IOMAP_F_NEW;
  783. return 0;
  784. }
  785. static int
  786. ext2_iomap_end(struct inode *inode, loff_t offset, loff_t length,
  787. ssize_t written, unsigned flags, struct iomap *iomap)
  788. {
  789. /*
  790. * Switch to buffered-io in case of any error.
  791. * Blocks allocated can be used by the buffered-io path.
  792. */
  793. if ((flags & IOMAP_DIRECT) && (flags & IOMAP_WRITE) && written == 0)
  794. return -ENOTBLK;
  795. if (iomap->type == IOMAP_MAPPED &&
  796. written < length &&
  797. (flags & IOMAP_WRITE))
  798. ext2_write_failed(inode->i_mapping, offset + length);
  799. return 0;
  800. }
  801. const struct iomap_ops ext2_iomap_ops = {
  802. .iomap_begin = ext2_iomap_begin,
  803. .iomap_end = ext2_iomap_end,
  804. };
  805. int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  806. u64 start, u64 len)
  807. {
  808. int ret;
  809. loff_t i_size;
  810. inode_lock(inode);
  811. i_size = i_size_read(inode);
  812. /*
  813. * iomap_fiemap() returns EINVAL for 0 length. Make sure we don't trim
  814. * length to 0 but still trim the range as much as possible since
  815. * ext2_get_blocks() iterates unmapped space block by block which is
  816. * slow.
  817. */
  818. if (i_size == 0)
  819. i_size = 1;
  820. len = min_t(u64, len, i_size);
  821. ret = iomap_fiemap(inode, fieinfo, start, len, &ext2_iomap_ops);
  822. inode_unlock(inode);
  823. return ret;
  824. }
  825. static int ext2_read_folio(struct file *file, struct folio *folio)
  826. {
  827. return mpage_read_folio(folio, ext2_get_block);
  828. }
  829. static void ext2_readahead(struct readahead_control *rac)
  830. {
  831. mpage_readahead(rac, ext2_get_block);
  832. }
  833. static int
  834. ext2_write_begin(const struct kiocb *iocb, struct address_space *mapping,
  835. loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
  836. {
  837. int ret;
  838. ret = block_write_begin(mapping, pos, len, foliop, ext2_get_block);
  839. if (ret < 0)
  840. ext2_write_failed(mapping, pos + len);
  841. return ret;
  842. }
  843. static int ext2_write_end(const struct kiocb *iocb,
  844. struct address_space *mapping,
  845. loff_t pos, unsigned len, unsigned copied,
  846. struct folio *folio, void *fsdata)
  847. {
  848. int ret;
  849. ret = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
  850. if (ret < len)
  851. ext2_write_failed(mapping, pos + len);
  852. return ret;
  853. }
  854. static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
  855. {
  856. return generic_block_bmap(mapping,block,ext2_get_block);
  857. }
  858. static int
  859. ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
  860. {
  861. return mpage_writepages(mapping, wbc, ext2_get_block);
  862. }
  863. static int
  864. ext2_dax_writepages(struct address_space *mapping, struct writeback_control *wbc)
  865. {
  866. struct ext2_sb_info *sbi = EXT2_SB(mapping->host->i_sb);
  867. return dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
  868. }
  869. const struct address_space_operations ext2_aops = {
  870. .dirty_folio = block_dirty_folio,
  871. .invalidate_folio = block_invalidate_folio,
  872. .read_folio = ext2_read_folio,
  873. .readahead = ext2_readahead,
  874. .write_begin = ext2_write_begin,
  875. .write_end = ext2_write_end,
  876. .bmap = ext2_bmap,
  877. .writepages = ext2_writepages,
  878. .migrate_folio = buffer_migrate_folio,
  879. .is_partially_uptodate = block_is_partially_uptodate,
  880. .error_remove_folio = generic_error_remove_folio,
  881. };
  882. static const struct address_space_operations ext2_dax_aops = {
  883. .writepages = ext2_dax_writepages,
  884. .dirty_folio = noop_dirty_folio,
  885. };
  886. /*
  887. * Probably it should be a library function... search for first non-zero word
  888. * or memcmp with zero_page, whatever is better for particular architecture.
  889. * Linus?
  890. */
  891. static inline int all_zeroes(__le32 *p, __le32 *q)
  892. {
  893. while (p < q)
  894. if (*p++)
  895. return 0;
  896. return 1;
  897. }
  898. /**
  899. * ext2_find_shared - find the indirect blocks for partial truncation.
  900. * @inode: inode in question
  901. * @depth: depth of the affected branch
  902. * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
  903. * @chain: place to store the pointers to partial indirect blocks
  904. * @top: place to the (detached) top of branch
  905. *
  906. * This is a helper function used by ext2_truncate().
  907. *
  908. * When we do truncate() we may have to clean the ends of several indirect
  909. * blocks but leave the blocks themselves alive. Block is partially
  910. * truncated if some data below the new i_size is referred from it (and
  911. * it is on the path to the first completely truncated data block, indeed).
  912. * We have to free the top of that path along with everything to the right
  913. * of the path. Since no allocation past the truncation point is possible
  914. * until ext2_truncate() finishes, we may safely do the latter, but top
  915. * of branch may require special attention - pageout below the truncation
  916. * point might try to populate it.
  917. *
  918. * We atomically detach the top of branch from the tree, store the block
  919. * number of its root in *@top, pointers to buffer_heads of partially
  920. * truncated blocks - in @chain[].bh and pointers to their last elements
  921. * that should not be removed - in @chain[].p. Return value is the pointer
  922. * to last filled element of @chain.
  923. *
  924. * The work left to caller to do the actual freeing of subtrees:
  925. * a) free the subtree starting from *@top
  926. * b) free the subtrees whose roots are stored in
  927. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  928. * c) free the subtrees growing from the inode past the @chain[0].p
  929. * (no partially truncated stuff there).
  930. */
  931. static Indirect *ext2_find_shared(struct inode *inode,
  932. int depth,
  933. int offsets[4],
  934. Indirect chain[4],
  935. __le32 *top)
  936. {
  937. Indirect *partial, *p;
  938. int k, err;
  939. *top = 0;
  940. for (k = depth; k > 1 && !offsets[k-1]; k--)
  941. ;
  942. partial = ext2_get_branch(inode, k, offsets, chain, &err);
  943. if (!partial)
  944. partial = chain + k-1;
  945. /*
  946. * If the branch acquired continuation since we've looked at it -
  947. * fine, it should all survive and (new) top doesn't belong to us.
  948. */
  949. write_lock(&EXT2_I(inode)->i_meta_lock);
  950. if (!partial->key && *partial->p) {
  951. write_unlock(&EXT2_I(inode)->i_meta_lock);
  952. goto no_top;
  953. }
  954. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  955. ;
  956. /*
  957. * OK, we've found the last block that must survive. The rest of our
  958. * branch should be detached before unlocking. However, if that rest
  959. * of branch is all ours and does not grow immediately from the inode
  960. * it's easier to cheat and just decrement partial->p.
  961. */
  962. if (p == chain + k - 1 && p > chain) {
  963. p->p--;
  964. } else {
  965. *top = *p->p;
  966. *p->p = 0;
  967. }
  968. write_unlock(&EXT2_I(inode)->i_meta_lock);
  969. while(partial > p)
  970. {
  971. brelse(partial->bh);
  972. partial--;
  973. }
  974. no_top:
  975. return partial;
  976. }
  977. /**
  978. * ext2_free_data - free a list of data blocks
  979. * @inode: inode we are dealing with
  980. * @p: array of block numbers
  981. * @q: points immediately past the end of array
  982. *
  983. * We are freeing all blocks referred from that array (numbers are
  984. * stored as little-endian 32-bit) and updating @inode->i_blocks
  985. * appropriately.
  986. */
  987. static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
  988. {
  989. ext2_fsblk_t block_to_free = 0, count = 0;
  990. ext2_fsblk_t nr;
  991. for ( ; p < q ; p++) {
  992. nr = le32_to_cpu(*p);
  993. if (nr) {
  994. *p = 0;
  995. /* accumulate blocks to free if they're contiguous */
  996. if (count == 0)
  997. goto free_this;
  998. else if (block_to_free == nr - count)
  999. count++;
  1000. else {
  1001. ext2_free_blocks (inode, block_to_free, count);
  1002. mark_inode_dirty(inode);
  1003. free_this:
  1004. block_to_free = nr;
  1005. count = 1;
  1006. }
  1007. }
  1008. }
  1009. if (count > 0) {
  1010. ext2_free_blocks (inode, block_to_free, count);
  1011. mark_inode_dirty(inode);
  1012. }
  1013. }
  1014. /**
  1015. * ext2_free_branches - free an array of branches
  1016. * @inode: inode we are dealing with
  1017. * @p: array of block numbers
  1018. * @q: pointer immediately past the end of array
  1019. * @depth: depth of the branches to free
  1020. *
  1021. * We are freeing all blocks referred from these branches (numbers are
  1022. * stored as little-endian 32-bit) and updating @inode->i_blocks
  1023. * appropriately.
  1024. */
  1025. static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
  1026. {
  1027. struct buffer_head * bh;
  1028. ext2_fsblk_t nr;
  1029. if (depth--) {
  1030. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1031. for ( ; p < q ; p++) {
  1032. nr = le32_to_cpu(*p);
  1033. if (!nr)
  1034. continue;
  1035. *p = 0;
  1036. bh = sb_bread(inode->i_sb, nr);
  1037. /*
  1038. * A read failure? Report error and clear slot
  1039. * (should be rare).
  1040. */
  1041. if (!bh) {
  1042. ext2_error(inode->i_sb, "ext2_free_branches",
  1043. "Read failure, inode=%ld, block=%ld",
  1044. inode->i_ino, nr);
  1045. continue;
  1046. }
  1047. ext2_free_branches(inode,
  1048. (__le32*)bh->b_data,
  1049. (__le32*)bh->b_data + addr_per_block,
  1050. depth);
  1051. bforget(bh);
  1052. ext2_free_blocks(inode, nr, 1);
  1053. mark_inode_dirty(inode);
  1054. }
  1055. } else
  1056. ext2_free_data(inode, p, q);
  1057. }
  1058. /* mapping->invalidate_lock must be held when calling this function */
  1059. static void __ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1060. {
  1061. __le32 *i_data = EXT2_I(inode)->i_data;
  1062. struct ext2_inode_info *ei = EXT2_I(inode);
  1063. int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
  1064. int offsets[4];
  1065. Indirect chain[4];
  1066. Indirect *partial;
  1067. __le32 nr = 0;
  1068. int n;
  1069. long iblock;
  1070. unsigned blocksize;
  1071. blocksize = inode->i_sb->s_blocksize;
  1072. iblock = (offset + blocksize-1) >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
  1073. #ifdef CONFIG_FS_DAX
  1074. WARN_ON(!rwsem_is_locked(&inode->i_mapping->invalidate_lock));
  1075. #endif
  1076. n = ext2_block_to_path(inode, iblock, offsets, NULL);
  1077. if (n == 0)
  1078. return;
  1079. /*
  1080. * From here we block out all ext2_get_block() callers who want to
  1081. * modify the block allocation tree.
  1082. */
  1083. mutex_lock(&ei->truncate_mutex);
  1084. if (n == 1) {
  1085. ext2_free_data(inode, i_data+offsets[0],
  1086. i_data + EXT2_NDIR_BLOCKS);
  1087. goto do_indirects;
  1088. }
  1089. partial = ext2_find_shared(inode, n, offsets, chain, &nr);
  1090. /* Kill the top of shared branch (already detached) */
  1091. if (nr) {
  1092. if (partial == chain)
  1093. mark_inode_dirty(inode);
  1094. else
  1095. mark_buffer_dirty_inode(partial->bh, inode);
  1096. ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  1097. }
  1098. /* Clear the ends of indirect blocks on the shared branch */
  1099. while (partial > chain) {
  1100. ext2_free_branches(inode,
  1101. partial->p + 1,
  1102. (__le32*)partial->bh->b_data+addr_per_block,
  1103. (chain+n-1) - partial);
  1104. mark_buffer_dirty_inode(partial->bh, inode);
  1105. brelse (partial->bh);
  1106. partial--;
  1107. }
  1108. do_indirects:
  1109. /* Kill the remaining (whole) subtrees */
  1110. switch (offsets[0]) {
  1111. default:
  1112. nr = i_data[EXT2_IND_BLOCK];
  1113. if (nr) {
  1114. i_data[EXT2_IND_BLOCK] = 0;
  1115. mark_inode_dirty(inode);
  1116. ext2_free_branches(inode, &nr, &nr+1, 1);
  1117. }
  1118. fallthrough;
  1119. case EXT2_IND_BLOCK:
  1120. nr = i_data[EXT2_DIND_BLOCK];
  1121. if (nr) {
  1122. i_data[EXT2_DIND_BLOCK] = 0;
  1123. mark_inode_dirty(inode);
  1124. ext2_free_branches(inode, &nr, &nr+1, 2);
  1125. }
  1126. fallthrough;
  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. break;
  1135. case EXT2_TIND_BLOCK:
  1136. ;
  1137. }
  1138. ext2_discard_reservation(inode);
  1139. mutex_unlock(&ei->truncate_mutex);
  1140. }
  1141. static void ext2_truncate_blocks(struct inode *inode, loff_t offset)
  1142. {
  1143. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1144. S_ISLNK(inode->i_mode)))
  1145. return;
  1146. if (ext2_inode_is_fast_symlink(inode))
  1147. return;
  1148. filemap_invalidate_lock(inode->i_mapping);
  1149. __ext2_truncate_blocks(inode, offset);
  1150. filemap_invalidate_unlock(inode->i_mapping);
  1151. }
  1152. static int ext2_setsize(struct inode *inode, loff_t newsize)
  1153. {
  1154. int error;
  1155. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1156. S_ISLNK(inode->i_mode)))
  1157. return -EINVAL;
  1158. if (ext2_inode_is_fast_symlink(inode))
  1159. return -EINVAL;
  1160. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1161. return -EPERM;
  1162. inode_dio_wait(inode);
  1163. if (IS_DAX(inode))
  1164. error = dax_truncate_page(inode, newsize, NULL,
  1165. &ext2_iomap_ops);
  1166. else
  1167. error = block_truncate_page(inode->i_mapping,
  1168. newsize, ext2_get_block);
  1169. if (error)
  1170. return error;
  1171. filemap_invalidate_lock(inode->i_mapping);
  1172. truncate_setsize(inode, newsize);
  1173. __ext2_truncate_blocks(inode, newsize);
  1174. filemap_invalidate_unlock(inode->i_mapping);
  1175. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  1176. if (inode_needs_sync(inode)) {
  1177. sync_mapping_buffers(inode->i_mapping);
  1178. sync_inode_metadata(inode, 1);
  1179. } else {
  1180. mark_inode_dirty(inode);
  1181. }
  1182. return 0;
  1183. }
  1184. static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
  1185. struct buffer_head **p)
  1186. {
  1187. struct buffer_head * bh;
  1188. unsigned long block_group;
  1189. unsigned long block;
  1190. unsigned long offset;
  1191. struct ext2_group_desc * gdp;
  1192. *p = NULL;
  1193. if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
  1194. ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
  1195. goto Einval;
  1196. block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
  1197. gdp = ext2_get_group_desc(sb, block_group, NULL);
  1198. if (!gdp)
  1199. goto Egdp;
  1200. /*
  1201. * Figure out the offset within the block group inode table
  1202. */
  1203. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
  1204. block = le32_to_cpu(gdp->bg_inode_table) +
  1205. (offset >> EXT2_BLOCK_SIZE_BITS(sb));
  1206. if (!(bh = sb_bread(sb, block)))
  1207. goto Eio;
  1208. *p = bh;
  1209. offset &= (EXT2_BLOCK_SIZE(sb) - 1);
  1210. return (struct ext2_inode *) (bh->b_data + offset);
  1211. Einval:
  1212. ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
  1213. (unsigned long) ino);
  1214. return ERR_PTR(-EINVAL);
  1215. Eio:
  1216. ext2_error(sb, "ext2_get_inode",
  1217. "unable to read inode block - inode=%lu, block=%lu",
  1218. (unsigned long) ino, block);
  1219. Egdp:
  1220. return ERR_PTR(-EIO);
  1221. }
  1222. void ext2_set_inode_flags(struct inode *inode)
  1223. {
  1224. unsigned int flags = EXT2_I(inode)->i_flags;
  1225. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
  1226. S_DIRSYNC | S_DAX);
  1227. if (flags & EXT2_SYNC_FL)
  1228. inode->i_flags |= S_SYNC;
  1229. if (flags & EXT2_APPEND_FL)
  1230. inode->i_flags |= S_APPEND;
  1231. if (flags & EXT2_IMMUTABLE_FL)
  1232. inode->i_flags |= S_IMMUTABLE;
  1233. if (flags & EXT2_NOATIME_FL)
  1234. inode->i_flags |= S_NOATIME;
  1235. if (flags & EXT2_DIRSYNC_FL)
  1236. inode->i_flags |= S_DIRSYNC;
  1237. if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
  1238. inode->i_flags |= S_DAX;
  1239. }
  1240. void ext2_set_file_ops(struct inode *inode)
  1241. {
  1242. inode->i_op = &ext2_file_inode_operations;
  1243. inode->i_fop = &ext2_file_operations;
  1244. if (IS_DAX(inode))
  1245. inode->i_mapping->a_ops = &ext2_dax_aops;
  1246. else
  1247. inode->i_mapping->a_ops = &ext2_aops;
  1248. }
  1249. struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
  1250. {
  1251. struct ext2_inode_info *ei;
  1252. struct buffer_head * bh = NULL;
  1253. struct ext2_inode *raw_inode;
  1254. struct inode *inode;
  1255. long ret = -EIO;
  1256. int n;
  1257. uid_t i_uid;
  1258. gid_t i_gid;
  1259. inode = iget_locked(sb, ino);
  1260. if (!inode)
  1261. return ERR_PTR(-ENOMEM);
  1262. if (!(inode_state_read_once(inode) & I_NEW))
  1263. return inode;
  1264. ei = EXT2_I(inode);
  1265. ei->i_block_alloc_info = NULL;
  1266. raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
  1267. if (IS_ERR(raw_inode)) {
  1268. ret = PTR_ERR(raw_inode);
  1269. goto bad_inode;
  1270. }
  1271. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  1272. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  1273. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  1274. if (!(test_opt (inode->i_sb, NO_UID32))) {
  1275. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  1276. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  1277. }
  1278. i_uid_write(inode, i_uid);
  1279. i_gid_write(inode, i_gid);
  1280. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  1281. inode->i_size = le32_to_cpu(raw_inode->i_size);
  1282. inode_set_atime(inode, (signed)le32_to_cpu(raw_inode->i_atime), 0);
  1283. inode_set_ctime(inode, (signed)le32_to_cpu(raw_inode->i_ctime), 0);
  1284. inode_set_mtime(inode, (signed)le32_to_cpu(raw_inode->i_mtime), 0);
  1285. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  1286. /* We now have enough fields to check if the inode was active or not.
  1287. * This is needed because nfsd might try to access dead inodes
  1288. * the test is that same one that e2fsck uses
  1289. * NeilBrown 1999oct15
  1290. */
  1291. if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
  1292. /* this inode is deleted */
  1293. ret = -ESTALE;
  1294. goto bad_inode;
  1295. }
  1296. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  1297. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  1298. ext2_set_inode_flags(inode);
  1299. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  1300. ei->i_frag_no = raw_inode->i_frag;
  1301. ei->i_frag_size = raw_inode->i_fsize;
  1302. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  1303. ei->i_dir_acl = 0;
  1304. if (ei->i_file_acl &&
  1305. !ext2_data_block_valid(EXT2_SB(sb), ei->i_file_acl, 1)) {
  1306. ext2_error(sb, "ext2_iget", "bad extended attribute block %u",
  1307. ei->i_file_acl);
  1308. ret = -EFSCORRUPTED;
  1309. goto bad_inode;
  1310. }
  1311. if (S_ISREG(inode->i_mode))
  1312. inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  1313. else
  1314. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  1315. if (i_size_read(inode) < 0) {
  1316. ret = -EFSCORRUPTED;
  1317. goto bad_inode;
  1318. }
  1319. ei->i_dtime = 0;
  1320. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  1321. ei->i_state = 0;
  1322. ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
  1323. ei->i_dir_start_lookup = 0;
  1324. /*
  1325. * NOTE! The in-memory inode i_data array is in little-endian order
  1326. * even on big-endian machines: we do NOT byteswap the block numbers!
  1327. */
  1328. for (n = 0; n < EXT2_N_BLOCKS; n++)
  1329. ei->i_data[n] = raw_inode->i_block[n];
  1330. if (S_ISREG(inode->i_mode)) {
  1331. ext2_set_file_ops(inode);
  1332. } else if (S_ISDIR(inode->i_mode)) {
  1333. inode->i_op = &ext2_dir_inode_operations;
  1334. inode->i_fop = &ext2_dir_operations;
  1335. inode->i_mapping->a_ops = &ext2_aops;
  1336. } else if (S_ISLNK(inode->i_mode)) {
  1337. if (ext2_inode_is_fast_symlink(inode)) {
  1338. inode->i_link = (char *)ei->i_data;
  1339. inode->i_op = &ext2_fast_symlink_inode_operations;
  1340. nd_terminate_link(ei->i_data, inode->i_size,
  1341. sizeof(ei->i_data) - 1);
  1342. } else {
  1343. inode->i_op = &ext2_symlink_inode_operations;
  1344. inode_nohighmem(inode);
  1345. inode->i_mapping->a_ops = &ext2_aops;
  1346. }
  1347. } else {
  1348. inode->i_op = &ext2_special_inode_operations;
  1349. if (raw_inode->i_block[0])
  1350. init_special_inode(inode, inode->i_mode,
  1351. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  1352. else
  1353. init_special_inode(inode, inode->i_mode,
  1354. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  1355. }
  1356. brelse (bh);
  1357. unlock_new_inode(inode);
  1358. return inode;
  1359. bad_inode:
  1360. brelse(bh);
  1361. iget_failed(inode);
  1362. return ERR_PTR(ret);
  1363. }
  1364. static int __ext2_write_inode(struct inode *inode, int do_sync)
  1365. {
  1366. struct ext2_inode_info *ei = EXT2_I(inode);
  1367. struct super_block *sb = inode->i_sb;
  1368. ino_t ino = inode->i_ino;
  1369. uid_t uid = i_uid_read(inode);
  1370. gid_t gid = i_gid_read(inode);
  1371. struct buffer_head * bh;
  1372. struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
  1373. int n;
  1374. int err = 0;
  1375. if (IS_ERR(raw_inode))
  1376. return -EIO;
  1377. /* For fields not tracking in the in-memory inode,
  1378. * initialise them to zero for new inodes. */
  1379. if (ei->i_state & EXT2_STATE_NEW)
  1380. memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
  1381. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  1382. if (!(test_opt(sb, NO_UID32))) {
  1383. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
  1384. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
  1385. /*
  1386. * Fix up interoperability with old kernels. Otherwise, old inodes get
  1387. * re-used with the upper 16 bits of the uid/gid intact
  1388. */
  1389. if (!ei->i_dtime) {
  1390. raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
  1391. raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
  1392. } else {
  1393. raw_inode->i_uid_high = 0;
  1394. raw_inode->i_gid_high = 0;
  1395. }
  1396. } else {
  1397. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
  1398. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
  1399. raw_inode->i_uid_high = 0;
  1400. raw_inode->i_gid_high = 0;
  1401. }
  1402. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  1403. raw_inode->i_size = cpu_to_le32(inode->i_size);
  1404. raw_inode->i_atime = cpu_to_le32(inode_get_atime_sec(inode));
  1405. raw_inode->i_ctime = cpu_to_le32(inode_get_ctime_sec(inode));
  1406. raw_inode->i_mtime = cpu_to_le32(inode_get_mtime_sec(inode));
  1407. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  1408. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  1409. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  1410. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  1411. raw_inode->i_frag = ei->i_frag_no;
  1412. raw_inode->i_fsize = ei->i_frag_size;
  1413. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  1414. if (!S_ISREG(inode->i_mode))
  1415. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  1416. else {
  1417. raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
  1418. if (inode->i_size > 0x7fffffffULL) {
  1419. if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
  1420. EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
  1421. EXT2_SB(sb)->s_es->s_rev_level ==
  1422. cpu_to_le32(EXT2_GOOD_OLD_REV)) {
  1423. /* If this is the first large file
  1424. * created, add a flag to the superblock.
  1425. */
  1426. spin_lock(&EXT2_SB(sb)->s_lock);
  1427. ext2_update_dynamic_rev(sb);
  1428. EXT2_SET_RO_COMPAT_FEATURE(sb,
  1429. EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
  1430. spin_unlock(&EXT2_SB(sb)->s_lock);
  1431. ext2_sync_super(sb, EXT2_SB(sb)->s_es, 1);
  1432. }
  1433. }
  1434. }
  1435. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  1436. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1437. if (old_valid_dev(inode->i_rdev)) {
  1438. raw_inode->i_block[0] =
  1439. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1440. raw_inode->i_block[1] = 0;
  1441. } else {
  1442. raw_inode->i_block[0] = 0;
  1443. raw_inode->i_block[1] =
  1444. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1445. raw_inode->i_block[2] = 0;
  1446. }
  1447. } else for (n = 0; n < EXT2_N_BLOCKS; n++)
  1448. raw_inode->i_block[n] = ei->i_data[n];
  1449. mark_buffer_dirty(bh);
  1450. if (do_sync) {
  1451. sync_dirty_buffer(bh);
  1452. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1453. printk ("IO error syncing ext2 inode [%s:%08lx]\n",
  1454. sb->s_id, (unsigned long) ino);
  1455. err = -EIO;
  1456. }
  1457. }
  1458. ei->i_state &= ~EXT2_STATE_NEW;
  1459. brelse (bh);
  1460. return err;
  1461. }
  1462. int ext2_write_inode(struct inode *inode, struct writeback_control *wbc)
  1463. {
  1464. return __ext2_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1465. }
  1466. int ext2_getattr(struct mnt_idmap *idmap, const struct path *path,
  1467. struct kstat *stat, u32 request_mask, unsigned int query_flags)
  1468. {
  1469. struct inode *inode = d_inode(path->dentry);
  1470. struct ext2_inode_info *ei = EXT2_I(inode);
  1471. unsigned int flags;
  1472. flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
  1473. if (flags & EXT2_APPEND_FL)
  1474. stat->attributes |= STATX_ATTR_APPEND;
  1475. if (flags & EXT2_COMPR_FL)
  1476. stat->attributes |= STATX_ATTR_COMPRESSED;
  1477. if (flags & EXT2_IMMUTABLE_FL)
  1478. stat->attributes |= STATX_ATTR_IMMUTABLE;
  1479. if (flags & EXT2_NODUMP_FL)
  1480. stat->attributes |= STATX_ATTR_NODUMP;
  1481. stat->attributes_mask |= (STATX_ATTR_APPEND |
  1482. STATX_ATTR_COMPRESSED |
  1483. STATX_ATTR_ENCRYPTED |
  1484. STATX_ATTR_IMMUTABLE |
  1485. STATX_ATTR_NODUMP);
  1486. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  1487. return 0;
  1488. }
  1489. int ext2_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  1490. struct iattr *iattr)
  1491. {
  1492. struct inode *inode = d_inode(dentry);
  1493. int error;
  1494. error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
  1495. if (error)
  1496. return error;
  1497. if (is_quota_modification(&nop_mnt_idmap, inode, iattr)) {
  1498. error = dquot_initialize(inode);
  1499. if (error)
  1500. return error;
  1501. }
  1502. if (i_uid_needs_update(&nop_mnt_idmap, iattr, inode) ||
  1503. i_gid_needs_update(&nop_mnt_idmap, iattr, inode)) {
  1504. error = dquot_transfer(&nop_mnt_idmap, inode, iattr);
  1505. if (error)
  1506. return error;
  1507. }
  1508. if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
  1509. error = ext2_setsize(inode, iattr->ia_size);
  1510. if (error)
  1511. return error;
  1512. }
  1513. setattr_copy(&nop_mnt_idmap, inode, iattr);
  1514. if (iattr->ia_valid & ATTR_MODE)
  1515. error = posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode);
  1516. mark_inode_dirty(inode);
  1517. return error;
  1518. }