inode.c 47 KB

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